You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.2 KiB
110 lines
3.2 KiB
// import 'dart:ffi'; // Not supported on web; not needed here
|
|
|
|
import 'package:voitrans/data/models/appconfig_model.dart';
|
|
|
|
import '/core/utils/logger.dart';
|
|
import '/data/models/user_Info.dart';
|
|
import '/data/services/ble_manager.dart';
|
|
import '/data/services/network/api.dart';
|
|
import '/routes/app_routes.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
/*
|
|
设备管理
|
|
*/
|
|
class DevicesController extends GetxController {
|
|
late final BleManager _bleManager;
|
|
|
|
// 修改为响应式变量
|
|
final RxMap<int, List<UserDevice>> devices = <int, List<UserDevice>>{}.obs;
|
|
|
|
// 或者使用这种方式
|
|
// final Rx<Map<int, List<UserDevice>>> devices = Rx<Map<int, List<UserDevice>>>({});
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
print("初始化设备管理1");
|
|
_bleManager = Get.find<BleManager>();
|
|
print("初始化设备管理2");
|
|
// 在初始化时加载设备数据
|
|
_loadDevices();
|
|
}
|
|
|
|
// 加载设备数据
|
|
void _loadDevices() async {
|
|
Logger.info("加载设备");
|
|
final response = await Api.getdevices();
|
|
if (response != null) {
|
|
if (response['devices'] != null) {
|
|
var devices = (response['devices'] as List<dynamic>)
|
|
.map((e) => UserDevice.fromJson(e as Map<String, dynamic>))
|
|
.toList();
|
|
User.instance.devices = devices;
|
|
} else {
|
|
User.instance.devices = [];
|
|
}
|
|
Logger.info("加载设备 ${User.instance.devices.length}");
|
|
}
|
|
devices.clear();
|
|
for (var device in User.instance.devices) {
|
|
if (!devices.containsKey(device.devicetype)) {
|
|
devices[device.devicetype] = [];
|
|
}
|
|
devices[device.devicetype]!.add(device);
|
|
}
|
|
devices.refresh();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
super.onClose();
|
|
// onClose 中不应该修改数据,这里应该是清理资源
|
|
}
|
|
|
|
void unbindearphone(UserDevice device) async {
|
|
final response = await Api.unbinddevice({
|
|
"id": device.id,
|
|
});
|
|
Logger.info("解绑设备 $response");
|
|
if (response != null) {
|
|
// 响应式更新
|
|
User.instance.devices.remove(device);
|
|
devices.clear();
|
|
for (var device in User.instance.devices) {
|
|
if (!devices.containsKey(device.devicetype)) {
|
|
devices[device.devicetype] = [];
|
|
}
|
|
devices[device.devicetype]!.add(device);
|
|
}
|
|
devices.refresh();
|
|
Logger.info(
|
|
"解绑设备 ${device.devicemac} 当前使用设备:${_bleManager.currentDeviceAddress}");
|
|
if (device.devicemac == _bleManager.currentDeviceAddress) {
|
|
Logger.info("解绑设备 $device 重新匹配!");
|
|
User.instance.removeUserDevice(device.id);
|
|
_bleManager.clearPairedDevices();
|
|
_bleManager.disconnect();
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
Get.toNamed(Routes.pairing);
|
|
}
|
|
return;
|
|
} else {
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 添加设备
|
|
void addDevice(UserDevice device) {
|
|
if (!devices.containsKey(device.devicetype)) {
|
|
devices[device.devicetype] = [];
|
|
}
|
|
devices[device.devicetype]!.add(device);
|
|
devices.refresh(); // 触发UI更新
|
|
}
|
|
|
|
// 获取指定类型的设备列表
|
|
List<UserDevice> getDevicesByType(int deviceType) {
|
|
return devices[deviceType] ?? [];
|
|
}
|
|
}
|
|
|