diff --git a/lib/core/translations/language/en_us.dart b/lib/core/translations/language/en_us.dart index bfac53331..b7f26a356 100644 --- a/lib/core/translations/language/en_us.dart +++ b/lib/core/translations/language/en_us.dart @@ -1428,6 +1428,10 @@ const Map enUS = { "connectionFailed": "Connection Failed", "retryConnection": "Retry Connection", "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": "Please keep device connected, do not close the app", "doNotDisconnect": "Do not disconnect", diff --git a/lib/core/translations/language/zh_cn.dart b/lib/core/translations/language/zh_cn.dart index 5355038c9..fcfa4c0f8 100644 --- a/lib/core/translations/language/zh_cn.dart +++ b/lib/core/translations/language/zh_cn.dart @@ -1306,6 +1306,10 @@ const Map zhCN = { "connectionFailed": "连接失败", "retryConnection": "重试连接", "ota_updateNow": "立即更新", + "otaVersionInfoFailed": "获取版本信息失败,请稍后重试", + "otaDeviceInfoUnavailable": "无法获取设备信息,请检查连接后重试", + "otaProductInfoUnavailable": "无法识别当前设备,请重新连接后重试", + "otaFirmwareVersionInvalid": "设备固件版本异常,请重新连接后重试", "keepConnectedDontClose": "请保持设备连接,不要关闭应用", "doNotDisconnect": "请勿断开连接", "charging": "充电中", diff --git a/lib/modules/ota_upgrade/controllers/ota_core_contropller.dart b/lib/modules/ota_upgrade/controllers/ota_core_contropller.dart index 146645d90..71936672e 100644 --- a/lib/modules/ota_upgrade/controllers/ota_core_contropller.dart +++ b/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_model.dart'; +enum OtaCoreErrorType { + deviceInfoUnavailable, + productInfoUnavailable, + firmwareVersionFormatInvalid, +} + +class OtaCoreException implements Exception { + final OtaCoreErrorType type; + final Map params; + final String debugMessage; + + OtaCoreException({ + required this.type, + this.params = const {}, + this.debugMessage = '', + }); + + @override + String toString() => debugMessage; +} + /// OTA核心服务 /// 整合设备检查、版本管理和状态管理功能 class OtaCoreService { @@ -450,7 +471,10 @@ class OtaCoreService { if (deviceInfo == null || deviceInfo.firmwareVersion.isEmpty || deviceInfo.firmwareProductId.isEmpty) { - throw "获取设备信息失败,请检查连接"; + throw OtaCoreException( + type: OtaCoreErrorType.deviceInfoUnavailable, + debugMessage: 'deviceInfoUnavailable', //获取设备信息失败,请检查连接 + ); } String deviceName = getDeviceName(); @@ -461,7 +485,11 @@ class OtaCoreService { // 如果根据设备名称找不到产品,尝试使用默认产品名称 product = AppConfig.getproduct("DeapSound"); 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) final firmwareParts = deviceInfo.firmwareVersion.split('.'); 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('.'); Logger().d("单端耳机版本: $currentVersion"); @@ -480,7 +513,12 @@ class OtaCoreService { // 双端耳机:固件版本有六位数(如:1.2.3.1.2.3) final firmwareParts = deviceInfo.firmwareVersion.split('.'); 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 secondPart = firmwareParts.sublist(3, 6).join('.'); @@ -488,7 +526,12 @@ class OtaCoreService { Logger().d("双端耳机版本 - firstPart: $firstPart, secondPart: $secondPart"); if (firstPart != secondPart) { - throw "双端耳机固件版本格式错误:${deviceInfo.firmwareVersion},前三位和后三位应该相同"; + throw OtaCoreException( + type: OtaCoreErrorType.firmwareVersionFormatInvalid, + params: {'firmwareVersion': deviceInfo.firmwareVersion}, + debugMessage: + 'double firmwareVersion mismatch: ${deviceInfo.firmwareVersion}', //双端耳机固件版本不一致,前三位和后三位不一样 + ); } currentVersion = firstPart; } else { diff --git a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart index dae6049dd..4fd5b841d 100644 --- a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart +++ b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart @@ -112,7 +112,7 @@ class OtaUpgradeController extends GetxController { latestVersion.value, ); } catch (e) { - _handleError('获取版本信息失败', e.toString()); + _handleError('error'.tr, _getUserFriendlyErrorMessage(e)); } finally { isFetchingDeviceInfo.value = false; } @@ -314,4 +314,18 @@ class OtaUpgradeController extends GetxController { errorMessage.value = 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; + } }