"""Raw acquisition producer for the external Kamil ADC radar mode.""" from __future__ import annotations import argparse from contextlib import suppress import logging from pathlib import Path import signal import threading import time import numpy as np from python_app.hardware_full.kamil_adc_service import KamilAdcService from python_app.hardware_full.switch_service import SwitchService from python_app.models.dataset_model import ComboKey, SweepCollection, TraceData from python_app.models.run_config_model import RunConfigModel, SwitchModel 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 Kamil ADC collector forever: a device/collector 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. Mirrors matrix_raw_producer._open_radar_with_retry. _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, radar: KamilAdcService, input_switch: SwitchService, output_switch: SwitchService, stop_requested: threading.Event, ) -> bool: """Open+configure the radar and both switches, retrying forever until stop. Used for both the initial open and every in-loop reconnect, so a collector that is absent at boot or disappears mid-run never kills the producer — it just waits. Any partially-opened components are closed before each retry so a relaunched collector starts clean. Returns ``True`` once everything is open, or ``False`` if a stop was requested before the device became available. Backoff is capped and every wait is interruptible by SIGTERM. """ # Tear down any prior open first: open()/switch.open() are idempotent no-ops # while still "open", so a mid-run reconnect must close them to force a fresh # collector relaunch and TTY re-attach. with suppress(Exception): input_switch.close() with suppress(Exception): output_switch.close() with suppress(Exception): radar.close() attempt = 0 delay = _OPEN_RETRY_MIN_S while not stop_requested.is_set(): try: radar.open(stop_event=stop_requested) radar.configure(config.radar.sweep) output_switch.open() input_switch.open() except Exception as exc: # noqa: BLE001 — waiting for the device is the point # Drop any partial open (collector process, TTY reader, switches) # before the next attempt so the relaunch starts from a clean state. with suppress(Exception): input_switch.close() with suppress(Exception): output_switch.close() with suppress(Exception): radar.close() attempt += 1 if attempt == 1 or attempt % _OPEN_RETRY_LOG_EVERY == 0: logger.warning( "Kamil ADC 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): return False delay = min(delay * 2.0, _OPEN_RETRY_MAX_S) continue if attempt > 0: logger.info("Kamil ADC opened after %d attempt(s).", attempt + 1) return True return False def main() -> int: """Run producer process until config or signal requests exit.""" parser = argparse.ArgumentParser(description="Publish Kamil ADC 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) if not config.is_kamil_adc: raise RuntimeError("kamil_adc_raw_producer requires radar.model='kamil_adc'") config.ensure_combos() 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 = KamilAdcService(config) input_switch = _switch_from_model(config.input_switch) output_switch = _switch_from_model(config.output_switch) try: if not _open_radar_with_retry(config, radar, input_switch, output_switch, stop_requested): return 0 # asked to stop before a device became available collection_id = 1 publish_failures = 0 while not stop_requested.is_set(): collection_start = time.monotonic() capture_start_ns = time.monotonic_ns() traces: list[TraceData] = [] try: for combo in config.combos: if stop_requested.is_set(): break output_switch.switch_to(combo.output) input_switch.switch_to(combo.input) if config.runtime.settling_ms > 0: time.sleep(config.runtime.settling_ms / 1000.0) sweep = radar.acquire() traces.append( TraceData( combo=ComboKey(input=combo.input, output=combo.output), frequency_hz=np.asarray(sweep.x, dtype=np.float32), s11=np.asarray(sweep.trace("s11"), dtype=np.complex64), s21=np.asarray(sweep.trace("s21"), dtype=np.complex64), ) ) except Exception as exc: # noqa: BLE001 — reconnect forever, never give up logger.warning( "Kamil ADC acquisition failed; reconnecting and waiting for the device: %s", exc, exc_info=True, ) if not _open_radar_with_retry(config, radar, input_switch, output_switch, stop_requested): break # stop requested while waiting to reconnect continue # An incomplete sweep set means the device dropped out (or a stop was # requested mid-collection). Only exit on stop; otherwise reconnect and # wait for the device rather than killing the producer. if len(traces) != len(config.combos): if stop_requested.is_set(): break logger.warning( "Kamil ADC produced an incomplete collection (%d of %d combos); " "reconnecting and waiting for the device", len(traces), len(config.combos), ) if not _open_radar_with_retry(config, radar, input_switch, output_switch, stop_requested): break # stop requested while waiting to reconnect continue collection = SweepCollection( collection_id=collection_id, monotonic_ns=time.monotonic_ns(), traces=traces, capture_start_ns=capture_start_ns, capture_end_ns=time.monotonic_ns(), ) payload = serialize_trace_collection(collection, RAW_MAGIC) if raw_writer.push(payload): raw_tap_writer.push(payload) # best-effort GUI tap; never fatal else: # Oversized payload vs the ring slot is a persistent config error, not # a device fault: log (throttled) and skip rather than killing the producer. publish_failures += 1 if publish_failures == 1 or publish_failures % 100 == 0: logger.error( "Raw payload %d B exceeds ring slot %d B; dropping collection %d (drops=%d)", len(payload), raw_writer.slot_size_bytes, collection_id, publish_failures, ) 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("Kamil ADC collection %d acquired in %.3f s", collection_id, collection_duration_s) collection_id += 1 finally: with suppress(Exception): output_switch.close() with suppress(Exception): input_switch.close() radar.close() raw_tap_writer.close() raw_writer.close() logger.info("Kamil ADC raw producer stopped") return 0 def _switch_from_model(model: SwitchModel) -> SwitchService: return SwitchService( name=model.name, positions=model.positions, mode=model.driver_mode, driver=model.driver, gpio_chip=model.gpio_chip, pin_a=model.pin_a, pin_b=model.pin_b, invert_logic=model.invert_logic, default_position=model.default_position, ) if __name__ == "__main__": raise SystemExit(main())