|
|
@ -1,3 +1,4 @@ |
|
|
|
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart'; |
|
|
import 'package:get/get.dart'; |
|
|
import 'package:get/get.dart'; |
|
|
import 'dart:async'; |
|
|
import 'dart:async'; |
|
|
import '../../../data/services/novice_guide_service.dart'; |
|
|
import '../../../data/services/novice_guide_service.dart'; |
|
|
@ -6,6 +7,7 @@ import '../../../core/utils/logger.dart'; |
|
|
import 'package:flutter/material.dart'; |
|
|
import 'package:flutter/material.dart'; |
|
|
import '../../../data/services/bluetooth_manager.dart'; |
|
|
import '../../../data/services/bluetooth_manager.dart'; |
|
|
import '../../../data/services/ble_manager.dart'; |
|
|
import '../../../data/services/ble_manager.dart'; |
|
|
|
|
|
import '../../ota_upgrade/controllers/ota_core_contropller.dart'; |
|
|
|
|
|
|
|
|
/// 连接状态枚举 |
|
|
/// 连接状态枚举 |
|
|
enum ConnectionStatus { |
|
|
enum ConnectionStatus { |
|
|
@ -28,6 +30,10 @@ class HomeController extends GetxController { |
|
|
static const Duration _batteryRefreshInterval = |
|
|
static const Duration _batteryRefreshInterval = |
|
|
Duration(seconds: 2); // 2秒刷新一次 |
|
|
Duration(seconds: 2); // 2秒刷新一次 |
|
|
|
|
|
|
|
|
|
|
|
// 添加OTA升级检查相关变量 |
|
|
|
|
|
final _otaCoreService = OtaCoreService(); |
|
|
|
|
|
static bool _hasCheckedOta = false; // 防止重复检查 |
|
|
|
|
|
|
|
|
bool get isFullyConnected => _isFullyConnected.value; |
|
|
bool get isFullyConnected => _isFullyConnected.value; |
|
|
RxBool get isFullyConnectedRx => _isFullyConnected; |
|
|
RxBool get isFullyConnectedRx => _isFullyConnected; |
|
|
BluetoothDevice? get currentDevice => _bluetoothManager.currentDevice; |
|
|
BluetoothDevice? get currentDevice => _bluetoothManager.currentDevice; |
|
|
@ -145,12 +151,18 @@ class HomeController extends GetxController { |
|
|
Future.delayed(const Duration(milliseconds: 500), () { |
|
|
Future.delayed(const Duration(milliseconds: 500), () { |
|
|
_refreshBatteryInfo(); |
|
|
_refreshBatteryInfo(); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 连接成功后检查OTA升级 |
|
|
|
|
|
Future.delayed(const Duration(seconds: 2), () { |
|
|
|
|
|
_checkOtaUpgrade(); |
|
|
|
|
|
}); |
|
|
} |
|
|
} |
|
|
// 根据连接状态控制定时器 |
|
|
// 根据连接状态控制定时器 |
|
|
if (isNowConnected) { |
|
|
if (isNowConnected) { |
|
|
_startBatteryRefreshTimer(); // 连接时启动定时器 |
|
|
_startBatteryRefreshTimer(); // 连接时启动定时器 |
|
|
} else { |
|
|
} else { |
|
|
_stopBatteryRefreshTimer(); // 断开时停止定时器 |
|
|
_stopBatteryRefreshTimer(); // 断开时停止定时器 |
|
|
|
|
|
_hasCheckedOta = false; // 重置OTA检查状态 |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -161,6 +173,7 @@ class HomeController extends GetxController { |
|
|
_isFullyConnected.value = false; |
|
|
_isFullyConnected.value = false; |
|
|
_isFullyConnected.refresh(); |
|
|
_isFullyConnected.refresh(); |
|
|
_stopBatteryRefreshTimer(); // 发生错误时停止定时器 |
|
|
_stopBatteryRefreshTimer(); // 发生错误时停止定时器 |
|
|
|
|
|
_hasCheckedOta = false; // 重置OTA检查状态 |
|
|
update(); |
|
|
update(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -327,4 +340,151 @@ class HomeController extends GetxController { |
|
|
//Logger.e(_tag, '刷新电池信息时发生错误: $e'); |
|
|
//Logger.e(_tag, '刷新电池信息时发生错误: $e'); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 检查OTA固件升级 |
|
|
|
|
|
/// 在BLE连接成功后调用,检查是否需要升级 |
|
|
|
|
|
Future<void> _checkOtaUpgrade() async { |
|
|
|
|
|
try { |
|
|
|
|
|
// 防止重复检查 |
|
|
|
|
|
if (_hasCheckedOta) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Logger.d(_tag, '开始检查OTA固件升级...'); |
|
|
|
|
|
|
|
|
|
|
|
// 检查BLE连接状态 |
|
|
|
|
|
if (!_isFullyConnected.value) { |
|
|
|
|
|
Logger.d(_tag, '设备未完全连接,跳过OTA检查'); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 获取当前版本信息 |
|
|
|
|
|
final currentInfo = await _otaCoreService.getCurrentVersionInfo(); |
|
|
|
|
|
final currentVersion = currentInfo['currentVersion'] as String; |
|
|
|
|
|
|
|
|
|
|
|
// 获取最新版本信息 |
|
|
|
|
|
final latestInfo = await _otaCoreService.getLatestVersionInfo(); |
|
|
|
|
|
final latestVersion = latestInfo['latestVersion'] as String; |
|
|
|
|
|
|
|
|
|
|
|
Logger.d(_tag, '当前版本: $currentVersion, 最新版本: $latestVersion'); |
|
|
|
|
|
|
|
|
|
|
|
// 检查是否有更新 |
|
|
|
|
|
final hasUpdate = |
|
|
|
|
|
_otaCoreService.isNewVersionAvailable(currentVersion, latestVersion); |
|
|
|
|
|
|
|
|
|
|
|
if (hasUpdate) { |
|
|
|
|
|
Logger.d(_tag, '发现新版本,显示升级提醒'); |
|
|
|
|
|
_showOtaUpgradeDialog( |
|
|
|
|
|
latestVersion, latestInfo['updateDescription'] as String?); |
|
|
|
|
|
} else { |
|
|
|
|
|
Logger.d(_tag, '当前已是最新版本'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_hasCheckedOta = true; |
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
Logger.e(_tag, '检查OTA升级失败: $e'); |
|
|
|
|
|
// 即使检查失败,也标记为已检查,避免重复尝试 |
|
|
|
|
|
_hasCheckedOta = true; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 显示OTA升级提醒对话框 |
|
|
|
|
|
/// [latestVersion] 最新版本号 |
|
|
|
|
|
/// [updateDescription] 更新描述 |
|
|
|
|
|
void _showOtaUpgradeDialog(String latestVersion, String? updateDescription) { |
|
|
|
|
|
Get.dialog( |
|
|
|
|
|
AlertDialog( |
|
|
|
|
|
title: Row( |
|
|
|
|
|
children: [ |
|
|
|
|
|
Icon( |
|
|
|
|
|
Icons.headset, |
|
|
|
|
|
color: Colors.blue[600], |
|
|
|
|
|
size: 24.sp, |
|
|
|
|
|
), |
|
|
|
|
|
SizedBox(width: 8.w), |
|
|
|
|
|
Text( |
|
|
|
|
|
'ota_newVersionFound'.tr, |
|
|
|
|
|
style: TextStyle( |
|
|
|
|
|
color: Colors.blue[700], |
|
|
|
|
|
fontWeight: FontWeight.w600, |
|
|
|
|
|
), |
|
|
|
|
|
), |
|
|
|
|
|
], |
|
|
|
|
|
), |
|
|
|
|
|
content: Column( |
|
|
|
|
|
mainAxisSize: MainAxisSize.min, |
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start, |
|
|
|
|
|
children: [ |
|
|
|
|
|
Text( |
|
|
|
|
|
'${'latestFirmwareVersion'.tr}: $latestVersion', |
|
|
|
|
|
style: TextStyle( |
|
|
|
|
|
fontWeight: FontWeight.w500, |
|
|
|
|
|
fontSize: 16.sp, |
|
|
|
|
|
), |
|
|
|
|
|
), |
|
|
|
|
|
// if (updateDescription != null && updateDescription.isNotEmpty) |
|
|
|
|
|
// SizedBox(height: 8.sp), |
|
|
|
|
|
// if (updateDescription != null && updateDescription.isNotEmpty) |
|
|
|
|
|
// Text( |
|
|
|
|
|
// updateDescription, |
|
|
|
|
|
// style: TextStyle( |
|
|
|
|
|
// color: Colors.grey[600], |
|
|
|
|
|
// fontSize: 14.sp, |
|
|
|
|
|
// ), |
|
|
|
|
|
// ), |
|
|
|
|
|
// if (updateDescription != null && updateDescription.isNotEmpty) |
|
|
|
|
|
// SizedBox(height: 8.sp), |
|
|
|
|
|
// Text( |
|
|
|
|
|
// '建议及时升级以获得更好的使用体验', |
|
|
|
|
|
// style: TextStyle( |
|
|
|
|
|
// color: Colors.orange[700], |
|
|
|
|
|
// fontSize: 12.sp, |
|
|
|
|
|
// ), |
|
|
|
|
|
// ), |
|
|
|
|
|
], |
|
|
|
|
|
), |
|
|
|
|
|
actions: [ |
|
|
|
|
|
Row( |
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween, |
|
|
|
|
|
children: [ |
|
|
|
|
|
// 稍后再说 (靠左) |
|
|
|
|
|
TextButton( |
|
|
|
|
|
onPressed: () { |
|
|
|
|
|
Logger.d(_tag, '用户取消OTA升级'); |
|
|
|
|
|
Get.back(); |
|
|
|
|
|
}, |
|
|
|
|
|
child: Text( |
|
|
|
|
|
'talkLater'.tr, |
|
|
|
|
|
style: TextStyle(color: Colors.grey[600]), |
|
|
|
|
|
), |
|
|
|
|
|
), |
|
|
|
|
|
// 立即升级 (靠右) |
|
|
|
|
|
ElevatedButton( |
|
|
|
|
|
onPressed: () { |
|
|
|
|
|
Logger.d(_tag, '用户确认OTA升级,跳转到升级页面'); |
|
|
|
|
|
Get.back(); |
|
|
|
|
|
Get.toNamed(Routes.OTA_UPGRADE); |
|
|
|
|
|
}, |
|
|
|
|
|
style: ElevatedButton.styleFrom( |
|
|
|
|
|
backgroundColor: Colors.blue[600], |
|
|
|
|
|
foregroundColor: Colors.white, |
|
|
|
|
|
shape: RoundedRectangleBorder( |
|
|
|
|
|
borderRadius: BorderRadius.circular(8.sp), |
|
|
|
|
|
), |
|
|
|
|
|
), |
|
|
|
|
|
child: Text( |
|
|
|
|
|
'ota_updateNow'.tr, |
|
|
|
|
|
style: TextStyle(fontSize: 14.sp), |
|
|
|
|
|
), |
|
|
|
|
|
), |
|
|
|
|
|
], |
|
|
|
|
|
), |
|
|
|
|
|
], |
|
|
|
|
|
shape: RoundedRectangleBorder( |
|
|
|
|
|
borderRadius: BorderRadius.circular(12.sp), |
|
|
|
|
|
), |
|
|
|
|
|
backgroundColor: Colors.white, |
|
|
|
|
|
), |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|