Browse Source

feat(home): 添加OTA升级检查功能

- 在连接成功后自动检查OTA固件升级
- 显示升级提醒对话框并提供升级选项
- 将充电盒标签改为国际化字符串
newdev_shunjiawei
lxm 1 year ago
parent
commit
a49b291041
  1. 160
      lib/modules/home/controllers/home_controller.dart
  2. 2
      lib/modules/home/views/home_view.dart

160
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<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,
),
);
}
}

2
lib/modules/home/views/home_view.dart

@ -504,7 +504,7 @@ class HomeView extends GetView<HomeController> {
// 充电盒
_buildCompactBatteryItem(
label: '充电盒',
label: 'chargingCase'.tr,
batteryLevel: caseBattery.value,
isCharging: caseCharging.value,
isDarkMode: isDarkMode,

Loading…
Cancel
Save