diff --git a/sfm/sfm_webapp.html b/sfm/sfm_webapp.html index 31e420c..6a49f14 100644 --- a/sfm/sfm_webapp.html +++ b/sfm/sfm_webapp.html @@ -1141,6 +1141,16 @@ ═══════════════════════════════════════════════════════════════════ -->
+ + +
@@ -1243,11 +1253,12 @@
+
- +
diff --git a/sfm/static/device_routing.js b/sfm/static/device_routing.js index 512d066..e2f44a0 100644 --- a/sfm/static/device_routing.js +++ b/sfm/static/device_routing.js @@ -44,6 +44,9 @@ const DEVICE_ROUTES = { slow_drip: ['POST', '/device/stop_monitoring_slow_drip', false], call_home_read: ['GET', '/device/call_home', false], call_home_write: ['POST', '/device/call_home', false], + call_home_state: ['GET', '/device/call_home/state', true], + call_home_on: ['POST', '/device/call_home/enable', true], + call_home_off: ['POST', '/device/call_home/disable', true], config_write: ['POST', '/device/config', false], event_waveform: ['GET', '/device/event/{index}/waveform', false], event_file: ['GET', '/device/event/{index}/blastware_file', false], @@ -82,10 +85,13 @@ function controlsFor(unit) { startStop: serialMode && (series === 3 || series === 4), hangup: series === 4, diagnostics: series === 3, - callHomeRead: series === 3, + // by host, the legacy read is Series 3's; by serial, both series (phase 4) + callHomeRead: series === 3 || (serialMode && series === 4), + callHomeChange: serialMode && (series === 3 || series === 4), configWrite: false, callHomeWrite: false, - reasons: { configWrite: 'Coming in phase 5 (#46)', callHomeWrite: 'Coming in phase 4' }, + reasons: { configWrite: 'Coming in phase 5 (#46)', + callHomeWrite: 'By serial: use Call-home On / Off (audited). Other call-home fields wait for the THOR round.' }, }; } @@ -178,18 +184,28 @@ function deviceErrorText(status, body, unit, statusText) { return status === 504 && !(body || {}).sent && unit && unit.host ? `${text} — ${NO_REPLY_HINT}` : text; } -// A start/stop that went out (or may have) is not "failed": its body carries -// `sent` (ChangeUnconfirmed, bench finding #10), and its message says what the -// unit did and names the audit record. -function changeErrorText(verb, status, body, unit, statusText) { - if ((body || {}).sent) return `${verb} monitoring not confirmed: ${errorDetail(body, statusText)}`; - return `${verb} monitoring failed: ${deviceErrorText(status, body, unit, statusText)}`; +// A change that went out (or may have) is not "failed": its body carries `sent` +// (ChangeUnconfirmed, bench finding #10), and its message says what the unit +// did and names the audit record. `label` is the whole name: "Start monitoring", +// "Enable call-home". +function changeErrorText(label, status, body, unit, statusText) { + if ((body || {}).sent) return `${label} not confirmed: ${errorDetail(body, statusText)}`; + return `${label} failed: ${deviceErrorText(status, body, unit, statusText)}`; } // By serial, settings are read-only until phase 5: no empty legacy edit form or // legacy tables, and the settings box shows from the start (#6, #12). function serialPanes(bySerial, settingsRead) { return { configForm: !bySerial, settingsBox: bySerial, settingsPlaceholder: bySerial && !settingsRead, - legacyDeviceTables: !bySerial }; + legacyDeviceTables: !bySerial, callHomeForm: !bySerial, callHomeBox: bySerial }; +} + +// A unit's call-home switch, read by serial (phase 4). `enabled` null is an +// enable byte never measured -- shown with the byte, never as On or Off. +function callHomeRows(state) { + const s = state || {}; + const sw = s.enabled === true ? 'On' : s.enabled === false ? 'Off' + : `Unknown (byte 5 = ${(s.detail || {}).byte5 ?? '?'})`; + return [['Call-home', sw], ['Dial string', s.dial_string || '—'], ['Series', s.series]]; } /* */ diff --git a/sfm/static/sfm_webapp.js b/sfm/static/sfm_webapp.js index 0697cdd..1a707d4 100644 --- a/sfm/static/sfm_webapp.js +++ b/sfm/static/sfm_webapp.js @@ -96,11 +96,71 @@ function applyControls() { hw.disabled = !c.callHomeWrite; hw.title = c.reasons.callHomeWrite; const chr = document.getElementById('ch-read-btn'); chr.disabled = !c.callHomeRead; - const s4 = series === 4 ? 'Not yet available for Series 4' : ''; + const s4 = series === 4 && !c.callHomeRead ? "Series 4: type the unit's serial to read its call-home" : ''; chr.title = s4; document.getElementById('ch-series-note').textContent = s4; + for (const id of ['ch-on-btn', 'ch-off-btn']) { + document.getElementById(id).disabled = !c.callHomeChange || changeInFlight; + } setMonitorButtons(); applyConfigPanes(); + applyCallHomePanes(); +} + +// The Call Home tab by serial (phase 4): the unit's switch and audited On / Off; +// the legacy multi-field form is Series 3 by host only. +let callHomeShownFor = null; +function applyCallHomePanes() { + const serial = normSerial(currentUnit().serial); + const p = serialPanes(Boolean(serial), false); + document.getElementById('ch-legacy').style.display = p.callHomeForm ? '' : 'none'; + document.getElementById('ch-serial').style.display = p.callHomeBox ? 'block' : 'none'; + document.getElementById('ch-write-btn').hidden = !p.callHomeForm; + document.getElementById('ch-clear-btn').hidden = !p.callHomeForm; + if (p.callHomeBox && callHomeShownFor !== serial) { + callHomeShownFor = null; + renderTextTable('ch-state', [['Call-home', 'Not read yet — press Read from Device']]); + } +} + +// `quiet`: the re-read after a change -- the change's own result stays on screen. +async function readCallHomeBySerial({ quiet = false } = {}) { + const btn = document.getElementById('ch-read-btn'); + btn.disabled = true; + if (!quiet) setChStatus('Reading call-home from the unit…'); + try { + const st = await deviceFetch('call_home_state'); + renderTextTable('ch-state', callHomeRows(st)); + callHomeShownFor = normSerial(st.serial); + if (!quiet) setChStatus(`Call-home read from ${st.serial}.`, 'ok'); + } catch (e) { + if (!quiet) setChStatus(`Read failed: ${e.message}`, 'error'); + else renderTextTable('ch-state', [['Call-home', `Re-read failed: ${e.message}`]]); + } finally { + btn.disabled = false; + } +} + +// One audited send, never retried; the body says what the unit did. +async function changeCallHome(on) { + const serial = normSerial(currentUnit().serial); + const label = on ? 'Enable call-home' : 'Disable call-home'; + if (!confirm(`Turn call-home ${on ? 'ON' : 'OFF'} on ${serial}?`)) return; + changeInFlight = true; applyControls(); + setChStatus(`${label}…`); + try { + const r = await deviceRequest(on ? 'call_home_on' : 'call_home_off'); + const body = await r.json().catch(() => ({})); + setChStatus(body.outcome ? changeSummary(body) + : changeErrorText(label, r.status, body, currentUnit(), r.statusText), + r.ok ? 'ok' : 'error'); + } catch (e) { + // No reply is not "nothing sent": the request may have reached the unit. + setChStatus(`${label}: no reply from SFM (${e.message}) — it may have been sent; ` + + `check the unit's state and GET /db/device_commands`, 'error'); + } + changeInFlight = false; applyControls(); + await readCallHomeBySerial({ quiet: true }); } // The Config tab by serial (#6): settings are read-only until phase 5, so the @@ -444,7 +504,7 @@ async function changeMonitoring(action) { if (body.outcome) { setStatus(changeSummary(body), r.ok ? 'ok' : 'error'); } else { - setStatus(changeErrorText(verb, r.status, body, currentUnit(), r.statusText), 'error'); + setStatus(changeErrorText(`${verb} monitoring`, r.status, body, currentUnit(), r.statusText), 'error'); } } catch (e) { // No reply is not "nothing sent": the request may have reached the unit. @@ -862,6 +922,7 @@ function clearCallHomeForm() { async function readCallHome() { if (!devHost()) { setChStatus('Not connected.', 'error'); return; } + if (serialMode()) { await readCallHomeBySerial(); return; } setChStatus('Reading call home config from device…'); document.getElementById('ch-read-btn').disabled = true; try { @@ -2275,6 +2336,9 @@ document.getElementById('api-base').value = window.location.origin; for (const id of ['dev-serial', 'dev-host', 'dev-port']) { document.getElementById(id).addEventListener('input', () => { unitConfirmed = null; applyControls(); }); } +// New controls are wired here, not with onclick= in the HTML (no inline JS). +document.getElementById('ch-on-btn').addEventListener('click', () => changeCallHome(true)); +document.getElementById('ch-off-btn').addEventListener('click', () => changeCallHome(false)); applyControls(); // Reflect any persisted mic-unit preference in the header pill on load diff --git a/tests/test_webapp_device_routing.py b/tests/test_webapp_device_routing.py index 9686eba..37e3578 100644 --- a/tests/test_webapp_device_routing.py +++ b/tests/test_webapp_device_routing.py @@ -28,7 +28,8 @@ NODE = shutil.which("node") pytestmark = pytest.mark.skipif(NODE is None, reason="node is not installed; the page-logic tests need it") EXPORTS = ["seriesFromSerial", "unitQuery", "deviceUrl", "confirmedSeries", "controlsFor", "eventView", "settingsRows", "perIndexLive", "escapeHtml", - "changeSummary", "downloadSummary", "firmwareLabel", "deviceErrorText", "changeErrorText", "serialPanes"] + "changeSummary", "downloadSummary", "firmwareLabel", "deviceErrorText", "changeErrorText", "serialPanes", + "callHomeRows"] def _block() -> str: @@ -108,7 +109,8 @@ def test_controls_confirmed_series3(): def test_controls_confirmed_series4(): c = run(["controlsFor", [_with(S4, {"serial": "UM20147", "series": 4})]])[0] - assert (c["hangup"], c["startStop"], c["diagnostics"], c["callHomeRead"]) == (True, True, False, False) + # phase 4: by serial a Micromate's call-home reads through /device/call_home/state + assert (c["hangup"], c["startStop"], c["diagnostics"], c["callHomeRead"]) == (True, True, False, True) def test_controls_confirmation_drops_when_the_serial_changes(): @@ -124,11 +126,13 @@ def test_controls_a_legacy_identify_confirms_without_a_typed_serial(): assert s == 3 and c["diagnostics"] is True and c["startStop"] is False # start/stop needs a serial -def test_controls_writes_wait_for_phases_4_and_5(): +def test_controls_config_waits_for_phase_5_and_the_legacy_call_home_form_stays_off(): for unit in (_with(S3, {"serial": "BE9441", "series": 3}), _with(S4, {"serial": "UM20147", "series": 4})): c = run(["controlsFor", [unit]])[0] assert c["configWrite"] is False and c["callHomeWrite"] is False - assert c["reasons"] == {"configWrite": "Coming in phase 5 (#46)", "callHomeWrite": "Coming in phase 4"} + assert c["reasons"] == {"configWrite": "Coming in phase 5 (#46)", + "callHomeWrite": "By serial: use Call-home On / Off (audited). Other call-home " + "fields wait for the THOR round."} def test_change_summary(): @@ -272,10 +276,12 @@ def test_a_change_that_was_sent_is_not_called_failed(): # #10: a ChangeUnconfirmed body carries `sent`; anything else is a real failure. sent = {"detail": "start monitoring was sent and acknowledged, but reading the state back failed: x " "-- audit record c7", "sent": "acknowledged", "audit_id": "c7"} - r = run(["changeErrorText", ["Start", 504, sent, S3, "HTTP 504"]], - ["changeErrorText", ["Stop", 503, {**sent, "sent": "unknown"}, S3, "HTTP 503"]], - ["changeErrorText", ["Start", 504, {"detail": "the unit did not answer: no reply"}, S3, "HTTP 504"]], - ["changeErrorText", ["Stop", 409, {"detail": "not BE9441: BE12599 answered"}, S3, "HTTP 409"]]) + r = run(["changeErrorText", ["Start monitoring", 504, sent, S3, "HTTP 504"]], + ["changeErrorText", ["Stop monitoring", 503, {**sent, "sent": "unknown"}, S3, "HTTP 503"]], + ["changeErrorText", ["Start monitoring", 504, {"detail": "the unit did not answer: no reply"}, S3, + "HTTP 504"]], + ["changeErrorText", ["Stop monitoring", 409, {"detail": "not BE9441: BE12599 answered"}, S3, + "HTTP 409"]]) assert r[0] == f"Start monitoring not confirmed: {sent['detail']}" assert r[1] == f"Stop monitoring not confirmed: {sent['detail']}" assert r[2] == f"Start monitoring failed: the unit did not answer: no reply — {HINT}" @@ -287,6 +293,37 @@ def test_serial_mode_hides_the_legacy_forms(): # #6 / #12: by serial, settings are read-only until phase 5 -- no empty edit # form, no empty legacy tables; the settings box shows from the start. r = run(["serialPanes", [True, False]], ["serialPanes", [True, True]], ["serialPanes", [False, False]]) - assert r == [{"configForm": False, "settingsBox": True, "settingsPlaceholder": True, "legacyDeviceTables": False}, - {"configForm": False, "settingsBox": True, "settingsPlaceholder": False, "legacyDeviceTables": False}, - {"configForm": True, "settingsBox": False, "settingsPlaceholder": False, "legacyDeviceTables": True}] + serial = {"configForm": False, "settingsBox": True, "legacyDeviceTables": False, + "callHomeForm": False, "callHomeBox": True} + legacy = {"configForm": True, "settingsBox": False, "settingsPlaceholder": False, "legacyDeviceTables": True, + "callHomeForm": True, "callHomeBox": False} + assert r == [{**serial, "settingsPlaceholder": True}, {**serial, "settingsPlaceholder": False}, legacy] + + + +# ── Call home by serial (phase 4) ───────────────────────────────────────────── + +def test_call_home_routes_need_a_serial(): + r = run(["deviceUrl", ["call_home_on", BLANK]], ["deviceUrl", ["call_home_on", S4]], + ["deviceUrl", ["call_home_off", S3]], ["deviceUrl", ["call_home_state", S4]]) + assert r[0] is None + assert r[1] == {"method": "POST", "path": "/device/call_home/enable?serial=UM20147&host=h&tcp_port=9034"} + assert r[2] == {"method": "POST", "path": "/device/call_home/disable?serial=BE9441&host=h&tcp_port=9034"} + assert r[3] == {"method": "GET", "path": "/device/call_home/state?serial=UM20147&host=h&tcp_port=9034"} + + +def test_call_home_change_needs_a_confirmed_series(): + r = run(["controlsFor", [S3]], ["controlsFor", [_with(S3, {"serial": "BE9441", "series": 3})]], + ["controlsFor", [_with(S4, {"serial": "UM20147", "series": 4})]], + ["controlsFor", [_with(BLANK, {"serial": "BE9441", "series": 3})]]) + assert [c["callHomeChange"] for c in r] == [False, True, True, False] + + +def test_call_home_rows(): + r = run(["callHomeRows", [{"enabled": True, "dial_string": "RADIO RING", "series": 4}]], + ["callHomeRows", [{"enabled": False, "dial_string": None, "series": 3}]], + ["callHomeRows", [{"enabled": None, "dial_string": "RADIO RING", "series": 3, + "detail": {"byte5": "0x07"}}]]) + assert r[0] == [["Call-home", "On"], ["Dial string", "RADIO RING"], ["Series", 4]] + assert r[1] == [["Call-home", "Off"], ["Dial string", "—"], ["Series", 3]] + assert r[2][0] == ["Call-home", "Unknown (byte 5 = 0x07)"]