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,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