"""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__) # Maximum number of acquisitions allowed to fail in a row before we give up and # let the supervisor restart the whole process. Picked high enough to survive # transient USB stalls (each retry triggers a full reset cycle of ~1-2s) but # bounded so a permanently broken device does not loop forever. _MAX_CONSECUTIVE_ACQUIRE_FAILURES = 20 # Cooldown applied between a failed acquire and the next reset attempt. Stops # us from busy-spinning when the device keeps refusing to come back. _ACQUIRE_FAILURE_COOLDOWN_S = 1.0 def _reset_radar_service( config: RunConfigModel, previous: MatrixRadarService | None ) -> MatrixRadarService: """Close `previous` (best-effort) and return a freshly opened+configured service.""" if previous is not None: with suppress(Exception): previous.close() radar = create_matrix_radar_service(config) radar.open() radar.configure(config.radar.sweep) return radar 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 consecutive_failures = 0 try: radar = _reset_radar_service(config, previous=None) collection_id = 1 while not stop_requested.is_set(): collection_start = time.monotonic() try: if radar is None: radar = _reset_radar_service(config, previous=None) collection = radar.acquire_collection(collection_id=collection_id) except Exception as exc: # noqa: BLE001 — top-level recovery is the point consecutive_failures += 1 if consecutive_failures > _MAX_CONSECUTIVE_ACQUIRE_FAILURES: logger.error( "Matrix radar acquisition failed %d times in a row; giving up. " "Last error: %s", consecutive_failures - 1, exc, ) raise logger.warning( "Matrix radar acquisition failed (%d/%d), resetting service: %s", consecutive_failures, _MAX_CONSECUTIVE_ACQUIRE_FAILURES, exc, exc_info=True, ) # Cooldown gives slow USB stacks (and the device firmware) time # to settle before the next open() attempt. if stop_requested.wait(_ACQUIRE_FAILURE_COOLDOWN_S): break try: radar = _reset_radar_service(config, previous=radar) except Exception as reset_exc: # noqa: BLE001 logger.warning( "Matrix radar reset (%d/%d) failed, will retry: %s", consecutive_failures, _MAX_CONSECUTIVE_ACQUIRE_FAILURES, reset_exc, exc_info=True, ) radar = None continue consecutive_failures = 0 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())