feat(sfm): DeviceAccess lists and downloads events for both series
Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
1 parent
0a01a8d2db
commit
9e2d8b62c3
3 files changed
+114
-5
No files matched your search
@@ -10,11 +10,13 @@ import time
|
||||
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 UnitAddress, UnitInfo, UnitSettings, UnitStatus
|
||||
from .models import DownloadedEvent, EventList, UnitAddress, UnitInfo, UnitSettings, UnitStatus
|
||||
from .s3 import Series3Adapter, default_s3_client_factory
|
||||
from .s4 import (
|
||||
Series4Session, default_s4_client_factory, s4_identify, s4_settings, s4_status,
|
||||
Series4Session, default_s4_client_factory, s4_download_event, s4_identify, s4_list_events,
|
||||
s4_settings, s4_status,
|
||||
)
|
||||
from .series import normalise_serial, series_for_serial
|
||||
|
||||
@@ -56,6 +58,18 @@ class DeviceAccess:
|
||||
def settings(self, address: UnitAddress, serial: str) -> UnitSettings:
|
||||
return self._dispatch(address, serial, self._s3.settings, s4_settings)
|
||||
|
||||
def list_events(self, address: UnitAddress, serial: str) -> EventList:
|
||||
return self._dispatch(address, serial, self._s3.list_events, s4_list_events)
|
||||
|
||||
def download_event(self, address: UnitAddress, serial: str, event_id: str) -> DownloadedEvent:
|
||||
"""Read-only on the unit. Holds the unit for the whole download, so
|
||||
other requests for it wait, then get 423."""
|
||||
key, stamp = parse_event_id(event_id, serial) # BadRequest before any I/O
|
||||
return self._dispatch(
|
||||
address, serial,
|
||||
lambda a, s: self._s3.download_event(a, s, key, stamp),
|
||||
lambda c, s: s4_download_event(c, s, key, stamp))
|
||||
|
||||
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:
|
||||
@@ -99,8 +113,9 @@ class DeviceAccess:
|
||||
self._forget_if_closed(address, session)
|
||||
# A held line the peer dropped still reads open (the transport only
|
||||
# learns at EOF), so the first request on it times out or fails.
|
||||
# Call up fresh and retry ONCE. Safe only because phase 1a
|
||||
# operations are reads (idempotent); writes (phase 3) must NOT
|
||||
# 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)):
|
||||
self._drop(address.key, session)
|
||||
|
||||
@@ -14,7 +14,9 @@ from micromate.client import MicromateClient
|
||||
from micromate.protocol import THOR_CHUNK_SIZE
|
||||
from minimateplus.transport import BaseTransport, SerialTransport, TcpTransport
|
||||
|
||||
from .errors import CallHomeInProgress, EventNotFound, MalformedReply, StaleEvent, WrongUnit, translate
|
||||
from .errors import (
|
||||
CallHomeInProgress, DeviceError, EventNotFound, MalformedReply, StaleEvent, WrongUnit, translate,
|
||||
)
|
||||
from .events import iso_of, make_event_id, stamp_of
|
||||
from .models import DownloadedEvent, EventList, EventRef, UnitAddress, UnitInfo, UnitSettings, UnitStatus
|
||||
from .series import normalise_serial
|
||||
@@ -157,6 +159,10 @@ class Series4Session:
|
||||
log.warning("RADIO RING from %s -- released the line so the call reaches THOR",
|
||||
self.address.key)
|
||||
raise CallHomeInProgress(_RING_MESSAGE) from exc
|
||||
except DeviceError:
|
||||
# The operation's own verdict (event not found, stale id): the line
|
||||
# is healthy, so keep it. Library errors still hang up below.
|
||||
raise
|
||||
except Exception as exc:
|
||||
self.hang_up()
|
||||
raise translate(exc, connecting=False) from exc
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
"""Events through the one door: same shape both series, the held line, ring safety."""
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
import os
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from device_access_fakes import FakeMicromateClient, FakeMiniMateClient # noqa: E402
|
||||
from sfm.device_access.access import DeviceAccess # noqa: E402
|
||||
from sfm.device_access.errors import BadRequest, CallHomeInProgress # noqa: E402
|
||||
from sfm.device_access.models import UnitAddress # noqa: E402
|
||||
from sfm.device_access.s4 import RingDetected # noqa: E402
|
||||
|
||||
ADDR = UnitAddress(host="10.0.0.5", tcp_port=9034, port=None, baud=None)
|
||||
|
||||
|
||||
def access(s3=(), s4=()):
|
||||
s3q, s4q = list(s3), list(s4)
|
||||
return DeviceAccess(s3_client_factory=lambda a, **kw: s3q.pop(0),
|
||||
s4_client_factory=lambda a, **kw: s4q.pop(0), start_reaper=False)
|
||||
|
||||
|
||||
def test_both_series_list_events_in_the_same_shape():
|
||||
s3 = access(s3=[FakeMiniMateClient()]).list_events(ADDR, "BE18193")
|
||||
s4 = access(s4=[FakeMicromateClient()]).list_events(ADDR, "UM20147")
|
||||
f3 = [set(dataclasses.asdict(e)) for e in s3.events]
|
||||
f4 = [set(dataclasses.asdict(e)) for e in s4.events]
|
||||
assert all(f == f3[0] for f in f3 + f4)
|
||||
|
||||
|
||||
def test_no_shared_field_is_empty_for_either_series():
|
||||
# A field one series cannot fill belongs under `detail`, not at the top level.
|
||||
for out in (access(s3=[FakeMiniMateClient()]).list_events(ADDR, "BE18193"),
|
||||
access(s4=[FakeMicromateClient()]).list_events(ADDR, "UM20147")):
|
||||
for e in out.events:
|
||||
empty = {k for k, v in dataclasses.asdict(e).items() if v is None}
|
||||
assert empty == set(), (out.series, empty)
|
||||
|
||||
|
||||
def test_a_micromate_list_and_download_share_one_held_line():
|
||||
fake = FakeMicromateClient()
|
||||
da = access(s4=[fake])
|
||||
listed = da.list_events(ADDR, "UM20147")
|
||||
dl = da.download_event(ADDR, "UM20147", listed.events[0].event_id)
|
||||
assert fake.opened == 1 and dl.idf_bytes
|
||||
assert da.held_serial(ADDR) == "UM20147"
|
||||
|
||||
|
||||
def test_an_id_from_another_unit_is_refused_before_any_io():
|
||||
fake = FakeMicromateClient()
|
||||
with pytest.raises(BadRequest):
|
||||
access(s4=[fake]).download_event(ADDR, "UM20147", "UM12947:055d4a81:20250531135901")
|
||||
assert fake.opened == 0
|
||||
|
||||
|
||||
def test_a_minimate_download_goes_through_the_series3_adapter():
|
||||
dl = access(s3=[FakeMiniMateClient()]).download_event(ADDR, "BE18193", "BE18193:01110000:-")
|
||||
assert dl.series == 3 and dl.a5_frames == ["frame"]
|
||||
|
||||
|
||||
def test_a_ring_mid_download_releases_the_line_and_downloads_nothing():
|
||||
fake = FakeMicromateClient()
|
||||
|
||||
def ring(*a, **kw):
|
||||
raise RingDetected("RADIO RING")
|
||||
fake.fetch_event = ring
|
||||
da = access(s4=[fake])
|
||||
with pytest.raises(CallHomeInProgress):
|
||||
da.download_event(ADDR, "UM20147", "UM20147:055d4a81:20250531135901")
|
||||
assert fake.closed == 1 and da.held_serial(ADDR) is None
|
||||
|
||||
|
||||
def test_a_stale_or_missing_event_keeps_the_line():
|
||||
# The operation's own verdict (404/409) says nothing is wrong with the link;
|
||||
# hanging up would cost a call-up for no reason.
|
||||
from sfm.device_access.errors import EventNotFound, StaleEvent
|
||||
fake = FakeMicromateClient()
|
||||
da = access(s4=[fake])
|
||||
with pytest.raises(StaleEvent):
|
||||
da.download_event(ADDR, "UM20147", "UM20147:055d4a81:20240101000000")
|
||||
with pytest.raises(EventNotFound):
|
||||
da.download_event(ADDR, "UM20147", "UM20147:deadbeef:-")
|
||||
assert fake.opened == 1 and fake.closed == 0
|
||||
assert da.held_serial(ADDR) == "UM20147"
|
||||
Reference in new issue
Block a user