BEC: added better exception hadnling
This commit is contained in:
@@ -7,6 +7,8 @@ from bec_lib.procedures.helper import FrontendProcedureHelper, BackendProcedureH
|
||||
|
||||
from aare.common.beamline import MXBeamline, mx_beamline, cfg_get
|
||||
from aare.common.logger_config import setup_logger
|
||||
from aare.common.logger_events import log_timing
|
||||
from aare.common.exception_handler import BECCommunicationError
|
||||
|
||||
logger = setup_logger("aareDAQ")
|
||||
|
||||
@@ -21,10 +23,6 @@ logger = setup_logger("aareDAQ")
|
||||
# helper.get.running_procedures()
|
||||
# helper.request.abort_queue()
|
||||
|
||||
def bec_exception_handler(exception: Exception):
|
||||
print(f"Exception: {exception}")
|
||||
|
||||
|
||||
class BeamlineState(str, Enum):
|
||||
ROBOT_SAMPLE_EXCHANGE = "robot_sample_exchange"
|
||||
SAMPLE_ALIGNMENT = "sample_alignment"
|
||||
@@ -61,6 +59,23 @@ class BECClientWorker:
|
||||
except Exception as e:
|
||||
raise Exception(f"Error: {e}")
|
||||
|
||||
def _raise_bec_error(self, exc: Exception, *, operation: str) -> None:
|
||||
message = f"BEC operation '{operation}' failed: {type(exc).__name__}: {exc}"
|
||||
logger.exception(message)
|
||||
|
||||
if isinstance(exc, AssertionError):
|
||||
raise BECCommunicationError(
|
||||
f"BEC internal assertion failed during '{operation}'",
|
||||
operation=operation,
|
||||
exception=exc,
|
||||
) from exc
|
||||
|
||||
raise BECCommunicationError(
|
||||
message,
|
||||
operation=operation,
|
||||
exception=exc,
|
||||
) from exc
|
||||
|
||||
def __planner(self):
|
||||
if self.simulated is None:
|
||||
logger.debug("Simulating planner")
|
||||
@@ -74,7 +89,10 @@ class BECClientWorker:
|
||||
if self.simulated:
|
||||
logger.debug(f"Simulating macro {macro_name}")
|
||||
return None
|
||||
return self.client.proc.run_macro(macro_name, *args, queue=queue)
|
||||
try:
|
||||
return self.client.proc.run_macro(macro_name, *args, queue=queue)
|
||||
except Exception as e:
|
||||
self._raise_bec_error(e, operation=f"run_macro:{macro_name}")
|
||||
|
||||
def run_macro_blocked(self, macro_name:str, *args, queue:str = "default", **kwargs):
|
||||
if self.simulated:
|
||||
@@ -87,14 +105,18 @@ class BECClientWorker:
|
||||
print(status)
|
||||
return status
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
return None
|
||||
self._raise_bec_error(e, operation=f"run_macro_blocked:{macro_name}")
|
||||
|
||||
@log_timing(logger, "BEC move_to")
|
||||
def move_to(self, state:BeamlineState):
|
||||
logger.info(f"BEC move_to requested: {state.value}")
|
||||
if self.simulated:
|
||||
logger.debug(f"Simulating move to {state.value}")
|
||||
return True
|
||||
return self.planner.move_to(state)
|
||||
try:
|
||||
return self.planner.move_to(state)
|
||||
except Exception as e:
|
||||
self._raise_bec_error(e, operation=f"planner.move_to:{state.value}")
|
||||
|
||||
def show_all_devices(self):
|
||||
return self.dev.show_all
|
||||
@@ -119,10 +141,14 @@ class BECClientWorker:
|
||||
|
||||
def det_z(self, value:float, timeout:int | None = None ):
|
||||
"""timeout is None or integer in s"""
|
||||
status = self.scans.mv(self.dev.det_z, value, relative=False)
|
||||
if timeout:
|
||||
status.wait(timeout=timeout)
|
||||
return status
|
||||
try:
|
||||
status = self.scans.mv(self.dev.det_z, value, relative=False)
|
||||
if timeout:
|
||||
status.wait(timeout=timeout)
|
||||
return status
|
||||
except Exception as e:
|
||||
self._raise_bec_error(e, operation=f"scans.mv:det_z:{value}")
|
||||
|
||||
# not implemented yet
|
||||
# self.cryo_pos = PD.cryo_pos
|
||||
# self.xrf_pos = PD.xrf_po
|
||||
@@ -143,20 +169,21 @@ if __name__ == "__main__":
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
# det_value = 900
|
||||
# print(f"moving detector to vale:{det_value}")
|
||||
# start = time.perf_counter()
|
||||
det_value = 980
|
||||
print(f"moving detector to vale:{det_value}")
|
||||
start = time.perf_counter()
|
||||
# print(client.get_det_z())
|
||||
# client.det_z(value=det_value, timeout=10)
|
||||
# print(time.perf_counter() - start)
|
||||
# print(client.get_det_z)
|
||||
# det_value = 985
|
||||
# print(f"moving detector to vale:{det_value}")
|
||||
# status = client.det_z(value=det_value)
|
||||
# status.wait(timeout=5)
|
||||
# print(client.get_det_z())
|
||||
status = client.det_z(value=det_value)
|
||||
status.wait(timeout=5)
|
||||
print(status)
|
||||
print(client.get_det_z())
|
||||
#client.mono_pitch_scan_runner()
|
||||
client.change_energy(12000)
|
||||
#client.change_energy(12000)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
Reference in New Issue
Block a user