diff --git a/src/aare/devices/bec_worker.py b/src/aare/devices/bec_worker.py index 164bae2e..2ee98499 100644 --- a/src/aare/devices/bec_worker.py +++ b/src/aare/devices/bec_worker.py @@ -2,6 +2,7 @@ from bec_lib.client import BECClient from bec_ipython_client import BECIPythonClient from bec_lib.service_config import ServiceConfig from bec_lib.procedures.helper import FrontendProcedureHelper, BackendProcedureHelper +#from pxii_bec.macros.pxii_guards import GuardViolation #specify up to 10 queue to runs in parallel, request more if needed! @@ -15,9 +16,28 @@ from bec_lib.procedures.helper import FrontendProcedureHelper, BackendProcedureH # helper.get.running_procedures() # helper.request.abort_queue() +def bec_exception_handler(exception: Exception): + print(f"Exception: {exception}") + +class OurBECDevices: + def __init__(self, BECDevices): + print("initialising devices") + init_positioned_devices() + dev = BECDevices + print("devices initialised") + self.coll_y = MultiPositionDevice(device = PD.coll_y, + states = dev.coll_y.user_parameter) + self.scintillator_diode_y = MultiPositionDevice(device = PD.diag_y, + states = dev.diag_y.user_parameter) + self.backlight_pos = PositionedDevice(device = PD.bl_pos) + #self.beamstop_pos = PositionedDevice(device = PD.bs_pos) + self.beamstop_z = GuardedPositionedDevice(device = PD.bs_z) + + self.goniometer_x = GuardedPositionedDevice(device = PD.gon_x) # may get deprecated with the IOC + class BECClientWorker: def __init__(self, service_config:ServiceConfig, name:str = "default"): - self.client = BECClient(config=service_config, name=name) + self.client = BECIPythonClient(config=service_config) self.client.start() #self.client.config.update_session_with_file("/sls/x10sa/config/bec/production/bec/bec_lib/bec_lib/config_helper.py") self.dev = self.client.device_manager.devices @@ -25,6 +45,8 @@ class BECClientWorker: self.macros = self.client.macros self.load_user_macros() self.helper = FrontendProcedureHelper(self.client.connector) + self.ProtectedDevices = OurBECDevices(BECDevices=self.dev) + def run_macro(self, macro_name:str, *args, queue:str = "default", **kwargs): return self.client.proc.run_macro(macro_name, *args, queue=queue) @@ -53,14 +75,14 @@ class BECClientWorker: self.client.shutdown() def common2rse(self): - status = self.scans.umv(dev.blight_pos, 0, relative=False) + status = self.scans.umv(self.dev.blight_pos, 0, relative=False) try: SE.blpos.checkpos() except NotImplemented as e: print(f"Backlight position not checked: {e}") print(f"Moved backlight to position {self.dev.blight_pos.read()} with status {status.status}") - self.scans.umv(dev.bs_z, 72.0, dev.coll_y, 20.0, dev.scin_y, 20.0, dev.cryo_pos, 1, relative=False) - self.scans.umv(dev.bs_pos, 0, relative=False) + self.scans.umv(self.dev.bs_z, 72.0, self.dev.coll_y, 20.0, self.dev.scin_y, 20.0, self.dev.cryo_pos, 1, relative=False) + self.scans.umv(self.dev.bs_pos, 0, relative=False) print(f"Moved everything else to park position with status {status.status}") if self.dev.bs_z.read().value != 72.0:# or self.dev.coll_y.read() != 20.0 or self.dev.scin_y.read() != 20.0: @@ -71,9 +93,6 @@ class BECClientWorker: self.scans.umv(self.dev.blight_pos, 1, relative=False) self.scans.umv(self.dev.bs_pos, 1, relative = False) - def a2e_runner(self, a, *hkl): - return a2e(a, *hkl) - def mono_pitch_scan_runner(self): print('this is a print statement from inside DAQ not BEC') try: @@ -82,10 +101,98 @@ class BECClientWorker: print(f"Error: {e}") +class MultiPositionDevice: + """inherits from the MultiPositionDevice BEC class + can move in and out + :param device is inisitialised with initialise_PD_devices() and can be called with PD.device_name + :states is a dictionary of str, float from bec.dev.device_name.user_parameter + """ + + def __init__(self, device, states: dict[str, float] | None = None): + self.device = device + self.states = states + + def move_to(self, position: str): + self.device.move_to(position) + + @property + def actual(self) -> float: + return self.device.actual + + @property + def state(self) -> str: + """returns the current state of the device as a string or unknown if out of a valid state position""" + return self.device.state + + @property + def is_at(self, state:str) -> bool: + return self.device.is_at("state") + + @property + def is_clear(self) -> bool: + return self.device.is_clear() + + +class PositionedDevice: + """inherits from the PositionedDevice BEC class + can move in and out + :param device is inisitialised with initialise_PD_devices() and can be called with PD.device_name + """ + + def __init__(self, device): + self.device = device + self.states = ["in", "out"] + + def move_in(self): + self.device.mvin() + + + def move_out(self): + self.device.mvout() + + @property + def is_in(self) -> bool: + return self.device.is_in() + + @property + def is_out(self) -> bool: + return self.device.is_out() + + +class GuardedPositionedDevice: + """inherits from the GuardedAxis BEC class + :param device is inisitialised with initialise_PD_devices() and can be called with PD.device_name + allowed functions are move(float) and actual which returns the feedback""" + + def __init__(self, device): + self.device = device + + def move(self, position: float): + self.device.move(position) + + @property + def actual(self) -> float: + return self.device.actual + + + + + + # not implemented yet + # self.cryo_pos = PD.cryo_pos + # self.xrf_pos = PD.xrf_po + if __name__ == "__main__": import time - service_config = ServiceConfig(redis={"host": "x10sa-bec-001.psi.ch", "port": 6379}) - client = BECClientWorker(service_config, "default") + print('starting BEC Client') + try: + service_config = ServiceConfig(redis={"host": "x10sa-bec-001.psi.ch", "port": 6379}) + client = BECClientWorker(service_config) + except Exception as e: + print(f"Error: {e}") + import sys + sys.exit(1) + client.show_all_devices() client.list_all_macros() try: @@ -95,10 +202,9 @@ if __name__ == "__main__": # energy = get_current_energy() # pos = get_dcm_motors_positions(energy) # print(energy, pos) - #init_positioned_devices() - #PD.xeye_x.mvout() + print(bs_z_policy(15.0)) #client.scans.umv(client.dev.xeye_x, 0, relative=False) - client.mono_pitch_scan_runner() + #client.mono_pitch_scan_runner() #client.mono_pitch_scan_runner() #b=client.run_macro_blocked("a2e", 160, "iln", queue="test") #b = client.run_macro_blocked("mono_pitch_scan", False, queue="default") @@ -112,6 +218,10 @@ if __name__ == "__main__": #print(status) #client.common2rse() #print(status) + except GuardViolation as e: + print(f"GuardViolation: {e}") + except RuntimeError as e: + print(f"RuntimeError: {e}") except Exception as e: print(f"Error: {e}") client.shutdown_client()