|
|
|
@ -63,6 +63,10 @@ class OtaUpgradeController extends GetxController { |
|
|
|
final RxDouble progress = 0.0.obs; // 控制器自己的可观察属性 |
|
|
|
final RxInt type = OtaEvent.NOT_STARTED.obs; |
|
|
|
final RxBool isUpgrade = false.obs; |
|
|
|
|
|
|
|
// 新增:详细状态提示 |
|
|
|
final RxString detailedStatus = ''.obs; |
|
|
|
final RxBool showDetailedStatus = false.obs; |
|
|
|
|
|
|
|
@override |
|
|
|
void onInit() { |
|
|
|
@ -289,6 +293,9 @@ class OtaUpgradeController extends GetxController { |
|
|
|
// 修改后的startDownload方法 |
|
|
|
Future<void> startDownload() async { |
|
|
|
isDownloading.value = true; |
|
|
|
showDetailedStatus.value = true; |
|
|
|
detailedStatus.value = '准备下载固件包...'; |
|
|
|
|
|
|
|
int lastIndex = updatepackageaddress.lastIndexOf('/'); |
|
|
|
String fileName = updatepackageaddress.substring(lastIndex + 1); |
|
|
|
String directoryName = "ota_upgrades"; |
|
|
|
@ -302,6 +309,7 @@ class OtaUpgradeController extends GetxController { |
|
|
|
|
|
|
|
type.value = DOWNLOADING_UP_FILES; |
|
|
|
try { |
|
|
|
detailedStatus.value = '正在获取下载目录...'; |
|
|
|
final Directory? downloadsDir = await getApplicationSupportDirectory(); |
|
|
|
if (downloadsDir == null) { |
|
|
|
throw FileSystemException('无法访问下载目录'); |
|
|
|
@ -310,12 +318,14 @@ class OtaUpgradeController extends GetxController { |
|
|
|
final targetDir = |
|
|
|
Directory('${downloadsDir.path}/Download/$directoryName'); |
|
|
|
if (!await targetDir.exists()) { |
|
|
|
detailedStatus.value = '创建下载目录...'; |
|
|
|
await targetDir.create(recursive: true); |
|
|
|
} |
|
|
|
|
|
|
|
final savePath = '${targetDir.path}/$fileName'; |
|
|
|
|
|
|
|
// 新增:检查同名文件逻辑 |
|
|
|
detailedStatus.value = '检查本地文件...'; |
|
|
|
bool existingSameFile = false; |
|
|
|
try { |
|
|
|
var files = await targetDir.list().toList(); |
|
|
|
@ -336,12 +346,13 @@ class OtaUpgradeController extends GetxController { |
|
|
|
|
|
|
|
if (existingSameFile) { |
|
|
|
//Get.snackbar('文件已存在', '跳过下载,直接更新'); |
|
|
|
|
|
|
|
detailedStatus.value = '文件已存在,准备升级...'; |
|
|
|
startUpgrading(savePath); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 新增:删除其他文件逻辑 |
|
|
|
detailedStatus.value = '清理旧文件...'; |
|
|
|
try { |
|
|
|
var files = await targetDir.list().toList(); |
|
|
|
for (var entity in files) { |
|
|
|
@ -357,6 +368,7 @@ class OtaUpgradeController extends GetxController { |
|
|
|
|
|
|
|
print('文件保存路径: $savePath'); |
|
|
|
|
|
|
|
detailedStatus.value = '开始下载固件包...'; |
|
|
|
final options = Options( |
|
|
|
receiveTimeout: Duration.zero, |
|
|
|
followRedirects: true, |
|
|
|
@ -370,14 +382,19 @@ class OtaUpgradeController extends GetxController { |
|
|
|
onReceiveProgress: (received, total) { |
|
|
|
if (total != -1) { |
|
|
|
downloadProgress.value = received / total; |
|
|
|
detailedStatus.value = '正在下载固件包... ${(received / total * 100).toStringAsFixed(1)}%'; |
|
|
|
} |
|
|
|
}, |
|
|
|
); |
|
|
|
File file = File(savePath); |
|
|
|
if (await file.exists()) { |
|
|
|
// Get.snackbar('下载完成', '文件已保存至: $savePath'); |
|
|
|
detailedStatus.value = '下载完成,准备升级...'; |
|
|
|
} else { |
|
|
|
Get.snackbar('错误', '文件未找到,请检查写入权限'); |
|
|
|
detailedStatus.value = '下载失败'; |
|
|
|
showDetailedStatus.value = false; |
|
|
|
return; |
|
|
|
} |
|
|
|
isDownloading.value = false; |
|
|
|
startUpgrading(savePath); |
|
|
|
@ -387,12 +404,19 @@ class OtaUpgradeController extends GetxController { |
|
|
|
} on FileSystemException catch (e) { |
|
|
|
Get.snackbar('存储失败', '无法写入文件: ${e.message}'); |
|
|
|
} catch (e) { |
|
|
|
Get.snackbar('未知错误', e.toString()); |
|
|
|
isDownloading.value = false; |
|
|
|
detailedStatus.value = '下载失败,请检查网络连接'; |
|
|
|
showDetailedStatus.value = false; |
|
|
|
Get.snackbar('下载失败', '请检查网络连接: $e'); |
|
|
|
print('下载失败: $e'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 开始升级 |
|
|
|
void startUpgrading(String path) async { |
|
|
|
detailedStatus.value = '准备设备连接...'; |
|
|
|
await Future.delayed(const Duration(milliseconds: 500)); |
|
|
|
detailedStatus.value = '正在连接设备...'; |
|
|
|
jlota.startUpgrading(address: _address, filePath: path); |
|
|
|
return; |
|
|
|
// isUpgrade.value = false; |
|
|
|
@ -467,29 +491,38 @@ class OtaUpgradeController extends GetxController { |
|
|
|
progress.value = event.progress!; |
|
|
|
type.value = event.type!; |
|
|
|
isUpgrade.value = true; // 开始升级 |
|
|
|
detailedStatus.value = '升级进度: ${(event.progress! * 100).toStringAsFixed(1)}%'; |
|
|
|
} |
|
|
|
if (event.event == 'onStartOTA') { |
|
|
|
type.value = OtaEvent.START_UPGRADING; |
|
|
|
type.value = START_UPGRADING; |
|
|
|
isUpgrade.value = true; // 开始升级 |
|
|
|
detailedStatus.value = 'OTA升级已开始...'; |
|
|
|
} |
|
|
|
if (event.event == 'onStopOTA') { |
|
|
|
type.value = OtaEvent.UPGRADE_SUCCESS; |
|
|
|
type.value = UPGRADE_SUCCESS; |
|
|
|
isUpgrade.value = false; |
|
|
|
detailedStatus.value = '升级成功完成'; |
|
|
|
showDetailedStatus.value = false; |
|
|
|
Get.snackbar('升级成功', '请退出重新连接设备'); |
|
|
|
_getCurrentVersion(); |
|
|
|
} |
|
|
|
if (event.event == 'onError') { |
|
|
|
type.value = OtaEvent.UPGRADE_FAILED; |
|
|
|
type.value = UPGRADE_FAILED; |
|
|
|
isUpgrade.value = false; |
|
|
|
detailedStatus.value = '升级失败,错误码:${event.code}'; |
|
|
|
showDetailedStatus.value = false; |
|
|
|
Get.snackbar('升级失败', '请检查设备连接或升级包,错误码:${event.code}'); |
|
|
|
} |
|
|
|
if (event.event == 'NOCONNECTION') { |
|
|
|
type.value = OtaEvent.CONNECTION_DISCONNECTION; |
|
|
|
type.value = CONNECTION_DISCONNECTION; |
|
|
|
isUpgrade.value = false; |
|
|
|
detailedStatus.value = '设备连接断开'; |
|
|
|
showDetailedStatus.value = false; |
|
|
|
} |
|
|
|
if (event.event == 'CONNECTION_SUCCESS') { |
|
|
|
type.value = OtaEvent.CONNECTION_SUCCESS; |
|
|
|
type.value = CONNECTION_SUCCESS; |
|
|
|
isUpgrade.value = true; // 开始升级 |
|
|
|
detailedStatus.value = '设备连接成功'; |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|