From ee02874cb0965b1c14504c16402448d5cc6d20c8 Mon Sep 17 00:00:00 2001 From: serversdown Date: Thu, 29 Oct 2026 13:15:44 -0400 Subject: [PATCH] feat(sfm): opt-in raw SUB 0x49 code on a Series-4 status read GET /device/monitor/status?serial=UM...&run_state=true adds detail.run_state = {code, name}. For the ring-path bench test: the code during and after a call-home (0x01 for ~18 s after a stop, inferred to be call-home attempts; 0x03 = calling home) is what the test watches. Opt-in because it is one more round trip (~0.65 s on cellular); a plain status read is unchanged. Series 3 has no 0x49 and ignores it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44 --- sfm/device_access/access.py | 7 +++++-- sfm/device_access/s4.py | 8 ++++++-- sfm/server.py | 5 ++++- tests/test_device_access_s4.py | 9 +++++++++ tests/test_server_device_access.py | 8 ++++++++ 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/sfm/device_access/access.py b/sfm/device_access/access.py index 72e2c34..a691f88 100644 --- a/sfm/device_access/access.py +++ b/sfm/device_access/access.py @@ -62,8 +62,11 @@ class DeviceAccess: def identify(self, address: UnitAddress, serial: str) -> UnitInfo: return self._dispatch(address, serial, self._s3.identify, s4_identify) - def status(self, address: UnitAddress, serial: str) -> UnitStatus: - return self._dispatch(address, serial, self._s3.status, s4_status) + def status(self, address: UnitAddress, serial: str, *, run_state: bool = False) -> UnitStatus: + """`run_state` adds Series 4's raw SUB 0x49 code to `detail` (one more + round trip); Series 3 has no 0x49 and ignores it.""" + return self._dispatch(address, serial, self._s3.status, + lambda client, s: s4_status(client, s, run_state=run_state)) def settings(self, address: UnitAddress, serial: str) -> UnitSettings: return self._dispatch(address, serial, self._s3.settings, s4_settings) diff --git a/sfm/device_access/s4.py b/sfm/device_access/s4.py index 10961c6..87e929c 100644 --- a/sfm/device_access/s4.py +++ b/sfm/device_access/s4.py @@ -249,14 +249,18 @@ def s4_identify(client: Any, serial: str) -> UnitInfo: ) -def s4_status(client: Any, serial: str) -> UnitStatus: +def s4_status(client: Any, serial: str, run_state: bool = False) -> UnitStatus: st = client.get_state() count = client.get_event_count() + detail = {} + if run_state: # one more round trip, so only when asked (the ring-path bench test) + rs = client.read_run_state() + detail["run_state"] = {"code": rs.code, "name": rs.name} return UnitStatus( serial=serial, series=4, monitoring=st.monitoring, battery_volts=st.battery_volts, memory_total_bytes=st.memory_total_bytes, memory_free_bytes=st.memory_free_bytes, device_time=st.device_time.isoformat() if st.device_time else None, - event_count=count, + event_count=count, detail=detail, ) diff --git a/sfm/server.py b/sfm/server.py index 519789e..58b06e8 100644 --- a/sfm/server.py +++ b/sfm/server.py @@ -1234,6 +1234,8 @@ def device_monitor_status( tcp_port: int = Query(DEFAULT_TCP_PORT, description=f"TCP port (default {DEFAULT_TCP_PORT})"), force: bool = Query(False, description="Bypass cache and re-read from device"), serial: Optional[str] = Query(None, description=_SERIAL_DOC), + run_state: bool = Query(False, description="With a UM serial: also read SUB 0x49 " + "(one more round trip) and return its raw code in detail.run_state"), ) -> dict: """ Read monitoring status from the device. @@ -1251,7 +1253,8 @@ def device_monitor_status( """ if serial: return _via_device_access( - lambda: _device_access().status(_unit_address(port, baud, host, tcp_port), serial)) + lambda: _device_access().status(_unit_address(port, baud, host, tcp_port), serial, + run_state=run_state)) baud = 38400 if baud is None else baud cache = get_cache() conn_key = SFMCache.make_conn_key(host, tcp_port, port, baud) diff --git a/tests/test_device_access_s4.py b/tests/test_device_access_s4.py index 2116fc2..c59e657 100644 --- a/tests/test_device_access_s4.py +++ b/tests/test_device_access_s4.py @@ -280,6 +280,15 @@ def test_s4_status_includes_the_cheap_event_count(): assert st.device_time == "2026-10-20T18:07:58" +def test_s4_status_reads_0x49_only_when_asked(): + # speed: 0x49 is one more round trip, so a plain status never reads it + fake = FakeMicromateClient(run_states=[0x01]) + assert "run_state" not in s4_status(fake, "UM20147").detail + assert "read_run_state" not in fake.calls + st = s4_status(fake, "UM20147", run_state=True) + assert st.detail["run_state"] == {"code": 1, "name": "unknown(0x01)"} + + def test_s4_settings_maps_the_decoded_setup(): s = s4_settings(FakeMicromateClient(), "UM20147") assert (s.series, s.setup_name, s.record_mode) == (4, "test2.MMB", "Histogram Combo") diff --git a/tests/test_server_device_access.py b/tests/test_server_device_access.py index 7d73182..93bd2f3 100644 --- a/tests/test_server_device_access.py +++ b/tests/test_server_device_access.py @@ -48,6 +48,14 @@ def test_status_and_settings_with_a_serial(da): assert srv.device_settings(serial="UM20147", **TCP)["record_mode"] == "Histogram Combo" +def test_status_run_state_is_opt_in(da): + # called directly, a Query default is truthy: pass it as FastAPI would + assert "run_state" not in srv.device_monitor_status(force=False, serial="UM20147", run_state=False, + **TCP)["detail"] + out = srv.device_monitor_status(force=False, serial="UM20147", run_state=True, **TCP) + assert out["detail"]["run_state"] == {"code": 0, "name": "idle"} + + def test_hangup(da): srv.device_monitor_status(force=False, serial="UM20147", **TCP) assert srv.device_hangup(**TCP) == {"hung_up": True}