180 lines
5.8 KiB
Dart
180 lines
5.8 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:EasyDartModule/EasyDartModule.dart';
|
|
import 'package:easydevice/src/ble_device.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/model/BleDeviceData.dart';
|
|
import 'package:vbvs_app/model/api_response.dart';
|
|
|
|
part 'blueteeth_bind_controller.g.dart'; // 由json_serializable自动生成的部分
|
|
|
|
@JsonSerializable()
|
|
class BlueteethBindModel {
|
|
int? read = 1; //是否不再提示教程 0 不再提示 1 需要提示
|
|
double? singal = -70; //扫描信号强度
|
|
|
|
List<BleDeviceData>? devicelist = []; //蓝牙扫描到的设备数据列表
|
|
List<BleDeviceData>? betDevicelist = []; //请求的
|
|
List? blelist = []; //蓝牙扫描到的设备数据列表
|
|
List? wifiList = [];
|
|
|
|
List bindArr = ["", "", ""];
|
|
|
|
String connectedWifiName = "";
|
|
|
|
int connectedRssi = 0;
|
|
|
|
String deviceName = "";
|
|
|
|
bool? deviceIndex0 = true;
|
|
bool? deviceIndex1 = false;
|
|
bool? deviceIndex2 = false;
|
|
|
|
BlueteethBindModel();
|
|
|
|
// 从JSON反序列化时的异常处理
|
|
|
|
factory BlueteethBindModel.fromJson(Map<String, dynamic> json) {
|
|
try {
|
|
return _$BlueteethBindModelFromJson(json);
|
|
} catch (e) {
|
|
// 在实际应用中,应该有更细致的异常处理策略和错误日志
|
|
return BlueteethBindModel(); // 或者返回一个带有错误信息的特定DeviceInfoModel实例
|
|
}
|
|
}
|
|
|
|
// 序列化为JSON时的异常处理
|
|
Map<String, dynamic> toJson() => _$BlueteethBindModelToJson(this);
|
|
}
|
|
|
|
class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
|
|
BlueteethBindController() {
|
|
attr = GetModel(BlueteethBindModel()).obs;
|
|
}
|
|
|
|
Timer? _statusTimer;
|
|
|
|
BLEDevice? currentDevice;
|
|
|
|
// 启动每10秒获取设备状态
|
|
void startStatusPolling() {
|
|
if (_statusTimer == null) {
|
|
_statusTimer = Timer.periodic(Duration(seconds: 10), (timer) {
|
|
updateDeviceStatus();
|
|
});
|
|
}
|
|
}
|
|
|
|
// 停止轮询
|
|
void stopStatusPolling() {
|
|
_statusTimer?.cancel();
|
|
_statusTimer = null;
|
|
}
|
|
|
|
// 你的已有方法
|
|
Future<void> 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';
|
|
}
|
|
|
|
var response = await EasyDartModule.dio.get(queryUrl);
|
|
|
|
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));
|
|
}
|
|
print("获取设备状态成功");
|
|
updateAll();
|
|
}
|
|
} catch (e) {
|
|
print("获取设备状态异常: $e");
|
|
EasyDartModule.logger.info("获取设备状态异常: $e");
|
|
DailyLogUtils.writeLog("获取设备状态异常: $e");
|
|
}
|
|
}
|
|
|
|
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}";
|
|
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(apiResponse, "蓝牙绑定.绑定成功".tr, "蓝牙绑定.绑定成功".tr);
|
|
if (res.code == HttpStatusCodes.ok) {
|
|
return res;
|
|
}
|
|
} else {
|
|
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
|
}
|
|
return apiResponse;
|
|
} catch (e) {
|
|
EasyDartModule.logger.info("蓝牙绑定.绑定异常: $e");
|
|
DailyLogUtils.writeLog("蓝牙绑定.绑定异常: $e");
|
|
}
|
|
return ApiResponse(code: -1, msg: "未知错误".tr); // Default return statement
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
stopStatusPolling(); // 控制器销毁时停止轮询
|
|
super.onClose();
|
|
}
|
|
|
|
bindDevice(Map<String, dynamic> map) {}
|
|
}
|