From bd84f2368fcc64b3d9acc4c69f6132d6ff2be616 Mon Sep 17 00:00:00 2001 From: appleb_m Date: Tue, 25 Nov 2025 18:01:07 +0100 Subject: [PATCH] daq:added http error handling for automation --- daq/src/aaredaq/daq.py | 39 ++++++++++++++++++++------------------- daq/src/aaredaq/server.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/daq/src/aaredaq/daq.py b/daq/src/aaredaq/daq.py index cc71a2ca..5a9cb43c 100644 --- a/daq/src/aaredaq/daq.py +++ b/daq/src/aaredaq/daq.py @@ -49,9 +49,22 @@ class TransformationInvalidException(Exception): class LoopCenteringFailed(Exception): - logger.error(f"Loop centering failed", extra={"exception:" : Exception}) - pass + def __init__(self, message="Loop Centering did not detect a sample"): + super().__init__(message) + self.message = message + logger.error(f"{message}", extra={"exception:" : Exception}) + def __str__(self): + return self.message + +class MountingFailed(Exception): + def __init__(self, message="A sample was not mounted"): + super().__init__(message) + self.message = message + logger.error(f"{message}", extra={"exception:" : Exception}) + + def __str__(self): + return self.message class AareDAQ: def __init__(self, cfg: BeamlineConfig, bl: MXBeamline): @@ -359,7 +372,7 @@ class AareDAQ: logger.error("No sample was detected in gripper") self.__devs.tell.dry(wait=True) self.__aare.sample_failed(target, failed_comment = "No sample was detected in gripper") - raise Exception("No sample was detected in gripper, drying gripper") + raise MountingFailed(f"No sample was detected in gripper, drying gripper {target.sample_name} {target.location} {target.pin}") elif value == "dry": logger.info("Robot is drying") else: @@ -370,7 +383,7 @@ class AareDAQ: if not mounted: logger.error(f"Failed to mount target: {target.db_id} {target.location} {target.pin}") self.__aare.sample_failed(target, failed_comment = "No sample was mounted") - raise Exception("No sample was mounted") + raise MountingFailed(f"No Sample detected on smart magent: {target.sample_name} {target.location} {target.pin}") if target is not None: self.__aare.sample_mounted(target) @@ -475,11 +488,6 @@ class AareDAQ: grid.file_prefix = f"{old_prefix}_{grid.omega_deg}deg" grid.omega_deg = geom.omega_deg res1 = self.__raster(grid) - self.__devs.reflector_up = True - time.sleep(0.1) - self.save_screenshot_db(sample.db_id, f"post_raster_{grid.omega_deg}deg") - time.sleep(0.1) - self.__devs.reflector_up = False grid.omega_deg += 90 self.__devs.aerotech.move(grid.omega_deg, wait=True) @@ -494,11 +502,6 @@ class AareDAQ: # self.__devs.smargon.target = geom.translate_smargon(offset) grid.smargon_top_left = geom.translate_smargon(offset) res2 = self.__raster(grid) - self.__devs.reflector_up = True - time.sleep(0.1) - self.save_screenshot_db(sample.db_id, f"post_raster_{grid.omega_deg}deg") - time.sleep(0.1) - self.__devs.reflector_up = False return CompletedRasterGrid(r=[res1, res2]) else: return None @@ -590,6 +593,9 @@ class AareDAQ: self.__devs.smargon.target = save_smargon_position if self.sample is not None and self.sample.db_id is not None and result is not None: + self.__devs.reflector_up = True + time.sleep(0.1) + self.save_screenshot_db(self.sample.db_id, f"post_raster_{r.omega_deg}deg") try: self.__aare.ingest_gridscan(sample = self.sample, raster_result =result, raster_request = r, geom = self.sample_geometry, @@ -607,11 +613,6 @@ class AareDAQ: else: raster_result = self.__raster(r) result = CompletedRasterGrid(r=[raster_result]) - self.__devs.reflector_up = True - time.sleep(0.1) - if self.sample is not None and self.sample.db_id is not None: - self.save_screenshot_db(self.sample.db_id, f"post_raster_{r.omega_deg}deg") - time.sleep(0.1) self.__set_state(BeamlineStateEnum.SampleAlignment) self.__cfg.state_busy = False return result diff --git a/daq/src/aaredaq/server.py b/daq/src/aaredaq/server.py index 0272a6d6..a53f79bb 100644 --- a/daq/src/aaredaq/server.py +++ b/daq/src/aaredaq/server.py @@ -25,7 +25,7 @@ from urllib3.exceptions import InsecureRequestWarning from aaredaq import auth from aaredaqlib.beamline import mx_beamline from aaredaq.config import BeamlineConfig -from aaredaq.daq import AareDAQ +from aaredaq.daq import AareDAQ, LoopCenteringFailed, TransformationInvalidException, MountingFailed from aaredaq.camera_stat_thread import start_image_stats_receiver, stop_image_stats_receiver @@ -395,9 +395,33 @@ async def rotation(val: RotationScanRequest, token: str = Depends(oauth2_scheme) @app.post("/scan/auto") async def auto(s: SampleShortInfo, token: str = Depends(oauth2_scheme)): auth.check_jwt_rw(cfg, auth.parse_token(token)) - runtime = daq.measure(s) + try: + runtime = daq.measure(s) + return f"{runtime:0.3f}" + except LoopCenteringFailed(Exception) as e: + raise HTTPException( + status_code=api_status.HTTP_404_NOT_FOUND, + detail=f"Loop centering failed: {e}", + ) + except TransformationInvalidException(Exception) as e: + raise HTTPException( + status_code=api_status.HTTP_400_BAD_REQUEST, + detail=f"Transformation invalid: {e}", + ) + except MountingFailed(Exception) as e: + raise HTTPException( + status_code=api_status.HTTP_404_NOT_FOUND, + detail=f"Mounting failed: {e}", + ) + except Exception as e: + logger.error(f"Exception in auto: {e}") + raise HTTPException( + status_code=api_status.HTTP_500_INTERNAL_SERVER_ERROR, + detail=f"error {e}" + ) return f"{runtime:0.3f}" + @app.post("/scan/smart_params") async def set_smart_params(p: SimpleScanParameters, token: str = Depends(oauth2_scheme)) -> str: token_data = auth.parse_token(token)