feat(sfm): call-home state and enable/disable by serial -- GET /device/call_home/state, POST /device/call_home/{enable,disable} (phase 4)

- DeviceAccess.call_home_state (a read) and set_call_home (change=True: never
  retried, audit required for a live change, dry run without one), routed by
  series like start/stop.  A config write is read back on its own window: up
  to 3 reads, 1 s apart, not the monitoring one.
- Routes, serial required (422 without), all three serial-aware: the state as
  a dict; enable / disable through _change_response, so their statuses are
  start/stop's and an unconfirmed change carries sent + audit_id.  The legacy
  GET / POST /device/call_home are unchanged (a Micromate serial is still
  refused there, 501).

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-02 17:01:14 -05:00
1 parent 453d42e32a
commit 06fa56db09
4 files changed
+233 -3

No files matched your search

+34 -3
View File
@@ -12,16 +12,22 @@ from typing import Any, Callable, Dict, Optional
from .errors import ConnectFailed, DeviceError, NoAnswer, NotSupported, WrongUnit
from .events import parse_event_id
from .locks import UnitLocks
from .models import ChangeResult, DownloadedEvent, EventList, UnitAddress, UnitInfo, UnitSettings, UnitStatus
from .models import (CallHomeState, ChangeResult, DownloadedEvent, EventList, UnitAddress, UnitInfo,
UnitSettings, UnitStatus)
from .s3 import Series3Adapter, default_s3_client_factory
from .s4 import (
Series4Session, default_s4_client_factory, s4_change_monitoring, s4_download_event, s4_identify, s4_list_events,
s4_settings, s4_status,
Series4Session, default_s4_client_factory, s4_call_home_state, s4_change_call_home, s4_change_monitoring,
s4_download_event, s4_identify, s4_list_events, s4_settings, s4_status,
)
from .series import normalise_serial, series_for_serial
log = logging.getLogger("sfm.device_access")
# A config write is read back at once, not after a sensor check: up to three
# reads, a second apart -- not the start/stop window.
_CALL_HOME_VERIFY_TIMEOUT_S = 3.0
_CALL_HOME_VERIFY_INTERVAL_S = 1.0
class DeviceAccess:
def __init__(
@@ -109,6 +115,31 @@ class DeviceAccess:
lambda c, s: s4_change_monitoring(c, s, address=address, **kw),
change=True)
def call_home_state(self, address: UnitAddress, serial: str) -> CallHomeState:
return self._dispatch(address, serial,
lambda a, s: self._s3.call_home_state(a, s),
lambda c, s: s4_call_home_state(c, s))
def set_call_home(self, address: UnitAddress, serial: str, *, enabled: bool,
dry_run: bool = False, even_if_already: bool = False,
caller: str = "api") -> ChangeResult:
"""Turn the unit's automatic call-home on or off (spec row 9): audited,
identity-checked, never retried, read back."""
serial = normalise_serial(serial)
series_for_serial(serial) # BadRequest (422) before the audit check
if not dry_run and self._audit is None:
raise NotSupported("no audit log is configured -- SFM refuses to change a unit "
"without one (nothing sent)")
kw = dict(enabled=enabled, dry_run=dry_run, even_if_already=even_if_already,
audit=self._audit, caller=caller, sleep=self._sleep,
verify_timeout_s=_CALL_HOME_VERIFY_TIMEOUT_S,
verify_interval_s=_CALL_HOME_VERIFY_INTERVAL_S)
return self._dispatch(
address, serial,
lambda a, s: self._s3.change_call_home(a, s, **kw),
lambda c, s: s4_change_call_home(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:
+59
View File
@@ -425,6 +425,7 @@ _SERIAL_AWARE_PATHS = frozenset({
"/device/info", "/device/connect", "/device/monitor/status", "/device/settings",
"/device/events", "/device/events/download",
"/device/monitor/start", "/device/monitor/stop",
"/device/call_home/state", "/device/call_home/enable", "/device/call_home/disable",
})
@@ -2055,6 +2056,64 @@ def device_monitor_stop(
# ── Call home config endpoints ───────────────────────────────────────────────
@app.get("/device/call_home/state")
def device_call_home_state(
serial: str = Query(..., description=_SERIAL_DOC),
host: Optional[str] = Query(None, description="TCP host — modem IP"),
tcp_port: int = Query(DEFAULT_TCP_PORT, description=f"TCP port (default {DEFAULT_TCP_PORT})"),
port: Optional[str] = Query(None, description="Serial port (e.g. COM5)"),
baud: Optional[int] = Query(None, description="Serial baud rate"),
) -> dict:
"""The unit's call-home switch and dial string, by serial, both series (phase 4).
`enabled` is null when the enable byte is not a value ever measured;
`detail` keeps `byte5` and the whole block as read."""
return _via_device_access(lambda: _device_access().call_home_state(
_unit_address(port, baud, host, tcp_port), serial))
def _set_call_home(enabled: bool, port, baud, host, tcp_port, serial, dry_run, even_if_already, caller):
return _change_response(lambda: _device_access().set_call_home(
_unit_address(port, baud, host, tcp_port), serial, enabled=enabled, dry_run=dry_run,
even_if_already=even_if_already, caller=caller or "api"))
@app.post("/device/call_home/enable")
def device_call_home_enable(
port: Optional[str] = Query(None, description="Serial port (e.g. COM5)"),
baud: Optional[int] = Query(None, description="Serial baud rate"),
host: Optional[str] = Query(None, description="TCP host — modem IP"),
tcp_port: int = Query(DEFAULT_TCP_PORT, description=f"TCP port (default {DEFAULT_TCP_PORT})"),
serial: str = Query(..., description=_SERIAL_DOC),
dry_run: bool = Query(False, description="Return the planned frames; send nothing"),
even_if_already: bool = Query(False, description="Send even if call-home is already on"),
caller: Optional[str] = Query(None, description="Who is asking (recorded in the audit row)"),
):
"""Turn the unit's automatic call-home ON (spec row 9), by serial, both series.
The same audited change as /device/monitor/start: identity read first (a
different unit is a 409, nothing sent), the 0x2C block read, one write that
changes only the enable byte (0x7E + 0x7F), never retried, then read back.
Statuses as /device/monitor/start; a change that went out but was not
confirmed carries `sent` and `audit_id`."""
return _set_call_home(True, port, baud, host, tcp_port, serial, dry_run, even_if_already, caller)
@app.post("/device/call_home/disable")
def device_call_home_disable(
port: Optional[str] = Query(None, description="Serial port (e.g. COM5)"),
baud: Optional[int] = Query(None, description="Serial baud rate"),
host: Optional[str] = Query(None, description="TCP host — modem IP"),
tcp_port: int = Query(DEFAULT_TCP_PORT, description=f"TCP port (default {DEFAULT_TCP_PORT})"),
serial: str = Query(..., description=_SERIAL_DOC),
dry_run: bool = Query(False, description="Return the planned frames; send nothing"),
even_if_already: bool = Query(False, description="Send even if call-home is already off"),
caller: Optional[str] = Query(None, description="Who is asking (recorded in the audit row)"),
):
"""Turn the unit's automatic call-home OFF -- as /device/call_home/enable."""
return _set_call_home(False, port, baud, host, tcp_port, serial, dry_run, even_if_already, caller)
@app.get("/device/call_home")
def device_call_home_get(
port: Optional[str] = Query(None, description="Serial port (e.g. COM5)"),