Browse Source

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

在固件升级前检查设备电量,当左右耳电量有低于30%时阻止升级并提示用户
移除视图中的电量显示区域,因检查逻辑已移至控制器
newdev_shunjiawei
lxm 1 year ago
parent
commit
7b8dcf36f4
  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(); Get.back();
Future.delayed(Duration(milliseconds: 300)); Future.delayed(Duration(milliseconds: 300));
// 检查设备电量
if (!_checkBatteryLevel()) {
return; // 电量不足,不执行升级
}
startDownload(); startDownload();
}, },
child: Text('更新'), child: Text('更新'),
@ -399,6 +405,60 @@ class OtaUpgradeController extends GetxController {
// isUpgrade.value = false; // 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() { void _listenToEvents() {
Ota.instance.eventStream.listen((event) { Ota.instance.eventStream.listen((event) {
if (event.event == 'onProgress' && 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 ?? '未知设备', value: deviceInfo?.name ?? '未知设备',
isDarkMode: isDarkMode, isDarkMode: isDarkMode,
), ),
SizedBox(height: 10.h), // SizedBox(height: 10.h),
// 电量信息区域 // // 电量信息区域
Row( // Row(
children: [ // children: [
// 左耳电量 // // 左耳电量
Expanded( // Expanded(
child: _buildBatteryInfo( // child: _buildBatteryInfo(
label: '左耳电量', // label: '左耳电量',
batteryLevel: deviceInfo?.leftEarBattery ?? 0, // batteryLevel: deviceInfo?.leftEarBattery ?? 0,
isCharging: deviceInfo?.leftEarCharging ?? false, // isCharging: deviceInfo?.leftEarCharging ?? false,
isDarkMode: isDarkMode, // isDarkMode: isDarkMode,
icon: Icons.headset, // icon: Icons.headset,
), // ),
), // ),
SizedBox(width: 10.w), // SizedBox(width: 10.w),
// 右耳电量 // // 右耳电量
Expanded( // Expanded(
child: _buildBatteryInfo( // child: _buildBatteryInfo(
label: '右耳电量', // label: '右耳电量',
batteryLevel: deviceInfo?.rightEarBattery ?? 0, // batteryLevel: deviceInfo?.rightEarBattery ?? 0,
isCharging: deviceInfo?.rightEarCharging ?? false, // isCharging: deviceInfo?.rightEarCharging ?? false,
isDarkMode: isDarkMode, // isDarkMode: isDarkMode,
icon: Icons.headset, // icon: Icons.headset,
), // ),
), // ),
], // ],
), // ),
], ],
), ),
); );

Loading…
Cancel
Save