Browse Source

feat(ota): 添加OTA错误处理的用户友好提示信息

添加多语言翻译和错误类型枚举,改进错误处理逻辑以显示更友好的用户提示
newdev_shunjiawei
lxm 10 months ago
parent
commit
155db3e81b
  1. 4
      lib/core/translations/language/en_us.dart
  2. 4
      lib/core/translations/language/zh_cn.dart
  3. 53
      lib/modules/ota_upgrade/controllers/ota_core_contropller.dart
  4. 16
      lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart

4
lib/core/translations/language/en_us.dart

@ -1428,6 +1428,10 @@ const Map<String, String> enUS = {
"connectionFailed": "Connection Failed", "connectionFailed": "Connection Failed",
"retryConnection": "Retry Connection", "retryConnection": "Retry Connection",
"ota_updateNow": "Update Now", "ota_updateNow": "Update Now",
"otaVersionInfoFailed": "Failed to get version info. Please try again later.",
"otaDeviceInfoUnavailable": "Unable to get device info. Please check the connection and retry.",
"otaProductInfoUnavailable": "Unable to identify the device. Please reconnect and retry.",
"otaFirmwareVersionInvalid": "Firmware version is abnormal. Please reconnect and retry.",
"keepConnectedDontClose": "keepConnectedDontClose":
"Please keep device connected, do not close the app", "Please keep device connected, do not close the app",
"doNotDisconnect": "Do not disconnect", "doNotDisconnect": "Do not disconnect",

4
lib/core/translations/language/zh_cn.dart

@ -1306,6 +1306,10 @@ const Map<String, String> zhCN = {
"connectionFailed": "连接失败", "connectionFailed": "连接失败",
"retryConnection": "重试连接", "retryConnection": "重试连接",
"ota_updateNow": "立即更新", "ota_updateNow": "立即更新",
"otaVersionInfoFailed": "获取版本信息失败,请稍后重试",
"otaDeviceInfoUnavailable": "无法获取设备信息,请检查连接后重试",
"otaProductInfoUnavailable": "无法识别当前设备,请重新连接后重试",
"otaFirmwareVersionInvalid": "设备固件版本异常,请重新连接后重试",
"keepConnectedDontClose": "请保持设备连接,不要关闭应用", "keepConnectedDontClose": "请保持设备连接,不要关闭应用",
"doNotDisconnect": "请勿断开连接", "doNotDisconnect": "请勿断开连接",
"charging": "充电中", "charging": "充电中",

53
lib/modules/ota_upgrade/controllers/ota_core_contropller.dart

@ -6,6 +6,27 @@ import '../../../data/services/bluetooth_manager.dart'; // 添加蓝牙管理器
import '../../../data/models/appconfig.dart'; import '../../../data/models/appconfig.dart';
import '../../../data/models/appconfig_model.dart'; import '../../../data/models/appconfig_model.dart';
enum OtaCoreErrorType {
deviceInfoUnavailable,
productInfoUnavailable,
firmwareVersionFormatInvalid,
}
class OtaCoreException implements Exception {
final OtaCoreErrorType type;
final Map<String, String> params;
final String debugMessage;
OtaCoreException({
required this.type,
this.params = const {},
this.debugMessage = '',
});
@override
String toString() => debugMessage;
}
/// OTA核心服务 /// OTA核心服务
/// 整合设备检查、版本管理和状态管理功能 /// 整合设备检查、版本管理和状态管理功能
class OtaCoreService { class OtaCoreService {
@ -450,7 +471,10 @@ class OtaCoreService {
if (deviceInfo == null || if (deviceInfo == null ||
deviceInfo.firmwareVersion.isEmpty || deviceInfo.firmwareVersion.isEmpty ||
deviceInfo.firmwareProductId.isEmpty) { deviceInfo.firmwareProductId.isEmpty) {
throw "获取设备信息失败,请检查连接"; throw OtaCoreException(
type: OtaCoreErrorType.deviceInfoUnavailable,
debugMessage: 'deviceInfoUnavailable', //获取设备信息失败,请检查连接
);
} }
String deviceName = getDeviceName(); String deviceName = getDeviceName();
@ -461,7 +485,11 @@ class OtaCoreService {
// 如果根据设备名称找不到产品,尝试使用默认产品名称 // 如果根据设备名称找不到产品,尝试使用默认产品名称
product = AppConfig.getproduct("DeapSound"); product = AppConfig.getproduct("DeapSound");
if (product == null) { if (product == null) {
throw "无法获取产品信息,设备名称: $deviceName"; throw OtaCoreException(
type: OtaCoreErrorType.productInfoUnavailable,
params: {'deviceName': deviceName},
debugMessage: 'productInfoUnavailable: $deviceName', //根据设备名称未找到产品信息
);
} }
} }
@ -471,7 +499,12 @@ class OtaCoreService {
// 单端耳机:固件版本只有三位数(如:1.2.3) // 单端耳机:固件版本只有三位数(如:1.2.3)
final firmwareParts = deviceInfo.firmwareVersion.split('.'); final firmwareParts = deviceInfo.firmwareVersion.split('.');
if (firmwareParts.length < 3) { if (firmwareParts.length < 3) {
throw "单端耳机固件版本格式错误:${deviceInfo.firmwareVersion},应为三位数格式(如:1.2.3)"; throw OtaCoreException(
type: OtaCoreErrorType.firmwareVersionFormatInvalid,
params: {'firmwareVersion': deviceInfo.firmwareVersion},
debugMessage:
'single firmwareVersion format invalid: ${deviceInfo.firmwareVersion}', //单端耳机固件版本格式无效
);
} }
currentVersion = firmwareParts.sublist(0, 3).join('.'); currentVersion = firmwareParts.sublist(0, 3).join('.');
Logger().d("单端耳机版本: $currentVersion"); Logger().d("单端耳机版本: $currentVersion");
@ -480,7 +513,12 @@ class OtaCoreService {
// 双端耳机:固件版本有六位数(如:1.2.3.1.2.3) // 双端耳机:固件版本有六位数(如:1.2.3.1.2.3)
final firmwareParts = deviceInfo.firmwareVersion.split('.'); final firmwareParts = deviceInfo.firmwareVersion.split('.');
if (firmwareParts.length < 6) { if (firmwareParts.length < 6) {
throw "双端耳机固件版本格式错误:${deviceInfo.firmwareVersion},应为六位数格式(如:1.2.3.1.2.3)"; throw OtaCoreException(
type: OtaCoreErrorType.firmwareVersionFormatInvalid,
params: {'firmwareVersion': deviceInfo.firmwareVersion},
debugMessage:
'double firmwareVersion length invalid: ${deviceInfo.firmwareVersion}', //双端耳机固件版本格式无效
);
} }
final firstPart = firmwareParts.sublist(0, 3).join('.'); final firstPart = firmwareParts.sublist(0, 3).join('.');
final secondPart = firmwareParts.sublist(3, 6).join('.'); final secondPart = firmwareParts.sublist(3, 6).join('.');
@ -488,7 +526,12 @@ class OtaCoreService {
Logger().d("双端耳机版本 - firstPart: $firstPart, secondPart: $secondPart"); Logger().d("双端耳机版本 - firstPart: $firstPart, secondPart: $secondPart");
if (firstPart != secondPart) { if (firstPart != secondPart) {
throw "双端耳机固件版本格式错误:${deviceInfo.firmwareVersion},前三位和后三位应该相同"; throw OtaCoreException(
type: OtaCoreErrorType.firmwareVersionFormatInvalid,
params: {'firmwareVersion': deviceInfo.firmwareVersion},
debugMessage:
'double firmwareVersion mismatch: ${deviceInfo.firmwareVersion}', //双端耳机固件版本不一致,前三位和后三位不一样
);
} }
currentVersion = firstPart; currentVersion = firstPart;
} else { } else {

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

@ -112,7 +112,7 @@ class OtaUpgradeController extends GetxController {
latestVersion.value, latestVersion.value,
); );
} catch (e) { } catch (e) {
_handleError('获取版本信息失败', e.toString()); _handleError('error'.tr, _getUserFriendlyErrorMessage(e));
} finally { } finally {
isFetchingDeviceInfo.value = false; isFetchingDeviceInfo.value = false;
} }
@ -314,4 +314,18 @@ class OtaUpgradeController extends GetxController {
errorMessage.value = message; errorMessage.value = message;
_uiService.showErrorSnackbar(title, message); _uiService.showErrorSnackbar(title, message);
} }
String _getUserFriendlyErrorMessage(Object error) {
if (error is OtaCoreException) {
switch (error.type) {
case OtaCoreErrorType.deviceInfoUnavailable:
return 'otaDeviceInfoUnavailable'.tr;
case OtaCoreErrorType.productInfoUnavailable:
return 'otaProductInfoUnavailable'.tr;
case OtaCoreErrorType.firmwareVersionFormatInvalid:
return 'otaFirmwareVersionInvalid'.tr;
}
}
return 'otaVersionInfoFailed'.tr;
}
} }

Loading…
Cancel
Save