#!/usr/bin/env python3 """Dry run a Micromate write against a live unit: do the reads, print the plan. Sends READS only. The write frames are built from the unit's own blocks and printed for review -- step 1 of the bench protocol for every first live write (parity spec, Testing). The protocol's opcode gate refuses every write SUB on this connection, so nothing else can go out even by mistake. sg dialout -c ".venv/bin/python bridges/mm_dry_run.py /dev/ttyACM0 call-home-off" .venv/bin/python bridges/mm_dry_run.py --host 10.0.0.5 --tcp-port 9034 setup-rewrite The JSON carries a `reads` block (length of each block read, and for setup-rewrite the blocks themselves in hex) so the plan can be checked against what the unit actually said. ⚠ `0x1A` reads the keypad's edit buffer live. A setup read taken while someone is in the unit's menu includes their unsaved edits, and pushing that plan would commit them. ⚠ The setup-rewrite JSON embeds the unit's whole `0x1A` block (job number, notes), so do not paste it anywhere public. """ from __future__ import annotations import argparse import json import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) from micromate.client import CONTENT, MicromateClient # noqa: E402 from micromate.writes import ( # noqa: E402 plan_call_home_enabled, plan_setup_push, plan_start_monitoring, plan_stop_monitoring, ) from minimateplus.transport import SerialTransport, TcpTransport # noqa: E402 ACTIONS = ("start", "stop", "call-home-on", "call-home-off", "setup-rewrite") def _content(data: bytes) -> bytes: """The protocol's read_* methods return the response data with its 11-byte prefix.""" return bytes(data)[CONTENT:] def main() -> int: ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument("port", nargs="?", help="serial port, e.g. /dev/ttyACM0") ap.add_argument("action", choices=ACTIONS) ap.add_argument("--host") ap.add_argument("--tcp-port", type=int, default=9034) ap.add_argument("--baud", type=int, default=115200) a = ap.parse_args() if not (a.port or a.host): ap.error("give a serial port or --host") transport = TcpTransport(a.host, port=a.tcp_port) if a.host else SerialTransport(a.port, a.baud) mm = MicromateClient(transport) # no execute(): nothing here can transmit a write mm.open() try: info = mm.connect() p = mm.protocol reads = {} if a.action == "start": plan = plan_start_monitoring() elif a.action == "stop": plan = plan_stop_monitoring() elif a.action.startswith("call-home"): ch = _content(p.read_call_home_config()) reads["0x2C"] = {"len": len(ch)} plan = plan_call_home_enabled(ch, a.action == "call-home-on") else: name = mm.get_active_setup() prefs = _content(p.read_event_index()) trig = _content(p.read_trigger_config()) setup = _content(p.read_compliance_config()) reads = {"active_setup": name, "0x08": {"len": len(prefs), "hex": prefs.hex()}, "0x2E": {"len": len(trig), "hex": trig.hex()}, "0x1A": {"len": len(setup), "hex": setup.hex()}} plan = plan_setup_push(prefs, trig, setup, name) out = plan.describe() out["unit"] = {"serial": info.serial, "firmware_line": info.firmware_line} out["reads"] = reads print(json.dumps(out, indent=1)) finally: mm.close() return 0 if __name__ == "__main__": sys.exit(main())