Files
seismo-relay/sfm/device_access/models.py
T

154 lines
5.0 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)
@dataclass
class ChangeResult:
"""The outcome of a change request. `outcome`: "dry_run" | "skipped" | "sent" |
"failed". `verified`: "ok" | "mismatch" | "not_checked". `plan` is the exact
frames planned (dry run) or sent."""
serial: str
series: int
action: str
dry_run: bool
sent: bool
outcome: str
verified: str
state_before: Optional[str]
state_after: Optional[str]
audit_id: Optional[str]
plan: Dict[str, Any] = field(default_factory=dict)
detail: Dict[str, Any] = field(default_factory=dict)