升级固件

This commit is contained in:
waner 2026-04-10 10:31:04 +08:00
parent 463d2ce897
commit faf88b1bfe
3 changed files with 44 additions and 12 deletions

View File

@ -192,7 +192,7 @@ Open:
- `sql/`
- `firmware/`
- `scripts/`
- `signature.sig` 需要能被设备密码卡使用“固件完整性密钥”完成验签
- `signature.sig` 需要能被配置的 PEM 公钥使用 `SM3withSM2` 软验签通过
- 服务端需要配置 `tms.upgrade.signature-public-key-pem-path`(公钥 PEM 文件路径)
- `manifest.json` 当前要求包含:
- `packageId`
@ -212,8 +212,9 @@ Open:
- 不满足最小兼容版本时拒绝升级
- `execute.sh` 必填,`precheck.sh`、`verify.sh`、`rollback.sh` 可选
- 执行顺序为:`precheck -> execute -> verify`
- `FIRMWARE` 不增加额外后端流程,具体固件刷写、重启、恢复提示由包内脚本负责
- 回滚不自动触发,需要调用回滚接口
- 升级成功后会更新 `tms_device_software_version`
- `TMS`、`RECEIVER` 升级成功后会更新 `tms_device_software_version``FIRMWARE` 暂不维护版本表
- 升级执行日志统一写入 `tms.upgrade.log-dir`
当前配置项:

View File

@ -98,16 +98,8 @@ public class UpgradeTaskRunner {
return;
}
// 只有 execute verify 都成功后才认为升级成功并回写设备软件版本表
DeviceSoftwareVersionEntity versionEntity = new DeviceSoftwareVersionEntity();
String componentCode = componentCode(task.getTaskType());
versionEntity.setComponentCode(componentCode);
versionEntity.setComponentName(componentName(componentCode));
versionEntity.setCurrentVersion(task.getTargetVersion());
versionEntity.setSourceType("UPGRADE");
versionEntity.setDetectedAt(LocalDateTime.now());
versionEntity.setRemarks("updated by offline upgrade task " + task.getTaskId());
deviceSoftwareVersionRepository.saveOrUpdate(versionEntity);
// 只有软件类升级才回写设备软件版本表固件版本第一版暂不在该表维护
updateSoftwareVersionIfRequired(task);
upgradeTaskRepository.updateStatus(
task.getTaskId(),
@ -221,6 +213,21 @@ public class UpgradeTaskRunner {
};
}
private void updateSoftwareVersionIfRequired(UpgradeTaskEntity task) {
String componentCode = componentCode(task.getTaskType());
if (!"TMS".equals(componentCode) && !"RECEIVER".equals(componentCode)) {
return;
}
DeviceSoftwareVersionEntity versionEntity = new DeviceSoftwareVersionEntity();
versionEntity.setComponentCode(componentCode);
versionEntity.setComponentName(componentName(componentCode));
versionEntity.setCurrentVersion(task.getTargetVersion());
versionEntity.setSourceType("UPGRADE");
versionEntity.setDetectedAt(LocalDateTime.now());
versionEntity.setRemarks("updated by offline upgrade task " + task.getTaskId());
deviceSoftwareVersionRepository.saveOrUpdate(versionEntity);
}
private String componentCode(String taskType) {
return trim(taskType);
}

View File

@ -82,6 +82,30 @@ class UpgradeTaskRunnerTest {
Assertions.assertEquals("标准收发器", versionRepository.store.get("RECEIVER").getComponentName());
}
@Test
void shouldNotUpdateSoftwareVersionWhenFirmwareUpgradeSucceeds() throws Exception {
Path tempDir = Files.createTempDirectory("upgrade-runner-test");
FileRecordEntity fileRecord = buildPackage(tempDir, false, "#!/bin/sh\necho execute\nexit 0\n", false);
InMemoryFileRecordRepository fileRepository = new InMemoryFileRecordRepository();
fileRepository.save(fileRecord);
InMemoryUpgradeTaskRepository taskRepository = new InMemoryUpgradeTaskRepository();
InMemoryDeviceSoftwareVersionRepository versionRepository = new InMemoryDeviceSoftwareVersionRepository();
UpgradeTaskRunner runner = new UpgradeTaskRunner(
taskRepository,
fileRepository,
versionRepository,
stagingService(tempDir.resolve("staging"), tempDir.resolve("logs")),
properties(tempDir.resolve("staging"), tempDir.resolve("logs")),
objectMapper
);
runner.run(task("UPG-001-F", fileRecord.getFileId(), "FIRMWARE", "V3.0.0"));
Assertions.assertEquals("SUCCESS", taskRepository.status);
Assertions.assertTrue(versionRepository.store.isEmpty());
}
@Test
void shouldMarkTaskFailedWhenExecuteScriptFails() throws Exception {
Path tempDir = Files.createTempDirectory("upgrade-runner-test");