Files
seismo-relay/scratch/mm_write_fixtures.py

99 lines
3.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""Cut Micromate write-session fixtures out of THOR's captures.
bridges/captures/ is gitignored, so the reproduction tests read these instead.
Each fixture is every request (as wire bytes) and every response (as its data
section) of one captured THOR session, paired in order, with the sha256 of the
capture files it came from. Re-run to regenerate:
.venv/bin/python scratch/mm_write_fixtures.py
"""
from __future__ import annotations
import hashlib
import json
import sys
from pathlib import Path
REPO = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(REPO))
from micromate.framing import ACK, DLE, ETX, STX, MicromateFrameParser, request_sub # noqa: E402
CAP = REPO / "bridges" / "captures" / "9-24-26 - micromate2"
OUT = REPO / "tests" / "fixtures" / "micromate-writes"
SESSIONS = {
"setup-push-new-test1": "20260924_181942_send_TEST1_to_unit",
"setup-push-overwrite-test1": "overwritetest/*20260924_185113_test1_overwrite_check",
"setup-push-read-scheduler": "20260924_191835_read_scheduler",
"setup-push-pull-schedule": "20260925_003029_pull_schedule,_turn_off_repeat_daily,_push_schedule",
"setup-push-multiday": "20260925_011907_send_multiday_schedule",
"call-home-enable-disable": "20260925_011620_ACH-change_1_setting",
"monitor-start-stop": "20260924_191214_turn_on_monitormode_then_attempt_to_write_config",
}
def _find(stem: str, kind: str) -> Path:
if "/" in stem:
sub, rest = stem.split("/", 1)
hits = sorted((CAP / sub).glob(f"raw_{kind}_{rest.lstrip('*')}.bin"))
else:
hits = sorted(CAP.glob(f"raw_{kind}_{stem}.bin"))
if len(hits) != 1:
raise SystemExit(f"expected one raw_{kind} file for {stem!r}, found {hits}")
return hits[0]
def requests(blob: bytes) -> list[bytes]:
out, i = [], 0
while i < len(blob):
if not (blob[i] == ACK and i + 1 < len(blob) and blob[i + 1] == STX):
i += 1
continue
j = i + 2
while j < len(blob):
if blob[j] == DLE and j + 1 < len(blob):
j += 2
continue
if blob[j] == ETX:
break
j += 1
out.append(blob[i:j + 1])
i = j + 1
return out
def main() -> None:
OUT.mkdir(parents=True, exist_ok=True)
for slug, stem in SESSIONS.items():
bw, s3 = _find(stem, "bw"), _find(stem, "s3")
reqs = requests(bw.read_bytes())
parser = MicromateFrameParser()
rsps = parser.feed(s3.read_bytes())
if len(reqs) != len(rsps):
raise SystemExit(f"{slug}: {len(reqs)} requests vs {len(rsps)} responses")
exchanges = []
lines = {rp.firmware_line for rp in rsps}
if len(lines) != 1 or lines & {"unknown"}:
raise SystemExit(f"{slug}: responses report firmware lines {sorted(lines)}")
for rq, rp in zip(reqs, rsps):
sub = request_sub(rq)
if rp.sub != 0xFF - sub or not rp.checksum_valid:
raise SystemExit(f"{slug}: response 0x{rp.sub:02X} does not answer 0x{sub:02X}")
exchanges.append({"sub": sub, "request": rq.hex(), "response_sub": rp.sub,
"response_data": rp.data.hex()})
doc = {
"source": str(bw.relative_to(REPO)),
"source_sha256": {"bw": hashlib.sha256(bw.read_bytes()).hexdigest(),
"s3": hashlib.sha256(s3.read_bytes()).hexdigest()},
"unit": "UM12947", "firmware_version": "11.0CB",
"firmware_line": lines.pop(),
"exchanges": exchanges,
}
(OUT / f"{slug}.json").write_text(json.dumps(doc, indent=1) + "\n")
print(f"{slug}: {len(exchanges)} exchanges")
if __name__ == "__main__":
main()