feat(sfm): start/stop monitoring for Series 4 -- identity, state, dry run, audit, read-back, never retried

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-23 16:29:03 -04:00
1 parent ff0cce1cb2
commit eada7ba767
7 files changed
+326 -13

No files matched your search

+35 -6
View File
@@ -12,10 +12,10 @@ from typing import Any, Callable, Dict, Optional
from .errors import ConnectFailed, DeviceError, NoAnswer, WrongUnit
from .events import parse_event_id
from .locks import UnitLocks
from .models import DownloadedEvent, EventList, UnitAddress, UnitInfo, UnitSettings, UnitStatus
from .models import ChangeResult, DownloadedEvent, EventList, UnitAddress, UnitInfo, UnitSettings, UnitStatus
from .s3 import Series3Adapter, default_s3_client_factory
from .s4 import (
Series4Session, default_s4_client_factory, s4_download_event, s4_identify, s4_list_events,
Series4Session, default_s4_client_factory, s4_change_monitoring, s4_download_event, s4_identify, s4_list_events,
s4_settings, s4_status,
)
from .series import normalise_serial, series_for_serial
@@ -34,7 +34,15 @@ class DeviceAccess:
idle_hangup_s: float = 300.0,
reaper_interval_s: float = 5.0,
start_reaper: bool = True,
audit: Any = None,
sleep: Callable[[float], None] = time.sleep,
verify_timeout_s: float = 60.0,
verify_interval_s: float = 2.0,
) -> None:
self._audit = audit
self._sleep = sleep
self._verify_timeout_s = verify_timeout_s
self._verify_interval_s = verify_interval_s
self._locks = UnitLocks(lock_timeout)
self._s3 = Series3Adapter(s3_client_factory)
self._s4_factory = s4_client_factory
@@ -70,6 +78,27 @@ class DeviceAccess:
lambda a, s: self._s3.download_event(a, s, key, stamp),
lambda c, s: s4_download_event(c, s, key, stamp))
def start_monitoring(self, address: UnitAddress, serial: str, *, dry_run: bool = False,
even_if_already: bool = False, caller: str = "api") -> ChangeResult:
return self._change_monitoring(address, serial, start=True, dry_run=dry_run,
even_if_already=even_if_already, caller=caller)
def stop_monitoring(self, address: UnitAddress, serial: str, *, dry_run: bool = False,
even_if_already: bool = False, caller: str = "api") -> ChangeResult:
return self._change_monitoring(address, serial, start=False, dry_run=dry_run,
even_if_already=even_if_already, caller=caller)
def _change_monitoring(self, address, serial, *, start, dry_run, even_if_already, caller):
kw = dict(start=start, dry_run=dry_run, even_if_already=even_if_already,
audit=self._audit, caller=caller, sleep=self._sleep,
verify_timeout_s=self._verify_timeout_s,
verify_interval_s=self._verify_interval_s)
return self._dispatch(
address, serial,
lambda a, s: self._s3.change_monitoring(a, s, **kw),
lambda c, s: s4_change_monitoring(c, s, address=address, **kw),
change=True)
def held_serial(self, address: UnitAddress) -> Optional[str]:
"""Serial of the OPEN held Series-4 line at this address, else None. No I/O."""
with self._sessions_guard:
@@ -92,7 +121,7 @@ class DeviceAccess:
def _dispatch(self, address: UnitAddress, serial: str,
s3_op: Callable[[UnitAddress, str], Any],
s4_op: Callable[[Any, str], Any]) -> Any:
s4_op: Callable[[Any, str], Any], *, change: bool = False) -> Any:
serial = normalise_serial(serial)
series = series_for_serial(serial) # raises BadRequest before any I/O
with self._locks.hold(address.key):
@@ -115,9 +144,9 @@ class DeviceAccess:
# learns at EOF), so the first request on it times out or fails.
# Call up fresh and retry ONCE. Safe only because every operation
# here is a read (identify, status, settings, list, download -- all
# idempotent); writes (phase 3) must NOT
# inherit this -- a write whose ack was lost must never be sent twice.
if was_held and isinstance(exc, (NoAnswer, ConnectFailed)):
# idempotent). A change (`change=True`) is never retried, by
# construction: a write whose ack was lost must never be sent twice.
if was_held and not change and isinstance(exc, (NoAnswer, ConnectFailed)):
self._drop(address.key, session)
session.hang_up()
session = self._session_for(address, serial)