feat(sfm): Series-3 adapter -- identify, status, settings into the shared models
Connect-per-request with the TCP cold-boot retry kept; a different unit answering is refused (409) whenever the serial is already in hand. 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
bb4b26835b
commit
5c6ca049c8
3 files changed
+331
No files matched your search
@@ -0,0 +1,124 @@
|
||||
"""Fakes of both device clients for sfm.device_access tests.
|
||||
|
||||
They return the libraries' real dataclasses, and FakeMicromateClient's setup
|
||||
is decoded from a real UM20147 read. `fail` is a queue of exceptions; each
|
||||
call pops one, if any, and raises it.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from micromate.models import MicromateDeviceInfo, MicromateState
|
||||
from micromate.setup_block import decode_setup
|
||||
from minimateplus.models import ComplianceConfig, DeviceInfo, MonitorStatus
|
||||
|
||||
SETUP_FIXTURE = Path(__file__).parent / "fixtures" / "micromate-setup" / "um20147_baseline.bin"
|
||||
|
||||
|
||||
class _Base:
|
||||
def __init__(self, fail=None, open_error=None):
|
||||
self.fail = list(fail or [])
|
||||
self.open_error = open_error
|
||||
self.calls: list[str] = []
|
||||
self.opened = 0
|
||||
self.closed = 0
|
||||
self._open = False
|
||||
|
||||
def _step(self, name):
|
||||
self.calls.append(name)
|
||||
if self.fail:
|
||||
raise self.fail.pop(0)
|
||||
|
||||
def open(self):
|
||||
self.opened += 1
|
||||
if self.open_error is not None:
|
||||
raise self.open_error
|
||||
self._open = True
|
||||
|
||||
def close(self):
|
||||
self.closed += 1
|
||||
self._open = False
|
||||
|
||||
|
||||
class FakeMiniMateClient(_Base):
|
||||
def __init__(self, serial="BE18193", *, monitoring=False, compliance="default", **kw):
|
||||
super().__init__(**kw)
|
||||
self.serial = serial
|
||||
self.monitoring = monitoring
|
||||
if compliance == "default":
|
||||
compliance = ComplianceConfig(
|
||||
recording_mode=0x01, sample_rate=1024, histogram_interval_sec=300,
|
||||
record_time=3.0, trigger_level_geo=0.5, alarm_level_geo=1.0, geo_range=0,
|
||||
setup_name="BRIDGE-A", project="Bridge job", client="Acme",
|
||||
operator="BH", sensor_location="Loc 1", notes=None,
|
||||
)
|
||||
self.compliance = compliance
|
||||
|
||||
def connect(self) -> DeviceInfo:
|
||||
self._step("connect")
|
||||
return DeviceInfo(
|
||||
serial=self.serial, firmware_minor=17, firmware_version="S338.17",
|
||||
dsp_version="10.72", manufacturer="Instantel", model="MiniMate Plus",
|
||||
compliance_config=self.compliance,
|
||||
)
|
||||
|
||||
def poll(self) -> None:
|
||||
self._step("poll")
|
||||
|
||||
def get_monitor_status(self) -> MonitorStatus:
|
||||
self._step("get_monitor_status")
|
||||
return MonitorStatus(
|
||||
is_monitoring=self.monitoring, battery_v=6.8,
|
||||
memory_total=983026, memory_free=900000,
|
||||
)
|
||||
|
||||
|
||||
class _FakeProtocol:
|
||||
def __init__(self, owner):
|
||||
self._owner = owner
|
||||
|
||||
def poll(self):
|
||||
self._owner._step("poll")
|
||||
|
||||
|
||||
class FakeMicromateClient(_Base):
|
||||
def __init__(self, serial="UM20147", *, monitoring=False, events=5,
|
||||
unsolicited=b"", **kw):
|
||||
super().__init__(**kw)
|
||||
self.serial = serial
|
||||
self.monitoring = monitoring
|
||||
self.events = events
|
||||
self.unsolicited = unsolicited
|
||||
self.protocol = _FakeProtocol(self)
|
||||
|
||||
def is_open(self) -> bool:
|
||||
return self._open
|
||||
|
||||
def connect(self) -> MicromateDeviceInfo:
|
||||
self._step("connect")
|
||||
return MicromateDeviceInfo(
|
||||
serial=self.serial, manufacturer="Instantel", model="MM/ISEE/S",
|
||||
firmware_line="thor", monitoring=self.monitoring, active_setup="test2.MMB",
|
||||
)
|
||||
|
||||
def get_state(self) -> MicromateState:
|
||||
self._step("get_state")
|
||||
return MicromateState(
|
||||
monitoring=self.monitoring,
|
||||
device_time=datetime.datetime(2026, 10, 20, 18, 7, 58),
|
||||
battery_volts=3.8, memory_total_bytes=15_000_000, memory_free_bytes=14_799_296,
|
||||
)
|
||||
|
||||
def get_event_count(self) -> int:
|
||||
self._step("get_event_count")
|
||||
return self.events
|
||||
|
||||
def get_setup(self):
|
||||
self._step("get_setup")
|
||||
return decode_setup(SETUP_FIXTURE.read_bytes()[11:])
|
||||
|
||||
def read_unsolicited(self) -> bytes:
|
||||
self._step("read_unsolicited")
|
||||
data, self.unsolicited = self.unsolicited, b""
|
||||
return data
|
||||
@@ -0,0 +1,86 @@
|
||||
"""The Series-3 adapter: connect-per-request, mapped into the shared models."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import minimateplus.protocol as mmp_proto # noqa: E402
|
||||
from device_access_fakes import FakeMiniMateClient # noqa: E402
|
||||
from sfm.device_access.errors import ( # noqa: E402
|
||||
ConnectFailed, MalformedReply, NoAnswer, WrongUnit,
|
||||
)
|
||||
from sfm.device_access.models import UnitAddress # noqa: E402
|
||||
from sfm.device_access.s3 import Series3Adapter # noqa: E402
|
||||
|
||||
TCP = UnitAddress(host="10.0.0.5", tcp_port=12345, port=None, baud=None)
|
||||
SERIAL_LINK = UnitAddress(host=None, tcp_port=None, port="/dev/ttyUSB0", baud=None)
|
||||
|
||||
|
||||
def adapter(*clients):
|
||||
queue = list(clients)
|
||||
return Series3Adapter(client_factory=lambda address: queue.pop(0))
|
||||
|
||||
|
||||
def test_identify_maps_device_info():
|
||||
fake = FakeMiniMateClient()
|
||||
info = adapter(fake).identify(TCP, "BE18193")
|
||||
assert (info.serial, info.series, info.model, info.firmware) == ("BE18193", 3, "MiniMate Plus", "S338.17")
|
||||
assert info.active_setup == "BRIDGE-A"
|
||||
assert info.monitoring is None # connect() does not read it on Series 3
|
||||
assert fake.closed == 1
|
||||
|
||||
|
||||
def test_a_different_unit_answering_is_refused():
|
||||
fake = FakeMiniMateClient(serial="BE11529")
|
||||
with pytest.raises(WrongUnit, match="BE18193.*BE11529"):
|
||||
adapter(fake).identify(TCP, "BE18193")
|
||||
assert fake.closed == 1
|
||||
|
||||
|
||||
def test_status_maps_monitor_status():
|
||||
fake = FakeMiniMateClient(monitoring=True)
|
||||
st = adapter(fake).status(TCP, "BE18193")
|
||||
assert (st.series, st.monitoring, st.battery_volts) == (3, True, 6.8)
|
||||
assert (st.memory_total_bytes, st.memory_free_bytes) == (983026, 900000)
|
||||
assert st.event_count is None and st.device_time is None
|
||||
assert fake.calls == ["poll", "get_monitor_status"]
|
||||
|
||||
|
||||
def test_settings_maps_the_compliance_config():
|
||||
s = adapter(FakeMiniMateClient()).settings(TCP, "BE18193")
|
||||
assert s.record_mode == "Continuous"
|
||||
assert (s.sample_rate, s.record_time_s, s.histogram_interval_s) == (1024, 3.0, 300)
|
||||
assert s.geo_trigger_ips == 0.5
|
||||
assert s.setup_name == "BRIDGE-A"
|
||||
assert s.notes == {"Project": "Bridge job", "Client": "Acme", "User Name": "BH", "Seis Loc": "Loc 1"}
|
||||
assert s.detail["record_mode_code"] == 0x01
|
||||
|
||||
|
||||
def test_settings_without_a_compliance_read_is_a_malformed_reply_and_not_retried():
|
||||
# One fake only: a retry would ask the factory for a second and fail.
|
||||
with pytest.raises(MalformedReply, match="settings"):
|
||||
adapter(FakeMiniMateClient(compliance=None)).settings(TCP, "BE18193")
|
||||
|
||||
|
||||
def test_a_cold_unit_over_tcp_gets_one_retry():
|
||||
first = FakeMiniMateClient(fail=[mmp_proto.TimeoutError("cold")])
|
||||
second = FakeMiniMateClient()
|
||||
info = adapter(first, second).identify(TCP, "BE18193")
|
||||
assert info.serial == "BE18193"
|
||||
assert first.closed == 1 and second.closed == 1
|
||||
|
||||
|
||||
def test_a_serial_link_is_not_retried():
|
||||
fake = FakeMiniMateClient(fail=[mmp_proto.TimeoutError("quiet")])
|
||||
with pytest.raises(NoAnswer):
|
||||
adapter(fake).identify(SERIAL_LINK, "BE18193")
|
||||
|
||||
|
||||
def test_a_failed_connect_is_connect_failed_and_not_retried():
|
||||
fake = FakeMiniMateClient(open_error=ConnectionRefusedError("refused"))
|
||||
with pytest.raises(ConnectFailed, match="could not connect"):
|
||||
adapter(fake).identify(TCP, "BE18193")
|
||||
Reference in new issue
Block a user