From 3840b15d7d41b0b6025ad2eb1319ad138ed816a4 Mon Sep 17 00:00:00 2001 From: Romain Bazile Date: Thu, 6 May 2021 02:32:42 +0200 Subject: [PATCH] imager: add proper handling of startup exceptions --- scripts/main.py | 19 +++++++++++++------ scripts/planktoscope/imager.py | 16 +++++++++++----- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index 1cf598c..5ba8dd4 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -120,9 +120,14 @@ if __name__ == "__main__": stepper_thread.start() # Starts the imager control process - logger.info("Starting the imager control process (step 3/4)") - imager_thread = planktoscope.imager.ImagerProcess(shutdown_event) - imager_thread.start() + logger.info("Starting the imager control process (step 3/6)") + try: + imager_thread = planktoscope.imager.ImagerProcess(shutdown_event) + except: + logger.error("The imager control process could not be started") + imager_thread = None + else: + imager_thread.start() # Starts the segmenter process logger.info("Starting the segmenter control process (step 4/4)") @@ -148,7 +153,7 @@ if __name__ == "__main__": if not stepper_thread.is_alive(): logger.error("The stepper process died unexpectedly! Oh no!") break - if not imager_thread.is_alive(): + if imager_thread and not imager_thread.is_alive(): logger.error("The imager process died unexpectedly! Oh no!") break if not segmenter_thread.is_alive(): @@ -160,12 +165,14 @@ if __name__ == "__main__": shutdown_event.set() time.sleep(1) stepper_thread.join() - imager_thread.join() + if imager_thread: + imager_thread.join() segmenter_thread.join() # Uncomment this for clean shutdown # module_thread.join() stepper_thread.close() - imager_thread.close() + if imager_thread: + imager_thread.close() segmenter_thread.close() # Uncomment this for clean shutdown # module_thread.close() diff --git a/scripts/planktoscope/imager.py b/scripts/planktoscope/imager.py index a46728a..a8608f0 100644 --- a/scripts/planktoscope/imager.py +++ b/scripts/planktoscope/imager.py @@ -93,7 +93,7 @@ class StreamingHandler(http.server.BaseHTTPRequestHandler): try: while True: try: - with open("/dev/shm/mjpeg/cam.jpg", "rb") as jpeg: + with open("/dev/shm/mjpeg/cam.jpg", "rb") as jpeg: # nosec frame = jpeg.read() except FileNotFoundError as e: logger.error(f"Camera has not been started yet") @@ -129,7 +129,6 @@ logger.info("planktoscope.imager is loaded") class ImagerProcess(multiprocessing.Process): """This class contains the main definitions for the imager of the PlanktoScope""" - @logger.catch def __init__(self, stop_event, iso=100, shutter_speed=1): """Initialize the Imager class @@ -179,7 +178,14 @@ class ImagerProcess(multiprocessing.Process): logger.exception( f"An exception has occured when starting up raspimjpeg: {e}" ) - exit(1) + try: + self.__camera.start(true) + except Exception as e: + logger.exception( + f"A second exception has occured when starting up raspimjpeg: {e}" + ) + logger.error(f"This error can't be recovered from, terminating now") + raise e if self.__camera.sensor_name == "IMX219": # Camera v2.1 self.__resolution = (3280, 2464) @@ -653,7 +659,7 @@ class ImagerProcess(multiprocessing.Process): return logger.debug(f"Copying the image from the temp file to {filename_path}") - shutil.copy("/dev/shm/mjpeg/image.jpg", filename_path) + shutil.copy("/dev/shm/mjpeg/image.jpg", filename_path) # nosec # TODO Try to stop the camera streaming and display instead each captured image # os.rename("/dev/shm/mjpeg/image.jpg", "/dev/shm/mjpeg/cam.jpg") logger.debug("Syncing the disk") @@ -792,4 +798,4 @@ class ImagerProcess(multiprocessing.Process): # This is called if this script is launched directly if __name__ == "__main__": # TODO This should be a test suite for this library - pass \ No newline at end of file + pass