165 lines
5.1 KiB
Dart
165 lines
5.1 KiB
Dart
// 批量升级封装类
|
||
import 'package:easydevice/easydevice.dart';
|
||
import 'package:ef/ef.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:vbvs_app/common/util/BluetoothFirmwareUpdater.dart';
|
||
import 'package:vbvs_app/component/tool/NewTopSlideNotification.dart';
|
||
import 'package:vbvs_app/pages/mh_page/device/model/BlueToothDataModel.dart';
|
||
import 'package:vbvs_app/pages/mh_page/device/upgrade/device_upgrade_controller.dart';
|
||
|
||
class BatchUpgradeManager {
|
||
static final BatchUpgradeManager _instance = BatchUpgradeManager._internal();
|
||
factory BatchUpgradeManager() => _instance;
|
||
BatchUpgradeManager._internal();
|
||
|
||
final MultiDeviceFirmwareUpdater _updater = MultiDeviceFirmwareUpdater();
|
||
final MHTDeviceUpgradeController _upgradeController = Get.find();
|
||
|
||
bool _isBatchUpgrading = false;
|
||
final List<BlueToothDataModel> _batchDevices = [];
|
||
|
||
// 开始批量升级
|
||
Future<void> startBatchUpgrade(List<BlueToothDataModel> devices) async {
|
||
if (_isBatchUpgrading) {
|
||
throw Exception("批量升级正在进行中".tr);
|
||
}
|
||
|
||
_isBatchUpgrading = true;
|
||
_batchDevices.clear();
|
||
_upgradeController.clearUpgradingList();
|
||
|
||
// 验证设备并添加到升级列表
|
||
for (var device in devices) {
|
||
if (device.selected == true) {
|
||
_batchDevices.add(device);
|
||
_upgradeController.addToUpgradingList(device);
|
||
}
|
||
}
|
||
|
||
if (_batchDevices.isEmpty) {
|
||
_isBatchUpgrading = false;
|
||
throw Exception("没有选择要升级的设备".tr);
|
||
}
|
||
|
||
// 开始批量升级
|
||
try {
|
||
await _startBatchUpgradeInternal();
|
||
} catch (e) {
|
||
_isBatchUpgrading = false;
|
||
rethrow;
|
||
}
|
||
}
|
||
|
||
// 内部批量升级实现
|
||
Future<void> _startBatchUpgradeInternal() async {
|
||
final List<Future> upgradeFutures = [];
|
||
|
||
for (var device in _batchDevices) {
|
||
final future = _startSingleDeviceUpgrade(device);
|
||
upgradeFutures.add(future);
|
||
}
|
||
|
||
// 等待所有升级完成
|
||
try {
|
||
await Future.wait(upgradeFutures, eagerError: false);
|
||
} finally {
|
||
_isBatchUpgrading = false;
|
||
_showBatchUpgradeCompleteNotification();
|
||
}
|
||
}
|
||
|
||
// 单个设备升级
|
||
Future<void> _startSingleDeviceUpgrade(BlueToothDataModel device) async {
|
||
try {
|
||
// 创建 THapp 实例
|
||
final thapp = THapp(device: device.scanResult.device);
|
||
|
||
// 获取固件URL - 需要您提供实现
|
||
final firmwareUrl = await _getFirmwareUrl();
|
||
if (firmwareUrl == null) {
|
||
throw Exception("固件URL为空".tr);
|
||
}
|
||
|
||
// 使用现有的升级器进行升级
|
||
await _updater.startUpgrade(
|
||
thapp: thapp,
|
||
mac: device.mac,
|
||
firmwareUrl: firmwareUrl,
|
||
);
|
||
|
||
// 升级成功,从列表中移除
|
||
_upgradeController.removeFromUpgradingList(device.mac);
|
||
|
||
} catch (e) {
|
||
// 升级失败,也从列表中移除(或者您可以保留失败设备用于重试)
|
||
_upgradeController.removeFromUpgradingList(device.mac);
|
||
rethrow;
|
||
}
|
||
}
|
||
|
||
// 获取固件URL - 需要您根据实际情况实现
|
||
Future<String?> _getFirmwareUrl() async {
|
||
// 这里可以从配置、用户选择或其他地方获取固件URL
|
||
// 例如:return _upgradeController.selectedFirmwareUrl;
|
||
return null;
|
||
}
|
||
|
||
// 取消批量升级
|
||
void cancelBatchUpgrade() {
|
||
for (var device in _batchDevices) {
|
||
if (_updater.isDeviceUpgrading(device.mac)) {
|
||
_updater.cancelUpgrade(device.mac);
|
||
}
|
||
_upgradeController.removeFromUpgradingList(device.mac);
|
||
}
|
||
|
||
_isBatchUpgrading = false;
|
||
_batchDevices.clear();
|
||
|
||
NewTopSlideNotification.show(
|
||
text: "批量升级已取消".tr,
|
||
textColor: Colors.orange,
|
||
);
|
||
}
|
||
|
||
// 显示批量升级完成通知
|
||
void _showBatchUpgradeCompleteNotification() {
|
||
final allStatus = _updater.getAllUpgradeStatus();
|
||
final completedCount = allStatus.values.where((status) =>
|
||
status['status'] == 'completed').length;
|
||
final failedCount = allStatus.values.where((status) =>
|
||
status['status'] == 'failed').length;
|
||
|
||
String message;
|
||
if (failedCount == 0) {
|
||
message = "批量升级完成,所有 ${_batchDevices.length} 个设备升级成功".tr;
|
||
} else {
|
||
message = "批量升级完成,$completedCount 个成功,$failedCount 个失败".tr;
|
||
}
|
||
|
||
NewTopSlideNotification.show(
|
||
text: message,
|
||
textColor: failedCount == 0 ? Colors.green : Colors.orange,
|
||
);
|
||
}
|
||
|
||
// 检查是否正在批量升级
|
||
bool get isBatchUpgrading => _isBatchUpgrading;
|
||
|
||
// 获取批量升级统计
|
||
Map<String, int> getBatchUpgradeStatistics() {
|
||
final allStatus = _updater.getAllUpgradeStatus();
|
||
|
||
return {
|
||
'total': _batchDevices.length,
|
||
'completed': allStatus.values.where((status) =>
|
||
status['status'] == 'completed').length,
|
||
'failed': allStatus.values.where((status) =>
|
||
status['status'] == 'failed').length,
|
||
'upgrading': allStatus.values.where((status) =>
|
||
status['status'] == 'upgrading' || status['status'] == 'downloading').length,
|
||
'waiting': allStatus.values.where((status) =>
|
||
status['status'] == 'waiting').length,
|
||
};
|
||
}
|
||
} |