From 3f249e23227fd42f27ef76e1f94d3fe9c5bbc8cf Mon Sep 17 00:00:00 2001 From: serversdown Date: Thu, 29 Oct 2026 11:56:36 -0400 Subject: [PATCH] feat(webapp): start/stop through the audited serial path, with the honest result - With a serial, Start / Stop ask first ("Start monitoring on BE9441?"), then POST once to the audited change -- never retried; both buttons stay off while it is in flight, so a double click cannot send twice. - The status bar shows changeSummary() of the ChangeResult, on a 409/502 too (dry run, already-in-state, confirmed a -> b, or the outcome and read-back); an error without one shows its detail verbatim; no reply from SFM says the command may have been sent and where to look, rather than that nothing was. - Then one status read updates the panel. The legacy 5 s confirm poll is not used in serial mode; with a blank serial start/stop are unchanged. - Diagnostics' Stop, which goes the same way in serial mode, shows the same summary; deviceFetch errors carry it for any non-200 ChangeResult. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44 --- sfm/sfm_webapp.html | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/sfm/sfm_webapp.html b/sfm/sfm_webapp.html index 6482d14..e02d8e7 100644 --- a/sfm/sfm_webapp.html +++ b/sfm/sfm_webapp.html @@ -1664,7 +1664,8 @@ async function deviceFetch(action, params = {}, opts = {}) { const body = await r.json().catch(() => ({})); if (!r.ok) { const d = body.detail; - const err = new Error(typeof d === 'string' ? d : d ? (d.message || JSON.stringify(d)) : r.statusText); + const err = new Error(body.outcome ? changeSummary(body) + : typeof d === 'string' ? d : d ? (d.message || JSON.stringify(d)) : r.statusText); err.status = r.status; err.body = body; throw err; } @@ -1908,14 +1909,17 @@ function updateMonitorPanel(s) { // Start/stop: in serial mode only for a unit whose series its own answer // confirmed; without a serial, as before (0x96/0x97 mean the same on both). let lastMonitoring = null; +let changeInFlight = false; // one start/stop at a time: a double click must not send twice function setMonitorButtons() { - const allowed = serialMode() ? controlsFor(currentUnit()).startStop : Boolean(devHost()); + const allowed = !changeInFlight + && (serialMode() ? controlsFor(currentUnit()).startStop : Boolean(devHost())); document.getElementById('mon-start-btn').disabled = !allowed || lastMonitoring === true; document.getElementById('mon-stop-btn').disabled = !allowed || lastMonitoring === false; } async function startMonitoring() { if (!devHost()) return; + if (serialMode()) { await changeMonitoring('start'); return; } const btn = document.getElementById('mon-start-btn'); btn.disabled = true; btn.textContent = '…'; setStatus('Starting monitoring…', 'loading'); @@ -1966,6 +1970,7 @@ async function _pollMonitorConfirm(attempt) { async function stopMonitoring() { if (!devHost()) return; + if (serialMode()) { await changeMonitoring('stop'); return; } const btn = document.getElementById('mon-stop-btn'); btn.disabled = true; btn.textContent = '…'; setStatus('Stopping monitoring…', 'loading'); @@ -1980,6 +1985,37 @@ async function stopMonitoring() { btn.textContent = '■ Stop'; } +// Serial mode: SFM's audited change -- identity and state read first, one send, +// never retried, then read back. The result body says what the unit did, on a +// 409/502 too; only the read that follows decides the panel. +async function changeMonitoring(action) { + const serial = normSerial(currentUnit().serial); + const verb = action === 'start' ? 'Start' : 'Stop'; + if (!confirm(`${verb} monitoring on ${serial}?`)) return; + const btn = document.getElementById(action === 'start' ? 'mon-start-btn' : 'mon-stop-btn'); + const label = btn.textContent; + changeInFlight = true; setMonitorButtons(); btn.textContent = '…'; + setStatus(action === 'start' ? 'Starting — the unit runs its sensor check first (about 35 s)' + : 'Stopping…', 'loading'); + try { + const r = await deviceRequest(action); + const body = await r.json().catch(() => ({})); + if (body.outcome) { + setStatus(changeSummary(body), r.ok ? 'ok' : 'error'); + } else { + const d = body.detail; + setStatus(`${verb} monitoring failed: ${typeof d === 'string' ? d : d ? JSON.stringify(d) : r.statusText}`, 'error'); + } + } catch (e) { + // No reply is not "nothing sent": the request may have reached the unit. + setStatus(`${verb} monitoring: no reply from SFM (${e.message}) — it may have been sent; ` + + `check the unit's state and GET /db/device_commands`, 'error'); + } + changeInFlight = false; btn.textContent = label; + await refreshMonitorStatus(); + setMonitorButtons(); +} + // ── Device tab ───────────────────────────────────────────────────────────────── function populateDeviceTab() { document.getElementById('no-device-msg').style.display = 'none'; @@ -2126,8 +2162,8 @@ async function diagStopMonitoring() { const btn = document.getElementById('diag-stop-btn'); btn.disabled = true; diagResult('diag-stop-result', 'Sending…'); try { - await deviceFetch('stop'); - diagResult('diag-stop-result', 'Stop acknowledged — recording halted.', 'ok'); + const r = await deviceFetch('stop'); + diagResult('diag-stop-result', r.outcome ? changeSummary(r) : 'Stop acknowledged — recording halted.', 'ok'); refreshDiagnostics(); } catch (e) { diagResult('diag-stop-result', `Failed: ${e.message}`, 'error');