Blastware names a direct download K441LTON.900 and only its call-in copy K441LTON.900H, so a folder of directly downloaded events cannot be sorted by kind. SFM's own files now always carry the letter. It is read from the file's own record-type byte (STRT[18], #41) by the new with_type_letter() -- not from the recording mode: BE9441's K441LTOK.R10 is a waveform that triggered 2 s into histogram K441LTOK.QZ0 (Histogram+Continuous). A file whose type does not read keeps the plain name; a name never fails a download. All three paths that name a MiniMate file SFM builds: the device-access download (the page's event list), WaveformStore.save (the legacy route's store copy, the bench call-in server), and the legacy /device/event/{i}/blastware_file download. Verified on BE9441's recorded 5A walk: 0111f210 -> K441LTON.900H, 011106a0 -> K441LTOK.R10W, byte-identical to Blastware's files. find_event_by_blastware_name tightens with it: a lettered name matches itself and the letterless form, not the other letter (a waveform and a histogram in the same second are different events); a letterless name still matches all three. Store consequence: new downloads only; rows and files stored before keep their names, and 'already stored' still finds them. No schema change, no migration. The event list shows the plain name until the event is downloaded (the type is not known before the 5A). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
211 lines
7.9 KiB
Python
211 lines
7.9 KiB
Python
"""SFM's 5A walk past the old 128-chunk cap, against Blastware (BE9441, 2026-10-26).
|
|
|
|
Event 2 of tests/fixtures/minimate-bench-10-26-26-histogram/ is a 71,088-byte
|
|
histogram: 139 requests, crossing two 64 KB pages. Before the walk was sized from
|
|
STRT, a caller's max_chunks=128 cut it short. Fed the unit's recorded replies,
|
|
SFM's walk must send exactly Blastware's requests and rebuild Blastware's file.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
import minimateplus.protocol as mp # noqa: E402
|
|
from minimate_replay import fast_clock # noqa: E402
|
|
from minimateplus.blastware_file import blastware_file_bytes, write_blastware_file # noqa: E402
|
|
from minimateplus.framing import S3FrameParser # noqa: E402
|
|
from minimateplus.models import Event # noqa: E402
|
|
|
|
FIX = Path(__file__).parent / "fixtures" / "minimate-bench-10-26-26-histogram"
|
|
|
|
|
|
def _unstuff(b: bytes) -> bytes:
|
|
out, k = bytearray(), 0
|
|
while k < len(b):
|
|
if b[k] == 0x10 and k + 1 < len(b):
|
|
out.append(b[k + 1]); k += 2
|
|
else:
|
|
out.append(b[k]); k += 1
|
|
return bytes(out)
|
|
|
|
|
|
def _requests() -> list:
|
|
raw, out, i = (FIX / "requests.bin").read_bytes(), [], 0
|
|
while i < len(raw):
|
|
if raw[i:i + 2] == b"\x41\x03":
|
|
i += 2
|
|
continue
|
|
if raw[i:i + 2] != b"\x41\x02":
|
|
i += 1
|
|
continue
|
|
j = i + 2
|
|
while raw[j] != 0x03:
|
|
j += 2 if raw[j] == 0x10 else 1
|
|
out.append(raw[i:j + 1])
|
|
i = j + 1
|
|
return out
|
|
|
|
|
|
def _addr_len(frame: bytes) -> tuple:
|
|
p = _unstuff(frame[2:-1])
|
|
return int.from_bytes(p[6:10], "big"), int.from_bytes(p[4:6], "big")
|
|
|
|
|
|
def _a5_replies() -> list:
|
|
raw, out, start, p = (FIX / "unit_replies.bin").read_bytes(), [], 0, S3FrameParser()
|
|
for i in range(len(raw)):
|
|
for f in p.feed(raw[i:i + 1]):
|
|
if f.sub == 0xA5:
|
|
out.append(raw[start:i + 1])
|
|
start = i + 1
|
|
return out
|
|
|
|
|
|
FIVE_A = [f for f in _requests() if _unstuff(f[2:-1])[2] == 0x5A]
|
|
REPLIES = {}
|
|
for _q, _r in zip(FIVE_A, _a5_replies()):
|
|
REPLIES.setdefault(_addr_len(_q), _r)
|
|
|
|
|
|
class ByAddress:
|
|
def __init__(self):
|
|
self.sent, self._buf = [], b""
|
|
|
|
def write(self, data):
|
|
data = bytes(data)
|
|
self.sent.append(data)
|
|
self._buf += REPLIES.get(_addr_len(data), b"")
|
|
|
|
def read(self, n):
|
|
out, self._buf = self._buf[:n], self._buf[n:]
|
|
return out
|
|
|
|
|
|
def _blastware_walk(start: int) -> list:
|
|
"""Blastware's last complete walk for the event at `start` (it also cancelled one)."""
|
|
starts = [k for k, f in enumerate(FIVE_A) if _addr_len(f) == (start, 0x200)]
|
|
for i in reversed(starts):
|
|
walk = [FIVE_A[i]]
|
|
while i + 1 < len(FIVE_A) and _addr_len(walk[-1])[1] == 0x200:
|
|
i += 1
|
|
walk.append(FIVE_A[i])
|
|
if _addr_len(walk[-1])[1] != 0x200:
|
|
return walk
|
|
raise AssertionError("no complete walk")
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _fast(monkeypatch):
|
|
fast_clock(monkeypatch)
|
|
|
|
|
|
def test_the_recording_pairs_up():
|
|
assert len(FIVE_A) == len(_a5_replies()) == 722
|
|
|
|
|
|
@pytest.mark.parametrize("key,bw,n", [("0111f210", "K441LTON.900", 139),
|
|
("011106a0", "K441LTOK.R10", 118)])
|
|
def test_the_walk_is_blastwares_and_rebuilds_its_file(key, bw, n, tmp_path):
|
|
t = ByAddress()
|
|
frames = mp.MiniMateProtocol(t).read_bulk_waveform_stream(
|
|
bytes.fromhex(key), stop_after_metadata=False, include_terminator=True,
|
|
strict=True, max_chunks=128)
|
|
want = _blastware_walk(int(key, 16))
|
|
assert len(want) == n and t.sent == want
|
|
ev = Event(index=0)
|
|
ev.timestamp = None
|
|
write_blastware_file(ev, frames, tmp_path / bw)
|
|
assert (tmp_path / bw).read_bytes() == (FIX / "blastware" / bw).read_bytes()
|
|
|
|
|
|
@pytest.mark.parametrize("key,bw", [("0111f210", "K441LTON.900"), ("011106a0", "K441LTOK.R10")])
|
|
def test_blastware_file_bytes_is_blastwares_file(key, bw):
|
|
# What a live MiniMate download hands to the store (#8): the file itself, as bytes.
|
|
frames = mp.MiniMateProtocol(ByAddress()).read_bulk_waveform_stream(
|
|
bytes.fromhex(key), stop_after_metadata=False, include_terminator=True,
|
|
strict=True, max_chunks=128)
|
|
ev = Event(index=0)
|
|
ev.timestamp = None
|
|
assert blastware_file_bytes(ev, frames) == (FIX / "blastware" / bw).read_bytes()
|
|
|
|
|
|
|
|
# SFM's own downloads are named with the W/H letter (2026-10-30), on every path
|
|
# that builds the file: the device-access download, the store's save, and the
|
|
# legacy per-index route. BE9441's walk gives one of each: 0111f210 is the
|
|
# histogram K441LTON.900, 011106a0 the waveform K441LTOK.R10.
|
|
|
|
import datetime # noqa: E402
|
|
|
|
from device_access_fakes import FakeMiniMateClient, s3_event # noqa: E402
|
|
from minimateplus.models import DeviceInfo # noqa: E402
|
|
from minimateplus.waveform_codec import decode_a5_frames # noqa: E402
|
|
|
|
LIVE = [("0111f210", datetime.datetime(2026, 10, 26, 17, 0, 36), "K441LTON.900H"),
|
|
("011106a0", datetime.datetime(2026, 10, 26, 16, 6, 37), "K441LTOK.R10W")]
|
|
|
|
|
|
def _walked_event(key, when):
|
|
frames = mp.MiniMateProtocol(ByAddress()).read_bulk_waveform_stream(
|
|
bytes.fromhex(key), stop_after_metadata=False, include_terminator=True,
|
|
strict=True, max_chunks=128)
|
|
ev = s3_event(0, key, when, frames=False)
|
|
ev._a5_frames = frames
|
|
ev.raw_samples = decode_a5_frames(frames) # as the real client's download decodes them
|
|
return ev
|
|
|
|
|
|
@pytest.mark.parametrize("key,when,name", LIVE)
|
|
def test_a_live_download_is_named_with_its_type_letter(key, when, name):
|
|
from sfm.device_access.models import UnitAddress
|
|
from sfm.device_access.s3 import Series3Adapter
|
|
# Range unread (no settings): the check then does not compare peaks, which
|
|
# these events have no Blastware report for -- only the name is under test.
|
|
fake = FakeMiniMateClient(serial="BE9441", events=[_walked_event(key, when)], compliance=None)
|
|
dl = Series3Adapter(client_factory=lambda address, **kw: fake).download_event(
|
|
UnitAddress(host="10.0.0.5", tcp_port=9034, port=None, baud=None), "BE9441",
|
|
bytes.fromhex(key), None)
|
|
assert dl.filename == name
|
|
|
|
|
|
@pytest.mark.parametrize("key,when,name", LIVE)
|
|
def test_the_store_names_a_saved_download_with_its_type_letter(key, when, name, tmp_path):
|
|
from sfm.waveform_store import WaveformStore
|
|
ev = _walked_event(key, when)
|
|
rec = WaveformStore(tmp_path).save(ev, serial="BE9441", a5_frames=ev._a5_frames)
|
|
assert rec["filename"] == name
|
|
assert (tmp_path / "BE9441" / name).read_bytes() == (FIX / "blastware" / name[:-1]).read_bytes()
|
|
|
|
|
|
@pytest.mark.parametrize("key,when,name", LIVE)
|
|
def test_the_legacy_file_route_names_its_file_with_the_type_letter(key, when, name, tmp_path, monkeypatch):
|
|
import sfm.server as srv
|
|
from sfm.database import SeismoDb
|
|
from sfm.waveform_store import WaveformStore
|
|
ev = _walked_event(key, when)
|
|
|
|
class Unit:
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, *a):
|
|
return False
|
|
|
|
def connect(self):
|
|
return DeviceInfo(serial="BE9441", firmware_minor=17)
|
|
|
|
def get_events(self, **kw):
|
|
return [ev]
|
|
monkeypatch.setattr(srv, "_build_client", lambda *a, **kw: Unit())
|
|
monkeypatch.setattr(srv, "_get_store", lambda: WaveformStore(tmp_path / "w"))
|
|
monkeypatch.setattr(srv, "_get_db", lambda: SeismoDb(tmp_path / "s.db"))
|
|
resp = srv.device_event_blastware_file(index=0, port=None, baud=38400, host="10.0.0.5",
|
|
tcp_port=9034, force=False)
|
|
assert resp.filename == name
|