"""SUB 0x1C monitor status, decoded from BE9441's own replies (2026-10-26). Recorded by the bridge (bridges/captures/10-26-26 - BE9441 blastware histogram/): * 16:24:54, idle, before the histogram run: flag 0x00. * 18:47:41, during a histogram run Brian confirmed at the unit: flag **0x0B**. Only 0x10 had ever been measured as "monitoring" (BE11529, 2026-04-08), so the status read "not monitoring" and the change path refused the state as unknown. * The same 18:47 reply's free memory, 0x000D0E04, holds a 04. The unit escapes it (10 04) and the parser keeps both bytes, so the fields counted from the end came out one byte off: 419.84 V and 251,655,168 bytes, against 6.76 V and 983,028. """ from __future__ import annotations import os import sys import pytest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from minimateplus.client import _decode_monitor_status # noqa: E402 from minimateplus.framing import S3FrameParser # noqa: E402 from sfm.device_access.s3 import s3_run_state # noqa: E402 IDLE_1624 = bytes.fromhex( "1002001010e300002c0000000000000000000000001a0a07ea0010101836000000000000010107cb00070000010107cb0013000000001002a4000efff4000e0de4fa03") MONITORING_1847 = bytes.fromhex( "1002001010e300002c00000000000000000000000b1a0a07ea00122f29000000000000010107cb00070000010107cb0013000000001002a4000efff4000d0e10043103") def _status(wire: bytes): frames = S3FrameParser().feed(wire) assert len(frames) == 1 and frames[0].sub == 0xE3 return _decode_monitor_status(frames[0].data) def test_idle_reply(): st = _status(IDLE_1624) assert (st.state_flag, st.is_monitoring, s3_run_state(st)) == (0x00, False, "idle") assert (st.battery_v, st.memory_total, st.memory_free) == (6.76, 983028, 921060) def test_a_histogram_run_reads_monitoring(): st = _status(MONITORING_1847) assert (st.state_flag, st.is_monitoring, s3_run_state(st)) == (0x0B, True, "monitoring") def test_an_escaped_byte_in_the_memory_field_does_not_shift_the_fields(): st = _status(MONITORING_1847) assert (st.battery_v, st.memory_total, st.memory_free) == (6.76, 983028, 855556) @pytest.mark.parametrize("value", [0x02, 0x03, 0x04, 0x10]) def test_every_escaped_value_in_the_last_ten_bytes_decodes(value): # free memory ending in each escaped value, encoded as the unit does st0 = _status(IDLE_1624) # the reply's last data byte (free memory's low byte, 0xE4) swapped for `value` assert IDLE_1624[-3] == 0xE4 frames = S3FrameParser().feed(IDLE_1624[:-3] + bytes([0x10, value]) + IDLE_1624[-2:]) st = _decode_monitor_status(frames[0].data) assert st.memory_free == (st0.memory_free & ~0xFF) | value assert (st.battery_v, st.memory_total) == (6.76, 983028)