Browse Source

优化获取设备信息

newdev_shunjiawei
fdp 1 year ago
parent
commit
87417a508c
  1. 10
      lib/data/services/ble_manager.dart
  2. 66
      lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart
  3. 2
      lib/modules/ota_upgrade/views/ota_upgrade_view.dart

10
lib/data/services/ble_manager.dart

@ -159,7 +159,7 @@ class BleManager extends GetxService {
final state = event['state'] as int; final state = event['state'] as int;
final oldState = _connectionState.value; final oldState = _connectionState.value;
_connectionState.value = state; _connectionState.value = state;
Logger.d('BleManager', '🔄 BLE状态更新: $oldState → $state'); Logger.d('BleManager', '🔄 BLE状态更新: $oldState → $state');
Logger.d('BleManager', '📡 通知监听器状态变化: ${_connectionState.value}'); Logger.d('BleManager', '📡 通知监听器状态变化: ${_connectionState.value}');
@ -418,7 +418,8 @@ class BleManager extends GetxService {
} else { } else {
Logger.warning('电池信息获取指令发送失败'); Logger.warning('电池信息获取指令发送失败');
} }
// 添加延迟
await Future.delayed(const Duration(seconds: 1));
// 获取设备版本信息 // 获取设备版本信息
final versionResult = await _bleService.getDeviceVersion(); final versionResult = await _bleService.getDeviceVersion();
if (versionResult) { if (versionResult) {
@ -426,7 +427,8 @@ class BleManager extends GetxService {
} else { } else {
Logger.warning('版本信息获取指令发送失败'); Logger.warning('版本信息获取指令发送失败');
} }
// 添加延迟
await Future.delayed(const Duration(seconds: 1));
// 获取产品id // 获取产品id
final productIdResult = await _bleService.getProductId(); final productIdResult = await _bleService.getProductId();
if (productIdResult) { if (productIdResult) {
@ -655,7 +657,7 @@ class BleManager extends GetxService {
} }
// ==================== 配对相关方法 ==================== // ==================== 配对相关方法 ====================
/// 检查设备是否支持Companion Device Manager /// 检查设备是否支持Companion Device Manager
Future<bool> isCompanionSupported() async { Future<bool> isCompanionSupported() async {
try { try {

66
lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart

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

2
lib/modules/ota_upgrade/views/ota_upgrade_view.dart

@ -182,7 +182,7 @@ class OtaUpgradeView extends GetView<OtaUpgradeController> {
isDarkMode ? Colors.blue[300] : Colors.blue), isDarkMode ? Colors.blue[300] : Colors.blue),
), ),
SizedBox(height: 20), SizedBox(height: 20),
Text('正在连接设备...', Text('正在获取设备信息...',
style: style:
TextStyle(color: isDarkMode ? Colors.grey[300] : Colors.grey)) TextStyle(color: isDarkMode ? Colors.grey[300] : Colors.grey))
], ],

Loading…
Cancel
Save