From 596df44e4c7a8a7ffc704dbf64663ce3184a19c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=87=AF?= Date: Thu, 19 Nov 2026 00:53:33 -0800 Subject: [PATCH] fix(hardware): recover firmware operations after failures --- .../controller/CppHttpController.java | 26 ++++- .../CppHttpFirmwareUpgradeTest.java | 38 +++++++ .../controller/FirmwareHttpTransferTest.java | 65 +++++++++++ .../service/FirmwareHttpTransferService.java | 107 ++++++++++++++++++ .../service/jni/AudioJNIBridgeService.java | 16 ++- .../service/jni/FirmwareUpgradeAccess.java | 19 +++- .../AudioJNIBridgeFinitePlaybackV1Test.java | 25 ++++ .../information/__tests__/upgrade.test.ts | 52 ++++++++- .../hardware/detail/information/upgrade.vue | 8 +- .../src/stores/__tests__/ota-upgrade.test.ts | 3 +- apps/frontend/src/stores/ota-upgrade.ts | 86 ++++++++++---- docs/specs/hardware-device-actions.md | 7 ++ docs/specs/hardware-device-actions.zh-CN.md | 7 ++ docs/verification/hardware-device-actions.md | 19 ++++ .../hardware-device-actions.zh-CN.md | 19 ++++ 15 files changed, 462 insertions(+), 35 deletions(-) create mode 100644 apps/backend/crydaq-server/src/test/java/com/opentest/controller/FirmwareHttpTransferTest.java create mode 100644 apps/backend/crydaq-service/src/main/java/com/opentest/service/FirmwareHttpTransferService.java diff --git a/apps/backend/crydaq-server/src/main/java/com/opentest/controller/CppHttpController.java b/apps/backend/crydaq-server/src/main/java/com/opentest/controller/CppHttpController.java index 159d9b3d..d00dc40d 100644 --- a/apps/backend/crydaq-server/src/main/java/com/opentest/controller/CppHttpController.java +++ b/apps/backend/crydaq-server/src/main/java/com/opentest/controller/CppHttpController.java @@ -9,6 +9,7 @@ import com.opentest.common.exception.CryBusinessException; import com.opentest.common.exception.ErrorCode; import com.opentest.service.CppHttpProxyService; +import com.opentest.service.FirmwareHttpTransferService; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; @@ -25,6 +26,8 @@ import jakarta.servlet.http.HttpServletRequest; import java.nio.charset.StandardCharsets; import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; /** * C++层HTTP接口调用控制层 @@ -44,6 +47,15 @@ public class CppHttpController extends BaseController { @Resource private CppHttpProxyService cppHttpProxyService; + @Resource + private FirmwareHttpTransferService firmwareHttpTransferService; + + @PostMapping("/api/system/upgrade/upload/cancel") + public Object cancelFirmwareUpload(@RequestHeader(CPP_SERVER_IP_HEADER) String serverIp, + @RequestBody Map command) { + firmwareHttpTransferService.cancel(command.get("uploadId"), serverIp); + return Map.of("code", 0, "data", Map.of("success", true)); + } /** * 具体的接口示例 - 更新设备配置 @@ -97,7 +109,13 @@ public Object proxyRequest( private Object forwardUpgrade(String serverIp, String path, HttpServletRequest request, byte[] body) { try (var target = cppHttpProxyService.openFirmwareTarget(serverIp, path)) { - return forward(target.url(), request, body); + Map headers = new HashMap<>(); + for (String name : new String[]{"Accept-Language", "Content-Type"}) { + if (request.getHeader(name) != null) headers.put(name, request.getHeader(name)); + } + String uploadId = UPGRADE_UPLOAD.equals(path) ? request.getHeader("Firmware-Upload-Id") : null; + var response = firmwareHttpTransferService.forward(target.url(), headers, body, uploadId, serverIp); + return buildProxyResponse(response.status(), response.body()); } } @@ -149,8 +167,10 @@ private Object forward(String targetUrl, HttpServletRequest request, byte[] body } private Object buildProxyResponse(HttpResponse response) { - int status = response.getStatus(); - String responseBody = response.body(); + return buildProxyResponse(response.getStatus(), response.body()); + } + + private Object buildProxyResponse(int status, String responseBody) { Object body = responseBody; boolean jsonResponse = false; try { diff --git a/apps/backend/crydaq-server/src/test/java/com/opentest/controller/CppHttpFirmwareUpgradeTest.java b/apps/backend/crydaq-server/src/test/java/com/opentest/controller/CppHttpFirmwareUpgradeTest.java index 8cd27ce5..14f27c2b 100644 --- a/apps/backend/crydaq-server/src/test/java/com/opentest/controller/CppHttpFirmwareUpgradeTest.java +++ b/apps/backend/crydaq-server/src/test/java/com/opentest/controller/CppHttpFirmwareUpgradeTest.java @@ -3,10 +3,12 @@ import com.opentest.common.exception.CryBusinessException; import com.opentest.common.exception.ErrorCode; import com.opentest.service.CppHttpProxyService; +import com.opentest.service.FirmwareHttpTransferService; import com.opentest.service.jni.FirmwareUpgradeAccess; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpServer; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import org.springframework.mock.web.MockHttpServletRequest; @@ -16,6 +18,11 @@ import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -35,9 +42,40 @@ class CppHttpFirmwareUpgradeTest { private final CppHttpProxyService service = mock(CppHttpProxyService.class); private final CppHttpController controller = new CppHttpController(); + private final FirmwareHttpTransferService transfers = new FirmwareHttpTransferService(); CppHttpFirmwareUpgradeTest() { ReflectionTestUtils.setField(controller, "cppHttpProxyService", service); + ReflectionTestUtils.setField(controller, "firmwareHttpTransferService", transfers); + } + + @AfterEach void closeTransfers() { transfers.close(); } + + @Test void cancelEndsTheStalledForwardAndReleasesTheDeviceReservation() throws Exception { + var access = new FirmwareUpgradeAccess(); + var entered = new CountDownLatch(1); + var release = new CountDownLatch(1); + String id = UUID.randomUUID().toString(); + HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); + server.createContext(UPLOAD, exchange -> { + exchange.getRequestBody().readAllBytes(); entered.countDown(); + try { release.await(5, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } + exchange.close(); + }); + server.start(); + try (var executor = Executors.newSingleThreadExecutor()) { + when(service.openFirmwareTarget(DEVICE, UPLOAD)).thenAnswer(invocation -> + new CppHttpProxyService.FirmwareTarget("http://127.0.0.1:" + server.getAddress().getPort() + UPLOAD, + access.reserveUpgrade(DEVICE))); + var request = post(UPLOAD); request.addHeader("Firmware-Upload-Id", id); + var result = executor.submit(() -> assertThrows(CryBusinessException.class, + () -> controller.proxyRequest(DEVICE, request, new byte[]{1}))); + assertTrue(entered.await(2, TimeUnit.SECONDS)); + assertThrows(CryBusinessException.class, () -> access.reserveActivity(List.of(DEVICE))); + controller.cancelFirmwareUpload(DEVICE, Map.of("uploadId", id)); + result.get(2, TimeUnit.SECONDS); + try (var activity = access.reserveActivity(List.of(DEVICE))) { assertTrue(activity != null); } + } finally { release.countDown(); server.stop(0); } } @Test void uploadAndConfirmTakeTheSameGuardThroughNormalizedRoutes() { diff --git a/apps/backend/crydaq-server/src/test/java/com/opentest/controller/FirmwareHttpTransferTest.java b/apps/backend/crydaq-server/src/test/java/com/opentest/controller/FirmwareHttpTransferTest.java new file mode 100644 index 00000000..d1f0fd01 --- /dev/null +++ b/apps/backend/crydaq-server/src/test/java/com/opentest/controller/FirmwareHttpTransferTest.java @@ -0,0 +1,65 @@ +package com.opentest.controller; + +import com.opentest.common.exception.CryBusinessException; +import com.opentest.service.FirmwareHttpTransferService; +import com.sun.net.httpserver.HttpServer; +import org.junit.jupiter.api.Test; +import org.springframework.test.util.ReflectionTestUtils; + +import java.net.InetSocketAddress; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class FirmwareHttpTransferTest { + @Test void noResponseEndsAtTheConfiguredDeadline() throws Exception { + exerciseStalledTransfer(false); + } + + @Test void cancellingTheAttemptEndsAStalledTransfer() throws Exception { + exerciseStalledTransfer(true); + } + + @Test void cancellationBeforeAdmissionPreventsSendingTheUpload() { + var service = new FirmwareHttpTransferService(); + String id = UUID.randomUUID().toString(); + try { + service.cancel(id, "192.0.2.5"); + assertThrows(CryBusinessException.class, () -> service.forward("http://127.0.0.1:1/upload", + Map.of(), new byte[]{1}, id, "192.0.2.5")); + assertThrows(CryBusinessException.class, () -> service.cancel("bad-id", "192.0.2.5")); + service.cancel(id, "192.0.2.5"); + assertThrows(CryBusinessException.class, () -> service.cancel(id, "192.0.2.6")); + } finally { service.close(); } + } + + private void exerciseStalledTransfer(boolean cancel) throws Exception { + var entered = new CountDownLatch(1); + var release = new CountDownLatch(1); + var service = new FirmwareHttpTransferService(); + ReflectionTestUtils.setField(service, "timeoutMs", cancel ? 10_000L : 500L); + HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); + server.createContext("/upload", exchange -> { + exchange.getRequestBody().readAllBytes(); entered.countDown(); + try { release.await(5, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } + exchange.close(); + }); + server.start(); + try (var executor = Executors.newSingleThreadExecutor()) { + String id = UUID.randomUUID().toString(); + var result = executor.submit(() -> assertThrows(CryBusinessException.class, + () -> service.forward("http://127.0.0.1:" + server.getAddress().getPort() + "/upload", + Map.of(), new byte[]{1}, id, "192.0.2.5"))); + assertTrue(entered.await(2, TimeUnit.SECONDS)); + if (cancel) service.cancel(id, "192.0.2.5"); + result.get(2, TimeUnit.SECONDS); + assertEquals(0L, entered.getCount()); + } finally { release.countDown(); server.stop(0); service.close(); } + } +} diff --git a/apps/backend/crydaq-service/src/main/java/com/opentest/service/FirmwareHttpTransferService.java b/apps/backend/crydaq-service/src/main/java/com/opentest/service/FirmwareHttpTransferService.java new file mode 100644 index 00000000..fe97719d --- /dev/null +++ b/apps/backend/crydaq-service/src/main/java/com/opentest/service/FirmwareHttpTransferService.java @@ -0,0 +1,107 @@ +package com.opentest.service; + +import com.opentest.common.exception.CryBusinessException; +import com.opentest.common.exception.ErrorCode; +import jakarta.annotation.PreDestroy; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; +import java.util.Map; +import java.util.Objects; +import java.util.UUID; +import java.util.concurrent.CancellationException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +/** Bounded firmware transfers; a client can cancel only its own upload attempt. */ +@Service +public class FirmwareHttpTransferService { + private static final long CANCEL_RETENTION_MS = 660_000; + private final Map uploads = new ConcurrentHashMap<>(); + private final HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build(); + @Value("${crydaq.firmware.http-timeout-ms:600000}") + private long timeoutMs = 600_000; + + public record Response(int status, String body) { } + + private static final class Upload { + private final String address; + private final long created = System.currentTimeMillis(); + private volatile CompletableFuture> request; + private boolean cancelled; + private boolean started; + private Upload(String address) { this.address = address; } + } + + public void cancel(String id, String address) { + Upload upload = entry(id, address); + synchronized (upload) { + upload.cancelled = true; + if (upload.request != null) upload.request.cancel(true); + } + } + + public Response forward(String url, Map headers, byte[] body, String uploadId, String address) { + Upload upload = uploadId == null ? null : entry(uploadId, address); + CompletableFuture> request = null; + boolean ownsUpload = false; + try { + long transferBudget = Math.max(30_000L, ((body == null ? 0L : body.length) + 511_999L) / 512_000L * 1_000L); + long deadlineMs = Math.min(timeoutMs, transferBudget); + HttpRequest.Builder builder = HttpRequest.newBuilder(URI.create(url)) + .timeout(Duration.ofMillis(deadlineMs)).POST(HttpRequest.BodyPublishers.ofByteArray(body == null ? new byte[0] : body)); + headers.forEach(builder::header); + if (upload == null) { + request = client.sendAsync(builder.build(), HttpResponse.BodyHandlers.ofString()); + } else { + synchronized (upload) { + if (upload.cancelled) { + ownsUpload = true; + throw new CancellationException(); + } + if (upload.started) throw new CryBusinessException(ErrorCode.PARAMETER, "固件上传标识已使用"); + upload.started = true; + ownsUpload = true; + request = client.sendAsync(builder.build(), HttpResponse.BodyHandlers.ofString()); + upload.request = request; + } + } + HttpResponse response = request.get(deadlineMs, TimeUnit.MILLISECONDS); + return new Response(response.statusCode(), response.body()); + } catch (InterruptedException failure) { + Thread.currentThread().interrupt(); + throw new CryBusinessException(ErrorCode.FAILED, failure); + } catch (ExecutionException | TimeoutException | CancellationException failure) { + throw new CryBusinessException(ErrorCode.FAILED, failure); + } finally { + if (request != null) request.cancel(true); + if (ownsUpload) uploads.remove(uploadId, upload); + } + } + + private Upload entry(String id, String address) { + try { + if (!UUID.fromString(id).toString().equalsIgnoreCase(id)) throw new IllegalArgumentException(); + } catch (IllegalArgumentException | NullPointerException failure) { + throw new CryBusinessException(ErrorCode.PARAMETER, "固件上传标识无效"); + } + long cutoff = System.currentTimeMillis() - CANCEL_RETENTION_MS; + uploads.entrySet().removeIf(entry -> entry.getValue().created < cutoff && entry.getValue().request == null); + Upload upload = uploads.computeIfAbsent(id, ignored -> new Upload(address)); + if (!Objects.equals(upload.address, address)) { + throw new CryBusinessException(ErrorCode.PARAMETER, "固件上传目标不匹配"); + } + return upload; + } + + @PreDestroy + public void close() { client.shutdownNow(); } +} diff --git a/apps/backend/crydaq-service/src/main/java/com/opentest/service/jni/AudioJNIBridgeService.java b/apps/backend/crydaq-service/src/main/java/com/opentest/service/jni/AudioJNIBridgeService.java index 03296dfa..abe0887f 100644 --- a/apps/backend/crydaq-service/src/main/java/com/opentest/service/jni/AudioJNIBridgeService.java +++ b/apps/backend/crydaq-service/src/main/java/com/opentest/service/jni/AudioJNIBridgeService.java @@ -2045,11 +2045,20 @@ public void continueCaptureStop(Device device, List channels) throws return; } // Stop也允许接收同一设备的完整领域对象,不能依赖Start曾改写某个共享对象的名称。 - device.setDeviceName(getJniDeviceId(device)); - String identity = device.getId() == null ? "name:" + device.getDeviceName() : "id:" + device.getId(); + String nativeId = normalizeJniDeviceId(device.getDeviceName(), device.getIpAddress()); + String identity = device.getId() == null ? "name:" + nativeId : "id:" + device.getId(); + List stoppedChannels = channels.stream().map(ChannelDto::getJniChannelNumber).toList(); // 仅串行同一设备的停止,不让慢设备占用整个 JNI 服务的实例锁。 synchronized (continuousCaptureStopLocks.computeIfAbsent(identity, key -> new Object())) { - stopContinuousCaptureDevice(device, channels); + boolean openDaq = "OPENDAQ".equalsIgnoreCase(channels.getFirst().getProtocol()); + try { + device.setDeviceName(getJniDeviceId(device)); + stopContinuousCaptureDevice(device, channels); + if (openDaq) continueCaptureManager.firmwareUpgradeAccess().captureStopped(nativeId, stoppedChannels); + } catch (RuntimeException | Error failure) { + if (openDaq) continueCaptureManager.firmwareUpgradeAccess().captureStopFailed(nativeId, stoppedChannels); + throw failure; + } } } @@ -4018,6 +4027,7 @@ public void disconnectDevice(String deviceId) { requireSelectedDevice("OPENDAQ", deviceId); int code = audioJNIBridge.disconnectDevice("OPENDAQ", deviceId); JNIUtils.checkJniReturn(code, "断开OPENDAQ设备"); + continueCaptureManager.firmwareUpgradeAccess().deviceDisconnected(deviceId); ownedOpenDaqConnections.remove(deviceId); explicitDeviceConnections.forget(deviceId); } finally { diff --git a/apps/backend/crydaq-service/src/main/java/com/opentest/service/jni/FirmwareUpgradeAccess.java b/apps/backend/crydaq-service/src/main/java/com/opentest/service/jni/FirmwareUpgradeAccess.java index 8394e77a..9e4fc909 100644 --- a/apps/backend/crydaq-service/src/main/java/com/opentest/service/jni/FirmwareUpgradeAccess.java +++ b/apps/backend/crydaq-service/src/main/java/com/opentest/service/jni/FirmwareUpgradeAccess.java @@ -14,6 +14,23 @@ public final class FirmwareUpgradeAccess { private final Map activities = new HashMap<>(); private final Set upgrades = new HashSet<>(); + private final Map> unconfirmedStops = new HashMap<>(); + + public synchronized void captureStopFailed(String deviceId, Collection channels) { + unconfirmedStops.computeIfAbsent(deviceId, ignored -> new HashSet<>()).addAll(channels); + } + + public synchronized void captureStopped(String deviceId, Collection channels) { + Set pending = unconfirmedStops.get(deviceId); + if (pending != null) { + pending.removeAll(channels); + if (pending.isEmpty()) unconfirmedStops.remove(deviceId); + } + } + + public synchronized void deviceDisconnected(String deviceId) { + unconfirmedStops.remove(deviceId); + } public synchronized Lease reserveActivity(Collection deviceIds) { List ids = deviceIds.stream().distinct().toList(); @@ -25,7 +42,7 @@ public synchronized Lease reserveActivity(Collection deviceIds) { } public synchronized Lease reserveUpgrade(String deviceId) { - if (activities.containsKey(deviceId) || !upgrades.add(deviceId)) { + if (activities.containsKey(deviceId) || unconfirmedStops.containsKey(deviceId) || !upgrades.add(deviceId)) { throw new CryBusinessException(ErrorCode.JNI_DEVICE_BUSY); } return new Lease(List.of(deviceId), true); diff --git a/apps/backend/crydaq-service/src/test/java/com/opentest/service/jni/AudioJNIBridgeFinitePlaybackV1Test.java b/apps/backend/crydaq-service/src/test/java/com/opentest/service/jni/AudioJNIBridgeFinitePlaybackV1Test.java index c469160d..d32222fe 100644 --- a/apps/backend/crydaq-service/src/test/java/com/opentest/service/jni/AudioJNIBridgeFinitePlaybackV1Test.java +++ b/apps/backend/crydaq-service/src/test/java/com/opentest/service/jni/AudioJNIBridgeFinitePlaybackV1Test.java @@ -14,10 +14,35 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; import static org.junit.jupiter.api.Assertions.*; class AudioJNIBridgeFinitePlaybackV1Test { + @Test + void failedNativeStopKeepsUpgradeBlockedUntilARealStopSucceeds() { + var service = new AudioJNIBridgeService(); + var stopFails = new AtomicBoolean(true); + var bridge = new AudioJNIBridge() { + @Override public int continueCaptureStop(String protocol, String id, int[] channels, int count) { + if (stopFails.get()) throw new IllegalStateException("native transport unavailable"); + return 0; + } + }; + ReflectionTestUtils.setField(service, "audioJNIBridge", bridge); + Device device = device("Sono-A"); device.setProtocol("OPENDAQ"); + var channels = List.of(input("Sono-A", "OPENDAQ", 1001, 1L)); + try (var capture = service.reserveExclusiveInputs(Map.of(device, channels))) { + assertThrows(IllegalStateException.class, () -> service.continueCaptureStop(device, channels)); + } + assertThrows(CryBusinessException.class, () -> service.reserveFirmwareUpgrade(device)); + stopFails.set(false); + service.continueCaptureStop(device, List.of(input("Sono-A", "OPENDAQ", 1002, 2L))); + assertThrows(CryBusinessException.class, () -> service.reserveFirmwareUpgrade(device)); + service.continueCaptureStop(device, channels); + try (var upgrade = service.reserveFirmwareUpgrade(device)) { assertNotNull(upgrade); } + } + @Test void firmwareUpgradeRejectsTheDeviceDuringFinitePlaybackAndReleasesAfterNativeFailure() { AudioJNIBridgeService service = new AudioJNIBridgeService(); diff --git a/apps/frontend/src/modules/device/hardware/detail/information/__tests__/upgrade.test.ts b/apps/frontend/src/modules/device/hardware/detail/information/__tests__/upgrade.test.ts index c59963e0..c4b9d304 100644 --- a/apps/frontend/src/modules/device/hardware/detail/information/__tests__/upgrade.test.ts +++ b/apps/frontend/src/modules/device/hardware/detail/information/__tests__/upgrade.test.ts @@ -53,8 +53,7 @@ const device = { deviceName: 'DAQ' } -function mountUpgrade() { - const pinia = createPinia() +function mountUpgrade(pinia = createPinia()) { setActivePinia(pinia) return mount(Upgrade, { props: { device }, @@ -156,7 +155,8 @@ describe('device firmware upgrade', () => { expect.objectContaining({ headers: { 'Cpp-Server-IP': '[fe80::5bce:4536:c2bd:2ea%3]', - 'Content-Type': 'application/octet-stream' + 'Content-Type': 'application/octet-stream', + 'Firmware-Upload-Id': expect.any(String) } }) ) @@ -240,11 +240,57 @@ describe('device firmware upgrade', () => { ) const upload = store.upload(device, { filePath: 'firmware.bin' }) await flushPromises() + mocks.post.mockResolvedValueOnce({ data: { success: true } }) await store.cancelUpload(device) + expect(mocks.post).toHaveBeenLastCalledWith( + '/v1/cpp/api/system/upgrade/upload/cancel', + { uploadId: expect.any(String) }, + expect.objectContaining({ headers: { 'Cpp-Server-IP': device.ipAddress } }) + ) finish({ data: { success: true } }) await upload expect(store.getUpgradeState(device).fileValidated).toBe(false) expect(store.getUpgradeState(device).uploadStatus).toBe('cancelled') expect(store.getUpgradeState(device).uploadError).toBe('') }) + + it('shows a network failure when cancelling the validated package', async () => { + const wrapper = mountUpgrade() + const store = useOtaUpgradeStore() + store.setUpgradeState(device, { editorOpen: true, fileValidated: true }) + mocks.post.mockRejectedValue(new Error('connection lost')) + await nextTick() + await wrapper + .findAll('button') + .find(button => button.text() === 'common.cancel')! + .trigger('click') + await flushPromises() + expect(wrapper.get('[role="alert"]').text()).toContain('(11000002)') + expect(store.getUpgradeState(device).fileValidated).toBe(true) + }) + + it('keeps the completion result when confirmation finishes in a reopened detail', async () => { + const pinia = createPinia() + const first = mountUpgrade(pinia) + const store = useOtaUpgradeStore() + store.setUpgradeState(device, { editorOpen: true, fileValidated: true }) + let finish!: (_value: unknown) => void + mocks.post.mockImplementation( + () => + new Promise(resolve => { + finish = resolve + }) + ) + await nextTick() + await first + .findAll('button') + .find(button => button.text() === 'device.upgradeAndRestart')! + .trigger('click') + first.unmount() + const reopened = mountUpgrade(pinia) + finish({ data: { success: true } }) + await flushPromises() + expect(reopened.find('.dc-device-hardware-upgrade__editor').exists()).toBe(false) + expect(reopened.get('.dc-device-hardware-upgrade__success').text()).toBe('message.success') + }) }) diff --git a/apps/frontend/src/modules/device/hardware/detail/information/upgrade.vue b/apps/frontend/src/modules/device/hardware/detail/information/upgrade.vue index cd68617a..2a5192b6 100644 --- a/apps/frontend/src/modules/device/hardware/detail/information/upgrade.vue +++ b/apps/frontend/src/modules/device/hardware/detail/information/upgrade.vue @@ -115,7 +115,7 @@ const props = defineProps<{ device: UpgradeDevice }>() const otaUpgradeStore = useOtaUpgradeStore() const upgradeState = computed(() => otaUpgradeStore.getUpgradeState(props.device)) const busy = computed(() => upgradeState.value.isUploading || upgradeState.value.isConfirming) -const completed = ref(false) +const completed = computed(() => upgradeState.value.completed) const uploadStatus = computed(() => upgradeState.value.uploadProgress < 100 ? translate('device.firmwareUploading', { percent: Math.round(upgradeState.value.uploadProgress) }) @@ -124,8 +124,7 @@ const uploadStatus = computed(() => function toggleEditor() { if (busy.value) return - completed.value = false - otaUpgradeStore.setUpgradeState(props.device, { editorOpen: !upgradeState.value.editorOpen }) + otaUpgradeStore.setUpgradeState(props.device, { completed: false, editorOpen: !upgradeState.value.editorOpen }) } async function upload() { @@ -137,7 +136,6 @@ async function upgrade() { if (busy.value) return try { if (await otaUpgradeStore.upgrade(props.device)) { - completed.value = true ElMessage.success(translate('message.success')) } } catch { @@ -191,7 +189,7 @@ async function replacePendingUpgrade() { function handleFilePathChange(value: string | string[]) { const path = Array.isArray(value) ? value[0] : value if (!path || busy.value) return - completed.value = false + otaUpgradeStore.setUpgradeState(props.device, { completed: false }) void otaUpgradeStore.selectFile(props.device, path) } diff --git a/apps/frontend/src/stores/__tests__/ota-upgrade.test.ts b/apps/frontend/src/stores/__tests__/ota-upgrade.test.ts index fa776b31..0eec31af 100644 --- a/apps/frontend/src/stores/__tests__/ota-upgrade.test.ts +++ b/apps/frontend/src/stores/__tests__/ota-upgrade.test.ts @@ -90,7 +90,8 @@ describe('ota upgrade store', () => { expect.objectContaining({ headers: { 'Cpp-Server-IP': '[fe80::5bce:4536:c2bd:2ea%3]', - 'Content-Type': 'application/octet-stream' + 'Content-Type': 'application/octet-stream', + 'Firmware-Upload-Id': expect.any(String) } }) ) diff --git a/apps/frontend/src/stores/ota-upgrade.ts b/apps/frontend/src/stores/ota-upgrade.ts index 7852539d..3a7b7b9d 100644 --- a/apps/frontend/src/stores/ota-upgrade.ts +++ b/apps/frontend/src/stores/ota-upgrade.ts @@ -27,6 +27,8 @@ interface UpgradeCancelToken { } interface UpgradeState { + completed: boolean + uploadId: string | null editorOpen: boolean isConfirming: boolean fileSize: number @@ -73,6 +75,8 @@ export const useOtaUpgradeStore = defineStore('ota-upgrade', () => { function initUpgradeState(device: UpgradeDevice, { filePath = '' }: { filePath?: string } = {}) { const initialState: UpgradeState = { + completed: false, + uploadId: null, editorOpen: false, isConfirming: false, fileSize: 0, @@ -117,8 +121,10 @@ export const useOtaUpgradeStore = defineStore('ota-upgrade', () => { function upload(device: UpgradeDevice, { filePath = '' }: { filePath?: string } = {}) { setUpgradeState(device, { + completed: false, filePath, attempt: ++attempt, + uploadId: null, isCancelled: false, uploadError: '', fileValidated: false, @@ -142,13 +148,16 @@ export const useOtaUpgradeStore = defineStore('ota-upgrade', () => { setUpgradeState(device, { fileSize }) const cancelToken = api.CancelToken.source() as unknown as UpgradeCancelToken + const uploadId = crypto.randomUUID() setUpgradeState(device, { - cancelToken + cancelToken, + uploadId }) const response = (await api.baseSilent().post('/v1/cpp/api/system/upgrade/upload', res.data, { headers: { 'Cpp-Server-IP': device?.ipAddress, + 'Firmware-Upload-Id': uploadId, 'Content-Type': 'application/octet-stream' }, transformRequest: [data => data], @@ -160,6 +169,18 @@ export const useOtaUpgradeStore = defineStore('ota-upgrade', () => { })) as UpgradeResponse if (isCurrent()) processUploadResponse(device, response) } catch (error) { + if (!isCurrent()) return + const uploadId = getUpgradeState(device).uploadId + if (uploadId && typeof (error as { code?: unknown })?.code !== 'number') { + await api + .baseSilent() + .post( + '/v1/cpp/api/system/upgrade/upload/cancel', + { uploadId }, + { headers: { 'Cpp-Server-IP': device.ipAddress } } + ) + .catch(() => {}) + } if (!isCurrent()) return setUpgradeState(device, { isUpgrading: false, @@ -177,6 +198,26 @@ export const useOtaUpgradeStore = defineStore('ota-upgrade', () => { upgradeState.attempt = ++attempt upgradeState.cancelToken?.cancel('') + try { + if (upgradeState.uploadId) { + await api + .baseSilent() + .post( + '/v1/cpp/api/system/upgrade/upload/cancel', + { uploadId: upgradeState.uploadId }, + { headers: { 'Cpp-Server-IP': device.ipAddress } } + ) + } + } catch (error) { + setUpgradeState(device, { + isUpgrading: false, + isUploading: false, + uploadStatus: UPGRADE_STATUS.FAILED, + uploadError: getCaughtErrorMessage(error, FE.NETWORK) + }) + return + } + await nextTick() setUpgradeState(device, { isUpgrading: false, @@ -190,29 +231,34 @@ export const useOtaUpgradeStore = defineStore('ota-upgrade', () => { } async function cancelPendingUpgrade(device: UpgradeDevice) { - const response = (await api.baseSilent().post( - '/v1/cpp/api/system/upgrade/action', - { action: 'cancel' }, - { - headers: { - 'Cpp-Server-IP': device?.ipAddress + try { + const response = (await api.baseSilent().post( + '/v1/cpp/api/system/upgrade/action', + { action: 'cancel' }, + { + headers: { + 'Cpp-Server-IP': device?.ipAddress + } } + )) as UpgradeResponse + const isSuccess = response?.data?.success + if (!isSuccess) { + const failure = upgradeError(response) + const message = failure.message + setUpgradeState(device, { + pendingConfirmation: true, + uploadError: message + }) + throw failure } - )) as UpgradeResponse - const isSuccess = response?.data?.success - if (!isSuccess) { - const failure = upgradeError(response) - const message = failure.message setUpgradeState(device, { - pendingConfirmation: true, - uploadError: message + pendingConfirmation: false, + uploadError: '' }) - throw failure + } catch (error) { + setUpgradeState(device, { uploadError: getCaughtErrorMessage(error, FE.NETWORK) }) + throw error } - setUpgradeState(device, { - pendingConfirmation: false, - uploadError: '' - }) } async function selectFile(device: UpgradeDevice, filePath: string) { @@ -222,6 +268,7 @@ export const useOtaUpgradeStore = defineStore('ota-upgrade', () => { setUpgradeState(device, { filePath, fileSize: 0, + completed: false, uploadError: '', pendingConfirmation: state.pendingConfirmation || state.fileValidated, fileValidated: false, @@ -256,6 +303,7 @@ export const useOtaUpgradeStore = defineStore('ota-upgrade', () => { )) as UpgradeResponse if (!response.data?.success) throw upgradeError(response) initUpgradeState(device) + setUpgradeState(device, { completed: true }) return true } catch (error) { setUpgradeState(device, { isUpgrading: false, uploadError: getCaughtErrorMessage(error, FE.UPDATE) }) diff --git a/docs/specs/hardware-device-actions.md b/docs/specs/hardware-device-actions.md index e3f05d02..2f7a88d3 100644 --- a/docs/specs/hardware-device-actions.md +++ b/docs/specs/hardware-device-actions.md @@ -67,6 +67,13 @@ The reference shows an Installing percentage and a demo completion state. The us 6. Closing details within the current application session preserves active task state; reopening restores its display. Retain existing upload/API error handling and pending-package replacement rules. Retries require an explicit user action; do not automatically repeat upgrade confirmation. 7. Prevent starting firmware upgrade on a device that is acquiring or playing. Hold a device reservation while forwarding upload and confirmation requests, excluding concurrent capture, finite playback, and streaming playback startup. Release it when the request completes or fails. Lock relevant upgrade actions during its upgrade flow. Closing details does not itself cancel the upgrade. +### Firmware failure recovery + +- A failed capture stop retains an unconfirmed stop for the device and physical channels. Releasing the logical session cannot admit firmware upgrades. A successful stop of those channels or a successful device disconnection clears it; stopping unrelated channels does not. +- Upload and confirmation forwarding have deadlines. Upload time is budgeted by package size and capped by `crydaq.firmware.http-timeout-ms` (600000 milliseconds by default); connection timeout is five seconds. +- Each upload has an independent identifier. Cancellation or a client transport failure cancels its backend HTTP task, including cancellation arriving before registration. Confirmation is never cancelled by this endpoint. Validated pending device packages retain the existing cancel and replacement policy. +- Pending-package cancellation network errors appear inline. Completion results live in per-device session state and survive closing and reopening the detail. + ### First-round decisions | Question | User decision | diff --git a/docs/specs/hardware-device-actions.zh-CN.md b/docs/specs/hardware-device-actions.zh-CN.md index ecef77da..11e5d14c 100644 --- a/docs/specs/hardware-device-actions.zh-CN.md +++ b/docs/specs/hardware-device-actions.zh-CN.md @@ -67,6 +67,13 @@ Status: implemented; verification recorded 6. 当前运行会话内关闭详情不丢弃正在进行的任务状态,再次打开恢复展示。上传或接口失败沿用现有错误处理和待确认包替换规则,由用户明确重试,不自动重复确认升级。 7. 设备正在采集或播放时禁止启动该设备的固件升级。上传及确认请求转发期间,持有该设备的升级占用,与采集、有限播放和持续播放启动互斥;请求完成或失败后释放。设备进入升级流程后锁定相关升级操作。关闭详情本身不表示取消升级。 +### 固件故障恢复规则 + +- 采集停止失败时保留该设备、该物理通道的未确认停止状态;逻辑会话释放不能使升级放行。对应通道真实停止成功或设备成功断开连接后才解除。停止其他通道不会清除该故障。 +- 上传与确认转发都有时间上限。上传按包大小预算时间,上限由 `crydaq.firmware.http-timeout-ms` 控制(默认 600000 毫秒),连接超时为 5 秒。 +- 每次上传使用独立标识。取消或客户端传输失败会取消对应的后端 HTTP 任务;取消在任务登记之前到达时也会阻止该任务发送。确认升级不使用此取消入口。设备已校验的待确认包仍按既有取消、替换规则处理。 +- 取消待确认包的网络错误展示在行内;升级完成结果随设备保存在会话状态中,关闭重开详情后仍可见。 + ### 第一轮确认记录 | 问题 | 用户决定 | diff --git a/docs/verification/hardware-device-actions.md b/docs/verification/hardware-device-actions.md index f5cb2132..de48c317 100644 --- a/docs/verification/hardware-device-actions.md +++ b/docs/verification/hardware-device-actions.md @@ -78,3 +78,22 @@ After an offline-clear request is submitted, a lost response still causes device - Backend CI failures in the original PR match the plugin-runtime and `CommonProtoCompatibilityTest` failures in the [main CI](https://github.com/crysound/OpenTest/actions/runs/44218810513) for baseline `404f249c`. The same baseline also has the nine existing frontend test-typecheck errors. Full CI is not passing; targeted results do not imply that it passed. These automated results do not replace the physical-device observations above. Earlier hardware testing predates this review revision. Hardware acceptance of the new mutual-exclusion behavior remains pending. + +## Firmware failure recovery retest + +On 2026-11-19, the isolated instance on `windows-huang` (192.168.5.224, THINK_KYLE) reproduced all four PR #65 findings before testing the fix. The physical SonoDAQ was device 51 at 192.168.5.157. Testing used the supplied V1.2.8 firmware (64,200,304 bytes), with backend, gRPC, and frontend ports 15447, 19447, and 19448. + +| Failure | Before | After | +| --- | --- | --- | +| Capture stop fails | Real JNI capture started. A temporary test agent threw a transport failure at stop; releasing the logical session allowed firmware admission. | Firmware remained blocked. Removing the fault and successfully stopping real capture restored admission. Automated coverage also verifies that stopping unrelated channels cannot clear the fault. | +| Upload response stalls | A test proxy forwarded the actual 61.2 MB firmware to the device and withheld its real validated response. Client waiting failed while backend occupancy still blocked real capture startup. | Cancelling the upload ended the backend HTTP task and real capture started. Separately, a temporary 8000 ms server deadline closed the connection after 8003 ms without user cancellation, after which real capture started. | +| Pending-package cancellation loses network | Blocking the browser cancellation request after successful device validation produced no error feedback. | The same failure displayed an inline network error (11000002), retained confirmation, and allowed an explicit retry. | +| Details close and reopen during confirmation | The device returned code 0/success true. Withholding that real response, closing and reopening details, then delivering it lost the result beside the version. | The same sequence displayed Operation successful and collapsed the editor. | + +These are **physical-device executions with controlled fault injection**. The stop exception was injected by a temporary test agent and is not claimed as a spontaneous native-device failure. Network failures were injected through the browser or a proxy forwarding real device requests; successful device responses were not fabricated. No power interruption was performed during flashing, and this is not full protocol certification. + +The fix branch is `codex/hardware-firmware-recovery`, based on main `b50ac074`. Targeted backend tests passed 77 cases and frontend hardware/state tests passed 38 cases. Two new frontend regressions failed before their fixes. Production builds, changed-file ESLint, and formatting passed. These targeted checks do not establish that full CI passed. + +The old isolated database had missed `V2.5.0_20261110160000__add_project_can_definition.sql`. Its database was backed up before applying the migration's ADD COLUMN; product migration logic was unchanged. Original packages and the database backup remain available. + +Evidence is local under `.runtime/tasks/firmware-recovery` and in the Windows dedicated build directory. Artifact names include `baseline-cancel-network-failure`, `baseline-confirm-reopen`, `baseline-upload-response-timeout`, `baseline-stop-failure`, `fixed-stop-failure`, `fixed-stop-recovery`, `fixed-cancel-network-failure`, `fixed-confirm-reopen`, `fixed-upload-cancelled`, `fixed-upload-timeout`, `fixed-capture-after-upload-cancel`, `fixed-capture-after-upload-timeout`, and `response-proxy-events`. Build packages were checked with SHA-256. Cleanup restores the default timeout, cancels the device's pending package, stops the proxy, and fully restarts the client to remove agent instrumentation. diff --git a/docs/verification/hardware-device-actions.zh-CN.md b/docs/verification/hardware-device-actions.zh-CN.md index 8eb10b03..eb3bbcc3 100644 --- a/docs/verification/hardware-device-actions.zh-CN.md +++ b/docs/verification/hardware-device-actions.zh-CN.md @@ -78,3 +78,22 @@ PR #63 合并后,补充修复其 AI 审查意见。固件代理按 IPv4、IPv6 - 原 PR 的后端 CI 失败项与基线 `404f249c` 的 [main CI](https://github.com/crysound/OpenTest/actions/runs/44218810513) 一致,均位于插件运行时测试与 `CommonProtoCompatibilityTest`。前端测试类型检查的 9 处既有错误也出现在同一基线中。完整 CI 未通过,不能将定向检查通过解释为全量 CI 成功。 本节自动化验证不替代前文的真机记录。此前的物理设备实测早于本轮审查修复,新增占用互斥的真机验收仍待完成。 + +## 固件故障恢复复测 + +2026-11-19 在 `windows-huang`(192.168.5.224,主机名 THINK_KYLE)的独立测试实例中,先复现 PR #65 的四个问题,再部署修复并复测。物理 SonoDAQ 地址为 192.168.5.157,设备名为 51;固件仍使用用户提供的 V1.2.8 包(64,200,304 字节)。后端、gRPC、前端分别使用 15447、19447、19448 端口。 + +| 故障 | 修复前观察 | 修复后观察 | +| --- | --- | --- | +| 停止采集失败 | 真实 JNI 采集启动成功;临时测试 agent 在停止调用处抛出传输失败,逻辑释放后仍可取得升级占用。 | 升级仍被拒绝;移除故障并执行真实停止成功后,升级恢复可用。自动化另外覆盖停止其他通道不会解除故障。 | +| 上传响应不返回 | 测试代理将 61.2 MB 固件原样转发真机,收到真实“Upgrade package validated”后暂扣响应。客户端等待失败后,后端仍占用设备,真实采集启动被拒绝。 | 点击取消后,后端 HTTP 任务结束,真实采集启动成功。另将后端上限临时设为 8000 毫秒,未取消时代理观察到连接在 8003 毫秒结束,随后真实采集启动成功。 | +| 取消待确认包网络失败 | 在真实校验成功后阻断浏览器取消请求,无错误提示。 | 同样阻断后出现行内网络错误(11000002),确认区保留,可明确重试。 | +| 确认期间关闭重开详情 | 真机确认接口返回 code 0、success true,暂扣这个真实响应,在详情关闭重开后放行,版本行无结果。 | 同样操作后版本行显示 Operation successful,编辑区收起。 | + +这些结果属于**真机执行加可控故障注入**。停止失败由测试 agent 注入,未伪装为原生设备自然故障;网络失败由浏览器或只转发真实设备请求的测试代理注入,未伪造设备成功响应。未在刷写期间断电,也未将上述测试描述为设备自然故障或全协议认证。 + +修复分支为 `codex/hardware-firmware-recovery`,基于 main `b50ac074`。定向后端 77 项、前端硬件与状态相关 38 项通过,新增两项前端用例先失败后修复通过;生产构建、改动文件 ESLint 和格式检查通过。未因定向检查通过而宣称全量 CI 已通过。 + +环境准备发现旧测试库漏执行 `V2.5.0_20261110160000__add_project_can_definition.sql`,先备份专用库再执行该迁移的 ADD COLUMN,未修改产品迁移逻辑。旧包和数据库备份均保留。 + +本地证据在 `.runtime/tasks/firmware-recovery`;Windows 原始证据在专用构建目录,包括 `baseline-cancel-network-failure`、`baseline-confirm-reopen`、`baseline-upload-response-timeout`、`baseline-stop-failure`、`fixed-stop-failure`、`fixed-stop-recovery`、`fixed-cancel-network-failure`、`fixed-confirm-reopen`、`fixed-upload-cancelled`、`fixed-upload-timeout`、`fixed-capture-after-upload-cancel`、`fixed-capture-after-upload-timeout` 和 `response-proxy-events`。构建包使用 SHA-256 核对。测试后恢复默认超时、取消设备端待确认包、停止测试代理,并完整重启客户端清除 agent 注入。