THOR's watcher (and Blastware's ACH) store every event through
/db/import/idf_file and /db/import/blastware_file, with the paired report.
Storing a download of the same event afterwards overwrote that richer copy:
on Series 4 the file, .h5 and sidecar were rewritten (extensions dropped);
on Series 3 the row lost PVS, project strings and ZC frequencies and was
repointed at a second, review-less file set.
persist_download now looks up (serial, timestamp) first. A row there is
returned as it stands -- inserted false, detail.already_stored true -- with
no store call, no insert_events and no sidecar write. Never merge.
- DownloadedEvent gains `timestamp` (the unit's own event time) and
`record` (Series 4's 0x0C fields); both adapters set them.
- The Series-3 `event is None` guard now runs before backfill/save, with a
message that is true there ("nothing was stored").
- Tests build the watcher's state exactly as the import endpoints do and
prove the row dict, the sidecar bytes and the file mtimes are unchanged.
No schema change, no migration, no codec change.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
135 lines
4.5 KiB
Python
135 lines
4.5 KiB
Python
"""Series-neutral models. Every operation returns the same shape for both
|
|
series; anything only one series has goes under `detail`."""
|
|
from __future__ import annotations
|
|
|
|
import datetime
|
|
from dataclasses import dataclass, field
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
from .errors import BadRequest
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class UnitAddress:
|
|
host: Optional[str]
|
|
tcp_port: Optional[int]
|
|
port: Optional[str]
|
|
baud: Optional[int]
|
|
|
|
def __post_init__(self) -> None:
|
|
if not self.host and not self.port:
|
|
raise BadRequest(
|
|
"specify 'host' (TCP, e.g. ?host=10.0.0.5&tcp_port=9034) "
|
|
"or 'port' (serial, e.g. ?port=/dev/ttyACM0)"
|
|
)
|
|
|
|
@property
|
|
def key(self) -> str:
|
|
"""One key per physical link -- what the per-unit lock is keyed on."""
|
|
if self.host:
|
|
return f"tcp:{self.host}:{self.tcp_port}"
|
|
return f"serial:{self.port}"
|
|
|
|
|
|
@dataclass
|
|
class UnitInfo:
|
|
serial: str
|
|
series: int
|
|
model: Optional[str]
|
|
firmware: Optional[str]
|
|
monitoring: Optional[bool]
|
|
active_setup: Optional[str]
|
|
detail: Dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
@dataclass
|
|
class UnitStatus:
|
|
serial: str
|
|
series: int
|
|
monitoring: bool
|
|
battery_volts: Optional[float]
|
|
memory_total_bytes: Optional[int]
|
|
memory_free_bytes: Optional[int]
|
|
device_time: Optional[str] # ISO, the unit's own local time
|
|
event_count: Optional[int] # None where it is not cheap (Series 3)
|
|
detail: Dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
@dataclass
|
|
class UnitSettings:
|
|
serial: str
|
|
series: int
|
|
setup_name: Optional[str]
|
|
record_mode: Optional[str] # the unit's own name for its mode
|
|
sample_rate: Optional[int]
|
|
record_time_s: Optional[float]
|
|
histogram_interval_s: Optional[int]
|
|
geo_trigger_ips: Optional[float]
|
|
notes: Dict[str, str] = field(default_factory=dict)
|
|
detail: Dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
@dataclass
|
|
class EventRef:
|
|
"""One stored event, as listed. `event_id` is opaque: pass it back to
|
|
download the event. `index` is its position in the list right now; an
|
|
erase renumbers it, so it is for display, never identity."""
|
|
event_id: str
|
|
serial: str
|
|
series: int
|
|
index: int
|
|
timestamp: Optional[str] # ISO, the unit's own local time
|
|
record_type: Optional[str] # "waveform" | "histogram"
|
|
peak_vector_sum_ips: Optional[float]
|
|
peaks_ips: Dict[str, float] # geophones only: tran / vert / long
|
|
filename: Optional[str] # the name the vendor's software gives it
|
|
detail: Dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
@dataclass
|
|
class EventList:
|
|
serial: str
|
|
series: int
|
|
events: List[EventRef]
|
|
detail: Dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
@dataclass
|
|
class EventDownload:
|
|
"""What a download stored. The file and waveform are then served at
|
|
/db/events/{db_event_id}/... ."""
|
|
event_id: str
|
|
serial: str
|
|
series: int
|
|
db_event_id: Optional[str]
|
|
stored_filename: str
|
|
filesize: Optional[int] # None only for a stored row with no file recorded
|
|
sha256: Optional[str] # None when the event was already stored
|
|
inserted: bool # False: already stored; nothing was written (detail.already_stored)
|
|
verified: Optional[bool] # True: decode self-check passed; None: no check exists
|
|
detail: Dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
@dataclass
|
|
class DownloadedEvent:
|
|
"""The device side's result, before SFM stores it. Not an HTTP shape: it
|
|
carries the payload -- a Series-3 Event + its 5A frames + the compliance
|
|
config, or a Series-4 file's bytes -- to sfm.event_ingest.
|
|
|
|
`timestamp` is the event's time as the unit reported it: the store looks up
|
|
an existing row by (serial, timestamp) before writing anything. `record` is
|
|
what the unit's own event record said (Series 4's 0x0C), for the fields the
|
|
file alone does not give."""
|
|
event_id: str
|
|
serial: str
|
|
series: int
|
|
filename: str
|
|
verified: Optional[bool]
|
|
idf_bytes: Optional[bytes] = field(default=None, repr=False)
|
|
event: Any = field(default=None, repr=False)
|
|
a5_frames: Optional[list] = field(default=None, repr=False)
|
|
compliance: Any = field(default=None, repr=False)
|
|
timestamp: Optional[datetime.datetime] = None
|
|
record: Dict[str, Any] = field(default_factory=dict)
|
|
detail: Dict[str, Any] = field(default_factory=dict)
|