From 9f9cce31ee5da4f9d716630091beb24a16522820 Mon Sep 17 00:00:00 2001 From: Prakriti Gupta Date: Mon, 16 Nov 2026 15:18:01 -0800 Subject: [PATCH] Initial daemons for hispec plcs Keep main's driver submodule pointers; only lightel is new Update PLC ip addresses Add target keyword Add a camserver config update Change from HTTP to using libby Updates to the camera server --- .gitmodules | 4 + config/hsfibers/hsfibers_fiberswitchblue.yaml | 101 +++ config/hsfibers/hsfibers_fiberswitchred.yaml | 101 +++ daemons/hsfibers/fiberswitch | 654 ++++++++++++++++++ docs/architecture/command_and_control.md | 2 +- docs/architecture/daemons.md | 27 + docs/architecture/evolution.md | 5 +- docs/architecture/overview.md | 1 + docs/operations/systemd.md | 2 + pyproject.toml | 6 + src/hispec/driver/lightel | 1 + src/hispec/fiberswitch/__init__.py | 16 + src/hispec/fiberswitch/archive.py | 58 ++ src/hispec/fiberswitch/camera.py | 141 ++++ src/hispec/fiberswitch/camserver.py | 354 ++++++++++ src/hispec/fiberswitch/controller.py | 149 ++++ src/hispec/fiberswitch/jobs.py | 124 ++++ src/hispec/fiberswitch/policy.py | 199 ++++++ src/hispec/fiberswitch/switch.py | 315 +++++++++ .../instances/hsfibers_fiberswitchblue.env | 11 + systemd/instances/hsfibers_fiberswitchred.env | 11 + tests/fiberswitch/test_camera.py | 121 ++++ tests/fiberswitch/test_camserver.py | 167 +++++ tests/fiberswitch/test_controller.py | 247 +++++++ tests/fiberswitch/test_switch.py | 233 +++++++ uv.lock | 92 ++- 26 files changed, 3118 insertions(+), 24 deletions(-) create mode 100644 config/hsfibers/hsfibers_fiberswitchblue.yaml create mode 100644 config/hsfibers/hsfibers_fiberswitchred.yaml create mode 100755 daemons/hsfibers/fiberswitch create mode 160000 src/hispec/driver/lightel create mode 100644 src/hispec/fiberswitch/__init__.py create mode 100644 src/hispec/fiberswitch/archive.py create mode 100644 src/hispec/fiberswitch/camera.py create mode 100644 src/hispec/fiberswitch/camserver.py create mode 100644 src/hispec/fiberswitch/controller.py create mode 100644 src/hispec/fiberswitch/jobs.py create mode 100644 src/hispec/fiberswitch/policy.py create mode 100644 src/hispec/fiberswitch/switch.py create mode 100644 systemd/instances/hsfibers_fiberswitchblue.env create mode 100644 systemd/instances/hsfibers_fiberswitchred.env create mode 100644 tests/fiberswitch/test_camera.py create mode 100644 tests/fiberswitch/test_camserver.py create mode 100644 tests/fiberswitch/test_controller.py create mode 100644 tests/fiberswitch/test_switch.py diff --git a/.gitmodules b/.gitmodules index b4d6209..3f8f3a9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -56,3 +56,7 @@ [submodule "src/hispec/driver/hispec-fiber-switcher"] path = src/hispec/driver/hispec-fiber-switcher url = https://github.com/COO-Utilities/hispec-fiber-switcher +[submodule "src/hispec/driver/lightel"] + path = src/hispec/driver/lightel + url = https://github.com/COO-Utilities/lightel + branch = main diff --git a/config/hsfibers/hsfibers_fiberswitchblue.yaml b/config/hsfibers/hsfibers_fiberswitchblue.yaml new file mode 100644 index 0000000..4d584bd --- /dev/null +++ b/config/hsfibers/hsfibers_fiberswitchblue.yaml @@ -0,0 +1,101 @@ +# fiberswitchblue: Blue (YJ) fiber switcher daemon config: the switcher PLC, +# its Lightel DI-2000 inspection camera, and the connection policy per port +# +# Usage: +# daemons/hsfibers/fiberswitch -c config/hsfibers/hsfibers_fiberswitchblue.yaml + +peer_id: fiberswitchblue +group_id: hsfibers + +switch: + # The PLC is the TCP server. TODO: confirm the blue unit's address and port. + host: 192.168.29.190 + port: 9001 + read_timeout_s: 2.0 + # Refuse motion unless the PLC reports automatic operation (message bit 2) + require_auto: true + # Seconds to wait for the PLC to report each operation done. start_s is how + # long a home or clean is given to start moving before the daemon polls for + # completion, since neither has a done flag of its own. + timeouts: + move_s: 30 + insert_s: 15 + clean_s: 180 + home_s: 60 + start_s: 2 + # Writable range, in mm, of each config_ axis register. A register + # without a range here is read only: a bad value can damage the switcher. + axis_limits: {} + # retractdist: [0.0, 10.0] + # caminsertpos: [0.0, 50.0] + # noncaminsertpos: [0.0, 50.0] + +camera: + # The DI-2000 is plugged into another machine (the Pi at 131.215.200.211): + # the libby camera peer serving it there, started with + # python camserver.py --device N --peer-id fiberswitchbluecam + # Remove it to open a camera on this machine; with it set, device_index, + # backend, width and height are the camera peer's and those below are unused. + remote: hsfibers.fiberswitchbluecam + # Seconds to wait for a frame or property from the camera peer + remote_timeout_s: 10 + # OpenCV device index. With the V4L2 backend it is the /dev/videoN number, + # which the vendor autofocus control needs. TODO: confirm for the blue camera. + device_index: 1 + # OpenCV capture backend name (cv2.CAP_); null for OpenCV's default + backend: V4L2 + width: null + height: null + # Images go under // as PNG, each with a JSON sidecar. + # Under /opt/hispec/data, which the service user can write. + archive_dir: /opt/hispec/data/fiberswitchblue/images + +# Policy overrides made at runtime, and each port's inspection history and +# reference image, persist here. +state_file: /opt/hispec/data/fiberswitchblue/state.json + +# Connection policy applied to every input group unless its own policy +# overrides a field: +# inspect_position inspection position that images this fiber end +# inspect never | always (every select) | interval +# inspect_interval_h hours between inspections when inspect is interval +# clean_interval_h clean on select after this many hours; null to clean +# only when an inspection finds dirt +# clean_mode inside | outside | both +# clean_on_dirty clean and re-inspect when dirt is above clean_threshold +# max_clean_attempts cleans before giving up and alerting +# clean_threshold dirt (fraction of the fiber-end area) to clean above +# alert_threshold dirt still present after cleaning that alerts +# camera camera settings to apply first, by name: exposure, +# gain, brightness, focus, autoexposure, autofocus +policy_defaults: + inspect: never + inspect_interval_h: 24 + clean_interval_h: null + clean_mode: both + clean_on_dirty: true + max_clean_attempts: 2 + clean_threshold: 0.005 + alert_threshold: 0.01 + +# TODO: replace with the as-built mapping before deploying. +# Camera ports: inserting here puts a fiber end in front of the camera. +# The PLC cleans at Port A 10 / Port B 2, so keep that out of both lists. +inspection_positions: + inspect1: {port_a: 13, port_b: 1} + +# Where the spectrograph fibers connect for science (Port A 1-13, Port B 1-5), +# each with its connection policy. Names become keyword suffixes, so keep +# them lowercase identifiers. +input_groups: + group1: + port_a: 1 + port_b: 1 + policy: {inspect_position: inspect1} + group2: + port_a: 2 + port_b: 1 + policy: {inspect_position: inspect1} + +logging: + level: INFO diff --git a/config/hsfibers/hsfibers_fiberswitchred.yaml b/config/hsfibers/hsfibers_fiberswitchred.yaml new file mode 100644 index 0000000..49d8c48 --- /dev/null +++ b/config/hsfibers/hsfibers_fiberswitchred.yaml @@ -0,0 +1,101 @@ +# fiberswitchred: Red (HK) fiber switcher daemon config: the switcher PLC, +# its Lightel DI-2000 inspection camera, and the connection policy per port +# +# Usage: +# daemons/hsfibers/fiberswitch -c config/hsfibers/hsfibers_fiberswitchred.yaml + +peer_id: fiberswitchred +group_id: hsfibers + +switch: + # The PLC is the TCP server. TODO: confirm the red unit's address and port. + host: 192.168.29.191 + port: 9001 + read_timeout_s: 2.0 + # Refuse motion unless the PLC reports automatic operation (message bit 2) + require_auto: true + # Seconds to wait for the PLC to report each operation done. start_s is how + # long a home or clean is given to start moving before the daemon polls for + # completion, since neither has a done flag of its own. + timeouts: + move_s: 30 + insert_s: 15 + clean_s: 180 + home_s: 60 + start_s: 2 + # Writable range, in mm, of each config_ axis register. A register + # without a range here is read only: a bad value can damage the switcher. + axis_limits: {} + # retractdist: [0.0, 10.0] + # caminsertpos: [0.0, 50.0] + # noncaminsertpos: [0.0, 50.0] + +camera: + # The DI-2000 is plugged into another machine (the Pi at 131.215.200.211): + # the libby camera peer serving it there, started with + # python camserver.py --device N --peer-id fiberswitchredcam + # Remove it to open a camera on this machine; with it set, device_index, + # backend, width and height are the camera peer's and those below are unused. + remote: hsfibers.fiberswitchredcam + # Seconds to wait for a frame or property from the camera peer + remote_timeout_s: 10 + # OpenCV device index. With the V4L2 backend it is the /dev/videoN number, + # which the vendor autofocus control needs. TODO: confirm for the red camera. + device_index: 0 + # OpenCV capture backend name (cv2.CAP_); null for OpenCV's default + backend: V4L2 + width: null + height: null + # Images go under // as PNG, each with a JSON sidecar. + # Under /opt/hispec/data, which the service user can write. + archive_dir: /opt/hispec/data/fiberswitchred/images + +# Policy overrides made at runtime, and each port's inspection history and +# reference image, persist here. +state_file: /opt/hispec/data/fiberswitchred/state.json + +# Connection policy applied to every input group unless its own policy +# overrides a field: +# inspect_position inspection position that images this fiber end +# inspect never | always (every select) | interval +# inspect_interval_h hours between inspections when inspect is interval +# clean_interval_h clean on select after this many hours; null to clean +# only when an inspection finds dirt +# clean_mode inside | outside | both +# clean_on_dirty clean and re-inspect when dirt is above clean_threshold +# max_clean_attempts cleans before giving up and alerting +# clean_threshold dirt (fraction of the fiber-end area) to clean above +# alert_threshold dirt still present after cleaning that alerts +# camera camera settings to apply first, by name: exposure, +# gain, brightness, focus, autoexposure, autofocus +policy_defaults: + inspect: never + inspect_interval_h: 24 + clean_interval_h: null + clean_mode: both + clean_on_dirty: true + max_clean_attempts: 2 + clean_threshold: 0.005 + alert_threshold: 0.01 + +# TODO: replace with the as-built mapping before deploying. +# Camera ports: inserting here puts a fiber end in front of the camera. +# The PLC cleans at Port A 10 / Port B 2, so keep that out of both lists. +inspection_positions: + inspect1: {port_a: 13, port_b: 1} + +# Where the spectrograph fibers connect for science (Port A 1-13, Port B 1-5), +# each with its connection policy. Names become keyword suffixes, so keep +# them lowercase identifiers. +input_groups: + group1: + port_a: 1 + port_b: 1 + policy: {inspect_position: inspect1} + group2: + port_a: 2 + port_b: 1 + policy: {inspect_position: inspect1} + +logging: + level: INFO diff --git a/daemons/hsfibers/fiberswitch b/daemons/hsfibers/fiberswitch new file mode 100755 index 0000000..796b9c3 --- /dev/null +++ b/daemons/hsfibers/fiberswitch @@ -0,0 +1,654 @@ +#!/usr/bin/env python3 +""" +Fiber Switcher Daemon (fiberswitch) + +One instance per channel (red and blue). Each drives that channel's fiber +switcher PLC (fiber_switcher.FiberSwitcher) and its Lightel DI-2000 fiber +inspection camera (di2000.Di2000), through three classes: + +- MechanicalSwitch: connect input groups, move to inspection positions, + clean, air purge and the PLC's axis settings. +- MechanicalSwitchCamera (keywords prefixed cam): refocus, light, detector + settings and capture. +- MechanicalSwitchController: select inputs per each port's connection + policy, inspect and clean fiber ends, reference and inspection images, + alerts. + +Positions are named in the config: `input_groups` are where the spectrograph +fibers connect for science, each with its connection policy, and +`inspection_positions` put a fiber end in front of the camera. + +Every command that moves the switcher or uses the camera runs in the +background, one at a time: the write returns once the command has started, +`busy` is true until it finishes and `jobstatus` then says how it went. +`halt` stops the switcher and aborts the command at once. (libby runs a +daemon's keyword handlers one at a time, so a command that blocked its +setter would also block `halt` and every status read.) +""" + +import argparse +import json +import re +import sys +from datetime import datetime, timezone +from functools import partial + +import cv2 +import inspection as lightel_inspection +from di2000 import Di2000 +from fiber_switcher import DEFAULT_PORT, FiberSwitcher +from libby import Libby + +from hispec import HispecDaemon +from hispec.fiberswitch.archive import ImageArchive +from hispec.fiberswitch.camera import InspectionCamera +from hispec.fiberswitch.camserver import RemoteDi2000 +from hispec.fiberswitch.controller import FiberSwitchController +from hispec.fiberswitch.jobs import JobRunner, register_job_keywords +from hispec.fiberswitch.policy import ControllerState +from hispec.fiberswitch.switch import (AXIS_SETTINGS, CLEAN_MODES, SwitchSequencer, alarm_word, + message_word, parse_positions, parse_target) + +# Input names become keyword suffixes +_NAME = re.compile(r"^[a-z][a-z0-9_]*$") + +# Camera detector settings exposed as cam float keywords +CAMERA_PROPERTIES = { + "exposure": (cv2.CAP_PROP_EXPOSURE, "Exposure, in the camera driver's units."), + "gain": (cv2.CAP_PROP_GAIN, "Gain, in the camera driver's units."), + "brightness": (cv2.CAP_PROP_BRIGHTNESS, "Brightness, in the camera driver's units."), + "focus": (cv2.CAP_PROP_FOCUS, "Focus position, in the camera driver's units."), +} + +# V4L2 values of CAP_PROP_AUTO_EXPOSURE, as lightel's inspection uses them +AUTO_EXPOSURE_MANUAL = 1.0 +AUTO_EXPOSURE_AUTO = 3.0 + + +class MechanicalSwitch: + """The switcher PLC and its keywords.""" + + def __init__(self, daemon, config): + self.daemon = daemon + self.host = config.get("host") + self.port = int(config.get("port", DEFAULT_PORT)) + self.axis_limits = config.get("axis_limits") or {} + self.driver = FiberSwitcher(log=True, + read_timeout=float(config.get("read_timeout_s", 2.0))) + timeouts = config.get("timeouts") or {} + self.sequencer = SwitchSequencer( + self.driver, + move_timeout_s=float(timeouts.get("move_s", 30.0)), + insert_timeout_s=float(timeouts.get("insert_s", 15.0)), + clean_timeout_s=float(timeouts.get("clean_s", 180.0)), + home_timeout_s=float(timeouts.get("home_s", 60.0)), + start_s=float(timeouts.get("start_s", 2.0)), + require_auto=bool(config.get("require_auto", True))) + + def connected(self): + """True when the PLC connection is open.""" + return bool(self.driver.is_connected()) + + def connect(self): + """Open the PLC connection.""" + if not self.host: + raise RuntimeError("no switch.host in the config") + self.driver.connect(self.host, self.port) + + def problem(self): + """What is wrong with the switcher, or None.""" + if not self.connected(): + return "switch disconnected" + try: + status = self.sequencer.status() + except Exception as e: # pylint: disable=W0718 + return f"switch not answering: {e}" + if alarm_word(status): + return f"switch alarm 0x{alarm_word(status):08X}" + if not self.sequencer.auto_ready(): + return "switch not in automatic operation" + return None + + def register_keywords(self, registry): + """Add the switch keywords.""" + d, seq, hw = self.daemon, self.sequencer, self._hw + inputs = ", ".join(d.input_groups) or "none" + positions = ", ".join(d.inspection_positions) or "none" + registry.bool("isconnected", getter=self.connected, setter=self._set_connected, + description="Connected to the switcher PLC; write to connect or " + "disconnect.") + registry.bool("ishomed", getter=lambda: seq.homed, setter=self._set_homed, + description="This daemon has seen a return to origin complete; write " + "true to home. Reads false after a daemon restart.") + registry.bool("autoready", getter=hw(seq.auto_ready), + description="PLC is in automatic operation, so it accepts motion.") + registry.int("porta", getter=hw(lambda: seq.ports()[0]), + description="Port A target position, 1-13.") + registry.int("portb", getter=hw(lambda: seq.ports()[1]), + description="Port B target position, 1-5.") + registry.string("target", getter=hw(lambda: ",".join(map(str, seq.ports()))), + setter=self._set_target, validator=_check_target, + description="Port A,Port B the switcher is set to, like '5,2'; write " + "a pair to retract, move there and insert, for manual " + "switching to any ports. Port A 1-13, Port B 1-5.") + registry.bool("inposition", getter=hw(lambda: seq.status().in_position), + description="Ports are at their target positions.") + registry.string("insertion", getter=hw(seq.insertion), + description="Connector insertion axis: removed, inserted or moving.") + registry.string("alarms", getter=hw(lambda: f"0x{alarm_word(seq.status()):08X}"), + description="PLC alarm bits 0-31; 0x00000000 when clear.") + registry.string("messages", getter=hw(lambda: f"0x{message_word(seq.status()):08X}"), + description="PLC message bits 0-31.") + registry.string("inputgroup", getter=hw(lambda: self._match(d.input_groups)), + setter=self._set_input, validator=d.check_input, + description="Input group connected to the spectrograph fibers, empty " + "when none; write a name to connect it directly, without " + f"its policy (see select). Inputs: {inputs}.") + registry.string("inspectpos", getter=hw(lambda: self._match(d.inspection_positions)), + setter=self._set_inspection, validator=self._check_inspection, + description="Inspection position the fiber is inserted at, empty " + "when none; write a name to put that fiber end in front " + f"of the camera. Positions: {positions}.") + registry.bool("inserted", getter=hw(lambda: seq.insertion() == "inserted"), + setter=self._set_inserted, + description="Connector is inserted; write to insert or retract at the " + "current position.") + registry.string("clean", getter=lambda: seq.last_clean_mode or "", + setter=self._set_clean, + validator=lambda v: None if v in CLEAN_MODES + else f"expected one of {CLEAN_MODES}", + description="Write inside, outside or both to run a cleaning cycle; " + "reads the last mode run.") + registry.bool("airpurge", getter=lambda: seq.air_purge, + setter=d.quick(self._require_connected, seq.set_air_purge), + nullable=True, + description="Air purge valve as last commanded (the PLC does not " + "report it); null until set. Placeholder for the " + "positive-pressure air system.") + for suffix, (getter, setter, description) in AXIS_SETTINGS.items(): + self._register_axis_setting(registry, suffix, getter, setter, description) + + def _register_axis_setting(self, registry, suffix, getter, setter, description): + limits = self.axis_limits.get(suffix) + writable = limits is not None + note = (f" Writable within {limits[0]}-{limits[1]} mm." if writable + else f" Read only: set switch.axis_limits.{suffix} in the config to allow " + "writes.") + # Look the driver method up per call rather than binding it now + # pylint: disable=unnecessary-lambda + registry.float( + f"config_{suffix}", + getter=self._hw(lambda: getattr(self.driver, getter)()), + setter=(self.daemon.quick(self._require_connected, + lambda v: getattr(self.driver, setter)(v)) + if writable else None), + validator=partial(_check_range, limits) if writable else None, + units="mm", description=description + note) + + def _hw(self, fn): + def getter(): + self._require_connected() + return fn() + return getter + + def _require_connected(self): + if not self.connected(): + raise RuntimeError("not connected to the switcher") + + def _match(self, positions): + for name, position in positions.items(): + if self.sequencer.at(position, inserted=True): + return name + return "" + + def _check_inspection(self, name): + if name not in self.daemon.inspection_positions: + return (f"unknown inspection position {name!r}; " + f"positions: {list(self.daemon.inspection_positions)}") + return None + + def _start(self, label, fn): + self._require_connected() + self.daemon.jobs.start(label, fn) + + def _set_input(self, name): + position = self.daemon.input_groups[name] + self._start(f"connect {name}", lambda cancel: self.sequencer.connect(position, cancel)) + + def _set_target(self, value): + position = parse_target(value) + self._start(f"connect {position.name}", + lambda cancel: self.sequencer.connect(position, cancel)) + + def _set_inspection(self, name): + # Inserting at a camera port puts the fiber end at the PLC's camera + # insertion position, in the inspection camera's field + position = self.daemon.inspection_positions[name] + self._start(f"inspection position {name}", + lambda cancel: self.sequencer.connect(position, cancel)) + + def _set_inserted(self, value): + self._start("insert" if value else "retract", + self.sequencer.insert if value else self.sequencer.retract) + + def _set_clean(self, mode): + self._start(f"clean {mode}", lambda cancel: self.sequencer.clean(mode, cancel)) + + def _set_homed(self, value): + if not value: + raise RuntimeError("write true to home; homing cannot be undone") + self._start("home", self.sequencer.home) + + def _set_connected(self, value): + if value: + if not self.connected(): + self.connect() + else: + self.daemon.require_idle() + self.driver.disconnect() + + +class MechanicalSwitchCamera: # pylint: disable=too-many-instance-attributes + """The Lightel DI-2000 inspection camera and its cam* keywords.""" + + def __init__(self, daemon, config): + self.daemon = daemon + self.remote = config.get("remote") + self.device_index = int(config.get("device_index", 0)) + self.backend = _backend(config.get("backend")) + self.width = config.get("width") + self.height = config.get("height") + archive_dir = config.get("archive_dir") + if not archive_dir: + raise ValueError("config must set camera.archive_dir") + # A camera plugged into another machine is reached through the + # camera peer serving it there; device_index, backend, width and + # height are then the peer's + self.driver = (RemoteDi2000(str(self.remote), self._camera_client, + timeout_s=float(config.get("remote_timeout_s", 10.0))) + if self.remote else Di2000(log=True)) + self._client_bind = config.get("remote_client_bind") + properties = {name: prop for name, (prop, _) in CAMERA_PROPERTIES.items()} + properties["autoexposure"] = cv2.CAP_PROP_AUTO_EXPOSURE + properties["autofocus"] = cv2.CAP_PROP_AUTOFOCUS + self.camera = InspectionCamera(self.driver, ImageArchive(archive_dir), + lightel_inspection, properties) + + def connected(self): + """True when the camera is open.""" + return bool(self.driver.is_connected()) + + def connect(self): + """Open the camera.""" + self.driver.connect(self.device_index, backend=self.backend, + width=self.width, height=self.height) + + def describe(self): + """Where the camera is, for the log.""" + return f"camera peer {self.remote}" if self.remote else f"camera {self.device_index}" + + def _camera_client(self): + """A libby connection of its own for the remote camera's RPCs. + + Not the daemon's: it runs keyword handlers on one thread, so a getter + waiting on it for the camera's reply would never see it. + """ + d = self.daemon + self_id = f"{d.peer_id}-camclient" + if str(d.transport).lower() == "zmq": + if not self._client_bind: + raise ValueError("camera.remote_client_bind is needed over zmq") + return Libby.zmq(self_id=self_id, bind=self._client_bind, + address_book=d.config_address_book(), group_id=d.group_id) + return Libby.rabbitmq(self_id=self_id, rabbitmq_url=d.config_rabbitmq_url(), + group_id=d.group_id) + + def problem(self): + """What is wrong with the camera, or None.""" + if self.connected(): + return None + return f"camera disconnected ({self.describe()})" + + def register_keywords(self, registry): + """Add the cam* keywords.""" + cam = self.camera + registry.bool("camconnected", getter=self.connected, setter=self._set_connected, + description="Inspection camera is open; write to open or release it.") + registry.trigger("camrefocus", + action=lambda: self._start("refocus", cam.refocus), + description="Bring the fiber end into focus (vendor autofocus on " + "Linux, else a focus sweep).") + registry.float("camfocusscore", getter=lambda: self._focus("score"), nullable=True, + description="Sharpness of the latest refocus; null before one.") + registry.string("camlight", getter=lambda: "fixed", + description="Placeholder: the DI-2000 illumination is not software " + "controllable through its driver.") + for name, (_, description) in CAMERA_PROPERTIES.items(): + registry.float(f"cam{name}", + getter=self._hw(partial(cam.get_setting, name)), + setter=self._setting(name), description=description) + registry.bool("camautoexposure", + getter=self._hw(lambda: cam.get_setting("autoexposure") + != AUTO_EXPOSURE_MANUAL), + setter=self._setting("autoexposure", lambda v: AUTO_EXPOSURE_AUTO + if v else AUTO_EXPOSURE_MANUAL), + description="Camera's own auto exposure; inspections turn it off.") + registry.bool("camautofocus", + getter=self._hw(lambda: bool(cam.get_setting("autofocus"))), + setter=self._setting("autofocus", lambda v: 1.0 if v else 0.0), + description="Camera's standard UVC continuous autofocus.") + registry.string("camcapture", getter=lambda: self._last("tag"), + setter=lambda tag: self._start(f"capture {tag}", + partial(_capture, cam, tag)), + description="Write a tag to grab and archive one frame as-is; " + "reads the latest image's tag.") + registry.string("camlastimage", getter=lambda: self._last("image"), + description="Path of the latest archived image, empty before one.") + registry.string("camlastresult", getter=lambda: json.dumps(cam.last_record or {}), + description="JSON metadata of the latest capture or inspection, as " + "in its sidecar.") + + def _hw(self, fn): + def getter(): + self._require_connected() + return fn() + return getter + + def _setting(self, name, convert=float): + return self.daemon.quick(self._require_connected, + lambda v: self.camera.set_setting(name, convert(v))) + + def _focus(self, field): + focus = self.camera.last_focus + return None if focus is None else focus[field] + + def _last(self, field): + record = self.camera.last_record + return "" if record is None else record.get(field, "") + + def _require_connected(self): + if not self.connected(): + raise RuntimeError("camera is not connected") + + def _start(self, label, fn): + self._require_connected() + self.daemon.jobs.start(label, fn) + + def _set_connected(self, value): + if value: + if not self.connected(): + self.connect() + else: + self.daemon.require_idle() + self.driver.disconnect() + + +class MechanicalSwitchController: # pylint: disable=too-few-public-methods + """Connection policy, inspect-and-clean and reference images, and their keywords.""" + + def __init__(self, daemon, switch, camera): + self.daemon = daemon + self.switch = switch + self.state = ControllerState( + daemon.get_config("state_file"), daemon.get_config("policy_defaults") or {}, + {name: (spec or {}).get("policy") or {} + for name, spec in (daemon.get_config("input_groups") or {}).items()}) + for message in self.state.dropped: + daemon.logger.warning("Ignoring saved policy override %s", message) + self.ctl = FiberSwitchController( + switch.sequencer, camera.camera, self.state, + input_groups=daemon.input_groups, + inspection_positions=daemon.inspection_positions, logger=daemon.logger) + self._camera_settings = set(camera.camera.properties) + self._last = {"inspectclean": "", "takereference": ""} + for name in self.state.inputs: + policy = self.state.policy(name) + self._check_position(policy.inspect_position) + self._check_camera_settings(policy.camera) + + def register_keywords(self, registry): + """Add the controller keywords.""" + d = self.daemon + inputs = ", ".join(self.state.inputs) + registry.string("select", getter=lambda: self.state.selected, + setter=lambda name: self._start("select", self.ctl.select_input, name), + validator=d.check_input, + description="Input group last selected; write a name to connect it " + "per its policy, cleaning and inspecting first if due. " + f"Inputs: {inputs}.") + registry.string("inspectclean", getter=lambda: self._last["inspectclean"], + setter=lambda name: self._start("inspectclean", + self.ctl.inspect_and_clean, name), + validator=d.check_input, + description="Write an input name to inspect its fiber end, clean " + "while dirty, archive the images and alert per its " + "policy. Leaves the fiber at its inspection position.") + registry.string("takereference", getter=lambda: self._last["takereference"], + setter=lambda name: self._start("takereference", + self.ctl.take_reference, name), + validator=d.check_input, + description="Write an input name to take a reference image of its " + "fiber end.") + registry.string("alert", getter=lambda: self.state.alert, setter=self._set_alert, + description="Latest unacknowledged alert, empty when none; write an " + "empty string to clear it.") + for name in self.state.inputs: + self._register_input_keywords(registry, name) + + def _register_input_keywords(self, registry, name): + history = partial(self.state.history, name) + registry.string(f"policy_{name}", + getter=lambda: json.dumps(self.state.policy(name).to_dict()), + setter=lambda value: self._set_policy(name, value), + description=f"Connection policy of {name}, as JSON. Write a JSON " + "object of the fields to change; {} resets to the " + "config.") + registry.string(f"inspectimage_{name}", getter=lambda: history().get("last_image", ""), + description=f"Path of {name}'s latest inspection image.") + registry.string(f"refimage_{name}", + getter=lambda: history().get("reference_image", ""), + description=f"Path of {name}'s reference image.") + registry.float(f"dirt_{name}", getter=lambda: history().get("last_dirt"), + nullable=True, + description=f"Dirt fraction of {name}'s fiber end at its latest " + "inspection.") + registry.bool(f"passed_{name}", getter=lambda: history().get("last_passed"), + nullable=True, + description=f"{name}'s latest inspection found the fiber end within " + "the alert threshold.") + registry.string(f"lastinspect_{name}", + getter=lambda: _iso(history().get("last_inspect")), + description=f"UTC time of {name}'s latest inspection.") + registry.string(f"lastclean_{name}", + getter=lambda: _iso(history().get("last_clean")), + description=f"UTC time {name}'s fiber end was last cleaned.") + + def _start(self, kind, fn, name): + # A command that needs the camera and finds it closed fails in the job, + # with the camera's own error + if not self.switch.connected(): + raise RuntimeError("not connected to the switcher") + self.daemon.jobs.start(f"{kind} {name}", lambda cancel: fn(name, cancel)) + if kind in self._last: + self._last[kind] = name + + def _check_position(self, position): + if position is not None and position not in self.daemon.inspection_positions: + raise ValueError(f"unknown inspection position {position!r}; " + f"positions: {list(self.daemon.inspection_positions)}") + + def _check_camera_settings(self, camera): + unknown = set(camera) - self._camera_settings + if unknown: + raise ValueError(f"unknown camera settings {sorted(unknown)}; " + f"known: {sorted(self._camera_settings)}") + + def _set_policy(self, name, value): + try: + changes = json.loads(value) if value else {} + except json.JSONDecodeError as e: + raise ValueError(f"policy must be a JSON object: {e}") from e + if not isinstance(changes, dict): + raise ValueError("policy must be a JSON object") + self._check_position(changes.get("inspect_position")) + self._check_camera_settings(changes.get("camera") or {}) + policy = self.state.update_policy(name, changes) + self.daemon.logger.info("Policy for %s now %s", name, policy.to_dict()) + + def _set_alert(self, value): + if value: + raise ValueError("write an empty string to clear the alert") + if self.state.alert: + self.daemon.logger.info("Alert cleared: %s", self.state.alert) + self.state.set_alert("") + + +# pylint: disable=too-many-instance-attributes +class FiberSwitchDaemon(HispecDaemon): # pylint: disable=W0223 + """One channel's fiber switcher, inspection camera and controller.""" + + def __init__(self): + super().__init__() + self.daemon_description = "Fiber Switcher Daemon" + self.jobs = None + self.input_groups = {} + self.inspection_positions = {} + self.switch = None + self.camera = None + self.controller = None + + def on_start(self): # pylint: disable=unused-argument + """Read the config, register keywords and connect to the hardware.""" + groups = self.get_config("input_groups") or {} + bad = [name for name in groups if not _NAME.match(str(name))] + if bad: + raise ValueError(f"input group names must be lowercase identifiers: {bad}") + self.input_groups = parse_positions(groups, "input group") + self.inspection_positions = parse_positions( + self.get_config("inspection_positions"), "inspection position") + self.jobs = JobRunner(self.logger) + self.switch = MechanicalSwitch(self, self.get_config("switch") or {}) + self.camera = MechanicalSwitchCamera(self, self.get_config("camera") or {}) + self.controller = MechanicalSwitchController(self, self.switch, self.camera) + + self.logger.info("Starting %s daemon: switcher %s:%s, %s, inputs %s", + self.peer_id, self.switch.host, self.switch.port, + self.camera.describe(), ", ".join(self.input_groups)) + registry = self.keyword_registry + registry.string("status", getter=self._status, + description="ok, or what is wrong with the switcher and camera.") + self.switch.register_keywords(registry) + self.camera.register_keywords(registry) + self.controller.register_keywords(registry) + register_job_keywords(registry, self.jobs, abort_action=self._halt, abort_name="halt") + registry.trigger("shutdown", action=self._shutdown, + description="Stop this daemon; refused while a command runs.") + + for part in (self.switch, self.camera): + try: + part.connect() + except Exception as e: # pylint: disable=W0718 + self.logger.error("Failed to connect %s: %s", type(part).__name__, e) + self.logger.info("Daemon started") + + def on_stop(self): # pylint: disable=unused-argument + """Halt any command, then disconnect both devices.""" + self.logger.info("Shutting down %s daemon", self.peer_id) + if self.jobs is not None and self.jobs.busy: + self._halt() + self.jobs.wait(5.0) + for part in (self.switch, self.camera): + if part is not None: + part.driver.disconnect() + if self.camera is not None and hasattr(self.camera.driver, "close"): + self.camera.driver.close() + + def check_input(self, name): + """Validator for keywords taking an input group name.""" + if name not in self.input_groups: + return f"unknown input group {name!r}; inputs: {list(self.input_groups)}" + return None + + def require_idle(self): + """Refuse while a command is running.""" + if self.jobs.busy: + raise RuntimeError(f"busy with {self.jobs.state}") + + def quick(self, require_connected, fn): + """A setter applied at once: needs the device connected and no command running.""" + def setter(value): + require_connected() + self.require_idle() + fn(value) + return setter + + def _status(self): + problems = [p for p in (self.switch.problem(), self.camera.problem()) if p] + return "; ".join(problems) or "ok" + + def _halt(self): + self.jobs.abort() + if self.switch.connected(): + self.switch.sequencer.stop() + + def _shutdown(self): + if self.jobs.busy: + raise RuntimeError(f"busy with {self.jobs.state}; halt before shutting down") + self.logger.info("Shutting down on keyword request") + self.request_stop() + + +def _capture(camera, tag, cancel): + camera.capture(tag, cancel) + + +def _check_target(value): + try: + parse_target(value) + except ValueError as e: + return str(e) + return None + + +def _check_range(limits, value): + if not limits[0] <= value <= limits[1]: + return f"{value} mm outside {limits[0]}-{limits[1]} mm" + return None + + +def _backend(name): + """OpenCV capture backend from its name (V4L2, DSHOW, ...); None for the default.""" + if not name: + return None + attr = f"CAP_{str(name).upper()}" + if not hasattr(cv2, attr): + raise ValueError(f"unknown OpenCV backend {name!r}") + return getattr(cv2, attr) + + +def _iso(epoch): + if epoch is None: + return "" + return datetime.fromtimestamp(float(epoch), timezone.utc).isoformat(timespec="seconds") + + +def main(): + """Main entry point for the daemon.""" + parser = argparse.ArgumentParser(description="Fiber Switcher Daemon") + parser.add_argument("-c", "--config", type=str, help="Path to config file (YAML or JSON)") + args = parser.parse_args() + if not args.config: + print("--config is required", file=sys.stderr) + sys.exit(2) + try: + FiberSwitchDaemon.from_config_file(args.config).serve() + except KeyboardInterrupt: + print("\nDaemon interrupted by user") + sys.exit(0) + except Exception as e: # pylint: disable=W0718 + print(f"Error running daemon: {e}", file=sys.stderr) + sys.exit(1) + + +if __name__ == "__main__": + main() diff --git a/docs/architecture/command_and_control.md b/docs/architecture/command_and_control.md index 59df8ea..17b4d1b 100644 --- a/docs/architecture/command_and_control.md +++ b/docs/architecture/command_and_control.md @@ -62,7 +62,7 @@ because both client and daemon normalise through the same function. libby show hsfei.ms.positionnamed libby modify hsfei.ms.positionnamed keck_mask libby list hsfei.ms.% -libby waitfor hsfei.ms.ismoving == false +libby waitfor '$hsfei.ms.ismoving == false' ``` `waitfor` is libby's equivalent of `ktl.waitFor`: it blocks until a keyword diff --git a/docs/architecture/daemons.md b/docs/architecture/daemons.md index 0061601..8e2cb16 100644 --- a/docs/architecture/daemons.md +++ b/docs/architecture/daemons.md @@ -30,6 +30,7 @@ their shared `pdu_base.py` base class. | `hscal/smc8_attenuator` | `standa.smc8` | Standa SMC8 (libximc) attenuator | `hscal_hketatten` | | `hspower/eaton_pdu` | `pdu.src.emat08_10` | Eaton EMAT08-10 networked PDU | `hspower_fei3/4`, `hspower_cal1`–`4`, `hspower_fib1`, `hspower_bspec1`, `hspower_rspec1` | | `hsfibers/trunkfiberbox` | `fib.TrunkInterfaceBox` | FIB trunk interface box, over MQTT 5 | `hsfibers_trunkfiberbox` | +| `hsfibers/fiberswitch` | `fiber_switcher.FiberSwitcher`, `di2000.Di2000` | Fiber switcher PLC (MELSEC MC over TCP) and its Lightel DI-2000 inspection camera (USB) | `hsfibers_fiberswitchred`, `hsfibers_fiberswitchblue` | Thirty-five instances are currently defined under `systemd/instances/`, running twelve distinct daemon scripts — the config-driven design paying off @@ -84,6 +85,32 @@ switch. | Instance | Mechanism | |---|---| | `hsfibers_trunkfiberbox` | trunk interface box: MEMS switches, routes, lasers, attenuators, photodiodes, all read only | +| `hsfibers_fiberswitchred`, `hsfibers_fiberswitchblue` | fiber switcher per channel: connects input groups, cleans, air purge; inspection camera (`cam*`); per-port connection policy, inspect and clean, reference images, alerts | + +Each fiber switcher instance holds the three classes of its design: +MechanicalSwitch (the PLC), MechanicalSwitchCamera (the DI-2000, keywords +prefixed `cam`) and MechanicalSwitchController (connection policy and +inspect-and-clean, which drives the other two directly). Commands that move +the switcher or use the camera run one at a time as a background job: a write +returns once the command starts, `busy` is true until it ends and `jobstatus` +then reads `ok`, `aborted` or `error: ...`. This keeps `halt` and every status +keyword answering mid-move, since libby runs a daemon's keyword handlers one +at a time on a single thread. Inspection images are archived as PNG with a +JSON sidecar under `camera.archive_dir`; policy changes and each port's +inspection history persist in `state_file`. + +The DI-2000 is a USB camera, so it opens only on the machine it is plugged +into. When that is not the daemon's host, run a libby camera peer there, one +per camera (`python camserver.py --device N --peer-id fiberswitchbluecam`; +`src/hispec/fiberswitch/camserver.py` needs only libby, numpy and lightel, so +it can be copied there alone), and set `camera.remote: +hsfibers.fiberswitchbluecam`. The daemon then reads frames and camera +settings through the broker, with its broker login, on a libby connection of +its own. A frame is larger than libby's 512 KB message limit, so it travels +compressed in chunks. The device index and backend are the camera peer's. +To switch to any ports by hand, write `target` (for example +`libby modify hsfibers.fiberswitchred.target 5,2`); it retracts, moves and +inserts like `inputgroup` does. ### `hspower` — power distribution diff --git a/docs/architecture/evolution.md b/docs/architecture/evolution.md index cf4240c..dbf09ab 100644 --- a/docs/architecture/evolution.md +++ b/docs/architecture/evolution.md @@ -160,8 +160,9 @@ Not yet on `main`, but shaping the next change: - **Ansible provisioning** (`pgupta/playbook`) — turning the host build notes into a playbook, so instrument hosts are provisioned reproducibly rather than by following an RST document by hand. -- **Fiber switcher driver** (`pgupta/add_fiber_drivers`) — the - `hispec-fiber-switcher` submodule is vendored but has no daemon yet. +- **Fiber switcher daemon** (`pgupta/plc_daemons`) — `hsfibers/fiberswitch`, + one instance per channel, drives the `hispec-fiber-switcher` PLC and the + `lightel` DI-2000 inspection camera, with per-port inspect-and-clean policy. ## Era 5 — the direction diff --git a/docs/architecture/overview.md b/docs/architecture/overview.md index ebe0b4d..c91446b 100644 --- a/docs/architecture/overview.md +++ b/docs/architecture/overview.md @@ -170,6 +170,7 @@ Every vendor driver is a git submodule from the | `ozoptics` | OZ Optics fiber components | | `pdu` | Eaton networked power distribution units | | `hispec-fiber-switcher` | HISPEC fiber switcher | +| `lightel` | Lightel DI-2000 fiber inspection camera | Keeping drivers as separate repositories is deliberate: the same controller models appear across COO instruments, and a driver fix benefits all of them diff --git a/docs/operations/systemd.md b/docs/operations/systemd.md index fbfc8a2..21404d9 100644 --- a/docs/operations/systemd.md +++ b/docs/operations/systemd.md @@ -460,6 +460,8 @@ Re-run `sudo /opt/hispec/app/systemd/install.sh` when the pull changed | `hsfei_piaadeploy` | `hispecserver` | `hsfei/xeryon` | `config/hsfei/hsfei_piaadeploy.yaml` | | `hsfei_yjfam` | `hispecserver` | `hsfei/xeryon` | `config/hsfei/hsfei_yjfam.yaml` | | `hsfibers_trunkfiberbox` | `hispecserver` | `hsfibers/trunkfiberbox` | `config/hsfibers/hsfibers_trunkfiberbox.yaml` | +| `hsfibers_fiberswitchred` | `hispecserver` | `hsfibers/fiberswitch` | `config/hsfibers/hsfibers_fiberswitchred.yaml` | +| `hsfibers_fiberswitchblue` | `hispecserver` | `hsfibers/fiberswitch` | `config/hsfibers/hsfibers_fiberswitchblue.yaml` | | `hispec_keygrabber` | `hispecserver` | `generic/keygrabber` | `config/hispec/hispec_keygrabber.yaml` | | `hspower_fei1` | `hispecserver` | `hspower/pdu` | `config/hspower/hspower_fei1.yaml` | | `hspower_fei2` | `hispecserver` | `hspower/pdu` | `config/hspower/hspower_fei2.yaml` | diff --git a/pyproject.toml b/pyproject.toml index 2dee34c..b04c091 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -71,6 +71,7 @@ ignored-modules = [ # Third-party dependencies, not installed in the lint environment "astropy", "camera_daemon", + "cv2", "hardware_device_base", "instrumentctl", "libby", @@ -90,6 +91,11 @@ ignored-modules = [ "SerialStream", # Device drivers vendored as git submodules "hispec.driver", + # Flat-module drivers (hispec-fiber-switcher, lightel), installed as + # workspace members under their own top-level names + "di2000", + "fiber_switcher", + "inspection", "PPC102_lib", ] diff --git a/src/hispec/driver/lightel b/src/hispec/driver/lightel new file mode 160000 index 0000000..2956af2 --- /dev/null +++ b/src/hispec/driver/lightel @@ -0,0 +1 @@ +Subproject commit 2956af2d78849f6aae6ff01ead64a7a86c76aa60 diff --git a/src/hispec/fiberswitch/__init__.py b/src/hispec/fiberswitch/__init__.py new file mode 100644 index 0000000..40599da --- /dev/null +++ b/src/hispec/fiberswitch/__init__.py @@ -0,0 +1,16 @@ +"""The fiber switcher daemon's logic: switch, inspection camera, controller. + +daemons/hsfibers/fiberswitch runs one instance per channel (red and blue), +each holding the three classes of the design: + +- MechanicalSwitch: the switcher PLC, through fiber_switcher.FiberSwitcher, + sequenced by switch.SwitchSequencer. +- MechanicalSwitchCamera: the Lightel DI-2000, through di2000.Di2000 and + lightel's inspection pipeline, wrapped by camera.InspectionCamera. +- MechanicalSwitchController: per-port connection policy, inspect-and-clean + and reference images, in controller.FiberSwitchController, which drives + the other two directly. + +Nothing here imports libby, cv2 or the drivers at module level, so the logic +can be tested with fakes. +""" diff --git a/src/hispec/fiberswitch/archive.py b/src/hispec/fiberswitch/archive.py new file mode 100644 index 0000000..60b0032 --- /dev/null +++ b/src/hispec/fiberswitch/archive.py @@ -0,0 +1,58 @@ +"""Archive of fiber-end inspection images: PNG plus a JSON sidecar. + +Each image is written under //_.png, with the +inspection metadata alongside it in a .json file of the same stem. An +annotated copy (fiber outline and defect boxes drawn on) can be written next +to it as _annotated.png. +""" + +from __future__ import annotations + +import json +import os +import re +from datetime import datetime, timezone +from pathlib import Path +from typing import Any, Callable, Dict, Optional + +_UNSAFE = re.compile(r"[^A-Za-z0-9_.-]+") + + +def _default_writer(path: str, image) -> bool: + import cv2 # pylint: disable=import-outside-toplevel + return bool(cv2.imwrite(path, image)) + + +def safe_tag(tag: str) -> str: + """A tag reduced to characters safe in a file name; 'image' when empty.""" + return _UNSAFE.sub("-", tag).strip("-.") or "image" + + +class ImageArchive: # pylint: disable=too-few-public-methods + """Write images and their metadata under a root directory.""" + + def __init__(self, root: str | os.PathLike, + write_image: Optional[Callable[[str, Any], bool]] = None, + now: Callable[[], datetime] = lambda: datetime.now(timezone.utc)) -> None: + self.root = Path(root).expanduser() + self._write_image = write_image or _default_writer + self._now = now + + def save(self, tag: str, image, metadata: Dict[str, Any], + annotated=None) -> Path: + """Write image (and annotated, if given) and its sidecar; return the PNG path.""" + stamp = self._now() + directory = self.root / stamp.strftime("%Y-%m-%d") + directory.mkdir(parents=True, exist_ok=True) + stem = f"{safe_tag(tag)}_{stamp.strftime('%Y%m%dT%H%M%S_%fZ')}" + path = directory / f"{stem}.png" + if not self._write_image(str(path), image): + raise OSError(f"could not write image to {path}") + record = {"tag": tag, "time": stamp.isoformat(), "image": str(path), **metadata} + if annotated is not None: + annotated_path = directory / f"{stem}_annotated.png" + if self._write_image(str(annotated_path), annotated): + record["annotated"] = str(annotated_path) + sidecar = path.with_suffix(".json") + sidecar.write_text(json.dumps(record, indent=2, default=str) + "\n", encoding="utf-8") + return path diff --git a/src/hispec/fiberswitch/camera.py b/src/hispec/fiberswitch/camera.py new file mode 100644 index 0000000..5fac449 --- /dev/null +++ b/src/hispec/fiberswitch/camera.py @@ -0,0 +1,141 @@ +"""Fiber-end inspection camera (MechanicalSwitchCamera). + +Wraps a connected di2000.Di2000 and the lightel inspection pipeline +(lightel's inspection module), and archives every capture with its metadata. +The pipeline is passed in rather than imported so this module, and its +tests, need neither OpenCV nor a camera. +""" + +from __future__ import annotations + +import threading +from typing import Any, Dict, Mapping, Optional + +from hispec.fiberswitch.jobs import JobAborted + + +def summarize(result) -> Dict[str, Any]: + """The JSON-safe parts of an inspection.InspectionResult.""" + region = result.region + illumination = result.illumination + autoexpose = result.autoexpose + return { + "kind": "inspection", + "passed": bool(result.passed), + "dirt": float(result.defect_area_fraction), + # OpenCV hands back numpy scalars, which json cannot encode + "defects": [{"cx": float(d.cx), "cy": float(d.cy), "area": float(d.area), + "bbox": [int(v) for v in d.bbox]} + for d in result.defects], + "region": _region(region), + "illumination": None if illumination is None else illumination.mode, + "focus": { + "score": float(result.autofocus.score), + "converged": bool(result.autofocus.converged), + "method": result.autofocus.method, + }, + "exposure": (None if autoexpose is None else { + "value": (None if autoexpose.exposure_value is None + else float(autoexpose.exposure_value)), + "converged": bool(autoexpose.converged), + "mean": float(autoexpose.quality.mean), + }), + } + + +class InspectionCamera: + """Refocus, capture and inspect with a DI-2000, archiving each image.""" + + def __init__(self, camera, archive, pipeline, # pylint: disable=too-many-arguments + properties: Mapping[str, int], + max_defect_area_fraction: float = 0.01) -> None: + self.camera = camera + self.archive = archive + self.pipeline = pipeline + self.properties = dict(properties) + self.max_defect_area_fraction = max_defect_area_fraction + self.last_focus: Optional[Dict[str, Any]] = None + self.last_record: Optional[Dict[str, Any]] = None + + def refocus(self, cancel: Optional[threading.Event] = None) -> Dict[str, Any]: + """Bring the fiber end into focus (vendor autofocus, else a sweep).""" + _check(cancel, "refocus") + result = self.pipeline.autofocus(self.camera) + self.last_focus = {"score": float(result.score), "converged": bool(result.converged), + "method": result.method, "value": result.focus_value} + return self.last_focus + + def capture(self, tag: str, cancel: Optional[threading.Event] = None) -> Dict[str, Any]: + """Grab one frame as-is and archive it with its focus score.""" + _check(cancel, "capture") + frame = self.camera.read_frame() + gray = self.pipeline.to_gray(frame) + region = self.pipeline.locate_fiber_end(gray) + metadata = { + "kind": "capture", + "focus": {"score": float(self.pipeline.focus_score(gray, region))}, + "region": _region(region), + "settings": self.settings(), + } + return self._archive(tag, frame, metadata) + + def inspect(self, tag: str, cancel: Optional[threading.Event] = None, + max_defect_area_fraction: Optional[float] = None) -> Dict[str, Any]: + """Focus, expose, look for dirt, and archive the frame and its annotation. + + max_defect_area_fraction sets the pass threshold; the camera's own + default when None. + """ + _check(cancel, "inspect") + max_dirt = (self.max_defect_area_fraction if max_defect_area_fraction is None + else max_defect_area_fraction) + result = self.pipeline.inspect(self.camera, max_defect_area_fraction=max_dirt) + metadata = summarize(result) + metadata["max_dirt"] = max_dirt + metadata["settings"] = self.settings() + annotated = self.pipeline.draw_result(result.frame, result) + return self._archive(tag, result.frame, metadata, annotated) + + def get_setting(self, name: str) -> float: + """Read one camera property by its short name.""" + return float(self.camera.get_property(self._property(name))) + + def set_setting(self, name: str, value: float) -> None: + """Write one camera property; raise if the camera rejects it.""" + if not self.camera.set_property(self._property(name), float(value)): + raise RuntimeError(f"camera rejected {name}={value}") + + def settings(self) -> Dict[str, Optional[float]]: + """Every known camera property, None where the camera does not report it.""" + values: Dict[str, Optional[float]] = {} + for name in self.properties: + try: + values[name] = self.get_setting(name) + except Exception: # pylint: disable=broad-exception-caught + values[name] = None + return values + + def _property(self, name: str) -> int: + try: + return self.properties[name] + except KeyError as e: + raise ValueError(f"unknown camera setting {name!r}; " + f"known: {sorted(self.properties)}") from e + + def _archive(self, tag: str, frame, metadata: Dict[str, Any], + annotated=None) -> Dict[str, Any]: + path = self.archive.save(tag, frame, metadata, annotated=annotated) + record = {"tag": tag, "image": str(path), **metadata} + self.last_record = record + return record + + +def _region(region) -> Optional[Dict[str, float]]: + if region is None: + return None + return {"cx": float(region.cx), "cy": float(region.cy), "r": float(region.r)} + + +def _check(cancel: Optional[threading.Event], what: str) -> None: + if cancel is not None and cancel.is_set(): + raise JobAborted(f"{what} aborted before it started") diff --git a/src/hispec/fiberswitch/camserver.py b/src/hispec/fiberswitch/camserver.py new file mode 100644 index 0000000..b04dacf --- /dev/null +++ b/src/hispec/fiberswitch/camserver.py @@ -0,0 +1,354 @@ +"""Serve a Lightel DI-2000 as a libby peer, for a daemon on another machine. + +The DI-2000 is a USB camera, so it can only be opened on the machine it is +plugged into. Run one camera peer per camera there: + + python camserver.py --device 0 --peer-id fiberswitchbluecam + +and point the fiber switch daemon at it with +``camera.remote: hsfibers.fiberswitchbluecam``. The daemon then uses +RemoteDi2000, which has the methods of di2000.Di2000 that the daemon and +lightel's inspection pipeline call, as libby RPCs through the broker: + + cam.status {connected, device_index} + cam.connect open the camera (on its --device) + cam.disconnect release it + cam.frame grab a frame and return its first chunk; {id, offset} + for the rest (see below) + cam.getproperty {id} -> {value} + cam.setproperty {id, value} -> {applied} + cam.autofocus trigger the vendor autofocus + cam.autofocusstatus -> {status} + +libby limits a message to 512 KB and a frame is about 900 KB, so a frame is +sent as a zlib-compressed .npy (lossless) in base64 chunks of FRAME_CHUNK +bytes: the first cam.frame call grabs the frame and returns its id, total +size and first chunk; later calls with that id and an offset return the rest. + +This file needs only libby, numpy and lightel's di2000 module, not the hispec +package, so it can be copied to the camera machine on its own. Broker +credentials come from HISPEC_RABBITMQ_USER and HISPEC_RABBITMQ_PASSWORD, +as for the HISPEC daemons; keep them in an environment file, not on the +command line. +""" + +from __future__ import annotations + +import argparse +import base64 +import io +import logging +import os +import threading +import time +import zlib +from typing import Any, Callable, Dict, Optional +from urllib.parse import quote + +import numpy as np + +# Raw bytes per frame chunk: base64 makes it 4/3 larger, which leaves room +# for the envelope inside libby's 512 KB message limit +FRAME_CHUNK = 256 * 1024 + +# The DI-2000 drops the odd control request (EPROTO) while its autofocus +# finishes; retry an autofocus status read this many times, this far apart +STATUS_RETRIES = 3 +STATUS_RETRY_S = 0.3 + +# Mirrors hispec.daemon.broker_url, which this file cannot import +BROKER_HOST = "hispec.caltech.edu" + +logger = logging.getLogger(__name__) + + +def broker_url() -> str: + """The shared broker URL, with credentials from the environment.""" + user = os.environ.get("HISPEC_RABBITMQ_USER") + password = os.environ.get("HISPEC_RABBITMQ_PASSWORD") + host = os.environ.get("HISPEC_BROKER_HOST") or BROKER_HOST + if not (user and password): + return f"amqp://{host}" + return f"amqp://{quote(user, safe='')}:{quote(password, safe='')}@{host}" + + +def encode_frame(frame) -> bytes: + """A frame as a zlib-compressed .npy.""" + buffer = io.BytesIO() + np.save(buffer, np.asarray(frame), allow_pickle=False) + return zlib.compress(buffer.getvalue(), 1) + + +def decode_frame(data: bytes) -> np.ndarray: + """The inverse of encode_frame.""" + return np.load(io.BytesIO(zlib.decompress(data)), allow_pickle=False) + + +class CameraService: + """The cam.* RPC handlers around one camera driver (di2000.Di2000 or a stand-in). + + libby runs a peer's handlers one at a time, so the camera is never used + by two requests at once. + """ + + def __init__(self, driver, open_camera: Callable[[], None]) -> None: + """open_camera opens driver on its configured device.""" + self.driver = driver + self.open_camera = open_camera + self._frame_id = 0 + self._frame = b"" + + def handlers(self) -> Dict[str, Callable[[Dict[str, Any]], Dict[str, Any]]]: + """Service key -> handler, for LibbyDaemon.add_services.""" + return { + "cam.status": self.status, + "cam.connect": self.connect, + "cam.disconnect": self.disconnect, + "cam.frame": self.frame, + "cam.getproperty": self.get_property, + "cam.setproperty": self.set_property, + "cam.autofocus": self.autofocus, + "cam.autofocusstatus": self.autofocus_status, + } + + def status(self, _payload): + """Whether the camera is open, and which device.""" + return {"ok": True, "connected": bool(self.driver.is_connected()), + "device_index": getattr(self.driver, "device_index", None)} + + def connect(self, _payload): + """Open the camera if it is not open.""" + if not self.driver.is_connected(): + self.open_camera() + return {"ok": True} + + def disconnect(self, _payload): + """Release the camera.""" + self.driver.disconnect() + return {"ok": True} + + def frame(self, payload): + """Grab a frame (no id) or continue sending one (id and offset).""" + if payload.get("id") is None: + self._frame = encode_frame(self.driver.read_frame()) + self._frame_id += 1 + offset = 0 + else: + if int(payload["id"]) != self._frame_id: + raise RuntimeError(f"frame {payload['id']} is gone; the latest is " + f"{self._frame_id}") + offset = int(payload.get("offset", 0)) + chunk = self._frame[offset:offset + FRAME_CHUNK] + return {"ok": True, "id": self._frame_id, "size": len(self._frame), + "offset": offset, "data": base64.b64encode(chunk).decode("ascii")} + + def get_property(self, payload): + """Read an OpenCV/UVC property.""" + return {"ok": True, "value": float(self.driver.get_property(int(payload["id"])))} + + def set_property(self, payload): + """Set an OpenCV/UVC property; applied is False if the camera refused.""" + applied = bool(self.driver.set_property(int(payload["id"]), float(payload["value"]))) + return {"ok": True, "applied": applied} + + def autofocus(self, _payload): + """Start the probe's onboard autofocus (Linux only).""" + self.driver.trigger_autofocus() + return {"ok": True} + + def autofocus_status(self, _payload): + """Onboard autofocus progress, as di2000.AutofocusStatus values. + + Retries a failed read: the camera does not answer the odd one while + it finishes focusing, and lightel's autofocus gives up on the first + error. + """ + for attempt in range(STATUS_RETRIES): + try: + return {"ok": True, "status": int(self.driver.get_autofocus_status())} + except OSError as e: + if attempt == STATUS_RETRIES - 1: + raise + logger.info("autofocus status read failed (%s); retrying", e) + time.sleep(STATUS_RETRY_S) + raise AssertionError("unreachable") + + +class RemoteCameraError(RuntimeError): + """The camera peer answered with an error, or did not answer.""" + + +class RemoteDi2000: + """A DI-2000 served by a camera peer on another machine. + + Has the di2000.Di2000 methods the fiber switch daemon and lightel's + inspection pipeline use. It makes its calls on its own libby connection, + built by make_libby on first use: the daemon's own connection runs keyword + handlers on one thread, so a handler waiting there for the camera's reply + would never see it. + """ + + def __init__(self, peer: str, make_libby: Callable[[], Any], timeout_s: float = 10.0, + status_timeout_s: float = 2.0) -> None: + if peer.count(".") != 1: + raise ValueError(f"camera.remote must be ., got {peer!r}") + self.peer = peer.lower() + self.timeout_s = timeout_s + self.status_timeout_s = status_timeout_s + self.device_index: Optional[int] = None + self._make_libby = make_libby + self._libby = None + self._lock = threading.Lock() + + def is_connected(self) -> bool: + """The camera peer answers and its camera is open.""" + try: + status = self._call("cam.status", timeout=self.status_timeout_s) + except RemoteCameraError: + return False + self.device_index = status.get("device_index") + return bool(status.get("connected")) + + def connect(self, *_args, **_kwargs) -> None: + """Ask the peer to open its camera; device arguments are the peer's.""" + try: + self._call("cam.connect") + except RemoteCameraError as e: + raise ConnectionError(f"camera peer {self.peer}: {e}") from e + + def disconnect(self) -> None: + """Ask the peer to release its camera; quiet if the peer is down.""" + try: + self._call("cam.disconnect", timeout=self.status_timeout_s) + except RemoteCameraError as e: + logger.warning("camera peer %s: disconnect failed: %s", self.peer, e) + + def close(self) -> None: + """Close this client's own libby connection.""" + with self._lock: + libby, self._libby = self._libby, None + if libby is not None: + libby.stop() + + def read_frame(self) -> np.ndarray: + """One frame from the camera.""" + reply = self._call("cam.frame") + frame_id, size = reply["id"], int(reply["size"]) + data = bytearray(base64.b64decode(reply["data"])) + while len(data) < size: + reply = self._call("cam.frame", {"id": frame_id, "offset": len(data)}) + chunk = base64.b64decode(reply["data"]) + if not chunk: + raise RemoteCameraError(f"frame {frame_id} ended at {len(data)} of {size} bytes") + data += chunk + return decode_frame(bytes(data)) + + def get_property(self, property_id: int) -> float: + """Read an OpenCV/UVC property.""" + return float(self._call("cam.getproperty", {"id": int(property_id)})["value"]) + + def set_property(self, property_id: int, value: float) -> bool: + """Set an OpenCV/UVC property; False if the camera refused it.""" + return bool(self._call("cam.setproperty", + {"id": int(property_id), "value": float(value)})["applied"]) + + def trigger_autofocus(self) -> None: + """Start the probe's onboard autofocus (Linux only, on the peer).""" + self._call("cam.autofocus") + + def get_autofocus_status(self) -> int: + """Onboard autofocus progress, as di2000.AutofocusStatus values.""" + return int(self._call("cam.autofocusstatus")["status"]) + + def _connection(self): + with self._lock: + if self._libby is None: + try: + self._libby = self._make_libby() + except Exception as e: # pylint: disable=broad-exception-caught + raise RemoteCameraError(f"cannot reach the broker: {e}") from e + return self._libby + + def _call(self, key: str, payload: Optional[dict] = None, + timeout: Optional[float] = None) -> Dict[str, Any]: + timeout = timeout or self.timeout_s + # libby waits the whole ttl for the request's ACK, then half of it for + # the reply, so double it to give the reply the full timeout + envelope = self._connection().rpc(self.peer, key, payload or {}, + ttl_ms=int(timeout * 2000)) + status = envelope.get("status") if isinstance(envelope, dict) else None + if status == "timeout": + raise RemoteCameraError(f"{self.peer} did not answer {key}") + if status == "too_large": + raise RemoteCameraError(f"{key} request too large") + resp = envelope.get("resp") if isinstance(envelope, dict) else None + if not isinstance(resp, dict): + raise RemoteCameraError(f"no reply from {self.peer} to {key}") + if not resp.get("ok"): + raise RemoteCameraError(resp.get("error", f"{key} failed")) + return resp + + +def make_daemon_class(): + """The camera peer's LibbyDaemon subclass (imports libby only when run).""" + from libby.daemon import LibbyDaemon # pylint: disable=import-outside-toplevel + + class CameraPeer(LibbyDaemon): + """Serves one DI-2000's cam.* RPCs.""" + + transport = "rabbitmq" + discovery_enabled = False + + def __init__(self) -> None: + super().__init__() + self.driver = None + self.open_camera = None + + def config_rabbitmq_url(self) -> str: + return self.rabbitmq_url or broker_url() + + def on_start(self) -> None: + service = CameraService(self.driver, self.open_camera) + self.add_services(service.handlers()) + try: + self.open_camera() + except ConnectionError as e: + self.logger.error("%s; serving anyway, cam.connect retries", e) + + def on_stop(self) -> None: + self.driver.disconnect() + + return CameraPeer + + +def main(argv=None) -> None: + """Serve one DI-2000 until interrupted.""" + parser = argparse.ArgumentParser(description=__doc__.split("\n\n", 1)[0]) + parser.add_argument("--peer-id", required=True, + help="libby peer id, e.g. fiberswitchbluecam") + parser.add_argument("--group", default="hsfibers", help="libby group id") + parser.add_argument("--device", type=int, default=0, help="camera index (/dev/videoN)") + parser.add_argument("--backend", default="V4L2", + help="OpenCV backend name (V4L2, DSHOW, ...); 'any' for the default") + parser.add_argument("--width", type=int) + parser.add_argument("--height", type=int) + parser.add_argument("-v", "--verbose", action="store_true") + args = parser.parse_args(argv) + logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO, + format="%(asctime)s %(name)s %(levelname)s %(message)s") + + import cv2 # pylint: disable=import-outside-toplevel + from di2000 import Di2000 # pylint: disable=import-outside-toplevel + + backend = None if args.backend.lower() == "any" else getattr( + cv2, f"CAP_{args.backend.upper()}") + peer = make_daemon_class().from_config({"peer_id": args.peer_id, "group_id": args.group}) + peer.driver = Di2000(log=True) + peer.open_camera = lambda: peer.driver.connect(args.device, backend=backend, + width=args.width, height=args.height) + logger.info("Serving DI-2000 %d as %s.%s", args.device, args.group, args.peer_id) + peer.serve() + + +if __name__ == "__main__": + main() diff --git a/src/hispec/fiberswitch/controller.py b/src/hispec/fiberswitch/controller.py new file mode 100644 index 0000000..d6b5cc7 --- /dev/null +++ b/src/hispec/fiberswitch/controller.py @@ -0,0 +1,149 @@ +"""Fiber switcher controller (MechanicalSwitchController). + +Connects input groups to the spectrograph per each port's ConnectionPolicy: +inspecting the fiber end with the camera, cleaning it when it is dirty, +archiving every image and raising an alert when cleaning does not help. + +It drives the switch (SwitchSequencer) and the camera (InspectionCamera) of +the same daemon directly; every method runs on the daemon's job thread and +takes the job's cancel event. +""" + +from __future__ import annotations + +import threading +import time +from datetime import datetime, timezone +from typing import Any, Callable, Dict, Mapping, Optional + +from hispec.fiberswitch.policy import ConnectionPolicy, ControllerState +from hispec.fiberswitch.switch import Position + +REFERENCE_SUFFIX = "-reference" + + +class FiberSwitchController: # pylint: disable=too-many-instance-attributes + """Select inputs, inspect and clean fiber ends, take reference images.""" + + # pylint: disable=too-many-arguments + def __init__(self, switch, camera, state: ControllerState, *, + input_groups: Mapping[str, Position], + inspection_positions: Mapping[str, Position], + logger=None, clock: Callable[[], float] = time.time) -> None: + self.switch = switch + self.camera = camera + self.state = state + self.input_groups = dict(input_groups) + self.inspection_positions = dict(inspection_positions) + self.logger = logger + self._clock = clock + + def select_input(self, name: str, cancel: Optional[threading.Event] = None) -> None: + """Connect name to the spectrograph, cleaning and inspecting first per policy.""" + policy = self.state.policy(name) + history = self.state.history(name) + if policy.clean_interval_h is not None and \ + self._due(history.get("last_clean"), policy.clean_interval_h): + self._log("info", "%s: clean due (every %g h)", name, policy.clean_interval_h) + self._clean(name, policy, cancel) + if self._inspect_due(policy, history): + self.inspect_and_clean(name, cancel) + self._log("info", "connecting %s", name) + self.switch.connect(self.input_groups[name], cancel) + self.state.set_selected(name) + + def inspect_and_clean(self, name: str, + cancel: Optional[threading.Event] = None) -> Dict[str, Any]: + """Inspect name's fiber end, clean while dirty, alert if it stays dirty. + + Leaves the fiber inserted at its inspection position; select an input + to reconnect it. + """ + policy = self._inspectable(name) + self._apply_camera_settings(policy) + cleans = 0 + while True: + record = self._inspect(name, policy, name, cancel) + found = record.get("region") is not None + if found and record["dirt"] <= policy.clean_threshold: + break + if not found or not policy.clean_on_dirty or cleans >= policy.max_clean_attempts: + break + self._log("info", "%s: dirt %.4f above %.4f, cleaning (%d of %d)", name, + record["dirt"], policy.clean_threshold, cleans + 1, + policy.max_clean_attempts) + self._clean(name, policy, cancel) + cleans += 1 + + if record.get("region") is None: + self._alert(name, f"fiber end not found in {record['image']}") + elif record["dirt"] > policy.alert_threshold: + self._alert(name, f"dirt {record['dirt']:.4f} above alert threshold " + f"{policy.alert_threshold:.4f} after {cleans} clean(s); " + f"see {record['image']}") + return record + + def take_reference(self, name: str, + cancel: Optional[threading.Event] = None) -> Dict[str, Any]: + """Inspect name's fiber end and keep the image as its reference.""" + policy = self._inspectable(name) + self._apply_camera_settings(policy) + record = self._inspect(name, policy, f"{name}{REFERENCE_SUFFIX}", cancel, + reference=True) + self.state.record(name, reference_image=record["image"]) + self._log("info", "%s: reference image %s", name, record["image"]) + return record + + # Internals + + def _inspect(self, name: str, policy: ConnectionPolicy, tag: str, + cancel: Optional[threading.Event], reference: bool = False) -> Dict[str, Any]: + self.switch.connect(self.inspection_positions[policy.inspect_position], cancel) + record = self.camera.inspect(tag, cancel, + max_defect_area_fraction=policy.alert_threshold) + if not reference: + self.state.record(name, last_inspect=self._clock(), last_image=record["image"], + last_dirt=record["dirt"], + last_passed=record.get("region") is not None + and record["dirt"] <= policy.alert_threshold) + self._log("info", "%s: inspected, dirt %.4f, image %s", name, record["dirt"], + record["image"]) + return record + + def _clean(self, name: str, policy: ConnectionPolicy, + cancel: Optional[threading.Event]) -> None: + self.switch.clean(policy.clean_mode, cancel) + self.state.record(name, last_clean=self._clock()) + + def _apply_camera_settings(self, policy: ConnectionPolicy) -> None: + for setting, value in policy.camera.items(): + self.camera.set_setting(setting, value) + + def _inspectable(self, name: str) -> ConnectionPolicy: + policy = self.state.policy(name) + if not policy.inspect_position: + raise ValueError(f"{name} has no inspect_position in its policy") + if policy.inspect_position not in self.inspection_positions: + raise ValueError(f"{name}: unknown inspection position " + f"{policy.inspect_position!r}") + return policy + + def _inspect_due(self, policy: ConnectionPolicy, history: Dict[str, Any]) -> bool: + if policy.inspect == "always": + return True + if policy.inspect == "interval": + return self._due(history.get("last_inspect"), policy.inspect_interval_h) + return False + + def _due(self, last: Optional[float], interval_h: float) -> bool: + return last is None or self._clock() - float(last) >= interval_h * 3600.0 + + def _alert(self, name: str, message: str) -> None: + stamp = datetime.fromtimestamp(self._clock(), timezone.utc).isoformat(timespec="seconds") + self.state.set_alert(f"{stamp} {name}: {message}") + self.state.record(name, last_passed=False) + self._log("error", "ALERT %s: %s", name, message) + + def _log(self, level: str, msg: str, *args) -> None: + if self.logger is not None: + getattr(self.logger, level)(msg, *args) diff --git a/src/hispec/fiberswitch/jobs.py b/src/hispec/fiberswitch/jobs.py new file mode 100644 index 0000000..07fe387 --- /dev/null +++ b/src/hispec/fiberswitch/jobs.py @@ -0,0 +1,124 @@ +"""One background job at a time, with keywords to watch it. + +libby runs every keyword handler on the transport's single receive thread, so +a setter that blocks for a move or an inspection also blocks every other +keyword on that daemon, including the one that would halt it. The daemons in +this package therefore validate a command in its setter, start it here and +return; clients poll `busy` and read `jobstatus` when it clears. +""" + +from __future__ import annotations + +import threading +from typing import Callable, Optional + +IDLE = "idle" +RUNNING = "running" +OK = "ok" +ABORTED = "aborted" +ERROR_PREFIX = "error: " + + +class JobAborted(RuntimeError): + """Raised inside a job that saw its cancel event set.""" + + +class JobRunner: + """Run one labelled job at a time on a worker thread.""" + + def __init__(self, logger=None) -> None: + self.logger = logger + self.cancel = threading.Event() + self._lock = threading.Lock() + self._thread: Optional[threading.Thread] = None + self._label = "" + self._status = IDLE + self._job_id = 0 + + @property + def busy(self) -> bool: + """True while a job is running.""" + with self._lock: + return self._thread is not None and self._thread.is_alive() + + @property + def state(self) -> str: + """The running job's label, else idle.""" + with self._lock: + running = self._thread is not None and self._thread.is_alive() + return self._label if running else IDLE + + @property + def status(self) -> str: + """Outcome of the latest job: running, ok, aborted or 'error: ...'.""" + with self._lock: + return self._status + + @property + def job_id(self) -> int: + """Number of the latest job started; 0 before the first.""" + with self._lock: + return self._job_id + + def start(self, label: str, fn: Callable[[threading.Event], None]) -> int: + """Run fn(cancel_event) on a worker thread; refuse while one is running. + + Returns the new job's id. + """ + with self._lock: + if self._thread is not None and self._thread.is_alive(): + raise RuntimeError(f"busy with {self._label}; wait or abort it first") + self.cancel.clear() + self._job_id += 1 + self._label = label + self._status = RUNNING + self._thread = threading.Thread(target=self._run, args=(label, fn), + name=f"job-{label}", daemon=True) + self._thread.start() + return self._job_id + + def abort(self) -> None: + """Ask the running job to stop at its next check.""" + self.cancel.set() + + def wait(self, timeout: Optional[float] = None) -> bool: + """Wait for the running job to finish; True if none is left running.""" + with self._lock: + thread = self._thread + if thread is None: + return True + thread.join(timeout) + return not thread.is_alive() + + def _run(self, label: str, fn: Callable[[threading.Event], None]) -> None: + try: + fn(self.cancel) + status = OK + except JobAborted as e: + status = ABORTED + self._log("warning", "%s aborted: %s", label, e) + except Exception as e: # pylint: disable=broad-exception-caught + status = f"{ERROR_PREFIX}{e}" + self._log("error", "%s failed: %s", label, e) + with self._lock: + self._status = status + + def _log(self, level: str, msg: str, *args) -> None: + if self.logger is not None: + getattr(self.logger, level)(msg, *args) + + +def register_job_keywords(registry, runner: JobRunner, *, abort_action=None, + abort_name: str = "abort") -> None: + """Register busy, state, jobid, jobstatus and the abort trigger.""" + registry.bool("busy", getter=lambda: runner.busy, + description="A command is running; poll until false, then read jobstatus.") + registry.string("state", getter=lambda: runner.state, + description="Label of the running command, else idle.") + registry.int("jobid", getter=lambda: runner.job_id, + description="Number of the latest command started.") + registry.string("jobstatus", getter=lambda: runner.status, + description="Outcome of the latest command: running, ok, aborted " + "or 'error: '.") + registry.trigger(abort_name, action=abort_action or runner.abort, + description="Stop the running command at its next safe point.") diff --git a/src/hispec/fiberswitch/policy.py b/src/hispec/fiberswitch/policy.py new file mode 100644 index 0000000..9fa781c --- /dev/null +++ b/src/hispec/fiberswitch/policy.py @@ -0,0 +1,199 @@ +"""Per-port connection policy and the controller's persisted state. + +A ConnectionPolicy says, for one input group, when to inspect and clean its +fiber end before connecting it, the dirt thresholds, and the camera settings +to inspect with. Defaults come from the config; changes made at runtime +through the policy keywords are saved to a JSON state file so they survive a +restart, along with each port's inspection history and reference image. +""" + +from __future__ import annotations + +import json +import os +import tempfile +import threading +from dataclasses import asdict, dataclass, field, fields, replace +from pathlib import Path +from typing import Any, Dict, Mapping, Optional + +from hispec.fiberswitch.switch import CLEAN_MODES + +INSPECT_MODES = ("never", "always", "interval") + + +@dataclass(frozen=True) +class ConnectionPolicy: # pylint: disable=too-many-instance-attributes + """When and how to inspect and clean one port before connecting it.""" + + # Inspection position (from the switch config) that images this port's fiber end + inspect_position: Optional[str] = None + # never, always (on every select), or interval (once per inspect_interval_h) + inspect: str = "never" + inspect_interval_h: float = 24.0 + # Clean on select once this many hours have passed since the last clean; + # null to clean only when an inspection finds dirt + clean_interval_h: Optional[float] = None + clean_mode: str = "both" + clean_on_dirty: bool = True + max_clean_attempts: int = 2 + # Dirt as a fraction of the fiber-end area: clean above clean_threshold, + # alert if still above alert_threshold once cleaning attempts run out + clean_threshold: float = 0.005 + alert_threshold: float = 0.01 + # Camera settings applied before inspecting, by name (exposure, gain, ...) + camera: Dict[str, float] = field(default_factory=dict) + + def validate(self) -> None: + """Raise ValueError if a field is out of range.""" + if self.inspect not in INSPECT_MODES: + raise ValueError(f"inspect must be one of {INSPECT_MODES}, not {self.inspect!r}") + if self.clean_mode not in CLEAN_MODES: + raise ValueError(f"clean_mode must be one of {CLEAN_MODES}, not {self.clean_mode!r}") + if self.inspect_interval_h <= 0: + raise ValueError("inspect_interval_h must be positive") + if self.clean_interval_h is not None and self.clean_interval_h <= 0: + raise ValueError("clean_interval_h must be positive or null") + if self.max_clean_attempts < 0: + raise ValueError("max_clean_attempts must be zero or more") + for name in ("clean_threshold", "alert_threshold"): + if not 0.0 <= getattr(self, name) <= 1.0: + raise ValueError(f"{name} must be a fraction between 0 and 1") + if self.inspect != "never" and not self.inspect_position: + raise ValueError("inspect_position is required unless inspect is never") + if not isinstance(self.camera, dict): + raise ValueError("camera must be a mapping of setting name to value") + + def updated(self, changes: Mapping[str, Any]) -> "ConnectionPolicy": + """A copy with changes applied and validated; unknown fields are an error.""" + known = {f.name for f in fields(self)} + unknown = set(changes) - known + if unknown: + raise ValueError(f"unknown policy fields {sorted(unknown)}; known: {sorted(known)}") + policy = replace(self, **_coerce(dict(changes))) + policy.validate() + return policy + + def to_dict(self) -> Dict[str, Any]: + """The policy as a JSON-safe dict.""" + return asdict(self) + + +def _coerce(changes: Dict[str, Any]) -> Dict[str, Any]: + """Cast the numeric fields, since JSON and YAML may give ints for floats.""" + for name in ("inspect_interval_h", "clean_threshold", "alert_threshold"): + if name in changes: + changes[name] = float(changes[name]) + if changes.get("clean_interval_h") is not None: + changes["clean_interval_h"] = float(changes["clean_interval_h"]) + if "max_clean_attempts" in changes: + changes["max_clean_attempts"] = int(changes["max_clean_attempts"]) + if "camera" in changes: + changes["camera"] = {str(k): float(v) for k, v in (changes["camera"] or {}).items()} + return changes + + +class ControllerState: # pylint: disable=too-many-instance-attributes + """Policies and per-port history, saved to a JSON file on every change. + + Each port's history holds last_inspect and last_clean (epoch seconds), + last_image, last_dirt, last_passed and reference_image. + """ + + def __init__(self, path: Optional[str | os.PathLike], + defaults: Mapping[str, Any], inputs: Mapping[str, Mapping[str, Any]]) -> None: + self.path = Path(path).expanduser() if path else None + self._lock = threading.RLock() + base = ConnectionPolicy().updated(defaults or {}) + self._configured = {name: base.updated(overrides or {}) + for name, overrides in inputs.items()} + self._overrides: Dict[str, Dict[str, Any]] = {} + self._history: Dict[str, Dict[str, Any]] = {name: {} for name in inputs} + self.alert = "" + self.selected = "" + # Saved overrides ignored on load, as ": ", for the daemon to log + self.dropped: list[str] = [] + self._load() + + @property + def inputs(self): + """The configured input group names.""" + return list(self._configured) + + def policy(self, name: str) -> ConnectionPolicy: + """The effective policy: config, then any runtime overrides.""" + with self._lock: + return self._configured[name].updated(self._overrides.get(name, {})) + + def update_policy(self, name: str, changes: Mapping[str, Any]) -> ConnectionPolicy: + """Apply and save runtime overrides; an empty mapping resets to the config.""" + with self._lock: + if not changes: + self._overrides.pop(name, None) + else: + merged = {**self._overrides.get(name, {}), **changes} + self._configured[name].updated(merged) # validate before keeping + self._overrides[name] = merged + self._save() + return self.policy(name) + + def history(self, name: str) -> Dict[str, Any]: + """A copy of one port's history.""" + with self._lock: + return dict(self._history[name]) + + def record(self, name: str, **values: Any) -> None: + """Update and save one port's history.""" + with self._lock: + self._history[name].update(values) + self._save() + + def set_alert(self, alert: str) -> None: + """Set (or clear, with '') the standing alert and save it.""" + with self._lock: + self.alert = alert + self._save() + + def set_selected(self, name: str) -> None: + """Record the input group last connected, and save it.""" + with self._lock: + self.selected = name + self._save() + + def _load(self) -> None: + if self.path is None or not self.path.exists(): + return + data = json.loads(self.path.read_text(encoding="utf-8")) + for name, overrides in (data.get("policies") or {}).items(): + if name not in self._configured: + self.dropped.append(f"{name}: no longer a configured input group") + continue + try: + self._configured[name].updated(overrides) + except (TypeError, ValueError) as e: + # The config changed under a saved override; keep the config's + self.dropped.append(f"{name}: {e}") + continue + self._overrides[name] = overrides + for name, history in (data.get("history") or {}).items(): + if name in self._history: + self._history[name].update(history) + self.alert = data.get("alert", "") + self.selected = data.get("selected", "") + + def _save(self) -> None: + if self.path is None: + return + data = {"policies": self._overrides, "history": self._history, + "alert": self.alert, "selected": self.selected} + self.path.parent.mkdir(parents=True, exist_ok=True) + # Write then rename, so a crash mid-write cannot leave a truncated file + fd, tmp = tempfile.mkstemp(dir=self.path.parent, prefix=f".{self.path.name}.") + try: + with os.fdopen(fd, "w", encoding="utf-8") as f: + json.dump(data, f, indent=2, sort_keys=True) + f.write("\n") + os.replace(tmp, self.path) + except BaseException: + Path(tmp).unlink(missing_ok=True) + raise diff --git a/src/hispec/fiberswitch/switch.py b/src/hispec/fiberswitch/switch.py new file mode 100644 index 0000000..79a05c1 --- /dev/null +++ b/src/hispec/fiberswitch/switch.py @@ -0,0 +1,315 @@ +"""Sequencing for the fiber switcher PLC (MechanicalSwitch). + +fiber_switcher.FiberSwitcher sends each PLC command and returns at once; the +only way to know a move or an insertion has finished is to poll the PLC's +status words. SwitchSequencer turns those commands into operations that wait +for the PLC to report completion, stop on an alarm, and can be cancelled. + +Positions are named in the config: input groups are where the spectrograph +fibers connect for science, inspection positions put a fiber end in front of +the Lightel camera. Both are a (Port A, Port B) pair. +""" + +from __future__ import annotations + +import threading +import time +from dataclasses import dataclass +from typing import Any, Callable, Dict, Optional, Tuple + +from hispec.fiberswitch.jobs import JobAborted + +# fiber_switcher's insertion_state codes +INSERTION_REMOVED = 1 +INSERTION_INSERTED = 3 +INSERTION_NAMES = {INSERTION_REMOVED: "removed", INSERTION_INSERTED: "inserted"} + +# messages_0_15 bit: the PLC is in automatic operation, so it accepts +# insert, retract and move commands +MSG_AUTOMATIC_OPERATION_AVAILABLE = 0x0004 + +PORT_A_RANGE = (1, 13) +PORT_B_RANGE = (1, 5) + +CLEAN_MODES = ("inside", "outside", "both") + +# The mm registers the daemon exposes, keyed by keyword suffix +AXIS_SETTINGS = { + "retractdist": ("get_retract_distance_mm", "set_retract_distance_mm", + "Insertion axis retract-back distance."), + "caminsertpos": ("get_camera_insertion_position_mm", + "set_camera_insertion_position_mm", + "Insertion position used at camera (inspection) ports."), + "noncaminsertpos": ("get_noncamera_insertion_position_mm", + "set_noncamera_insertion_position_mm", + "Insertion position used at non-camera ports."), +} + + +class SwitchError(RuntimeError): + """The switcher refused a command, raised an alarm or timed out.""" + + +@dataclass(frozen=True) +class Position: + """A named (Port A, Port B) pair.""" + + name: str + port_a: int + port_b: int + + @property + def ports(self) -> Tuple[int, int]: + """The (Port A, Port B) pair.""" + return (self.port_a, self.port_b) + + +def parse_positions(spec: Optional[Dict[str, Any]], kind: str) -> Dict[str, Position]: + """Build named positions from a config mapping of name -> {port_a, port_b}.""" + positions = {} + for name, entry in (spec or {}).items(): + if not isinstance(entry, dict): + raise ValueError(f"{kind} {name!r}: expected a mapping with port_a and port_b") + try: + port_a, port_b = int(entry["port_a"]), int(entry["port_b"]) + except (KeyError, TypeError, ValueError) as e: + raise ValueError(f"{kind} {name!r}: port_a and port_b must be integers") from e + _check_port("A", port_a, PORT_A_RANGE, f"{kind} {name!r}") + _check_port("B", port_b, PORT_B_RANGE, f"{kind} {name!r}") + positions[str(name)] = Position(str(name), port_a, port_b) + return positions + + +def parse_target(value: str) -> Position: + """A Port A, Port B pair typed by hand: "5,2", "5 2" or "A5 B2".""" + parts = str(value).replace(",", " ").split() + try: + if len(parts) != 2: + raise ValueError + port_a = int(parts[0][1:] if parts[0][:1] in "Aa" else parts[0]) + port_b = int(parts[1][1:] if parts[1][:1] in "Bb" else parts[1]) + except ValueError as e: + raise ValueError(f"expected Port A and Port B, like '5,2'; got {value!r}") from e + _check_port("A", port_a, PORT_A_RANGE, "target") + _check_port("B", port_b, PORT_B_RANGE, "target") + return Position(f"A{port_a}B{port_b}", port_a, port_b) + + +def _check_port(port: str, value: int, limits: Tuple[int, int], where: str) -> None: + if not limits[0] <= value <= limits[1]: + raise ValueError(f"{where}: Port {port} position {value} outside " + f"{limits[0]}-{limits[1]}") + + +def alarm_word(status) -> int: + """Both alarm words as one 32-bit value.""" + return status.alarms_0_15 | (status.alarms_16_31 << 16) + + +def message_word(status) -> int: + """Both message words as one 32-bit value.""" + return status.messages_0_15 | (status.messages_16_31 << 16) + + +def ports_of(status) -> Tuple[int, int]: + """The (Port A, Port B) target positions in a status reply.""" + return (status.port_a_target - 100, status.port_b_target - 100) + + +# pylint: disable=too-many-instance-attributes +class SwitchSequencer: + """Blocking, cancellable operations on a FiberSwitcher. + + Every operation polls the PLC until it reports the expected state, and + raises SwitchError on an alarm or a timeout. Pass the job's cancel event + to make an operation stop, and the switcher halt, when it is set. + """ + + # pylint: disable=too-many-arguments + def __init__(self, switcher, *, move_timeout_s: float = 30.0, + insert_timeout_s: float = 15.0, clean_timeout_s: float = 180.0, + home_timeout_s: float = 60.0, start_s: float = 2.0, + poll_s: float = 0.2, status_ttl_s: float = 0.5, + require_auto: bool = True, + clock: Callable[[], float] = time.monotonic, + sleep: Callable[[float], None] = time.sleep) -> None: + self.switcher = switcher + self.move_timeout_s = move_timeout_s + self.insert_timeout_s = insert_timeout_s + self.clean_timeout_s = clean_timeout_s + self.home_timeout_s = home_timeout_s + self.start_s = start_s + self.poll_s = poll_s + self.status_ttl_s = status_ttl_s + self.require_auto = require_auto + self._clock = clock + self._sleep = sleep + self._cache_lock = threading.Lock() + self._cached: Tuple[Optional[float], Any] = (None, None) + self.last_clean_mode: Optional[str] = None + self.air_purge: Optional[bool] = None + # Whether this process has seen a return to origin complete; the PLC + # does not report it, so it reads false after a daemon restart + self.homed = False + + # Status + + def status(self, fresh: bool = False): + """Return the PLC status, reusing one younger than status_ttl_s.""" + with self._cache_lock: + fetched_at, value = self._cached + now = self._clock() + if not fresh and fetched_at is not None and now - fetched_at < self.status_ttl_s: + return value + value = self.switcher.read_status() + self._cached = (now, value) + return value + + def ports(self) -> Tuple[int, int]: + """Current (Port A, Port B) target positions.""" + return ports_of(self.status()) + + def insertion(self) -> str: + """removed, inserted, or moving while between the two.""" + return INSERTION_NAMES.get(self.status().insertion_state, "moving") + + def auto_ready(self) -> bool: + """The PLC reports automatic operation, so it will take motion commands.""" + return bool(self.status().messages_0_15 & MSG_AUTOMATIC_OPERATION_AVAILABLE) + + def at(self, position: Position, inserted: bool) -> bool: + """True when the switcher is at position, in position and (if asked) inserted.""" + status = self.status() + if ports_of(status) != position.ports or not status.in_position: + return False + return status.insertion_state == INSERTION_INSERTED if inserted else True + + # Operations + + def home(self, cancel: Optional[threading.Event] = None) -> None: + """Return to origin; the PLC needs this once after power-on. + + in_position can still be set from before the command, so this waits + start_s for the motion to begin before polling for it. + """ + self.homed = False + self.switcher.home(wait=False) + self._invalidate() + self._pause(self.start_s, cancel) + self._wait(lambda s: s.in_position, self.home_timeout_s, "return to origin", cancel) + self.homed = True + + def connect(self, position: Position, cancel: Optional[threading.Event] = None) -> None: + """Retract, move to position, and insert the fiber there. + + Does nothing if the fiber is already inserted at position, rather than + pulling it out and pushing it back in. + """ + self._require_auto() + self._invalidate() + if self.at(position, inserted=True): + return + self.retract(cancel) + self._move(position, cancel) + self.insert(cancel) + + def move(self, position: Position, cancel: Optional[threading.Event] = None) -> None: + """Retract and move to position, leaving the fiber retracted.""" + self._require_auto() + self.retract(cancel) + self._move(position, cancel) + + def retract(self, cancel: Optional[threading.Event] = None) -> None: + """Retract the fiber at the current position, if not already retracted.""" + self._require_auto() + if self.status(fresh=True).insertion_state == INSERTION_REMOVED: + return + self.switcher.retract_fiber() + self._invalidate() + self._wait(lambda s: s.insertion_state == INSERTION_REMOVED, + self.insert_timeout_s, "retract", cancel) + + def insert(self, cancel: Optional[threading.Event] = None) -> None: + """Insert the fiber at the current position.""" + self._require_auto() + self.switcher.insert_fiber() + self._invalidate() + self._wait(lambda s: s.insertion_state == INSERTION_INSERTED, + self.insert_timeout_s, "insert", cancel) + + def clean(self, mode: str, cancel: Optional[threading.Event] = None) -> None: + """Retract, then run a cleaning cycle and wait for the PLC to settle. + + The PLC moves to its cleaning position itself. It publishes no + cleaning-done flag, so this waits start_s for the cycle to + begin, then for the switcher to be in position with the insertion + axis at rest. + """ + if mode not in CLEAN_MODES: + raise ValueError(f"unknown clean mode {mode!r}; expected one of {CLEAN_MODES}") + self._require_auto() + self.retract(cancel) + self.switcher.clean(mode) + self.last_clean_mode = mode + self._invalidate() + self._pause(self.start_s, cancel) + self._wait(lambda s: s.in_position and s.insertion_state in INSERTION_NAMES, + self.clean_timeout_s, f"clean ({mode})", cancel) + + def stop(self) -> None: + """Stop motion now; safe to call from any thread.""" + self.switcher.stop() + self._invalidate() + + def set_air_purge(self, value: bool) -> None: + """Open (True) or close the air purge valve.""" + if value: + self.switcher.open_air_purge() + else: + self.switcher.close_air_purge() + self.air_purge = bool(value) + + # Internals + + def _move(self, position: Position, cancel: Optional[threading.Event]) -> None: + self.switcher.set_target_positions(position.port_a, position.port_b) + self.switcher.move_to_target() + self._invalidate() + self._wait(lambda s: s.in_position and ports_of(s) == position.ports, + self.move_timeout_s, f"move to {position.name}", cancel) + + def _require_auto(self) -> None: + if self.require_auto and not self.status(fresh=True).messages_0_15 \ + & MSG_AUTOMATIC_OPERATION_AVAILABLE: + raise SwitchError("switcher is not in automatic operation; " + "check the PLC mode and home it") + + def _wait(self, done: Callable[[Any], bool], timeout_s: float, what: str, + cancel: Optional[threading.Event]) -> Any: + deadline = self._clock() + timeout_s + while True: + self._check_cancel(cancel, what) + status = self.status(fresh=True) + alarms = alarm_word(status) + if alarms: + raise SwitchError(f"{what}: switcher alarm 0x{alarms:08X}") + if done(status): + return status + if self._clock() >= deadline: + raise SwitchError(f"{what}: timed out after {timeout_s:g} s") + self._sleep(self.poll_s) + + def _pause(self, seconds: float, cancel: Optional[threading.Event]) -> None: + deadline = self._clock() + seconds + while self._clock() < deadline: + self._check_cancel(cancel, "pause") + self._sleep(min(self.poll_s, max(deadline - self._clock(), 0.0))) + + def _check_cancel(self, cancel: Optional[threading.Event], what: str) -> None: + if cancel is not None and cancel.is_set(): + self.stop() + raise JobAborted(f"{what} halted") + + def _invalidate(self) -> None: + with self._cache_lock: + self._cached = (None, None) diff --git a/systemd/instances/hsfibers_fiberswitchblue.env b/systemd/instances/hsfibers_fiberswitchblue.env new file mode 100644 index 0000000..003c1e7 --- /dev/null +++ b/systemd/instances/hsfibers_fiberswitchblue.env @@ -0,0 +1,11 @@ +# fiberswitchblue: Blue (YJ) fiber switcher and inspection camera daemon (hsfibers) +# +# hispec deploy hsfibers_fiberswitchblue +# +# copies this file to /etc/hispec/instances/hsfibers_fiberswitchblue.env and +# config/hsfibers/hsfibers_fiberswitchblue.yaml to +# /etc/hispec/hsfibers_fiberswitchblue.yaml, then enables and starts the daemon. +# The host must reach the blue PLC and either have the blue DI-2000 plugged in +# over USB or reach the camserver named by camera.remote in the config. +HISPEC_DAEMON=hsfibers/fiberswitch +HISPEC_HOST=hispecserver diff --git a/systemd/instances/hsfibers_fiberswitchred.env b/systemd/instances/hsfibers_fiberswitchred.env new file mode 100644 index 0000000..3025d76 --- /dev/null +++ b/systemd/instances/hsfibers_fiberswitchred.env @@ -0,0 +1,11 @@ +# fiberswitchred: Red (HK) fiber switcher and inspection camera daemon (hsfibers) +# +# hispec deploy hsfibers_fiberswitchred +# +# copies this file to /etc/hispec/instances/hsfibers_fiberswitchred.env and +# config/hsfibers/hsfibers_fiberswitchred.yaml to +# /etc/hispec/hsfibers_fiberswitchred.yaml, then enables and starts the daemon. +# The host must reach the red PLC and either have the red DI-2000 plugged in +# over USB or reach the camserver named by camera.remote in the config. +HISPEC_DAEMON=hsfibers/fiberswitch +HISPEC_HOST=hispecserver diff --git a/tests/fiberswitch/test_camera.py b/tests/fiberswitch/test_camera.py new file mode 100644 index 0000000..235bc96 --- /dev/null +++ b/tests/fiberswitch/test_camera.py @@ -0,0 +1,121 @@ +"""Tests for the inspection camera wrapper and the image archive, with fakes.""" + +import json +from datetime import datetime, timezone +from types import SimpleNamespace + +import numpy as np +import pytest + +from hispec.fiberswitch.archive import ImageArchive, safe_tag +from hispec.fiberswitch.camera import InspectionCamera + +FIXED = datetime(2026, 11, 16, 12, 0, 0, 123456, tzinfo=timezone.utc) + + +def fake_writer(path, image): + """Write the 'image' as text so the test can check it.""" + with open(path, "w", encoding="utf-8") as f: + f.write(str(image)) + return True + + +def archive(tmp_path): + """An archive with a fixed clock and a text writer.""" + return ImageArchive(tmp_path, write_image=fake_writer, now=lambda: FIXED) + + +class FakeCam: + """A Di2000 with settable properties and a fixed frame.""" + + def __init__(self): + self.props = {1: 10.0} + + def read_frame(self): + """Return a stand-in frame.""" + return "frame" + + def get_property(self, prop): + """Read a property.""" + return self.props[prop] + + def set_property(self, prop, value): + """Write a property; prop 2 is rejected, as a camera may do.""" + if prop == 2: + return False + self.props[prop] = value + return True + + +def fake_pipeline(dirt=0.02): + """The parts of lightel's inspection module the wrapper calls.""" + region = SimpleNamespace(cx=5.0, cy=6.0, r=3.0) + result = SimpleNamespace( + frame="frame", region=region, passed=dirt <= 0.01, defect_area_fraction=dirt, + # numpy scalars, as OpenCV returns them + defects=[SimpleNamespace(cx=np.float64(1.0), cy=2.0, area=np.float32(4.0), + bbox=tuple(np.int32(v) for v in (0, 1, 2, 2)))], + illumination=SimpleNamespace(mode="front"), + autofocus=SimpleNamespace(score=12.5, converged=True, method="vendor_trigger", + focus_value=None), + autoexpose=SimpleNamespace(exposure_value=-5.0, converged=True, + quality=SimpleNamespace(mean=140.0))) + return SimpleNamespace( + inspect=lambda camera, max_defect_area_fraction: result, + autofocus=lambda camera: result.autofocus, + draw_result=lambda frame, res: "annotated", + to_gray=lambda frame: frame, + locate_fiber_end=lambda gray: region, + focus_score=lambda gray, region: 7.0) + + +def test_safe_tag(): + """Tags are reduced to file-name-safe characters.""" + assert safe_tag("group 1/../x") == "group-1-..-x" + assert safe_tag("") == "image" + + +def test_archive_writes_png_and_sidecar(tmp_path): + """save writes the image, the annotated copy and a JSON sidecar under the date.""" + path = archive(tmp_path).save("group1", "img", {"dirt": 0.5}, annotated="ann") + assert path.parent == tmp_path / "2026-11-16" + assert path.name == "group1_20261116T120000_123456Z.png" + sidecar = json.loads(path.with_suffix(".json").read_text()) + assert sidecar["dirt"] == 0.5 and sidecar["tag"] == "group1" + assert sidecar["annotated"].endswith("_annotated.png") + + +def test_inspect_archives_and_summarizes(tmp_path): + """inspect archives the frame with its result and keeps it as last_record.""" + camera = InspectionCamera(FakeCam(), archive(tmp_path), fake_pipeline(dirt=0.02), + {"exposure": 1}) + record = camera.inspect("group1") + assert record["kind"] == "inspection" and record["passed"] is False + assert record["dirt"] == 0.02 and record["defects"][0]["bbox"] == [0, 1, 2, 2] + assert record["focus"]["method"] == "vendor_trigger" + assert record["settings"] == {"exposure": 10.0} + sidecar = json.loads((tmp_path / "2026-11-16" / + "group1_20261116T120000_123456Z.json").read_text()) + assert sidecar["dirt"] == 0.02 + assert camera.last_record is record + json.dumps(record) # lastresult publishes it as JSON + + +def test_capture_and_refocus(tmp_path): + """capture archives the raw frame with a focus score; refocus records the result.""" + camera = InspectionCamera(FakeCam(), archive(tmp_path), fake_pipeline(), {}) + assert camera.capture("look")["focus"]["score"] == 7.0 + assert camera.refocus()["converged"] is True + + +def test_settings(tmp_path): + """Settings are read and written by name; rejected writes and unknown names raise.""" + camera = InspectionCamera(FakeCam(), archive(tmp_path), fake_pipeline(), + {"exposure": 1, "gain": 2, "missing": 3}) + camera.set_setting("exposure", 4) + assert camera.get_setting("exposure") == 4.0 + assert camera.settings()["missing"] is None + with pytest.raises(RuntimeError, match="rejected"): + camera.set_setting("gain", 1) + with pytest.raises(ValueError, match="unknown camera setting"): + camera.get_setting("zoom") diff --git a/tests/fiberswitch/test_camserver.py b/tests/fiberswitch/test_camserver.py new file mode 100644 index 0000000..44367d5 --- /dev/null +++ b/tests/fiberswitch/test_camserver.py @@ -0,0 +1,167 @@ +"""The camera peer and RemoteDi2000 over libby (ZMQ on loopback), with a fake camera.""" + +import socket + +import numpy as np +import pytest +from libby import Libby + +from hispec.fiberswitch.camserver import (FRAME_CHUNK, CameraService, RemoteCameraError, + RemoteDi2000, decode_frame, encode_frame, + make_daemon_class) + +PEER = "hsfibers.testcam" + + +class FakeCamera: + """Stands in for di2000.Di2000.""" + + def __init__(self): + self.connected = True + self.device_index = 3 + self.props = {10: 2.5} + # Noise does not compress, so the frame takes several chunks + self.frame = np.random.default_rng(0).integers(0, 256, (480, 640, 3), dtype=np.uint8) + self.triggered = 0 + self.reads = 0 + + def is_connected(self): + return self.connected + + def disconnect(self): + self.connected = False + + def read_frame(self): + if not self.connected: + raise RuntimeError("camera not connected") + self.reads += 1 + return self.frame + + def get_property(self, prop): + return self.props.get(prop, 0.0) + + def set_property(self, prop, value): + if prop == 99: + return False + self.props[prop] = value + return True + + def trigger_autofocus(self): + self.triggered += 1 + + def get_autofocus_status(self): + return 3 + + +def _port(): + with socket.socket() as s: + s.bind(("127.0.0.1", 0)) + return s.getsockname()[1] + + +@pytest.fixture(name="served") +def fixture_served(): + camera = FakeCamera() + peer_addr = f"tcp://127.0.0.1:{_port()}" + client_addr = f"tcp://127.0.0.1:{_port()}" + cls = type("ZmqPeer", (make_daemon_class(),), {"transport": "zmq"}) + peer = cls.from_config({"peer_id": "testcam", "group_id": "hsfibers", "bind": peer_addr, + "address_book": {}, "discovery_enabled": False}) + peer.driver = camera + peer.open_camera = lambda: setattr(camera, "connected", True) + peer.start() + remote = RemoteDi2000(PEER, lambda: Libby.zmq(self_id="testclient", bind=client_addr, + address_book={PEER: peer_addr}), + timeout_s=3.0) + yield camera, remote + remote.close() + peer.stop() + + +def test_frame_round_trips_losslessly_in_chunks(served): + camera, remote = served + assert len(encode_frame(camera.frame)) > 2 * FRAME_CHUNK + frame = remote.read_frame() + assert frame.dtype == np.uint8 and np.array_equal(frame, camera.frame) + assert camera.reads == 1 + + +def test_properties(served): + camera, remote = served + assert remote.get_property(10) == 2.5 + assert remote.set_property(11, 7) is True and camera.props[11] == 7.0 + assert remote.set_property(99, 1) is False + + +def test_connect_disconnect_and_status(served): + camera, remote = served + assert remote.is_connected() and remote.device_index == 3 + remote.disconnect() + assert not camera.connected and not remote.is_connected() + remote.connect(0, backend=None) + assert camera.connected and remote.is_connected() + + +def test_autofocus(served): + camera, remote = served + remote.trigger_autofocus() + assert camera.triggered == 1 and remote.get_autofocus_status() == 3 + + +def test_peer_errors_are_raised(served): + camera, remote = served + camera.connected = False + with pytest.raises(RemoteCameraError, match="camera not connected"): + remote.read_frame() + + +def test_absent_peer(): + addr = f"tcp://127.0.0.1:{_port()}" + remote = RemoteDi2000("hsfibers.nobody", + lambda: Libby.zmq(self_id="lonely", bind=addr, address_book={}), + timeout_s=0.3, status_timeout_s=0.3) + try: + assert not remote.is_connected() + with pytest.raises(ConnectionError): + remote.connect() + remote.disconnect() # quiet when the peer is down + finally: + remote.close() + + +def test_stale_frame_id_is_refused(): + camera = FakeCamera() + service = CameraService(camera, lambda: None) + first = service.frame({}) + service.frame({}) + with pytest.raises(RuntimeError, match="gone"): + service.frame({"id": first["id"], "offset": FRAME_CHUNK}) + + +def test_autofocus_status_retries_transient_errors(monkeypatch): + monkeypatch.setattr("hispec.fiberswitch.camserver.STATUS_RETRY_S", 0.0) + camera = FakeCamera() + replies = [OSError(71, "Protocol error"), 2] + + def status(): + reply = replies.pop(0) + if isinstance(reply, Exception): + raise reply + return reply + + camera.get_autofocus_status = status + assert CameraService(camera, lambda: None).autofocus_status({})["status"] == 2 + + camera.get_autofocus_status = lambda: (_ for _ in ()).throw(OSError(71, "Protocol error")) + with pytest.raises(OSError): + CameraService(camera, lambda: None).autofocus_status({}) + + +def test_encode_decode(): + frame = np.arange(60, dtype=np.uint8).reshape(4, 5, 3) + assert np.array_equal(decode_frame(encode_frame(frame)), frame) + + +def test_peer_name_must_be_group_dot_peer(): + with pytest.raises(ValueError): + RemoteDi2000("131.215.200.211:8762", lambda: None) diff --git a/tests/fiberswitch/test_controller.py b/tests/fiberswitch/test_controller.py new file mode 100644 index 0000000..523a5e4 --- /dev/null +++ b/tests/fiberswitch/test_controller.py @@ -0,0 +1,247 @@ +"""Tests for the controller's policy, state, jobs and orchestration, with fakes.""" + +import threading + +import pytest + +from hispec.fiberswitch.controller import FiberSwitchController +from hispec.fiberswitch.jobs import ABORTED, OK, JobAborted, JobRunner +from hispec.fiberswitch.policy import ConnectionPolicy, ControllerState +from hispec.fiberswitch.switch import Position + + +# Policy and state + +def test_policy_validation(): + """Bad values and unknown fields are refused; ints are accepted for floats.""" + policy = ConnectionPolicy().updated({"clean_threshold": 0, "inspect_interval_h": 12}) + assert policy.clean_threshold == 0.0 and policy.inspect_interval_h == 12.0 + with pytest.raises(ValueError, match="unknown policy fields"): + policy.updated({"cleanliness": 1}) + with pytest.raises(ValueError, match="clean_mode"): + policy.updated({"clean_mode": "scrub"}) + with pytest.raises(ValueError, match="inspect_position is required"): + policy.updated({"inspect": "always"}) + + +def test_state_persists_overrides_and_history(tmp_path): + """Overrides and history survive a reload; {} resets to the config.""" + path = tmp_path / "state.json" + inputs = {"group1": {"inspect_position": "inspect1"}} + state = ControllerState(path, {"max_clean_attempts": 1}, inputs) + state.update_policy("group1", {"inspect": "always"}) + state.record("group1", last_dirt=0.2) + state.set_alert("dirty") + + reloaded = ControllerState(path, {"max_clean_attempts": 1}, inputs) + assert reloaded.policy("group1").inspect == "always" + assert reloaded.policy("group1").max_clean_attempts == 1 + assert reloaded.history("group1")["last_dirt"] == 0.2 + assert reloaded.alert == "dirty" + + reloaded.update_policy("group1", {}) + assert reloaded.policy("group1").inspect == "never" + + +def test_state_drops_saved_overrides_the_config_no_longer_allows(tmp_path): + """A saved override made invalid by a config change is dropped, not fatal.""" + path = tmp_path / "state.json" + state = ControllerState(path, {}, {"group1": {"inspect_position": "inspect1"}}) + state.update_policy("group1", {"inspect": "always"}) + reloaded = ControllerState(path, {}, {"group1": {}}) + assert reloaded.policy("group1").inspect == "never" + assert reloaded.dropped and "inspect_position" in reloaded.dropped[0] + + +def test_invalid_update_is_not_saved(tmp_path): + """A rejected policy change leaves the old policy in place.""" + state = ControllerState(tmp_path / "s.json", {}, {"group1": {}}) + with pytest.raises(ValueError): + state.update_policy("group1", {"clean_threshold": 2}) + assert state.policy("group1").clean_threshold == 0.005 + + +# Jobs + +def test_job_runner_outcomes(): + """A job reports ok, its error, or aborted; a second job is refused while busy.""" + runner = JobRunner() + gate = threading.Event() + runner.start("wait", lambda cancel: gate.wait(5)) + assert runner.busy and runner.state == "wait" + with pytest.raises(RuntimeError, match="busy with wait"): + runner.start("other", lambda cancel: None) + gate.set() + assert runner.wait(5) and runner.status == OK and runner.state == "idle" + + def fail(_): + raise ValueError("nope") + runner.start("fail", fail) + runner.wait(5) + assert runner.status == "error: nope" + + def until_cancelled(cancel): + cancel.wait(5) + raise JobAborted("stopped") + runner.start("abortable", until_cancelled) + runner.abort() + runner.wait(5) + assert runner.status == ABORTED and runner.job_id == 3 + + +# Orchestration + +class FakeSequencer: + """Records the switch operations the controller asks for.""" + + def __init__(self): + self.commands = [] + + def connect(self, position, cancel=None): # pylint: disable=unused-argument + """Record a connect.""" + self.commands.append(("connect", position.name)) + + def clean(self, mode, cancel=None): # pylint: disable=unused-argument + """Record a clean.""" + self.commands.append(("clean", mode)) + + +class FakeCamera: + """Returns a scripted series of dirt values from inspect.""" + + def __init__(self, dirts, found=True): + self.dirts = list(dirts) + self.found = found + self.settings = {} + self.tags = [] + self.thresholds = [] + + def inspect(self, tag, cancel=None, max_defect_area_fraction=None): # pylint: disable=W0613 + """Run an inspection.""" + self.tags.append(tag) + self.thresholds.append(max_defect_area_fraction) + return {"image": f"/archive/{tag}-{len(self.tags)}.png", "dirt": self.dirts.pop(0), + "region": {"cx": 1, "cy": 1, "r": 1} if self.found else None} + + def set_setting(self, name, value): + """Apply a camera setting.""" + self.settings[name] = value + + +class Now: # pylint: disable=too-few-public-methods + """A settable epoch clock.""" + + def __init__(self): + self.t = 1_000_000.0 + + def __call__(self): + return self.t + + +GROUPS = {"group1": Position("group1", 1, 1)} +INSPECTION = {"inspect1": Position("inspect1", 13, 1)} + + +def make_controller(tmp_path, dirts=(0.0,), found=True, **policy): + """A controller over fakes with one input, group1.""" + state = ControllerState(tmp_path / "state.json", {}, + {"group1": {"inspect_position": "inspect1", **policy}}) + switch, camera, now = FakeSequencer(), FakeCamera(dirts, found), Now() + ctl = FiberSwitchController(switch, camera, state, input_groups=GROUPS, + inspection_positions=INSPECTION, clock=now) + return ctl, switch, camera, now + + +def test_clean_fiber_is_inspected_once(tmp_path): + """A clean fiber end needs no cleaning and raises no alert.""" + ctl, switch, camera, _ = make_controller(tmp_path, dirts=[0.001]) + record = ctl.inspect_and_clean("group1") + assert record["dirt"] == 0.001 + assert switch.commands == [("connect", "inspect1")] + assert camera.tags == ["group1"] + assert camera.thresholds == [0.01] # the policy's alert threshold + assert ctl.state.alert == "" + history = ctl.state.history("group1") + assert history["last_passed"] is True and history["last_image"] == record["image"] + + +def test_dirty_fiber_is_cleaned_until_clean(tmp_path): + """Dirt above clean_threshold is cleaned and re-inspected.""" + ctl, switch, _, _ = make_controller(tmp_path, dirts=[0.05, 0.001]) + ctl.inspect_and_clean("group1") + assert switch.commands == [("connect", "inspect1"), ("clean", "both"), + ("connect", "inspect1")] + assert ctl.state.alert == "" + assert ctl.state.history("group1")["last_clean"] is not None + + +def test_stays_dirty_alerts_after_max_attempts(tmp_path): + """Dirt still above alert_threshold after the last clean raises an alert.""" + ctl, switch, _, _ = make_controller(tmp_path, dirts=[0.05, 0.05, 0.05], + max_clean_attempts=2) + ctl.inspect_and_clean("group1") + assert [c for c in switch.commands if c[0] == "clean"] == [("clean", "both")] * 2 + assert "group1: dirt 0.0500 above alert threshold" in ctl.state.alert + assert ctl.state.history("group1")["last_passed"] is False + + +def test_fiber_not_found_alerts_without_cleaning(tmp_path): + """No fiber end in the image is an alert, not a reason to clean.""" + ctl, switch, _, _ = make_controller(tmp_path, dirts=[0.0], found=False) + ctl.inspect_and_clean("group1") + assert ("clean", "both") not in switch.commands + assert "fiber end not found" in ctl.state.alert + + +def test_camera_settings_are_applied_first(tmp_path): + """The policy's camera settings are written before inspecting.""" + ctl, _, camera, _ = make_controller(tmp_path, camera={"gain": 4}) + ctl.inspect_and_clean("group1") + assert camera.settings == {"gain": 4.0} + + +def test_select_inspects_only_when_due(tmp_path): + """interval inspects on the first select and again once the interval passes.""" + ctl, switch, camera, now = make_controller(tmp_path, dirts=[0.0, 0.0], + inspect="interval", inspect_interval_h=1) + ctl.select_input("group1") + assert camera.tags == ["group1"] + assert switch.commands[-1] == ("connect", "group1") + assert ctl.state.selected == "group1" + + now.t += 1800 + ctl.select_input("group1") + assert len(camera.tags) == 1 + + now.t += 1800 + ctl.select_input("group1") + assert len(camera.tags) == 2 + + +def test_select_cleans_on_its_interval(tmp_path): + """clean_interval_h cleans before connecting when it has elapsed.""" + ctl, switch, _, _ = make_controller(tmp_path, clean_interval_h=24) + ctl.select_input("group1") + assert switch.commands == [("clean", "both"), ("connect", "group1")] + ctl.select_input("group1") + assert switch.commands[-1] == ("connect", "group1") + assert switch.commands.count(("clean", "both")) == 1 + + +def test_take_reference(tmp_path): + """A reference image is stored without touching the inspection history.""" + ctl, _, camera, _ = make_controller(tmp_path) + record = ctl.take_reference("group1") + assert camera.tags == ["group1-reference"] + history = ctl.state.history("group1") + assert history["reference_image"] == record["image"] + assert "last_inspect" not in history + + +def test_inspect_needs_a_position(): + """An input without inspect_position cannot be inspected.""" + state = ControllerState(None, {}, {"group1": {}}) + ctl = FiberSwitchController(FakeSequencer(), FakeCamera([0.0]), state, + input_groups=GROUPS, inspection_positions=INSPECTION) + with pytest.raises(ValueError, match="no inspect_position"): + ctl.inspect_and_clean("group1") diff --git a/tests/fiberswitch/test_switch.py b/tests/fiberswitch/test_switch.py new file mode 100644 index 0000000..ca07da7 --- /dev/null +++ b/tests/fiberswitch/test_switch.py @@ -0,0 +1,233 @@ +"""Tests for the fiber switcher sequencer, against a fake PLC.""" +# The fakes mirror fiber_switcher.FiberSwitcher's methods, documented there +# pylint: disable=missing-function-docstring,too-many-instance-attributes + +import threading +from dataclasses import dataclass + +import pytest + +from hispec.fiberswitch.jobs import JobAborted +from hispec.fiberswitch.switch import (parse_target, INSERTION_INSERTED, INSERTION_REMOVED, + MSG_AUTOMATIC_OPERATION_AVAILABLE, Position, + SwitchError, SwitchSequencer, parse_positions) + + +@dataclass +class Status: + """The fields of fiber_switcher.SwitcherStatus the sequencer reads.""" + + port_a_target: int + port_b_target: int + in_position: bool + insertion_state: int + alarms_0_15: int = 0 + alarms_16_31: int = 0 + messages_0_15: int = MSG_AUTOMATIC_OPERATION_AVAILABLE + messages_16_31: int = 0 + + +class FakeSwitcher: + """A PLC whose commands complete after `lag` status reads.""" + + def __init__(self, lag=2): + self.lag = lag + self.ports = (1, 1) + self.current = (1, 1) + self.insertion = INSERTION_REMOVED + self.alarms = 0 + self.auto = True + self.calls = [] + self._pending = [] # (reads left, callable) + + def read_status(self): + still = [] + for left, apply in self._pending: + if left <= 1: + apply() + else: + still.append((left - 1, apply)) + self._pending = still + return Status(self.ports[0] + 100, self.ports[1] + 100, + in_position=self.current == self.ports, + insertion_state=self.insertion, + alarms_0_15=self.alarms, + messages_0_15=MSG_AUTOMATIC_OPERATION_AVAILABLE if self.auto else 0) + + def _later(self, apply): + self._pending.append((self.lag, apply)) + + def set_target_positions(self, port_a, port_b): + self.calls.append(("target", port_a, port_b)) + self.ports = (port_a, port_b) + + def move_to_target(self): + self.calls.append(("move",)) + ports = self.ports + self._later(lambda: setattr(self, "current", ports)) + + def insert_fiber(self): + self.calls.append(("insert",)) + self._later(lambda: setattr(self, "insertion", INSERTION_INSERTED)) + + def retract_fiber(self): + self.calls.append(("retract",)) + self._later(lambda: setattr(self, "insertion", INSERTION_REMOVED)) + + def clean(self, mode): + self.calls.append(("clean", mode)) + self.ports = (10, 2) + self._later(lambda: setattr(self, "current", (10, 2))) + + def home(self, wait=True): + self.calls.append(("home", wait)) + self.current = None + self._later(lambda: setattr(self, "current", self.ports)) + + def stop(self): + self.calls.append(("stop",)) + + def open_air_purge(self): + self.calls.append(("purge", True)) + + def close_air_purge(self): + self.calls.append(("purge", False)) + + +class Clock: + """A clock that advances only when the sequencer sleeps.""" + + def __init__(self): + self.t = 0.0 + + def __call__(self): + return self.t + + def sleep(self, dt): + self.t += dt + + +def make(switcher=None, **kwargs): + """A sequencer on a fake PLC and fake clock.""" + clock = Clock() + switcher = switcher or FakeSwitcher() + kwargs.setdefault("start_s", 0.0) + return SwitchSequencer(switcher, clock=clock, sleep=clock.sleep, status_ttl_s=0.0, + **kwargs), switcher + + +GROUP = Position("group1", 5, 2) + + +def test_parse_positions(): + """Config positions become Position objects; out-of-range ports are refused.""" + positions = parse_positions({"a": {"port_a": 13, "port_b": 5}}, "input group") + assert positions["a"].ports == (13, 5) + with pytest.raises(ValueError, match="Port A position 14"): + parse_positions({"a": {"port_a": 14, "port_b": 1}}, "input group") + with pytest.raises(ValueError, match="integers"): + parse_positions({"a": {"port_a": 1}}, "input group") + + +def test_parse_target(): + for text in ("5,2", "5 2", " 5 , 2 ", "A5 B2", "a5,b2"): + assert parse_target(text).ports == (5, 2) + for bad in ("5", "5,2,1", "x,2", "14,1", "1,6", "0,1", ""): + with pytest.raises(ValueError): + parse_target(bad) + + +def test_connect_retracts_moves_and_inserts(): + """connect retracts first, then targets, moves, and inserts once in position.""" + seq, plc = make() + plc.insertion = INSERTION_INSERTED + seq.connect(GROUP) + assert plc.calls == [("retract",), ("target", 5, 2), ("move",), ("insert",)] + assert seq.at(GROUP, inserted=True) + + +def test_connect_is_a_no_op_when_already_connected(): + """An inserted fiber at the requested position is left alone.""" + seq, plc = make() + plc.ports = plc.current = GROUP.ports + plc.insertion = INSERTION_INSERTED + seq.connect(GROUP) + assert not plc.calls + + +def test_connect_skips_retract_when_already_removed(): + """A retracted fiber is not retracted again.""" + seq, plc = make() + seq.connect(GROUP) + assert plc.calls[0] == ("target", 5, 2) + + +def test_alarm_stops_the_wait(): + """A PLC alarm during a move raises with the alarm bits.""" + seq, plc = make() + plc.move_to_target = lambda: setattr(plc, "alarms", 0x1000) + with pytest.raises(SwitchError, match="alarm 0x00001000"): + seq.connect(GROUP) + + +def test_timeout(): + """A move that never completes times out.""" + seq, plc = make(move_timeout_s=1.0) + plc.move_to_target = lambda: None + with pytest.raises(SwitchError, match="timed out"): + seq.connect(GROUP) + + +def test_refuses_without_automatic_operation(): + """Motion is refused unless the PLC reports automatic operation.""" + seq, plc = make() + plc.auto = False + with pytest.raises(SwitchError, match="automatic operation"): + seq.connect(GROUP) + assert not plc.calls + + +def test_require_auto_can_be_disabled(): + """require_auto=False skips the automatic-operation check.""" + seq, plc = make(require_auto=False) + plc.auto = False + seq.connect(GROUP) + assert seq.at(GROUP, inserted=True) + + +def test_cancel_stops_the_switcher(): + """A set cancel event halts the PLC and raises JobAborted.""" + seq, plc = make() + cancel = threading.Event() + cancel.set() + with pytest.raises(JobAborted): + seq.connect(GROUP, cancel) + assert ("stop",) in plc.calls + + +def test_clean_retracts_then_waits_for_the_cycle(): + """clean retracts, runs the mode, and returns once the PLC settles.""" + seq, plc = make() + plc.insertion = INSERTION_INSERTED + seq.clean("outside") + assert plc.calls[:2] == [("retract",), ("clean", "outside")] + assert seq.last_clean_mode == "outside" + with pytest.raises(ValueError): + seq.clean("sideways") + + +def test_home_sets_homed(): + """homed is false until a return to origin completes.""" + seq, plc = make() + assert not seq.homed + seq.home() + assert seq.homed + assert plc.calls == [("home", False)] + + +def test_air_purge_records_last_command(): + """The PLC does not report the valve, so the last command is remembered.""" + seq, plc = make() + assert seq.air_purge is None + seq.set_air_purge(True) + assert seq.air_purge is True and plc.calls == [("purge", True)] diff --git a/uv.lock b/uv.lock index 243522e..1a26a42 100644 --- a/uv.lock +++ b/uv.lock @@ -1,5 +1,5 @@ version = 1 -revision = 5 +revision = 3 requires-python = ">=3.11" resolution-markers = [ "python_full_version >= '3.12'", @@ -14,6 +14,7 @@ members = [ "hispec-gui", "inficon", "lakeshore", + "lightel-di2000", "newport", "onewire", "ozoptics", @@ -83,12 +84,12 @@ name = "atlas" version = "0.1.0" source = { git = "https://github.com/CaltechOpticalObservatories/atlas.git#38a6a0a1143f3f43384eef613138e870164c9d9c" } dependencies = [ - { name = "astropy" }, - { name = "matplotlib" }, - { name = "numpy", version = "2.5.4", source = { registry = "https://pypi.org/simple" } }, - { name = "psutil" }, - { name = "pyqt5" }, - { name = "pyyaml" }, + { name = "astropy", marker = "python_full_version >= '3.12'" }, + { name = "matplotlib", marker = "python_full_version >= '3.12'" }, + { name = "numpy", version = "2.5.4", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.12'" }, + { name = "psutil", marker = "python_full_version >= '3.12'" }, + { name = "pyqt5", marker = "python_full_version >= '3.12'" }, + { name = "pyyaml", marker = "python_full_version >= '3.12'" }, ] [[package]] @@ -518,7 +519,7 @@ resolution-markers = [ "python_full_version < '3.12'", ] dependencies = [ - { name = "numpy", version = "2.4.6", source = { registry = "https://pypi.org/simple" } }, + { name = "numpy", version = "2.4.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.12'" }, ] sdist = { url = "https://files.pythonhosted.org/packages/58/01/1253e6698a07380cd31a736d248a3f2a50a7c88779a1813da27503cadc2a/contourpy-1.3.3.tar.gz", hash = "sha256:083e12155b210502d0bca491432bb04d56dc3432f95a979b429f2848c3dbe880", size = 13466174, upload-time = "2025-07-26T12:03:12.549Z" } wheels = [ @@ -603,7 +604,7 @@ resolution-markers = [ "python_full_version >= '3.12'", ] dependencies = [ - { name = "numpy", version = "2.5.4", source = { registry = "https://pypi.org/simple" } }, + { name = "numpy", version = "2.5.4", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.12'" }, ] sdist = { url = "https://files.pythonhosted.org/packages/83/5a/a55177dd22553a277388e8a1b3220e92de91bacb28356cdc73caa240121d/contourpy-1.4.0.tar.gz", hash = "sha256:20156f5a1ac4f8ce02656e39a61e82164a3d359796dc8026f75b062783d500e1", size = 13323726, upload-time = "2026-09-11T19:05:05.808Z" } wheels = [ @@ -916,7 +917,7 @@ requires-dist = [ { name = "instrumentctl", git = "https://github.com/CaltechOpticalObservatories/instrumentctl.git" }, { name = "keyring" }, { name = "libby", extras = ["influxdb", "otel"], git = "https://github.com/CaltechOpticalObservatories/libby.git?rev=libby-0.2.0" }, - { name = "libximc" }, + { name = "libximc", specifier = ">=3.1.4" }, { name = "matplotlib", marker = "extra == 'scripts'" }, { name = "paho-mqtt", specifier = ">=2" }, { name = "paramiko" }, @@ -978,7 +979,7 @@ name = "importlib-metadata" version = "10.1.0" source = { registry = "https://pypi.org/simple" } dependencies = [ - { name = "zipp" }, + { name = "zipp", marker = "python_full_version < '3.12'" }, ] sdist = { url = "https://files.pythonhosted.org/packages/98/f6/4eda8458eb00ec03e715c376c6b23b3def69687924c68d36d1c4003d3bc4/importlib_metadata-10.1.0.tar.gz", hash = "sha256:82b04047fa09792c1a8cfeddfe08bec79e4b13c29c8469b0d3949860f61017e8", size = 67761, upload-time = "2026-10-24T00:01:44.856Z" } wheels = [ @@ -1098,7 +1099,7 @@ name = "jinxed" version = "2.1.0" source = { registry = "https://pypi.org/simple" } dependencies = [ - { name = "ansicon" }, + { name = "ansicon", marker = "sys_platform == 'win32'" }, ] sdist = { url = "https://files.pythonhosted.org/packages/39/d7/6e6d474ec5eaeca6a61acc17766bb19563b3a372b4b9d92910078f5fe49f/jinxed-2.1.0.tar.gz", hash = "sha256:7e755b831faa2443d44fb4ce7c0202eb9c3ed39bd5bf1193365888f4f6092b54", size = 61287, upload-time = "2026-07-03T15:46:48.153Z" } wheels = [ @@ -1295,6 +1296,35 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/7d/44/e8ed5b2c996a7ea9b723c1334433845c7c7aacfc2bfb5c2225edff04c0d2/libximc-3.1.4-py3-none-any.whl", hash = "sha256:5ffab6d83ad8dd5f8ca85baff8a9b9c04fd32d89ac7b0ab505e0e7abde314ac7", size = 5587145, upload-time = "2026-11-10T08:35:31.919Z" }, ] +[[package]] +name = "lightel-di2000" +version = "0.1.0" +source = { editable = "src/hispec/driver/lightel" } +dependencies = [ + { name = "hardware-device-base" }, + { name = "numpy", version = "2.4.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.12'" }, + { name = "numpy", version = "2.5.4", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.12'" }, + { name = "opencv-python" }, +] + +[package.optional-dependencies] +dev = [ + { name = "pytest" }, +] +gui = [ + { name = "pyside6" }, +] + +[package.metadata] +requires-dist = [ + { name = "hardware-device-base", git = "https://github.com/COO-Utilities/hardware_device_base.git" }, + { name = "numpy" }, + { name = "opencv-python", specifier = ">=4.8" }, + { name = "pyside6", marker = "extra == 'gui'", specifier = ">=6.5" }, + { name = "pytest", marker = "extra == 'dev'", specifier = ">=7" }, +] +provides-extras = ["dev", "gui"] + [[package]] name = "matplotlib" version = "3.11.2" @@ -1590,6 +1620,26 @@ dependencies = [ [package.metadata] requires-dist = [{ name = "hardware-device-base", git = "https://github.com/COO-Utilities/hardware_device_base?rev=v1.0.1" }] +[[package]] +name = "opencv-python" +version = "5.0.0.93" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "numpy", version = "2.4.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.12'" }, + { name = "numpy", version = "2.5.4", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.12'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/79/4c/a438d23e09ce2033c09f7b784ad2fbdb0adf529e434101ed28f142226f98/opencv_python-5.0.0.93.tar.gz", hash = "sha256:66aac3e5b5faa48d4025816592f3af19e4bfc2c68dec067bae2dbb4ca10aa9e2", size = 81802749, upload-time = "2026-07-02T06:59:53.815Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/9c/75/76f6ade78f6102c61034f828e2a22616708df2c9504bc8d6af9dd8f73dc5/opencv_python-5.0.0.93-cp37-abi3-macosx_13_0_arm64.whl", hash = "sha256:198a75138241810206a17c829dbcc40a7cb1841cda538ca86cbbfc6c7d95f898", size = 48322443, upload-time = "2026-07-02T05:50:25.466Z" }, + { url = "https://files.pythonhosted.org/packages/15/8c/bc1bda6aae69a32e9d84fc34153ba104cd25226861eb4aea33b2cea4860d/opencv_python-5.0.0.93-cp37-abi3-macosx_14_0_x86_64.whl", hash = "sha256:6bbc32f59e1b1a7db7b39c81f63d00625f041d333037fd8702f6da52cc39108b", size = 34782755, upload-time = "2026-07-02T05:51:30.556Z" }, + { url = "https://files.pythonhosted.org/packages/f4/8a/b04776ec45d2dea08a1b176f1829201db3515d4ed16c35f8fcc9fa7beb16/opencv_python-5.0.0.93-cp37-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:e2b4272e736836f66c2d176e43ab8101f3a00d45654916399f52e150c58981ac", size = 50614064, upload-time = "2026-07-02T06:53:22.604Z" }, + { url = "https://files.pythonhosted.org/packages/95/54/eb47866b94f2b5b42dde17644b78055ef1ee05aae59962c7290e55270803/opencv_python-5.0.0.93-cp37-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:f8b6d0a212253dd26ad338c812f1f23ca118fdf05a9c8c6b9444f161aa8c5881", size = 71064711, upload-time = "2026-07-02T06:54:13.148Z" }, + { url = "https://files.pythonhosted.org/packages/93/da/962579f1e703cbf8c5422fd1f576467dcb3b5b0b0b81c1471c979764353a/opencv_python-5.0.0.93-cp37-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:08d5d91d967b58d6db86073b2ad3eaef88ca4ebdfd45c9059bf59f5ded0c7ad2", size = 49798576, upload-time = "2026-07-02T06:54:33.781Z" }, + { url = "https://files.pythonhosted.org/packages/cf/4c/c73f828fdbcd37eaf21d08fa852544a3ca7c2dbb3ea76873d64f2ea413d1/opencv_python-5.0.0.93-cp37-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:c8de2dec111122a02e8beb28e16c31904992dfd6186560b142a92c71403c1039", size = 73783032, upload-time = "2026-07-02T06:55:03.415Z" }, + { url = "https://files.pythonhosted.org/packages/e2/4b/edaf83b996ca5a1a3d8ccad485706b9c6d4742b13b9c4586bf1c1e7d9423/opencv_python-5.0.0.93-cp37-abi3-win32.whl", hash = "sha256:4b4b1a34c79bf8d3738e3cfe9a9e67b51a79663f6b692cbdad8c31f570da4157", size = 35564734, upload-time = "2026-07-02T05:49:57.704Z" }, + { url = "https://files.pythonhosted.org/packages/21/f0/9fa6e85cb10c8eb36a0222d27e50fe381b86ce49a55446bf39f491727564/opencv_python-5.0.0.93-cp37-abi3-win_amd64.whl", hash = "sha256:f90ba04b8f73bc5c3814037699739f0156f597338a98f05956c684e7c3ca10d2", size = 44000345, upload-time = "2026-07-02T05:49:54.971Z" }, +] + [[package]] name = "opentelemetry-api" version = "1.45.2" @@ -1917,8 +1967,8 @@ resolution-markers = [ "python_full_version < '3.12'", ] dependencies = [ - { name = "pyserial" }, - { name = "pyusb" }, + { name = "pyserial", marker = "python_full_version < '3.12'" }, + { name = "pyusb", marker = "python_full_version < '3.12'" }, ] sdist = { url = "https://files.pythonhosted.org/packages/c0/78/7652f5f6b26e61de1a9b1df71d22a7163cd3db8eeb03856a5460abcf10c3/pipython-2.11.0.6.tar.gz", hash = "sha256:dcd68c6fe22383861a3682fc388a55c3560d1d108dc6720fb5f69ab76ee99f26", size = 167238, upload-time = "2025-04-07T13:18:46.567Z" } wheels = [ @@ -1933,8 +1983,8 @@ resolution-markers = [ "python_full_version >= '3.12'", ] dependencies = [ - { name = "pyserial" }, - { name = "pyusb" }, + { name = "pyserial", marker = "python_full_version >= '3.12'" }, + { name = "pyusb", marker = "python_full_version >= '3.12'" }, ] sdist = { url = "https://files.pythonhosted.org/packages/ba/6b/41da44d4d8173db5b09a43d87eeb8bfe6e50e19f39611a757becdb447c5c/pipython-2.13.0.2.tar.gz", hash = "sha256:916120591c323cfd2850996419687d8bdc11dd762bfc0c29ccad96cbeb9839d8", size = 1222572, upload-time = "2026-07-15T08:15:28.501Z" } wheels = [ @@ -2120,8 +2170,8 @@ name = "pyqt5" version = "5.15.11" source = { registry = "https://pypi.org/simple" } dependencies = [ - { name = "pyqt5-qt5" }, - { name = "pyqt5-sip" }, + { name = "pyqt5-qt5", marker = "python_full_version >= '3.12'" }, + { name = "pyqt5-sip", marker = "python_full_version >= '3.12'" }, ] sdist = { url = "https://files.pythonhosted.org/packages/0e/07/c9ed0bd428df6f87183fca565a79fee19fa7c88c7f00a7f011ab4379e77a/PyQt5-5.15.11.tar.gz", hash = "sha256:fda45743ebb4a27b4b1a51c6d8ef455c4c1b5d610c90d2934c7802b5c1557c52", size = 3216775, upload-time = "2024-07-19T08:39:57.756Z" } wheels = [ @@ -2509,7 +2559,7 @@ resolution-markers = [ "python_full_version < '3.12'", ] dependencies = [ - { name = "numpy", version = "2.4.6", source = { registry = "https://pypi.org/simple" } }, + { name = "numpy", version = "2.4.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.12'" }, ] sdist = { url = "https://files.pythonhosted.org/packages/7a/97/5a3609c4f8d58b039179648e62dd220f89864f56f7357f5d4f45c29eb2cc/scipy-1.17.1.tar.gz", hash = "sha256:95d8e012d8cb8816c226aef832200b1d45109ed4464303e997c5b13122b297c0", size = 30573822, upload-time = "2026-02-23T00:26:24.851Z" } wheels = [ @@ -2583,7 +2633,7 @@ resolution-markers = [ "python_full_version >= '3.12'", ] dependencies = [ - { name = "numpy", version = "2.5.4", source = { registry = "https://pypi.org/simple" } }, + { name = "numpy", version = "2.5.4", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.12'" }, ] sdist = { url = "https://files.pythonhosted.org/packages/7e/74/66de6258867beb2ef08f35f9f2ac017a52cacd5081714d239ff1a442d458/scipy-1.18.1.tar.gz", hash = "sha256:52c4b7422442aba924d03ad4019852b08a92e64ea187b933135687bfe2747307", size = 30781235, upload-time = "2026-08-21T23:28:50.599Z" } wheels = [ @@ -2725,7 +2775,7 @@ requires-dist = [ { name = "black", marker = "extra == 'dev'" }, { name = "flake8", marker = "extra == 'dev'" }, { name = "hardware-device-base", git = "https://github.com/COO-Utilities/hardware_device_base?rev=v1.0.1" }, - { name = "libximc" }, + { name = "libximc", specifier = ">=3.1.4" }, { name = "pytest", marker = "extra == 'dev'" }, { name = "pytest-mock", marker = "extra == 'dev'" }, ]