feat(micromate): read the schedule file (read-only)

MicromateProtocol.read_file: 0x94 + path, then 0x48 pages to an
all-zero reply -- THOR's frames byte for byte (UM12947, 2026-09-24),
including params[3] = 1 on 0x94 and params[7] = 1 on the first 0x48.
micromate.schedule parses the 260-byte records (two- and five-entry
schedules from THOR's captures) and names the scheduled starts that
would load a setup other than the active one, ignoring case.  The
client gains the raw (unstripped) active-setup name and the prefs /
trigger / setup content reads a setup push sends back.

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-11-03 21:37:30 -05:00
1 parent 5ba72740dd
commit d0335dbbbd
6 files changed
+281 -1

No files matched your search

+23
View File
@@ -48,6 +48,7 @@ from .models import (
MicromateState,
)
from .protocol import MicromateProtocol, ProtocolError
from .schedule import SCHEDULE_PATH
from .setup_block import decode_setup
log = logging.getLogger(__name__)
@@ -333,6 +334,28 @@ class MicromateClient:
"""`SUB 0x41` — the loaded `.MMB` file name, e.g. `TEST1.mmb`."""
return _cstring(_content(self._proto.read_active_setup_name()))
def get_active_setup_raw(self) -> str:
"""The `0x41` name exactly as stored, up to its NUL -- NOT stripped, so a
setup push writes back the very name the unit holds."""
return _content(self._proto.read_active_setup_name()).split(b"\x00")[0].decode("ascii", "replace")
def get_prefs_content(self) -> bytes:
"""`SUB 0x08` -- the preferences block's content (a setup push sends it back)."""
return _content(self._proto.read_event_index())
def get_trigger_content(self) -> bytes:
"""`SUB 0x2E` -- the trigger block's content (a setup push sends it back)."""
return _content(self._proto.read_trigger_config())
def get_setup_content(self) -> bytes:
"""`SUB 0x1A` -- the setup block's content, undecoded (2,092 B, CRC last).
⚠ The keypad's edit buffer: unsaved menu edits come back too."""
return _content(self._proto.read_compliance_config())
def read_schedule(self) -> bytes:
"""`schedule.dat`, read-only (`micromate.schedule.parse_schedule` reads it)."""
return self._proto.read_file(SCHEDULE_PATH)
def get_setup(self, *, verify: bool = True) -> MicromateSetup:
"""`SUB 0x1A` — what the unit is set to, decoded. One read.
+31
View File
@@ -93,6 +93,8 @@ SUB_STATE = 0x49
SUB_BULK_DOWNLOAD = 0x5A
SUB_POLL = 0x5B
SUB_ARM_EVENT = 0x93
SUB_FILE_READ_NEXT = 0x48 # file transfer: next page of the file opened by 0x94
SUB_FILE_OPEN_READ = 0x94 # file transfer: open a file by path, for reading
# ⚠ Reads are SINGLE-STEP. Series III probes at offset 0 to learn the length,
# then reads again at that length; a Micromate returns the whole block when
@@ -102,6 +104,10 @@ READ_ALL = 0xFFFF
# The two commands that do NOT use READ_ALL, and the data length each returned
# on UM12947 (firmware 11.0CB).
_FILE_OPEN_PARAMS = bytes([0, 0, 0, 1, 0, 0, 0, 0, 0, 0])
_FILE_FIRST_PAGE_PARAMS = bytes([0, 0, 0, 0, 0, 0, 0, 1, 0, 0])
_MAX_FILE_PAGES = 64
_OFFSETS = {
SUB_POLL: 0x0030, # 59 B — the one offset THOR treats as a constant
SUB_SERIAL: 0x000A, # 21 B
@@ -376,6 +382,31 @@ class MicromateProtocol:
"""
return self._read(SUB_SETUP_NEXT)
# ── Files, by path (read-only) ────────────────────────────────────────────
def read_file(self, path: str) -> bytes:
"""A file off the unit, by full path: `0x94` + the path opens it, then
`0x48` pages follow until an all-zero reply. The first `0x48` reply is a
descriptor, not file content. Frames are THOR's exactly (UM12947 reading
`schedule.dat`, 2026-09-24): `0x94` carries `params[3] = 1` and the path,
unpadded, as data with offset = its length; the first `0x48` carries
`params[7] = 1`, the rest none. Returns the content (each page's data
after its 11-byte prefix), joined.
"""
raw = path.encode("ascii")
self._request(build_request(SUB_FILE_OPEN_READ, len(raw), _FILE_OPEN_PARAMS, raw),
0xFF - SUB_FILE_OPEN_READ)
body = bytearray()
for page in range(_MAX_FILE_PAGES):
f = self._exchange(SUB_FILE_READ_NEXT,
params=_FILE_FIRST_PAGE_PARAMS if page == 0 else bytes(10))
if page == 0:
continue # the descriptor
if not any(f.data):
return bytes(body)
body += f.data[11:]
raise ProtocolError(f"{path}: no end of file after {_MAX_FILE_PAGES} pages")
# ── Event chain ───────────────────────────────────────────────────────────
def arm_event(self) -> MicromateFrame:
+75
View File
@@ -0,0 +1,75 @@
"""schedule.py -- a Micromate's schedule file, parsed. Pure; read-only by design.
THOR's scheduler lives in a file on the unit, `\\system\\schedule\\schedule.dat`,
read by path (`MicromateProtocol.read_file`). The format is decoded in
docs/micromate_protocol_reference.md, *The schedule record format* (measured on
UM12947, 2026-09-24/25): 260-byte records,
[0] record 0 only: 3 = repeat on, 2 = repeat off
[4] half-hour slot, 0-47
[5] day (3 in every record seen -- undetermined)
[6] action bitmask: 2 start monitoring with setup, 4 stop, 8 self-check,
16 auto call home
[7] setup-name length
[8:260] setup name (empty for actions that take none)
Why SFM reads it (phase 5): each scheduled start LOADS the setup it names. A
settings change to any other setup would be replaced at the next start, so SFM
refuses that change -- `schedule_conflicts`. SFM never writes the schedule.
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import List, Tuple
SCHEDULE_PATH = "\\system\\schedule\\schedule.dat"
_RECORD = 260
_NAME_MAX = _RECORD - 8
_ACTIONS = ((0x02, "start_with_setup"), (0x04, "stop"), (0x08, "self_check"), (0x10, "call_home"))
_KNOWN_BITS = sum(bit for bit, _ in _ACTIONS)
@dataclass(frozen=True)
class ScheduleEntry:
index: int
time: str # "HH:MM", from the half-hour slot
day: int # the raw byte; undetermined
action_bits: int
actions: Tuple[str, ...] # named bits; any other set bit as "unknown(0xNN)"
setup: str # "" when the action takes no setup
def _actions(bits: int) -> Tuple[str, ...]:
named = tuple(name for bit, name in _ACTIONS if bits & bit)
unknown = tuple(f"unknown(0x{1 << i:02x})" for i in range(8)
if bits & (1 << i) and not (1 << i) & _KNOWN_BITS)
return named + unknown
def parse_schedule(body: bytes) -> List[ScheduleEntry]:
"""The entries in a schedule file body. A record with nothing in [4:8] and
no name ends the list (the file is padded with zeros)."""
out: List[ScheduleEntry] = []
for i in range(len(body) // _RECORD):
r = body[i * _RECORD:(i + 1) * _RECORD]
n = min(r[7], _NAME_MAX)
name = r[8:8 + n].decode("ascii", "replace") if n else ""
if not any(r[4:8]) and not name:
break
slot = r[4]
out.append(ScheduleEntry(index=i, time=f"{slot // 2:02d}:{(slot % 2) * 30:02d}", day=r[5],
action_bits=r[6], actions=_actions(r[6]), setup=name))
return out
def _same_setup(a: str, b: str) -> bool:
return a.strip().casefold() == b.strip().casefold()
def schedule_conflicts(entries: List[ScheduleEntry], active_setup: str) -> List[ScheduleEntry]:
"""The scheduled starts that would load a setup other than `active_setup`
(compared ignoring case and surrounding spaces: THOR and the keypad disagree
on case, e.g. `factory.MMB` / `test2.mmb`)."""
return [e for e in entries
if "start_with_setup" in e.actions and e.setup and not _same_setup(e.setup, active_setup)]