feat(sfm): start/stop monitoring by serial -- dry run, audit, read-back
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
ce5d2cb0d6
commit
c3e7e169c0
2 files changed
+180
-6
No files matched your search
+54
-5
@@ -87,6 +87,7 @@ import dataclasses
|
||||
from sfm.device_access import DeviceAccess, DeviceError, UnitAddress
|
||||
from sfm.device_access.errors import BadRequest
|
||||
from sfm.device_access.series import normalise_serial
|
||||
from sfm.audit import DbAudit
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
@@ -359,7 +360,8 @@ def _run_with_retry(fn, *, is_tcp: bool):
|
||||
# legacy Series-3 path unchanged (spec 2026-10-20).
|
||||
|
||||
# Constructing it does no I/O and starts no thread until a Series-4 session exists.
|
||||
_device_access_instance: DeviceAccess = DeviceAccess()
|
||||
# The audit sink is mandatory for live changes: without one they are refused.
|
||||
_device_access_instance: DeviceAccess = DeviceAccess(audit=DbAudit(_get_db))
|
||||
|
||||
|
||||
def _device_access() -> DeviceAccess:
|
||||
@@ -380,9 +382,22 @@ def _via_device_access(fn) -> dict:
|
||||
return dataclasses.asdict(result) if dataclasses.is_dataclass(result) else result
|
||||
|
||||
|
||||
def _change_response(fn) -> JSONResponse:
|
||||
"""Run a start/stop; the ChangeResult is the body, its outcome picks the status.
|
||||
|
||||
DeviceError mapping is _via_device_access's -- not repeated here.
|
||||
"""
|
||||
body = _via_device_access(fn)
|
||||
status = 200
|
||||
if body["outcome"] == "failed":
|
||||
status = 409 if body["verified"] == "mismatch" else 502
|
||||
return JSONResponse(status_code=status, content=body)
|
||||
|
||||
|
||||
_SERIAL_AWARE_PATHS = frozenset({
|
||||
"/device/info", "/device/connect", "/device/monitor/status", "/device/settings",
|
||||
"/device/events", "/device/events/download",
|
||||
"/device/monitor/start", "/device/monitor/stop",
|
||||
})
|
||||
|
||||
|
||||
@@ -1838,15 +1853,32 @@ def device_rescue(
|
||||
@app.post("/device/monitor/start")
|
||||
def device_monitor_start(
|
||||
port: Optional[str] = Query(None, description="Serial port (e.g. COM5)"),
|
||||
baud: int = Query(38400, description="Serial baud rate"),
|
||||
baud: Optional[int] = Query(None, description="Serial baud rate"),
|
||||
host: Optional[str] = Query(None, description="TCP host — modem IP or ACH relay"),
|
||||
tcp_port: int = Query(DEFAULT_TCP_PORT, description=f"TCP port (default {DEFAULT_TCP_PORT})"),
|
||||
) -> dict:
|
||||
serial: Optional[str] = Query(None, 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 the unit is already in the target state"),
|
||||
caller: Optional[str] = Query(None, description="Who is asking (recorded in the audit row)"),
|
||||
):
|
||||
"""
|
||||
Command the device to start monitoring (recording triggered events).
|
||||
|
||||
Sends SUB 0x96 and waits for ack SUB 0x69.
|
||||
|
||||
With `serial`: the unit's identity is read first (a different unit is a 409,
|
||||
nothing sent), then its state; `dry_run` returns the planned frames and sends
|
||||
nothing; a live change is audited, verified by reading the state back, and
|
||||
never retried. The response is the ChangeResult (409 if the unit never
|
||||
reached the state, 502 if the ack was lost).
|
||||
|
||||
Without `serial`: the legacy behaviour above, unchanged.
|
||||
"""
|
||||
if serial:
|
||||
return _change_response(lambda: _device_access().start_monitoring(
|
||||
_unit_address(port, baud, host, tcp_port), serial, dry_run=dry_run,
|
||||
even_if_already=even_if_already, caller=caller or "api"))
|
||||
baud = 38400 if baud is None else baud
|
||||
conn_key = _live_cache.make_conn_key(host, tcp_port, port, baud)
|
||||
with _build_client(port=port, baud=baud, host=host, tcp_port=tcp_port) as client:
|
||||
try:
|
||||
@@ -1864,15 +1896,32 @@ def device_monitor_start(
|
||||
@app.post("/device/monitor/stop")
|
||||
def device_monitor_stop(
|
||||
port: Optional[str] = Query(None, description="Serial port (e.g. COM5)"),
|
||||
baud: int = Query(38400, description="Serial baud rate"),
|
||||
baud: Optional[int] = Query(None, description="Serial baud rate"),
|
||||
host: Optional[str] = Query(None, description="TCP host — modem IP or ACH relay"),
|
||||
tcp_port: int = Query(DEFAULT_TCP_PORT, description=f"TCP port (default {DEFAULT_TCP_PORT})"),
|
||||
) -> dict:
|
||||
serial: Optional[str] = Query(None, 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 the unit is already in the target state"),
|
||||
caller: Optional[str] = Query(None, description="Who is asking (recorded in the audit row)"),
|
||||
):
|
||||
"""
|
||||
Command the device to stop monitoring.
|
||||
|
||||
Sends SUB 0x97 and waits for ack SUB 0x68.
|
||||
|
||||
With `serial`: the unit's identity is read first (a different unit is a 409,
|
||||
nothing sent), then its state; `dry_run` returns the planned frames and sends
|
||||
nothing; a live change is audited, verified by reading the state back, and
|
||||
never retried. The response is the ChangeResult (409 if the unit never
|
||||
reached the state, 502 if the ack was lost).
|
||||
|
||||
Without `serial`: the legacy behaviour above, unchanged.
|
||||
"""
|
||||
if serial:
|
||||
return _change_response(lambda: _device_access().stop_monitoring(
|
||||
_unit_address(port, baud, host, tcp_port), serial, dry_run=dry_run,
|
||||
even_if_already=even_if_already, caller=caller or "api"))
|
||||
baud = 38400 if baud is None else baud
|
||||
conn_key = _live_cache.make_conn_key(host, tcp_port, port, baud)
|
||||
with _build_client(port=port, baud=baud, host=host, tcp_port=tcp_port) as client:
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user