Browse Source

feat(ota_upgrade): 添加电量检查逻辑防止低电量升级

在固件升级前检查设备电量,当左右耳电量有低于30%时阻止升级并提示用户
移除视图中的电量显示区域,因检查逻辑已移至控制器
weicu
lxm 1 year ago
parent
commit
ee3a652102
  1. 60
      lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart
  2. 54
      lib/modules/ota_upgrade/views/ota_upgrade_view.dart

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

@ -271,6 +271,12 @@ class OtaUpgradeController extends GetxController {
Get.back();
Future.delayed(Duration(milliseconds: 300));
// 检查设备电量
if (!_checkBatteryLevel()) {
return; // 电量不足,不执行升级
}
startDownload();
},
child: Text('更新'),
@ -399,6 +405,60 @@ class OtaUpgradeController extends GetxController {
// isUpgrade.value = false;
}
// 检查设备电量是否满足升级条件
bool _checkBatteryLevel() {
try {
final deviceInfo = _bleManager.deviceInfo.value;
if (deviceInfo == null) {
Get.snackbar(
'电量检查失败',
'无法获取设备信息,请检查设备连接',
snackPosition: SnackPosition.BOTTOM,
backgroundColor: Colors.orange,
colorText: Colors.white,
);
return false;
}
final leftBattery = deviceInfo.leftEarBattery;
final rightBattery = deviceInfo.rightEarBattery;
// 检查左右耳电量是否都大于30%
if (leftBattery <= 30 || rightBattery <= 30) {
String message = '设备电量不足,无法进行固件升级\n';
message += '升级要求:左右耳电量均需大于30%\n';
message += '当前电量:左耳 ${leftBattery}%,右耳 ${rightBattery}%\n';
message += '请充电后再试';
Get.dialog(
AlertDialog(
title: Text('电量不足'),
content: Text(message),
actions: [
TextButton(
onPressed: () => Get.back(),
child: Text('确定'),
),
],
),
);
return false;
}
return true;
} catch (e) {
Get.snackbar(
'电量检查失败',
'检查设备电量时发生错误:$e',
snackPosition: SnackPosition.BOTTOM,
backgroundColor: Colors.red,
colorText: Colors.white,
);
return false;
}
}
void _listenToEvents() {
Ota.instance.eventStream.listen((event) {
if (event.event == 'onProgress' &&

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

@ -221,33 +221,33 @@ class OtaUpgradeView extends GetView<OtaUpgradeController> {
value: deviceInfo?.name ?? '未知设备',
isDarkMode: isDarkMode,
),
SizedBox(height: 10.h),
// 电量信息区域
Row(
children: [
// 左耳电量
Expanded(
child: _buildBatteryInfo(
label: '左耳电量',
batteryLevel: deviceInfo?.leftEarBattery ?? 0,
isCharging: deviceInfo?.leftEarCharging ?? false,
isDarkMode: isDarkMode,
icon: Icons.headset,
),
),
SizedBox(width: 10.w),
// 右耳电量
Expanded(
child: _buildBatteryInfo(
label: '右耳电量',
batteryLevel: deviceInfo?.rightEarBattery ?? 0,
isCharging: deviceInfo?.rightEarCharging ?? false,
isDarkMode: isDarkMode,
icon: Icons.headset,
),
),
],
),
// SizedBox(height: 10.h),
// // 电量信息区域
// Row(
// children: [
// // 左耳电量
// Expanded(
// child: _buildBatteryInfo(
// label: '左耳电量',
// batteryLevel: deviceInfo?.leftEarBattery ?? 0,
// isCharging: deviceInfo?.leftEarCharging ?? false,
// isDarkMode: isDarkMode,
// icon: Icons.headset,
// ),
// ),
// SizedBox(width: 10.w),
// // 右耳电量
// Expanded(
// child: _buildBatteryInfo(
// label: '右耳电量',
// batteryLevel: deviceInfo?.rightEarBattery ?? 0,
// isCharging: deviceInfo?.rightEarCharging ?? false,
// isDarkMode: isDarkMode,
// icon: Icons.headset,
// ),
// ),
// ],
// ),
],
),
);

Loading…
Cancel
Save