feat(minimateplus): list events from headers, download one without the rest
list_event_headers() walks every event's 0A/1E/0C/1F without starting a 5A. download_event_by_key() downloads one event in full and walks past the others without their 5A, stopping once it has it. Both ride two new keyword options on get_events() (headers_only, only_key); the default path is unchanged. 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
ff7188a034
commit
d5484d10c0
2 files changed
+174
-1
No files matched your search
+37
-1
@@ -450,7 +450,7 @@ class MiniMateClient:
|
||||
proto.confirm_erase_all()
|
||||
log.info("delete_all_events: erase confirmed — device memory cleared")
|
||||
|
||||
def get_events(self, full_waveform: bool = False, debug: bool = False, stop_after_index: Optional[int] = None, skip_waveform_for_keys: Optional[set] = None, skip_waveform_for_events: Optional[dict] = None, extra_chunks_after_metadata: int = 1) -> list[Event]:
|
||||
def get_events(self, full_waveform: bool = False, debug: bool = False, stop_after_index: Optional[int] = None, skip_waveform_for_keys: Optional[set] = None, skip_waveform_for_events: Optional[dict] = None, extra_chunks_after_metadata: int = 1, headers_only: bool = False, only_key: Optional[bytes] = None) -> list[Event]:
|
||||
"""
|
||||
Download all stored events from the device using the confirmed
|
||||
1E → 0A → 0C → 5A → 1F event-iterator protocol.
|
||||
@@ -479,6 +479,10 @@ class MiniMateClient:
|
||||
|
||||
Raises:
|
||||
ProtocolError: on unrecoverable communication failure.
|
||||
headers_only: never start a 5A -- every event takes the bypass below.
|
||||
only_key: download only the event with this key (in full, per
|
||||
full_waveform); walk the others without their 5A, and stop once
|
||||
that event is read.
|
||||
"""
|
||||
proto = self._require_proto()
|
||||
|
||||
@@ -577,6 +581,11 @@ class MiniMateClient:
|
||||
"get_events: key=%s (key, ts=%s) match — skipping 5A bulk download",
|
||||
cur_key.hex(), actual_ts,
|
||||
)
|
||||
elif headers_only or (only_key is not None and cur_key != only_key):
|
||||
# The same bypass as a (key, ts) match: 0A + 1E(arm) + 0C were
|
||||
# read; 1F(arm) + POLL + 5A are not sent, and 1F(browse) below is
|
||||
# safe because no 5A was ever started.
|
||||
skip_5a = True
|
||||
|
||||
arm_key4: Optional[bytes] = None
|
||||
a5_ok = False
|
||||
@@ -713,6 +722,10 @@ class MiniMateClient:
|
||||
events.append(ev)
|
||||
idx += 1
|
||||
|
||||
if only_key is not None and cur_key == only_key:
|
||||
log.info("get_events: read key=%s -- stopping", cur_key.hex())
|
||||
break
|
||||
|
||||
# Early exit: if the caller only wants events up to a specific
|
||||
# index, stop iterating once we've collected it.
|
||||
if stop_after_index is not None and idx > stop_after_index:
|
||||
@@ -741,6 +754,29 @@ class MiniMateClient:
|
||||
log.info("get_events: downloaded %d event(s)", len(events))
|
||||
return events
|
||||
|
||||
def list_event_headers(self) -> list[Event]:
|
||||
"""Every stored event's 0C record -- timestamp, type, peaks -- without its 5A.
|
||||
|
||||
`get_events()` downloads every event in full even for metadata
|
||||
(`stop_after_metadata` is a no-op), so listing that way costs one
|
||||
complete download per event. This walks 0A + 1E(arm) + 0C + 1F(browse)
|
||||
per event -- the bypass `skip_waveform_for_events` already takes -- and
|
||||
never starts a 5A. The events carry no `_a5_frames`.
|
||||
"""
|
||||
return self.get_events(headers_only=True)
|
||||
|
||||
def download_event_by_key(self, key4: bytes) -> Optional[Event]:
|
||||
"""Download ONE event in full; walk past the events before it without
|
||||
their 5A. None if no full record has that key.
|
||||
|
||||
`get_events(stop_after_index=N)` downloads events 0..N in full and keeps
|
||||
one; this downloads only the one asked for.
|
||||
"""
|
||||
for ev in self.get_events(full_waveform=True, only_key=key4):
|
||||
if ev._waveform_key == key4:
|
||||
return ev
|
||||
return None
|
||||
|
||||
def download_waveform(self, event: Event) -> None:
|
||||
"""
|
||||
Download the full raw ADC waveform for a previously-retrieved event
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
"""MiniMateClient's header-only walk: list without 5A, download one without the rest."""
|
||||
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.client as mc # noqa: E402
|
||||
from minimateplus.client import MiniMateClient # noqa: E402
|
||||
from minimateplus.models import Timestamp # noqa: E402
|
||||
|
||||
END = b"\x00" * 8
|
||||
KEYS = [bytes.fromhex(k) for k in ("01110000", "0111245a", "01114290")]
|
||||
STAMPS = {
|
||||
KEYS[0]: (2026, 10, 1, 8, 0, 0),
|
||||
KEYS[1]: (2026, 10, 2, 9, 30, 15),
|
||||
KEYS[2]: (2026, 10, 3, 17, 45, 59),
|
||||
}
|
||||
|
||||
|
||||
class FakeProto:
|
||||
"""Records every call; serves a three-event chain the way the unit does."""
|
||||
|
||||
def __init__(self, keys=KEYS, partial=()):
|
||||
self.keys = list(keys)
|
||||
self.partial = set(partial)
|
||||
self.calls: list[tuple] = []
|
||||
self._pos = 0
|
||||
|
||||
def _entry(self, i):
|
||||
if i >= len(self.keys):
|
||||
return b"\x00\x00\x00\x00", END
|
||||
return self.keys[i], self.keys[i] + b"\x00\x00\x00\x46"
|
||||
|
||||
def read_event_first(self, token=0):
|
||||
self.calls.append(("1E", token))
|
||||
if token == 0:
|
||||
self._pos = 0
|
||||
return self._entry(self._pos)
|
||||
|
||||
def read_waveform_header(self, key4):
|
||||
self.calls.append(("0A", key4))
|
||||
return b"", (0x2C if key4 in self.partial else 0x46)
|
||||
|
||||
def read_waveform_record(self, key4):
|
||||
self.calls.append(("0C", key4))
|
||||
return key4 # decoded by the patched decoder below
|
||||
|
||||
def advance_event(self, browse=False):
|
||||
self.calls.append(("1F", "browse" if browse else "arm"))
|
||||
if browse:
|
||||
self._pos += 1
|
||||
return self._entry(self._pos)
|
||||
return self._entry(self._pos + 1)
|
||||
|
||||
def poll(self):
|
||||
self.calls.append(("POLL",))
|
||||
|
||||
def read_bulk_waveform_stream(self, key4, **kw):
|
||||
self.calls.append(("5A", key4))
|
||||
return ["frame"]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(monkeypatch):
|
||||
def decode_record(record, ev):
|
||||
y, mo, d, h, mi, s = STAMPS[record]
|
||||
ev.timestamp = Timestamp(raw=b"", flag=0x10, year=y, unknown_byte=0,
|
||||
month=mo, day=d, hour=h, minute=mi, second=s)
|
||||
ev.record_type = "Waveform"
|
||||
monkeypatch.setattr(mc, "_decode_waveform_record_into", decode_record)
|
||||
monkeypatch.setattr(mc, "_decode_a5_metadata_into", lambda frames, ev: None)
|
||||
monkeypatch.setattr(mc, "_decode_a5_waveform", lambda frames, ev: None)
|
||||
|
||||
def make(proto):
|
||||
c = MiniMateClient(port="/dev/null")
|
||||
c._proto = proto
|
||||
return c
|
||||
return make
|
||||
|
||||
|
||||
def _subs(proto):
|
||||
return [c[0] for c in proto.calls]
|
||||
|
||||
|
||||
def test_listing_headers_never_starts_a_5a(client):
|
||||
proto = FakeProto()
|
||||
events = client(proto).list_event_headers()
|
||||
assert [e._waveform_key for e in events] == KEYS
|
||||
assert "5A" not in _subs(proto) and "POLL" not in _subs(proto)
|
||||
assert ("1F", "arm") not in proto.calls
|
||||
assert all(e._a5_frames is None for e in events)
|
||||
|
||||
|
||||
def test_listing_headers_sends_only_the_commands_get_events_already_sends(client):
|
||||
proto = FakeProto()
|
||||
client(proto).list_event_headers()
|
||||
assert _subs(proto) == ["1E"] + ["0A", "1E", "0C", "1F"] * len(KEYS)
|
||||
|
||||
|
||||
def test_listed_events_carry_their_timestamps(client):
|
||||
events = client(FakeProto()).list_event_headers()
|
||||
assert (events[1].timestamp.day, events[1].timestamp.second) == (2, 15)
|
||||
|
||||
|
||||
def test_downloading_one_event_starts_exactly_one_5a(client):
|
||||
proto = FakeProto()
|
||||
ev = client(proto).download_event_by_key(KEYS[1])
|
||||
assert ev is not None and ev._waveform_key == KEYS[1]
|
||||
assert [c for c in proto.calls if c[0] == "5A"] == [("5A", KEYS[1])]
|
||||
assert ev._a5_frames == ["frame"]
|
||||
|
||||
|
||||
def test_downloading_one_event_stops_once_it_has_it(client):
|
||||
proto = FakeProto()
|
||||
client(proto).download_event_by_key(KEYS[1])
|
||||
assert ("0A", KEYS[2]) not in proto.calls
|
||||
|
||||
|
||||
def test_the_events_before_it_are_walked_without_their_5a(client):
|
||||
proto = FakeProto()
|
||||
client(proto).download_event_by_key(KEYS[2])
|
||||
assert [c[1] for c in proto.calls if c[0] == "5A"] == [KEYS[2]]
|
||||
assert proto.calls.count(("1F", "arm")) == 1
|
||||
|
||||
|
||||
def test_a_key_that_is_not_stored_returns_none(client):
|
||||
assert client(FakeProto()).download_event_by_key(bytes.fromhex("0111ffff")) is None
|
||||
|
||||
|
||||
def test_a_partial_record_is_skipped_by_both(client):
|
||||
events = client(FakeProto(partial={KEYS[1]})).list_event_headers()
|
||||
assert [e._waveform_key for e in events] == [KEYS[0], KEYS[2]]
|
||||
assert client(FakeProto(partial={KEYS[1]})).download_event_by_key(KEYS[1]) is None
|
||||
Reference in new issue
Block a user