314 lines
10 KiB
Dart
314 lines
10 KiB
Dart
import 'dart:async';
|
||
import 'dart:convert';
|
||
|
||
import 'package:EasyDartModule/EasyDartModule.dart';
|
||
import 'package:easydevice/easydevice.dart';
|
||
import 'package:ef/ef.dart';
|
||
import 'package:json_annotation/json_annotation.dart';
|
||
import 'package:vbvs_app/common/color/ServiceConstant.dart';
|
||
import 'package:vbvs_app/common/color/app_uri_status.dart';
|
||
import 'package:vbvs_app/common/util/DailyLogUtils.dart';
|
||
import 'package:vbvs_app/common/util/MyUtils.dart';
|
||
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
|
||
import 'package:vbvs_app/controller/person/person_controller.dart';
|
||
import 'package:vbvs_app/model/BleDeviceData.dart';
|
||
import 'package:vbvs_app/model/api_response.dart';
|
||
|
||
part 'blueteeth_bind_controller.g.dart';
|
||
|
||
@JsonSerializable()
|
||
class BlueteethBindModel {
|
||
int? read = 1;
|
||
double? singal = -70;
|
||
|
||
@JsonKey(ignore: true)
|
||
List<BleDeviceData>? devicelist = []; //蓝牙扫描
|
||
@JsonKey(ignore: true)
|
||
List<BleDeviceData>? betDevicelist = []; //网络状态
|
||
@JsonKey(ignore: true)
|
||
List? blelist = [];
|
||
|
||
List bindArr = ["", "", ""];
|
||
String connectedWifiName = "";
|
||
int connectedRssi = 0;
|
||
String deviceName = "";
|
||
|
||
bool? deviceIndex0 = true;
|
||
bool? deviceIndex1 = false;
|
||
bool? deviceIndex2 = false;
|
||
|
||
bool wifiPassShow = false;
|
||
String? wifiPass;
|
||
|
||
BlueteethBindModel();
|
||
|
||
factory BlueteethBindModel.fromJson(Map<String, dynamic> json) {
|
||
try {
|
||
return _$BlueteethBindModelFromJson(json);
|
||
} catch (e) {
|
||
return BlueteethBindModel();
|
||
}
|
||
}
|
||
|
||
Map<String, dynamic> toJson() => _$BlueteethBindModelToJson(this);
|
||
}
|
||
|
||
class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
|
||
BlueteethBindController() {
|
||
attr = GetModel(BlueteethBindModel()).obs;
|
||
}
|
||
|
||
Timer? _statusTimer;
|
||
|
||
THapp? currentDevice;
|
||
RxInt wifiStatus = 0.obs;
|
||
RxList wifiList = [].obs;
|
||
RxMap connect_wifi = {}.obs;
|
||
RxString scanMac = "".obs;
|
||
|
||
RxString? currentDeviceMac = "".obs;
|
||
RxString? cid = "".obs ;
|
||
|
||
RxString search = "".obs;//搜索关键字
|
||
|
||
RxInt connectStatus = 0.obs;
|
||
|
||
RxMap selectWifi = {}.obs; //正在连接wifi信息
|
||
|
||
// 安全展示 TopSlideNotification
|
||
void safeShowNotification(String msg) {
|
||
try {
|
||
final ctx = Get.context;
|
||
if (ctx != null && ctx.mounted) {
|
||
TopSlideNotification.show(
|
||
ctx,
|
||
text: msg,
|
||
textColor: themeController.currentColor.sc9,
|
||
);
|
||
} else {
|
||
print("TopSlideNotification 未显示:context 不可用或未挂载");
|
||
}
|
||
} catch (e, stack) {
|
||
// print("TopSlideNotification 显示异常: $e\n$stack");
|
||
}
|
||
}
|
||
|
||
// 启动每10秒获取设备状态
|
||
void startStatusPolling() {
|
||
updateDeviceStatus().then((res) {
|
||
if (res.code == HttpStatusCodes.ok) {
|
||
updateAll();
|
||
} else {
|
||
safeShowNotification(res.msg ?? "获取设备状态异常");
|
||
EasyDartModule.logger.info("获取设备状态异常: $res");
|
||
DailyLogUtils.writeLog("获取设备状态异常: $res");
|
||
}
|
||
});
|
||
|
||
if (_statusTimer == null) {
|
||
_statusTimer = Timer.periodic(Duration(seconds: 2), (timer) {
|
||
updateDeviceStatus().then((res) {
|
||
if (res.code == HttpStatusCodes.ok) {
|
||
updateAll();
|
||
} else {
|
||
safeShowNotification(res.msg ?? "获取设备状态异常");
|
||
EasyDartModule.logger.info("获取设备状态异常: $res");
|
||
DailyLogUtils.writeLog("获取设备状态异常: $res");
|
||
}
|
||
}).catchError((e, stack) {
|
||
print("updateDeviceStatus 执行异常: $e\n$stack");
|
||
safeShowNotification("设备状态请求失败");
|
||
EasyDartModule.logger.info("设备状态异常: $e");
|
||
DailyLogUtils.writeLog("设备状态异常: $e");
|
||
});
|
||
});
|
||
}
|
||
}
|
||
|
||
void stopStatusPolling() {
|
||
_statusTimer?.cancel();
|
||
_statusTimer = null;
|
||
}
|
||
|
||
Future<ApiResponse> updateDeviceStatus() async {
|
||
try {
|
||
String serviceAddress = ServiceConstant.service_address;
|
||
String serviceName = ServiceConstant.server_service;
|
||
String serviceApi = ServiceConstant.get_bluetooth_device_status;
|
||
String queryUrl = "$serviceAddress$serviceName$serviceApi";
|
||
|
||
if (model.devicelist != null && model.devicelist!.isNotEmpty) {
|
||
final macParams = model.devicelist!
|
||
.map((device) => "mac=${Uri.encodeQueryComponent(device.mac!)}")
|
||
.join("&");
|
||
|
||
if (queryUrl.contains('?')) {
|
||
queryUrl += '&$macParams';
|
||
} else {
|
||
queryUrl += '?$macParams';
|
||
}
|
||
String? language = "";
|
||
if (languageController.selectLanguage != null) {
|
||
language = languageController.selectLanguage.value!.language_code;
|
||
}
|
||
if (language != null && language.isNotEmpty) {
|
||
if (queryUrl.contains("?")) {
|
||
queryUrl += "&lang=$language";
|
||
} else {
|
||
queryUrl += "?lang=$language";
|
||
}
|
||
}
|
||
|
||
var response = await EasyDartModule.dio.get(queryUrl);
|
||
var responseData =
|
||
response.data is String ? jsonDecode(response.data) : response.data;
|
||
ApiResponse res =
|
||
ApiResponse.fromJson(responseData, (object) => object);
|
||
if (res.code != HttpStatusCodes.ok) return res;
|
||
|
||
if (response.data['data'] != null && response.data['data'] is List) {
|
||
List<dynamic> responseList = response.data['data'];
|
||
Map<String, BleDeviceData> deviceMap = {
|
||
for (var d in model.devicelist!)
|
||
if (d.mac != null) d.mac!: d
|
||
};
|
||
|
||
List<String> slaveMacsToRemove = [];
|
||
|
||
for (var item in responseList) {
|
||
String mac = item['mac'];
|
||
String? bindMac = item['bindMac'];
|
||
bool? bind = item['bind'];
|
||
|
||
if (deviceMap.containsKey(mac)) {
|
||
BleDeviceData currentDevice = deviceMap[mac]!;
|
||
currentDevice.bind = bind;
|
||
if (bindMac != null && deviceMap.containsKey(bindMac)) {
|
||
BleDeviceData masterDevice = deviceMap[bindMac]!;
|
||
masterDevice.slave = currentDevice;
|
||
slaveMacsToRemove.add(mac);
|
||
}
|
||
}
|
||
}
|
||
|
||
model.devicelist!
|
||
.removeWhere((device) => slaveMacsToRemove.contains(device.mac));
|
||
model.betDevicelist = model.devicelist!;
|
||
} else {
|
||
model.betDevicelist = [];
|
||
}
|
||
|
||
updateAll();
|
||
return res;
|
||
}
|
||
} catch (e) {
|
||
print("获取设备状态异常: $e");
|
||
EasyDartModule.logger.info("获取设备状态异常: $e");
|
||
DailyLogUtils.writeLog("获取设备状态异常: $e");
|
||
return ApiResponse(code: -1, msg: "请求失败".tr);
|
||
}
|
||
|
||
return ApiResponse(code: -1, msg: "未知错误".tr);
|
||
}
|
||
|
||
Future<ApiResponse> bindDeviceAndMAC(BleDeviceData d) async {
|
||
try {
|
||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "蓝牙绑定.绑定失败".tr);
|
||
String serviceAddress = ServiceConstant.service_address;
|
||
String serviceName = ServiceConstant.server_service;
|
||
String serviceApi = ServiceConstant.device_bind;
|
||
String queryUrl = "$serviceAddress$serviceName$serviceApi";
|
||
String? language = "";
|
||
if (languageController.selectLanguage != null) {
|
||
language = languageController.selectLanguage.value!.language_code;
|
||
}
|
||
if (language != null && language.isNotEmpty) {
|
||
if (queryUrl.contains("?")) {
|
||
queryUrl += "&lang=$language";
|
||
} else {
|
||
queryUrl += "?lang=$language";
|
||
}
|
||
}
|
||
var data = {
|
||
"deviceType": 1,
|
||
"mac": d.mac,
|
||
};
|
||
var response =
|
||
await EasyDartModule.dio.post(queryUrl, data: jsonEncode(data));
|
||
if (response != null) {
|
||
var responseData =
|
||
response.data is String ? jsonDecode(response.data) : response.data;
|
||
ApiResponse res =
|
||
ApiResponse.fromJson(responseData, (object) => object);
|
||
MyUtils.formatResponse(res, "绑定成功.绑定成功".tr, "绑定成功.绑定成功".tr);
|
||
if (res.code == HttpStatusCodes.ok) {
|
||
PersonController personController = Get.find();
|
||
personController.currentPersonId.value = res.data['id'];
|
||
return res;
|
||
} else {
|
||
return res;
|
||
}
|
||
} else {
|
||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||
}
|
||
} catch (e) {
|
||
EasyDartModule.logger.info("蓝牙绑定.绑定异常: $e");
|
||
DailyLogUtils.writeLog("蓝牙绑定.绑定异常: $e");
|
||
}
|
||
return ApiResponse(code: -1, msg: "未知错误".tr);
|
||
}
|
||
|
||
@override
|
||
void onClose() {
|
||
stopStatusPolling();
|
||
super.onClose();
|
||
}
|
||
|
||
bindDevice(Map<String, dynamic> map) {}
|
||
|
||
Future<ApiResponse> bindDeviceByScan(String mac) async {
|
||
try {
|
||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "蓝牙绑定.绑定失败".tr);
|
||
String serviceAddress = ServiceConstant.service_address;
|
||
String serviceName = ServiceConstant.server_service;
|
||
String serviceApi = ServiceConstant.device_bind;
|
||
String queryUrl = "$serviceAddress$serviceName$serviceApi";
|
||
String? language = "";
|
||
if (languageController.selectLanguage != null) {
|
||
language = languageController.selectLanguage.value!.language_code;
|
||
}
|
||
if (language != null && language.isNotEmpty) {
|
||
if (queryUrl.contains("?")) {
|
||
queryUrl += "&lang=$language";
|
||
} else {
|
||
queryUrl += "?lang=$language";
|
||
}
|
||
}
|
||
var data = {
|
||
"deviceType": 1,
|
||
"mac": mac,
|
||
};
|
||
var response =
|
||
await EasyDartModule.dio.post(queryUrl, data: jsonEncode(data));
|
||
if (response != null) {
|
||
var responseData =
|
||
response.data is String ? jsonDecode(response.data) : response.data;
|
||
ApiResponse res =
|
||
ApiResponse.fromJson(responseData, (object) => object);
|
||
MyUtils.formatResponse(res, "绑定成功.绑定成功".tr, "绑定成功.绑定成功".tr);
|
||
if (res.code == HttpStatusCodes.ok) {
|
||
return res;
|
||
}
|
||
apiResponse.msg = res.msg ?? apiResponse.msg;
|
||
} else {
|
||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||
}
|
||
return apiResponse;
|
||
} catch (e) {
|
||
EasyDartModule.logger.info("蓝牙绑定.绑定异常: $e");
|
||
DailyLogUtils.writeLog("蓝牙绑定.绑定异常: $e");
|
||
}
|
||
return ApiResponse(code: -1, msg: "未知错误".tr);
|
||
}
|
||
}
|