From d59b3854031bc70054028d09e87937b2be01cc44 Mon Sep 17 00:00:00 2001 From: Mirko Holler Date: Tue, 17 Nov 2026 15:22:41 +0100 Subject: [PATCH] feat(client): OMNY session beamline state omny_vacuum (+ scan interlock entry) SESSION_BEAMLINE_STATES: per-session states added to the defaults; OMNY: omny_vacuum = DeviceWithinLimitsState on omny_vcs.measurement_ok (0.5..1.5; vacuum PLC alive, EXPVG1 + EXPVG2 OPEN, WI2VG1 CLOSED). Skipped when omny_vcs is not loaded. The session states are added to the scan interlock (required valid) in their session and removed from it in the others, so a flOMNI/LamNI session is never locked by a missing state; default states' interlock entries are not touched. post_startup passes the session name. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01M8zoDygkWk6LupmHh3tyET --- .../startup/beamline_states.py | 70 ++++++++++++++++++- .../startup/post_startup.py | 5 +- .../test_default_beamline_states.py | 55 +++++++++++++++ 3 files changed, 125 insertions(+), 5 deletions(-) diff --git a/csaxs_bec/bec_ipython_client/startup/beamline_states.py b/csaxs_bec/bec_ipython_client/startup/beamline_states.py index c83b197b..d16130fa 100644 --- a/csaxs_bec/bec_ipython_client/startup/beamline_states.py +++ b/csaxs_bec/bec_ipython_client/startup/beamline_states.py @@ -58,20 +58,84 @@ DEFAULT_BEAMLINE_STATES = [ ] +# Extra states per session (added to the defaults). The scan interlock watches them only in their +# session: set_default_beamline_states() adds them to the interlock there and removes them from it +# in the other sessions, so a flOMNI/LamNI session is never locked by a state that does not exist. +SESSION_BEAMLINE_STATES = { + # OMNY vacuum (csaxs_bec.devices.omny.omny_vcs, Mirko 2026-11-17): measurement_ok = 1 when the + # vacuum PLC is alive, EXPVG1 + EXPVG2 OPEN and WI2VG1 CLOSED; without it the measurement stops. + "omny": [ + DeviceWithinLimitsStateConfig( + name="omny_vacuum", + device="omny_vcs", + signal="omny_vcs_measurement_ok", + low_limit=0.5, + high_limit=1.5, + tolerance=0.1, + ), + ], +} +SESSION_STATE_NAMES = {state.name for states in SESSION_BEAMLINE_STATES.values() for state in states} + + +def beamline_states_for_session(session: str | None, devices=None) -> list: + """DEFAULT_BEAMLINE_STATES + the session's extra states (only those whose device is loaded, + when `devices` is given).""" + extra = SESSION_BEAMLINE_STATES.get((session or "").lower(), []) + if devices is not None: + skipped = [state.name for state in extra if state.device not in devices] + if skipped: + print(f"Beamline states {', '.join(skipped)} skipped: their device is not loaded.") + extra = [state for state in extra if state.device in devices] + return list(DEFAULT_BEAMLINE_STATES) + extra + + +def _sync_interlock(bec, states) -> None: + """Session states in the scan interlock: watched (required "valid") when defined, removed + from the watch list when not. The default states' interlock entries are not touched.""" + try: + actors = getattr(bec, "builtin_actors", None) + interlock = getattr(actors, "scan_interlock", None) if actors is not None else None + if interlock is None: + return + watched = interlock.states_watched or {} + active = {state.name for state in states} + for name in sorted(SESSION_STATE_NAMES): + if name in active and name not in watched: + interlock.add_state_to_interlock(name, "valid") + logger.info(f"Beamline state {name} added to the scan interlock.") + elif name not in active and name in watched: + interlock.remove_state_from_interlock(name) + logger.info(f"Beamline state {name} removed from the scan interlock.") + except Exception as exc: # pylint: disable=broad-except + logger.warning(f"Scan interlock not updated: {exc}") + + def _definition(state) -> tuple: return (state.state_type, tuple(sorted(state.model_dump().items(), key=lambda kv: kv[0]))) -def set_default_beamline_states(bec, states=None, ready_timeout: float = 5.0) -> bool: +def set_default_beamline_states(bec, states=None, ready_timeout: float = 5.0, session: str | None = None) -> bool: """ - Make the beamline states in BEC exactly `states` (default: DEFAULT_BEAMLINE_STATES). + Make the beamline states in BEC exactly `states` (default: DEFAULT_BEAMLINE_STATES plus the + session's extra states, SESSION_BEAMLINE_STATES), then keep the session states' scan interlock + entries in step (_sync_interlock). Never raises: problems are logged and printed, so the client startup always continues. Returns: True if the states match the defaults afterwards (changed or already correct). """ - states = DEFAULT_BEAMLINE_STATES if states is None else states + if states is None: + devices = getattr(getattr(bec, "device_manager", None), "devices", None) + states = beamline_states_for_session(session, devices) + ok = _apply_states(bec, states, ready_timeout) + if ok: + _sync_interlock(bec, states) + return ok + + +def _apply_states(bec, states, ready_timeout: float) -> bool: try: manager = getattr(bec, "beamline_states", None) if manager is None: diff --git a/csaxs_bec/bec_ipython_client/startup/post_startup.py b/csaxs_bec/bec_ipython_client/startup/post_startup.py index 8fd65711..3414d35b 100644 --- a/csaxs_bec/bec_ipython_client/startup/post_startup.py +++ b/csaxs_bec/bec_ipython_client/startup/post_startup.py @@ -146,7 +146,8 @@ def _check_account_mismatch(): if _args.session.lower() == "lamni" or _args.session.lower() == "flomni" or _args.session.lower() == "omny": _check_account_mismatch() - # DEFAULT BEAMLINE STATES (watched by the scan interlock of the tomography scans) + # DEFAULT BEAMLINE STATES (watched by the scan interlock of the tomography scans), + # plus the session's own states (OMNY: omny_vacuum) from csaxs_bec.bec_ipython_client.startup.beamline_states import set_default_beamline_states - set_default_beamline_states(bec) \ No newline at end of file + set_default_beamline_states(bec, session=_args.session.lower()) \ No newline at end of file diff --git a/tests/tests_bec_ipython_client/test_default_beamline_states.py b/tests/tests_bec_ipython_client/test_default_beamline_states.py index 72e11134..dabb4758 100644 --- a/tests/tests_bec_ipython_client/test_default_beamline_states.py +++ b/tests/tests_bec_ipython_client/test_default_beamline_states.py @@ -4,6 +4,7 @@ from bec_lib.bl_states import DeviceWithinLimitsStateConfig from csaxs_bec.bec_ipython_client.startup.beamline_states import ( DEFAULT_BEAMLINE_STATES, + beamline_states_for_session, set_default_beamline_states, ) @@ -90,3 +91,57 @@ def test_not_ready_or_unavailable_does_not_raise(): assert not set_default_beamline_states(_bec(FakeStateManager(ready=False)), ready_timeout=0.1) assert not set_default_beamline_states(SimpleNamespace(beamline_states=None)) assert not set_default_beamline_states(object()) + + +# ── session states (OMNY vacuum) and their scan interlock entries ──────────────────────────── + + +class FakeInterlock: + def __init__(self, watched=None): + self.states_watched = dict(watched or {}) + + def add_state_to_interlock(self, name, required_value=None): + self.states_watched[name] = [required_value] + + def remove_state_from_interlock(self, name): + self.states_watched.pop(name, None) + + +def _bec_il(manager, interlock, devices=DEFAULT_DEVICES | {"omny_vcs"}): + b = _bec(manager, devices) + b.builtin_actors = SimpleNamespace(scan_interlock=interlock) + return b + + +def test_omny_session_adds_the_vacuum_state_and_watches_it(): + manager, il = FakeStateManager(DEFAULT_BEAMLINE_STATES), FakeInterlock({"curr_device_within_limits_state": ["valid"]}) + assert set_default_beamline_states(_bec_il(manager, il), session="omny") + vac = manager._states["omny_vacuum"] + assert (vac.device, vac.signal, vac.low_limit, vac.high_limit) == ("omny_vcs", "omny_vcs_measurement_ok", 0.5, 1.5) + assert _names(manager) == {s.name for s in DEFAULT_BEAMLINE_STATES} | {"omny_vacuum"} + assert il.states_watched["omny_vacuum"] == ["valid"] + assert il.states_watched["curr_device_within_limits_state"] == ["valid"] # default entries untouched + + +def test_other_session_removes_the_vacuum_state_from_the_interlock(): + omny = beamline_states_for_session("omny") + manager, il = FakeStateManager(omny), FakeInterlock({"omny_vacuum": ["valid"], "shutter_device_within_limits_state": ["valid"]}) + assert set_default_beamline_states(_bec_il(manager, il), session="flomni") + assert "omny_vacuum" not in _names(manager) + assert "omny_vacuum" not in il.states_watched + assert "shutter_device_within_limits_state" in il.states_watched + + +def test_omny_session_without_vcs_device_keeps_the_defaults(capsys): + manager, il = FakeStateManager(), FakeInterlock() + assert set_default_beamline_states(_bec_il(manager, il, DEFAULT_DEVICES), session="omny") + assert _names(manager) == {s.name for s in DEFAULT_BEAMLINE_STATES} + assert "omny_vacuum" not in il.states_watched + assert "skipped" in capsys.readouterr().out + + +def test_matching_omny_states_untouched_but_interlock_synced(): + manager, il = FakeStateManager(beamline_states_for_session("omny")), FakeInterlock() + assert set_default_beamline_states(_bec_il(manager, il), session="omny") + assert manager.calls == [] + assert il.states_watched == {"omny_vacuum": ["valid"]} -- 2.54.1