fix(sfm): wake a MiniMate before the read-back reconnect reads its serial (#9 review)
The reconnect in ce441a0 opened a fresh connection and sent a bare 0x15 (the
identity check) before any SESSION_RESET + POLL. It exists for a unit that has
just started -- sensor check, then monitoring -- and a monitoring MiniMate
ignores a bare request until woken (measured 2026-04-08), so the identity read
would have timed out and the start reported unconfirmed anyway. Now: open,
wake (the same retried wake the read-back uses), read the serial, read 0x1C --
the order the status read and the legacy write routes use. Still one
reconnect, TCP only, nothing re-sent.
The fake gains bare_requests_ignored (off unless a test asks): a fresh
connection's reads time out, with the real client's TimeoutError, until woken.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
1 parent
1626e521ae
commit
dbd8a91661
3 files changed
+41
-4
No files matched your search
@@ -249,8 +249,12 @@ class Series3Adapter:
|
|||||||
address.key, exc)
|
address.key, exc)
|
||||||
client.close()
|
client.close()
|
||||||
client.open()
|
client.open()
|
||||||
|
# Wake first: the identity read is a bare 0x15, which a unit that
|
||||||
|
# has just started ignores until SESSION_RESET + POLL (the order
|
||||||
|
# the status read and the legacy write routes use: poll, then read).
|
||||||
|
client.wake(attempt_s=S3_WAKE_ATTEMPT_S, deadline_s=S3_WAKE_DEADLINE_S)
|
||||||
self._check(client.read_serial(), serial, address)
|
self._check(client.read_serial(), serial, address)
|
||||||
return _woken_state(client)
|
return s3_run_state(client.get_monitor_status())
|
||||||
|
|
||||||
return run_change(
|
return run_change(
|
||||||
serial=serial, series=3, start=start, dry_run=dry_run,
|
serial=serial, series=3, start=start, dry_run=dry_run,
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ SETUP_FIXTURE = Path(__file__).parent / "fixtures" / "micromate-setup" / "um2014
|
|||||||
THOR_IDF = Path(__file__).parent / "fixtures" / "thor-idf"
|
THOR_IDF = Path(__file__).parent / "fixtures" / "thor-idf"
|
||||||
|
|
||||||
|
|
||||||
|
from minimateplus.protocol import TimeoutError as mmp_TimeoutError # noqa: E402
|
||||||
|
|
||||||
|
|
||||||
class _Base:
|
class _Base:
|
||||||
def __init__(self, fail=None, open_error=None):
|
def __init__(self, fail=None, open_error=None):
|
||||||
self.fail = list(fail or [])
|
self.fail = list(fail or [])
|
||||||
@@ -75,9 +78,14 @@ def default_s3_events():
|
|||||||
|
|
||||||
class FakeMiniMateClient(_Base):
|
class FakeMiniMateClient(_Base):
|
||||||
def __init__(self, serial="BE18193", *, monitoring=False, compliance="default", events=None,
|
def __init__(self, serial="BE18193", *, monitoring=False, compliance="default", events=None,
|
||||||
monitor_states=None, write_error=None, serial_reported=None, wake_errors=None, **kw):
|
monitor_states=None, write_error=None, serial_reported=None, wake_errors=None,
|
||||||
|
bare_requests_ignored=False, **kw):
|
||||||
super().__init__(**kw)
|
super().__init__(**kw)
|
||||||
self.serial = serial
|
self.serial = serial
|
||||||
|
# As a monitoring MiniMate (measured 2026-04-08): on a fresh connection a
|
||||||
|
# read before SESSION_RESET + POLL goes unanswered. Off unless a test asks.
|
||||||
|
self.bare_requests_ignored = bare_requests_ignored
|
||||||
|
self._woken = False
|
||||||
# read_serial()'s answer (connect() keeps answering `serial`), and errors
|
# read_serial()'s answer (connect() keeps answering `serial`), and errors
|
||||||
# raised by wake() once a write has gone out -- the read-back's wakes.
|
# raised by wake() once a write has gone out -- the read-back's wakes.
|
||||||
self.serial_reported = serial if serial_reported is None else serial_reported
|
self.serial_reported = serial if serial_reported is None else serial_reported
|
||||||
@@ -98,8 +106,18 @@ class FakeMiniMateClient(_Base):
|
|||||||
self.download_requests: list = [] # (key4, expected_timestamp) per download
|
self.download_requests: list = [] # (key4, expected_timestamp) per download
|
||||||
self.last_download = None
|
self.last_download = None
|
||||||
|
|
||||||
|
def open(self):
|
||||||
|
super().open()
|
||||||
|
self._woken = False
|
||||||
|
|
||||||
|
def _bare_read(self, name):
|
||||||
|
self._step(name)
|
||||||
|
if self.bare_requests_ignored and not self._woken:
|
||||||
|
raise mmp_TimeoutError(f"{name}: no reply (the unit was not woken)")
|
||||||
|
|
||||||
def connect(self) -> DeviceInfo:
|
def connect(self) -> DeviceInfo:
|
||||||
self._step("connect")
|
self._step("connect")
|
||||||
|
self._woken = True # connect() starts with the startup handshake
|
||||||
return DeviceInfo(
|
return DeviceInfo(
|
||||||
serial=self.serial, firmware_minor=17, firmware_version="S338.17",
|
serial=self.serial, firmware_minor=17, firmware_version="S338.17",
|
||||||
dsp_version="10.72", manufacturer="Instantel", model="MiniMate Plus",
|
dsp_version="10.72", manufacturer="Instantel", model="MiniMate Plus",
|
||||||
@@ -108,18 +126,20 @@ class FakeMiniMateClient(_Base):
|
|||||||
|
|
||||||
def poll(self) -> None:
|
def poll(self) -> None:
|
||||||
self._step("poll")
|
self._step("poll")
|
||||||
|
self._woken = True
|
||||||
|
|
||||||
def wake(self, **kw) -> None:
|
def wake(self, **kw) -> None:
|
||||||
self._step("wake")
|
self._step("wake")
|
||||||
if self.writes and self.wake_errors:
|
if self.writes and self.wake_errors:
|
||||||
raise self.wake_errors.pop(0)
|
raise self.wake_errors.pop(0)
|
||||||
|
self._woken = True
|
||||||
|
|
||||||
def read_serial(self) -> str:
|
def read_serial(self) -> str:
|
||||||
self._step("read_serial")
|
self._bare_read("read_serial")
|
||||||
return self.serial_reported
|
return self.serial_reported
|
||||||
|
|
||||||
def get_monitor_status(self) -> MonitorStatus:
|
def get_monitor_status(self) -> MonitorStatus:
|
||||||
self._step("get_monitor_status")
|
self._bare_read("get_monitor_status")
|
||||||
state = self.monitor_states.pop(0) if len(self.monitor_states) > 1 else self.monitor_states[0]
|
state = self.monitor_states.pop(0) if len(self.monitor_states) > 1 else self.monitor_states[0]
|
||||||
# A bool is a measured state; an int (or None: a short section) is the raw 0x1C flag byte.
|
# A bool is a measured state; an int (or None: a short section) is the raw 0x1C flag byte.
|
||||||
flag = (0x10 if state else 0x00) if isinstance(state, bool) else state
|
flag = (0x10 if state else 0x00) if isinstance(state, bool) else state
|
||||||
|
|||||||
@@ -816,6 +816,19 @@ def test_series3_start_read_back_reconnects_after_a_dropped_line():
|
|||||||
assert fake.opened == 2 and "read_serial" in fake.calls
|
assert fake.opened == 2 and "read_serial" in fake.calls
|
||||||
|
|
||||||
|
|
||||||
|
def test_series3_read_back_reconnect_wakes_the_unit_before_reading_it():
|
||||||
|
# The unit the reconnect is for has just started (sensor check, then
|
||||||
|
# monitoring), and a monitoring MiniMate ignores a bare request until it is
|
||||||
|
# woken -- so the fresh connection's identity read must come after the wake.
|
||||||
|
fake = FakeMiniMateClient(monitor_states=[False, True], bare_requests_ignored=True,
|
||||||
|
wake_errors=[BrokenPipeError(32, "Broken pipe")])
|
||||||
|
r = access3(fake).start_monitoring(TCP3, "BE18193")
|
||||||
|
assert (r.outcome, r.verified, r.state_after) == ("sent", "ok", "monitoring")
|
||||||
|
assert fake.writes == ["start_monitoring"] and fake.opened == 2
|
||||||
|
after = fake.calls[fake.calls.index("start_monitoring"):]
|
||||||
|
assert after.index("read_serial") > len(after) - 1 - after[::-1].index("wake")
|
||||||
|
|
||||||
|
|
||||||
def test_series3_read_back_reconnects_only_once():
|
def test_series3_read_back_reconnects_only_once():
|
||||||
fake = FakeMiniMateClient(monitor_states=[False],
|
fake = FakeMiniMateClient(monitor_states=[False],
|
||||||
wake_errors=[BrokenPipeError(32, "x"), BrokenPipeError(32, "x")])
|
wake_errors=[BrokenPipeError(32, "x"), BrokenPipeError(32, "x")])
|
||||||
|
|||||||
Reference in new issue
Block a user