From a49b291041e3222df7fb8af82b4bd47d8b0ff095 Mon Sep 17 00:00:00 2001 From: lxm <1274844347@qq.com> Date: Sat, 23 Aug 2025 19:23:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(home):=20=E6=B7=BB=E5=8A=A0OTA=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E6=A3=80=E6=9F=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在连接成功后自动检查OTA固件升级 - 显示升级提醒对话框并提供升级选项 - 将充电盒标签改为国际化字符串 --- .../home/controllers/home_controller.dart | 160 ++++++++++++++++++ lib/modules/home/views/home_view.dart | 2 +- 2 files changed, 161 insertions(+), 1 deletion(-) diff --git a/lib/modules/home/controllers/home_controller.dart b/lib/modules/home/controllers/home_controller.dart index 83a2330e2..68c70b643 100644 --- a/lib/modules/home/controllers/home_controller.dart +++ b/lib/modules/home/controllers/home_controller.dart @@ -1,3 +1,4 @@ +import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; import 'dart:async'; import '../../../data/services/novice_guide_service.dart'; @@ -6,6 +7,7 @@ import '../../../core/utils/logger.dart'; import 'package:flutter/material.dart'; import '../../../data/services/bluetooth_manager.dart'; import '../../../data/services/ble_manager.dart'; +import '../../ota_upgrade/controllers/ota_core_contropller.dart'; /// 连接状态枚举 enum ConnectionStatus { @@ -28,6 +30,10 @@ class HomeController extends GetxController { static const Duration _batteryRefreshInterval = Duration(seconds: 2); // 2秒刷新一次 + // 添加OTA升级检查相关变量 + final _otaCoreService = OtaCoreService(); + static bool _hasCheckedOta = false; // 防止重复检查 + bool get isFullyConnected => _isFullyConnected.value; RxBool get isFullyConnectedRx => _isFullyConnected; BluetoothDevice? get currentDevice => _bluetoothManager.currentDevice; @@ -145,12 +151,18 @@ class HomeController extends GetxController { Future.delayed(const Duration(milliseconds: 500), () { _refreshBatteryInfo(); }); + + // 连接成功后检查OTA升级 + Future.delayed(const Duration(seconds: 2), () { + _checkOtaUpgrade(); + }); } // 根据连接状态控制定时器 if (isNowConnected) { _startBatteryRefreshTimer(); // 连接时启动定时器 } else { _stopBatteryRefreshTimer(); // 断开时停止定时器 + _hasCheckedOta = false; // 重置OTA检查状态 } } @@ -161,6 +173,7 @@ class HomeController extends GetxController { _isFullyConnected.value = false; _isFullyConnected.refresh(); _stopBatteryRefreshTimer(); // 发生错误时停止定时器 + _hasCheckedOta = false; // 重置OTA检查状态 update(); } } @@ -327,4 +340,151 @@ class HomeController extends GetxController { //Logger.e(_tag, '刷新电池信息时发生错误: $e'); } } + + /// 检查OTA固件升级 + /// 在BLE连接成功后调用,检查是否需要升级 + Future _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, + ), + ); + } } diff --git a/lib/modules/home/views/home_view.dart b/lib/modules/home/views/home_view.dart index d24e726fb..5afc3586f 100644 --- a/lib/modules/home/views/home_view.dart +++ b/lib/modules/home/views/home_view.dart @@ -504,7 +504,7 @@ class HomeView extends GetView { // 充电盒 _buildCompactBatteryItem( - label: '充电盒', + label: 'chargingCase'.tr, batteryLevel: caseBattery.value, isCharging: caseCharging.value, isDarkMode: isDarkMode,