167 lines
6.4 KiB
Python
167 lines
6.4 KiB
Python
"""Raw acquisition producer for matrix-mode radars (LibreVNA multi-device, SN9000)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import logging
|
|
from contextlib import suppress
|
|
from pathlib import Path
|
|
import signal
|
|
import threading
|
|
import time
|
|
|
|
from python_app.hardware_full.matrix_radar_service import MatrixRadarService, create_matrix_radar_service
|
|
from python_app.models.run_config_model import RunConfigModel
|
|
from python_app.orchestration.shm import ShmRingWriter
|
|
from python_app.storage.npz.serialize import RAW_MAGIC, serialize_trace_collection
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# The producer waits for the matrix radar forever: a device that is absent at boot
|
|
# or disappears mid-run must never kill the producer, only make it wait. Reconnect
|
|
# uses a capped exponential backoff so a long absence does not busy-spin, and every
|
|
# wait is interruptible by SIGINT/SIGTERM (stop_requested) for a prompt clean exit.
|
|
_OPEN_RETRY_MIN_S = 1.0
|
|
_OPEN_RETRY_MAX_S = 10.0
|
|
# Throttle open-failure logging during a long wait so a permanently absent device
|
|
# does not flood the process log: log the first failure, then every Nth attempt.
|
|
_OPEN_RETRY_LOG_EVERY = 30
|
|
|
|
|
|
def _open_radar_with_retry(
|
|
config: RunConfigModel,
|
|
previous: MatrixRadarService | None,
|
|
stop_requested: threading.Event,
|
|
) -> MatrixRadarService | None:
|
|
"""Open+configure the matrix radar, retrying forever until success or stop.
|
|
|
|
Used for both the initial open and every in-loop reconnect, so a device that is
|
|
absent at boot or disappears mid-run never kills the producer — it just waits.
|
|
Returns the opened service, or ``None`` if a stop was requested before any device
|
|
became available. Backoff is capped and every wait is interruptible by SIGTERM.
|
|
"""
|
|
if previous is not None:
|
|
with suppress(Exception):
|
|
previous.close()
|
|
|
|
attempt = 0
|
|
delay = _OPEN_RETRY_MIN_S
|
|
while not stop_requested.is_set():
|
|
radar = create_matrix_radar_service(config)
|
|
try:
|
|
radar.open()
|
|
radar.configure(config.radar.sweep)
|
|
except Exception as exc: # noqa: BLE001 — waiting for the device is the point
|
|
with suppress(Exception):
|
|
radar.close() # drop any partial open before the next attempt
|
|
attempt += 1
|
|
if attempt == 1 or attempt % _OPEN_RETRY_LOG_EVERY == 0:
|
|
logger.warning(
|
|
"Matrix radar not available (attempt %d); retrying every up to %.0fs "
|
|
"until the device is present: %s",
|
|
attempt,
|
|
_OPEN_RETRY_MAX_S,
|
|
exc,
|
|
)
|
|
if stop_requested.wait(delay):
|
|
with suppress(Exception):
|
|
radar.close()
|
|
return None
|
|
delay = min(delay * 2.0, _OPEN_RETRY_MAX_S)
|
|
continue
|
|
|
|
if attempt > 0:
|
|
logger.info("Matrix radar opened after %d attempt(s).", attempt + 1)
|
|
return radar
|
|
|
|
return None
|
|
|
|
|
|
def main() -> int:
|
|
"""Run producer process until config or signal requests exit."""
|
|
parser = argparse.ArgumentParser(description="Publish matrix-radar raw sweeps to SHM rings")
|
|
parser.add_argument("--config", required=True, type=Path, help="Path to run_config.json")
|
|
args = parser.parse_args()
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(name)s: %(message)s")
|
|
stop_requested = threading.Event()
|
|
|
|
def request_stop(_signum: int, _frame: object) -> None:
|
|
stop_requested.set()
|
|
|
|
signal.signal(signal.SIGINT, request_stop)
|
|
signal.signal(signal.SIGTERM, request_stop)
|
|
|
|
config = RunConfigModel.load_from_path(args.config)
|
|
config.apply_device_model_constraints()
|
|
if not config.is_matrix_radar:
|
|
raise RuntimeError(
|
|
"matrix_raw_producer requires a matrix-mode radar.model "
|
|
"(librevna_multi or sn9000)"
|
|
)
|
|
|
|
raw_writer = ShmRingWriter(
|
|
config.rings.raw.name,
|
|
config.rings.raw.capacity,
|
|
config.rings.raw.slot_size_bytes,
|
|
)
|
|
raw_tap_writer = ShmRingWriter(
|
|
config.rings.raw_tap.name,
|
|
config.rings.raw_tap.capacity,
|
|
config.rings.raw_tap.slot_size_bytes,
|
|
)
|
|
|
|
radar: MatrixRadarService | None = None
|
|
try:
|
|
radar = _open_radar_with_retry(config, previous=None, stop_requested=stop_requested)
|
|
if radar is None:
|
|
return 0 # asked to stop before a device became available
|
|
collection_id = 1
|
|
while not stop_requested.is_set():
|
|
collection_start = time.monotonic()
|
|
try:
|
|
collection = radar.acquire_collection(collection_id=collection_id)
|
|
except Exception as exc: # noqa: BLE001 — reconnect forever, never give up
|
|
logger.warning(
|
|
"Matrix radar acquisition failed; reconnecting and waiting for the device: %s",
|
|
exc,
|
|
exc_info=True,
|
|
)
|
|
radar = _open_radar_with_retry(config, previous=radar, stop_requested=stop_requested)
|
|
if radar is None:
|
|
break # stop requested while waiting to reconnect
|
|
continue
|
|
|
|
payload = serialize_trace_collection(collection, RAW_MAGIC)
|
|
if not raw_writer.push(payload):
|
|
raise RuntimeError(
|
|
f"Raw payload size {len(payload)} exceeds ring slot size {raw_writer.slot_size_bytes}"
|
|
)
|
|
if not raw_tap_writer.push(payload):
|
|
raise RuntimeError(
|
|
f"Raw tap payload size {len(payload)} exceeds ring slot size {raw_tap_writer.slot_size_bytes}"
|
|
)
|
|
if not config.runtime.continuous:
|
|
break
|
|
collection_duration_s = time.monotonic() - collection_start
|
|
if collection_id == 1 or collection_id % 20 == 0 or collection_duration_s > 2.0:
|
|
logger.info(
|
|
"matrix radar collection %d acquired in %.3f s",
|
|
collection_id,
|
|
collection_duration_s,
|
|
)
|
|
collection_id += 1
|
|
finally:
|
|
if radar is not None:
|
|
with suppress(Exception):
|
|
radar.close()
|
|
raw_tap_writer.close()
|
|
raw_writer.close()
|
|
|
|
logger.info("matrix radar raw producer stopped")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|