|
|
|
@ -20,21 +20,39 @@ class OtaUpgradeController extends GetxController { |
|
|
|
static const int CONNECTION_DISCONNECTION = 5; // 连接断开 |
|
|
|
static const int CONNECTION_SUCCESS = 6; // 连接成功 |
|
|
|
static const int DOWNLOADING_UP_FILES = 7; // 下载升级文件 |
|
|
|
|
|
|
|
// 错误信息,用于存储错误提示 |
|
|
|
final RxString errorMessage = ''.obs; |
|
|
|
final RxBool isFetchingDeviceInfo = false.obs; |
|
|
|
|
|
|
|
// 最新版本号 |
|
|
|
final RxString latestVersion = ''.obs; |
|
|
|
|
|
|
|
// 当前设备固件版本号 |
|
|
|
final RxString currentVersion = ''.obs; |
|
|
|
|
|
|
|
// 更新包地址,用于下载更新文件 |
|
|
|
String updatepackageaddress = ''; |
|
|
|
|
|
|
|
// 下载进度,范围为 0.0 到 1.0 |
|
|
|
final RxDouble downloadProgress = 0.0.obs; |
|
|
|
|
|
|
|
// 是否有可用更新的状态 |
|
|
|
final isUpdateAvailable = true.obs; |
|
|
|
//int currentProgress = 0; // 当前进度值,范围0-100 |
|
|
|
|
|
|
|
// 当前升级进度,范围为 0.0 到 1.0 |
|
|
|
final RxDouble progress = 0.0.obs; |
|
|
|
|
|
|
|
// OTA 单例实例,用于处理升级相关逻辑 |
|
|
|
final jlota = Ota.instance; |
|
|
|
|
|
|
|
// 是否正在下载更新包的状态 |
|
|
|
final RxBool isDownloading = false.obs; |
|
|
|
// 蓝牙服务 |
|
|
|
|
|
|
|
// 蓝牙管理服务,用于获取设备信息和通信 |
|
|
|
final _bleManager = Get.find<BleManager>(); |
|
|
|
final maxRetryAttempts = 3; // 最大重试次数 |
|
|
|
int currentRetryCount = 0; |
|
|
|
final RxBool isFetchingDeviceInfo = true.obs; |
|
|
|
@override |
|
|
|
void onInit() { |
|
|
|
super.onInit(); |
|
|
|
@ -92,33 +110,38 @@ class OtaUpgradeController extends GetxController { |
|
|
|
|
|
|
|
Future<void> _getCurrentVersion() async { |
|
|
|
try { |
|
|
|
isFetchingDeviceInfo.value = true; |
|
|
|
errorMessage.value = ''; |
|
|
|
// 添加获取设备信息的重试机制 |
|
|
|
DeviceInfo? deviceInfo = _bleManager.deviceInfo.value; |
|
|
|
|
|
|
|
// 第一次检查设备信息 |
|
|
|
if (deviceInfo == null) { |
|
|
|
// 显示加载提示 |
|
|
|
isFetchingDeviceInfo.value = true; |
|
|
|
// // 第一次检查设备信息 |
|
|
|
// if (deviceInfo == null) { |
|
|
|
// // 尝试重新获取设备信息 |
|
|
|
// await _bleManager.fetchDeviceInfo(); |
|
|
|
// } |
|
|
|
|
|
|
|
// 尝试重新获取设备信息 |
|
|
|
await _bleManager.fetchDeviceInfo(); |
|
|
|
|
|
|
|
// 等待后再次获取设备信息 |
|
|
|
while (currentRetryCount < maxRetryAttempts) { |
|
|
|
deviceInfo = _bleManager.deviceInfo.value; |
|
|
|
isFetchingDeviceInfo.value = false; |
|
|
|
|
|
|
|
// 仍然为空则抛出异常 |
|
|
|
if (deviceInfo == null) { |
|
|
|
throw const FormatException('无法获取设备信息,请检查设备连接'); |
|
|
|
// 检查信息有效性 |
|
|
|
if (deviceInfo != null && |
|
|
|
deviceInfo.firmwareVersion.isNotEmpty && |
|
|
|
deviceInfo.firmwareProductId.isNotEmpty) { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 检查固件信息有效性 |
|
|
|
if (deviceInfo.firmwareVersion.isEmpty || |
|
|
|
// 获取设备信息 |
|
|
|
await _bleManager.fetchDeviceInfo(); |
|
|
|
await Future.delayed(const Duration(seconds: 5)); |
|
|
|
currentRetryCount++; |
|
|
|
} |
|
|
|
// 最终验证 |
|
|
|
if (deviceInfo == null || |
|
|
|
deviceInfo.firmwareVersion.isEmpty || |
|
|
|
deviceInfo.firmwareProductId.isEmpty) { |
|
|
|
throw const FormatException('设备固件信息不完整'); |
|
|
|
throw "获取设备信息失败,请检查连接"; |
|
|
|
} |
|
|
|
|
|
|
|
// 正常流程 |
|
|
|
currentVersion.value = deviceInfo.firmwareVersion; |
|
|
|
print('getequipmentinfo,id: ${deviceInfo.firmwareProductId}'); |
|
|
|
@ -134,7 +157,8 @@ class OtaUpgradeController extends GetxController { |
|
|
|
|
|
|
|
final resp = OtaConfigResp.fromJson(response); |
|
|
|
print('getequipmentinfo,resp.systemversion: ${resp.systemversion}'); |
|
|
|
latestVersion.value = "1"; |
|
|
|
latestVersion.value = resp.systemversion!; |
|
|
|
|
|
|
|
updatepackageaddress = resp.updatepackageaddress!; |
|
|
|
|
|
|
|
// 版本检查 |
|
|
|
|