245 lines
7.8 KiB
Dart
245 lines
7.8 KiB
Dart
import 'package:easydevice/easydevice.dart';
|
|
import 'package:ef/ef.dart';
|
|
import 'package:json_annotation/json_annotation.dart';
|
|
import 'package:vbvs_app/common/util/BluetoothFirmwareUpdater.dart';
|
|
import 'package:vbvs_app/common/util/FirmwareVersionService.dart';
|
|
import 'package:vbvs_app/common/util/MyUtils.dart';
|
|
import 'package:vbvs_app/component/tool/NewTopSlideNotification.dart';
|
|
import 'package:vbvs_app/enum/APPDeviceUpgrade.dart';
|
|
import 'package:vbvs_app/pages/mh_page/device/controller/mht_bluetooth_controller.dart';
|
|
import 'package:vbvs_app/pages/mh_page/device/model/BlueToothDataModel.dart';
|
|
|
|
part 'device_upgrade_controller.g.dart';
|
|
|
|
@JsonSerializable()
|
|
class MHTDeviceUpgradeModel {
|
|
bool? bluetooth = false; // 蓝牙开关
|
|
double? singal = -100;
|
|
|
|
@JsonKey(ignore: true)
|
|
List<BlueToothDataModel>? blueRawData = []; //蓝牙原始数据
|
|
@JsonKey(ignore: true)
|
|
List<BlueToothDataModel>? upgradingDevices = []; // 正在升级的设备列表
|
|
|
|
MHTDeviceUpgradeModel();
|
|
|
|
static MHTDeviceUpgradeModel fromJson(Map<String, dynamic> json) =>
|
|
_$MHTDeviceUpgradeModelFromJson(json);
|
|
Map<String, dynamic> toJson() => _$MHTDeviceUpgradeModelToJson(this);
|
|
}
|
|
|
|
class MHTDeviceUpgradeController
|
|
extends GetControllerEx<MHTDeviceUpgradeModel> {
|
|
MHTDeviceUpgradeController() {
|
|
attr = GetModel(MHTDeviceUpgradeModel()).obs;
|
|
}
|
|
|
|
//选择的固件
|
|
FirmwareVersionInfo? selectVerison; //当前选中的固件信息
|
|
int maxLimit = 5;
|
|
MHTBlueToothController mhtBlueToothController = Get.find();
|
|
int? upgradeType = 1;
|
|
|
|
List<FirmwareVersionInfo>? firmwareList = [];
|
|
|
|
RxList? selectedDiseaseIds = [].obs; //已选中的列表
|
|
RxList diseaseList = [].obs; //升级类型列表
|
|
|
|
// 添加设备到升级列表
|
|
void addToUpgradingList(BlueToothDataModel device) {
|
|
if (!model.upgradingDevices!.any((d) => d.mac == device.mac)) {
|
|
model.upgradingDevices!.add(device);
|
|
update();
|
|
}
|
|
}
|
|
|
|
// 从升级列表移除设备
|
|
void removeFromUpgradingList(String mac) {
|
|
//todo 移除蓝牙连接
|
|
model.upgradingDevices!.removeWhere((device) => device.mac == mac);
|
|
update();
|
|
}
|
|
|
|
// 清空升级列表
|
|
void clearUpgradingList() {
|
|
//todo 移除蓝牙连接
|
|
model.upgradingDevices!.clear();
|
|
update();
|
|
}
|
|
|
|
// 获取正在升级的设备数量
|
|
int get upgradingDeviceCount => model.upgradingDevices?.length ?? 0;
|
|
|
|
// 检查设备是否正在升级
|
|
bool isDeviceUpgrading(String mac) {
|
|
return model.upgradingDevices!.any((device) => device.mac == mac);
|
|
}
|
|
|
|
//开始更新
|
|
Future<void> startUpgrade() async {
|
|
// 获取全部设备数据
|
|
var allDevices = model.blueRawData;
|
|
|
|
// 获取正在升级的设备数据
|
|
var upgradingDevices = model.upgradingDevices;
|
|
|
|
// 筛选出所有选中的设备
|
|
var selectedDevices =
|
|
allDevices!.where((device) => device.selected == true).toList();
|
|
|
|
// 排除正在升级的设备(即将已经在升级中的设备从选中的设备列表中移除)
|
|
var devicesToUpgrade = selectedDevices.where((device) {
|
|
// 检查设备是否在正在升级的设备列表中
|
|
return !upgradingDevices!
|
|
.any((upgradingDevice) => upgradingDevice.mac == device.mac);
|
|
}).toList();
|
|
|
|
if (devicesToUpgrade.isEmpty) {
|
|
NewTopSlideNotification.show(
|
|
text: "没有需要升级的设备".tr, textColor: themeController.currentColor.sc9);
|
|
ef.log("没有需要升级的设备");
|
|
return;
|
|
}
|
|
|
|
// 检查固件信息
|
|
if (selectVerison == null || selectVerison!.downloadUrl.isEmpty) {
|
|
NewTopSlideNotification.show(
|
|
text: "请先选择有效的固件版本".tr, textColor: themeController.currentColor.sc9);
|
|
throw Exception("请先选择有效的固件版本");
|
|
}
|
|
|
|
// mhtBlueToothController.currentUpgradeUrl = selectVerison!.downloadUrl;
|
|
// 打印最终需要升级的设备
|
|
ef.log("需要升级的设备:${devicesToUpgrade}");
|
|
|
|
for (var device in devicesToUpgrade) {
|
|
// await _startSingleDeviceUpgrade(device);
|
|
_startSingleDeviceUpgrade(device);
|
|
}
|
|
if (devicesToUpgrade.isNotEmpty) {
|
|
devicesToUpgrade.forEach((device) {
|
|
// 添加设备到正在升级列表
|
|
device.process = 0;
|
|
device.upgradeInfo = selectVerison;
|
|
addToUpgradingList(device);
|
|
// 发送升级指令
|
|
// mhtBlueToothController.sendUpgradeCommand(device);
|
|
});
|
|
}
|
|
print(model.upgradingDevices!);
|
|
}
|
|
|
|
// 启动单个设备升级
|
|
Future<void> _startSingleDeviceUpgrade(BlueToothDataModel device) async {
|
|
try {
|
|
// 更新设备状态
|
|
device.process = 0;
|
|
device.upgradeInfo = selectVerison;
|
|
device.selected = false; // 升级开始后取消选中状态
|
|
device.upgradeStatus = APPDeviceUpgrade.upgrading.value;
|
|
|
|
// 添加到升级列表
|
|
addToUpgradingList(device);
|
|
|
|
// 创建 THapp 实例
|
|
final thapp = THapp(device: device.scanResult.device);
|
|
|
|
// 确保设备连接
|
|
if (!thapp.isConnected) {
|
|
await thapp.device.connect();
|
|
}
|
|
|
|
// 使用 MultiDeviceFirmwareUpdater 开始升级
|
|
await MultiDeviceFirmwareUpdater().startUpgrade(
|
|
thapp: thapp,
|
|
mac: device.mac,
|
|
firmwareUrl: selectVerison!.downloadUrl,
|
|
);
|
|
|
|
ef.log("设备 ${device.mac} 升级任务已启动");
|
|
} catch (e, stack) {
|
|
ef.log("设备 ${device.mac} 升级启动失败: $e\n$stack");
|
|
|
|
// 更新设备状态为失败
|
|
device.process = -1;
|
|
device.upgradeStatus = APPDeviceUpgrade.fail.value;
|
|
update();
|
|
|
|
// 从升级列表中移除失败设备(可选,取决于业务需求)
|
|
// removeFromUpgradingList(device.mac);
|
|
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
// 取消单个设备升级
|
|
void cancelUpgrade(String mac) {
|
|
try {
|
|
MultiDeviceFirmwareUpdater().cancelUpgrade(mac);
|
|
removeFromUpgradingList(mac);
|
|
ef.log("设备 $mac 升级已取消");
|
|
|
|
} catch (e, stack) {
|
|
ef.log("取消设备 $mac 升级失败: $e\n$stack");
|
|
}
|
|
}
|
|
|
|
// 取消所有设备升级
|
|
void cancelAllUpgrades() {
|
|
try {
|
|
MultiDeviceFirmwareUpdater().cancelAllUpgrades();
|
|
clearUpgradingList();
|
|
ef.log("所有设备升级已取消");
|
|
} catch (e, stack) {
|
|
ef.log("取消所有设备升级失败: $e\n$stack");
|
|
}
|
|
}
|
|
|
|
// 获取设备升级状态
|
|
Map<String, dynamic>? getUpgradeStatus(String mac) {
|
|
return MultiDeviceFirmwareUpdater().getUpgradeStatus(mac);
|
|
}
|
|
|
|
// 获取所有设备升级状态
|
|
Map<String, Map<String, dynamic>> getAllUpgradeStatus() {
|
|
return MultiDeviceFirmwareUpdater().getAllUpgradeStatus();
|
|
}
|
|
|
|
// 检查设备是否正在升级(包括底层状态)
|
|
bool isDeviceInUpgrading(String mac) {
|
|
return MultiDeviceFirmwareUpdater().isDeviceUpgrading(mac) ||
|
|
isDeviceUpgrading(mac);
|
|
}
|
|
|
|
// 清理已完成的任务
|
|
void cleanupCompletedTasks() {
|
|
MultiDeviceFirmwareUpdater().cleanupCompletedTasks();
|
|
|
|
// 同时清理本地升级列表中已完成的任务
|
|
model.upgradingDevices!.removeWhere((device) {
|
|
final status = getUpgradeStatus(device.mac);
|
|
return status == null ||
|
|
['completed', 'failed', 'cancelled'].contains(status['status']);
|
|
});
|
|
update();
|
|
}
|
|
|
|
// 更新设备进度(供 MultiDeviceFirmwareUpdater 回调使用)
|
|
void updateDeviceProgress(String mac, int progress, String status) {
|
|
final device = model.upgradingDevices!.firstWhere((d) => d.mac == mac);
|
|
|
|
if (device != null) {
|
|
device.process = progress;
|
|
|
|
// 如果升级完成或失败,更新相关状态
|
|
if (status == 'completed' && progress == 100) {
|
|
device.newVersion = int.parse(selectVerison?.version ?? '0');
|
|
// 可以在这里添加其他完成后的逻辑
|
|
} else if (status == 'failed') {
|
|
device.process = -1;
|
|
}
|
|
update();
|
|
}
|
|
}
|
|
}
|