更新分享
This commit is contained in:
@@ -2,54 +2,54 @@ import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:EasyDartModule/EasyDartModule.dart';
|
||||
import 'package:easydevice/src/ble_device.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'; // 由json_serializable自动生成的部分
|
||||
part 'blueteeth_bind_controller.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class BlueteethBindModel {
|
||||
int? read = 1; //是否不再提示教程 0 不再提示 1 需要提示
|
||||
double? singal = -70; //扫描信号强度
|
||||
int? read = 1;
|
||||
double? singal = -70;
|
||||
|
||||
List<BleDeviceData>? devicelist = []; //蓝牙扫描到的设备数据列表
|
||||
List<BleDeviceData>? betDevicelist = []; //请求的
|
||||
List? blelist = []; //蓝牙扫描到的设备数据列表
|
||||
List? wifiList = [];
|
||||
@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;
|
||||
|
||||
BlueteethBindModel();
|
||||
bool wifiPassShow = false;
|
||||
String? wifiPass;
|
||||
|
||||
// 从JSON反序列化时的异常处理
|
||||
BlueteethBindModel();
|
||||
|
||||
factory BlueteethBindModel.fromJson(Map<String, dynamic> json) {
|
||||
try {
|
||||
return _$BlueteethBindModelFromJson(json);
|
||||
} catch (e) {
|
||||
// 在实际应用中,应该有更细致的异常处理策略和错误日志
|
||||
return BlueteethBindModel(); // 或者返回一个带有错误信息的特定DeviceInfoModel实例
|
||||
return BlueteethBindModel();
|
||||
}
|
||||
}
|
||||
|
||||
// 序列化为JSON时的异常处理
|
||||
Map<String, dynamic> toJson() => _$BlueteethBindModelToJson(this);
|
||||
}
|
||||
|
||||
@@ -60,30 +60,73 @@ class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
|
||||
|
||||
Timer? _statusTimer;
|
||||
|
||||
BLEDevice? currentDevice;
|
||||
THapp? currentDevice;
|
||||
RxInt wifiStatus = 0.obs;
|
||||
RxList wifiList = [].obs;
|
||||
RxMap connect_wifi = {}.obs;
|
||||
RxString scanMac = "".obs;
|
||||
|
||||
// 安全展示 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: 10), (timer) {
|
||||
updateDeviceStatus();
|
||||
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<void> updateDeviceStatus() async {
|
||||
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}";
|
||||
String queryUrl = "$serviceAddress$serviceName$serviceApi";
|
||||
|
||||
if (model.devicelist != null && model.devicelist!.isNotEmpty) {
|
||||
final macParams = model.devicelist!
|
||||
@@ -97,13 +140,16 @@ class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
|
||||
}
|
||||
|
||||
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
|
||||
for (var d in model.devicelist!) if (d.mac != null) d.mac!: d
|
||||
};
|
||||
|
||||
List<String> slaveMacsToRemove = [];
|
||||
@@ -123,17 +169,26 @@ class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
model.devicelist!
|
||||
.removeWhere((device) => slaveMacsToRemove.contains(device.mac));
|
||||
model.betDevicelist = model.devicelist!;
|
||||
} else {
|
||||
model.betDevicelist = [];
|
||||
}
|
||||
|
||||
print("获取设备状态成功");
|
||||
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 {
|
||||
@@ -142,7 +197,7 @@ class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.device_bind;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
String queryUrl = "$serviceAddress$serviceName$serviceApi";
|
||||
var data = {
|
||||
"deviceType": 1,
|
||||
"mac": d.mac,
|
||||
@@ -154,10 +209,55 @@ class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
|
||||
response.data is String ? jsonDecode(response.data) : response.data;
|
||||
ApiResponse res =
|
||||
ApiResponse.fromJson(responseData, (object) => object);
|
||||
MyUtils.formatResponse(apiResponse, "蓝牙绑定.绑定成功".tr, "蓝牙绑定.绑定成功".tr);
|
||||
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";
|
||||
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);
|
||||
}
|
||||
@@ -166,14 +266,6 @@ class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
|
||||
EasyDartModule.logger.info("蓝牙绑定.绑定异常: $e");
|
||||
DailyLogUtils.writeLog("蓝牙绑定.绑定异常: $e");
|
||||
}
|
||||
return ApiResponse(code: -1, msg: "未知错误".tr); // Default return statement
|
||||
return ApiResponse(code: -1, msg: "未知错误".tr);
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
stopStatusPolling(); // 控制器销毁时停止轮询
|
||||
super.onClose();
|
||||
}
|
||||
|
||||
bindDevice(Map<String, dynamic> map) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user