fix: read beamline state from BEC on server startup
CI / lint (push) Canceled after 12s
CI / test (3.11) (push) Canceled after 10s
CI / test (3.12) (push) Canceled after 7s
CI / test (3.13) (push) Canceled after 5s
CI / test-with-beamline-plugins (pxi_bec) (push) Canceled after 2s
CI / test-with-beamline-plugins (pxii_bec) (push) Canceled after 0s
CI / test-with-beamline-plugins (pxiii_bec) (push) Canceled after 0s
CI / test-with-coverage (push) Canceled after 0s
CI / coverage-analysis (push) Canceled after 0s
Build and Publish / release (push) Canceled after 0s
Docs build and publish / docker (push) Successful in 8s

This commit was merged in pull request #143.
This commit is contained in:
2026-08-20 12:12:50 +02:00
committed by perl_d
parent 809818f9fa
commit dd04a2fc89
4 changed files with 36 additions and 2 deletions
+3
View File
@@ -357,6 +357,9 @@ class AareDAQ:
},
}
def read_current_state_from_bec(self):
return self._devs.read_current_state_from_bec()
def restart_bec_worker(self) -> dict[str, object]:
self._cfg.try_set_busy(timeout=360)
try:
+4 -1
View File
@@ -10,7 +10,7 @@ from aarecommon.config.logger import setup_logger
from aarecommon.config.logger_events import log_timing
from aarecommon.math.coordinate import AerotechCoordinate, SmargonCoordinate
from aarecommon.models.beamline import MXBeamline
from aarecommon.models.models import SampleCameraSettings, StagePositionEnum
from aarecommon.models.models import BeamlineStateEnum, SampleCameraSettings, StagePositionEnum
from epics import PV
from aare.devices import aerotech, smargon
@@ -353,6 +353,9 @@ class BeamlineDevices:
def smargon_initialize(self):
self._smargon.initialize()
def read_current_state_from_bec(self) -> BeamlineStateEnum:
return self.bec_worker.read_current_state()
if __name__ == "__main__":
from aarecommon.config.beamline import mx_beamline
+1
View File
@@ -109,6 +109,7 @@ async def lifespan(application: FastAPI):
bl = mx_beamline()
cfg = BeamlineConfig(bl)
daq = AareDAQ(cfg, bl)
cfg.state = daq.read_current_state_from_bec()
try:
cfg.reset_automation_progress()
+28 -1
View File
@@ -7,6 +7,7 @@ from aarecommon.config.logger import setup_logger
from aarecommon.config.logger_events import log_timing
from aarecommon.errors.exception_handler import BECCommunicationError
from aarecommon.models.beamline import MXBeamline
from aarecommon.models.models import BeamlineStateEnum
from bec_ipython_client import BECIPythonClient
from bec_ipython_client.signals import OperationMode
from bec_lib.device import RPCError, ScanRequestError
@@ -49,6 +50,24 @@ class BeamlineState(str, Enum):
XTAL_SNAPSHOT = "xtal_snapshot"
def _bec_state_to_aare_state(bec_state: BeamlineState) -> BeamlineStateEnum:
map = {
BeamlineState.BEAMSTOP_ALIGNMENT: BeamlineStateEnum.BeamstopAlignment,
BeamlineState.SAMPLE_ALIGNMENT: BeamlineStateEnum.SampleAlignment,
BeamlineState.DATA_COLLECTION: BeamlineStateEnum.DataCollection,
BeamlineState.DC_XRF: BeamlineStateEnum.XrayFluorescence,
BeamlineState.MANUAL_SAMPLE_EXCHANGE: BeamlineStateEnum.SampleExchange,
BeamlineState.BEAM_VISUALISATION: BeamlineStateEnum.BeamLocation,
BeamlineState.FLUX_MEASUREMENT: BeamlineStateEnum.FluxMeasurement,
BeamlineState.BEAMSTOP_ALIGNMENT: BeamlineStateEnum.BeamstopAlignment,
BeamlineState.MAINTENANCE: BeamlineStateEnum.Maintenance,
BeamlineState.XTAL_SNAPSHOT: BeamlineStateEnum.XtalSnapshot,
}
if bec_state in map:
return map[bec_state]
return BeamlineStateEnum.Maintenance
class BECClientWorker:
def __init__(self, beamline: MXBeamline, name: str = "default"):
BEAMLINE = beamline.value.lower()
@@ -101,7 +120,7 @@ class BECClientWorker:
self._zoom = self.dev.scam_zoom
self._ring_current = self.dev.sls_current
except Exception as e:
logger.error(f"Error initialising planar and position devices: {e}")
logger.error(f"Error initialising planner and position devices: {e}")
self.position_devices = None
self.planner = None
self._backlight_brightness = None
@@ -115,6 +134,14 @@ class BECClientWorker:
self.ring_current = None
raise RuntimeError(f"Error initialising BEC devices: {e}") from e
def read_current_state(self) -> BeamlineStateEnum:
if self.planner is None:
return BeamlineStateEnum.Maintenance
matching_states = self.planner.current_state()
if matching_states is None or len(matching_states) > 1:
return BeamlineStateEnum.Maintenance
return _bec_state_to_aare_state(matching_states[0])
def _bec_error(
self, exc: Exception, *, operation: str, tags: list[str] | None = None
) -> BECCommunicationError: