feat(micromate): get_event_count() and read_unsolicited()

0x06 content[0:4] moves from the bench tool into the client, tested against a real UM20147 read and its chain-walk count.  read_unsolicited() reads without sending, for spotting RADIO RING on an idle session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
serversdownandClaude Opus 5.5 committed 2026-10-21 01:04:35 -04:00
1 parent 31181f64e6
commit fa032567c2
2 files changed
+49

No files matched your search

+22
View File
@@ -53,6 +53,11 @@ CONTENT = 11
# two of them disagree with docs/micromate_protocol_reference.md.
_STATE_FLAG = 0 # 0x49: 0x00 idle, 0x02 monitoring
# 0x06 storage range. content[0:4] is the event count: 3/3 against the chain
# walk, and THOR's ReadEventRecordCount agrees. content[4:8] is very likely
# the monitor-log count -- graded a strong candidate, not settled, so not exposed.
_SR_EVENT_COUNT = slice(0, 4)
_MS_FLAG = 1 # 0x1C: monitoring flag — test NON-ZERO
_MS_DAY, _MS_MONTH, _MS_YEAR = 2, 3, slice(4, 6)
_MS_UNKNOWN_6 = 6 # ⚠ sub-second tick (~4020/s, step 4, 64 ms wrap) — NOT the hour
@@ -264,6 +269,23 @@ class MicromateClient:
raw=data,
)
def get_event_count(self) -> int:
"""`SUB 0x06` -- events stored. One read; no chain walk."""
c = _content(self._proto.read_storage_range())
if len(c) < 4:
raise ProtocolError(f"storage range content is {len(c)} B, need at least 4")
return int.from_bytes(c[_SR_EVENT_COUNT], "big")
def read_unsolicited(self) -> bytes:
"""Whatever the unit sent unprompted. **Sends nothing.**
One transport read. Bytes between commands are noise to the protocol
(every request resets the parser), so reading them here takes nothing
the protocol needs. A session calls this while idle to notice
RADIO RING without waiting for its next keep-alive.
"""
return self._transport.read(4096)
@staticmethod
def _parse_clock(c: bytes) -> Optional[datetime.datetime]:
"""The unit's own clock, in its own local time.
+27
View File
@@ -547,3 +547,30 @@ def test_is_open_works_on_a_real_transport():
mm = MicromateClient(TcpTransport("127.0.0.1", port=9)) # never connected
assert mm.is_open() is False
# ── get_event_count() / read_unsolicited() ────────────────────────────────────
# 0x06 response data captured from UM20147 (11.0BD) by Task 2, Step 1. The
# expected count is that session's independent chain-walk count.
SR_UM20147 = bytes.fromhex("2400000000000000000000000000050000000900000000000000000000000600000000000000000000000000000000")
SR_UM20147_CHAIN_WALK_COUNT = 5
def test_event_count_is_content_0_4_and_matches_the_chain_walk():
mm, t = client([frame(0xF9, SR_UM20147, flags=FLAGS_THOR)])
assert mm.get_event_count() == SR_UM20147_CHAIN_WALK_COUNT
assert [w[5] for w in t.written] == [0x06]
def test_a_truncated_storage_range_raises():
mm, _ = client([frame(0xF9, bytes(CONTENT) + b"\x00\x05", flags=FLAGS_THOR)])
with pytest.raises(ProtocolError, match="storage range"):
mm.get_event_count()
def test_read_unsolicited_sends_nothing_and_returns_what_arrived():
"""Used to spot RADIO RING on an idle session. It must never write."""
mm, t = client([b"\r\nRADIO RING\r\n"])
assert mm.read_unsolicited() == b"\r\nRADIO RING\r\n"
assert t.written == []