Restructured camera rescue

This commit is contained in:
Joel Collins 2021-01-06 17:31:45 +00:00
parent 3501a5b624
commit 61554af270

View file

@ -2,21 +2,23 @@ import logging
from .error_sources import ErrorSource from .error_sources import ErrorSource
picamera_import_error = ErrorSource( # Error/exception messages we have enumerated and expect
( PICAMERA_ERROR_MAP = {
"Picamera module could not be imported.", "Camera is not enabled. Try running 'sudo raspi-config' and ensure that the camera has been enabled.": ErrorSource(
"Check physical connections to the camera as it may be damaged or disconnected.", "Camera is not enabled. Try running 'sudo raspi-config' and ensure that the camera has been enabled."
) ),
) "Failed to enable connection: Out of resources": ErrorSource(
"Camera already in use by another application. Please reboot your microscope."
picamera_errors = ( ),
"Camera is not enabled. Try running 'sudo raspi-config' and ensure that the camera has been enabled.",
)
picamera_mmal_errors = {
"Failed to enable connection: Out of resources": "Camera already in use by another application."
} }
# Basic import error. Usually damaged or disconnected camera
PICAMERA_IMPORT_ERROR = ErrorSource(
(
"Picamera module could not be imported. Check physical connections to the camera as it may be damaged or disconnected.",
)
)
def main(): def main():
error_sources = [] error_sources = []
@ -25,20 +27,15 @@ def main():
try: try:
import picamerax import picamerax
except Exception as e: # pylint: disable=W0703 except Exception as e: # pylint: disable=W0703
# TODO: Parse exception object for a few different issues, and append error_sources.append(PICAMERA_IMPORT_ERROR)
# different error sources E.g. pi with camera already in use,
# disconnected, unsupported OS, etc
logging.error(e)
error_sources.append(picamera_import_error)
else: else:
try: try:
_ = picamerax.PiCamera() _ = picamerax.PiCamera()
except picamerax.PiCameraError as e: except picamerax.PiCameraError as e:
if e.args[0] in picamera_errors: msg = e.args[0]
error_sources.append(ErrorSource(e.args[0])) if msg in PICAMERA_ERROR_MAP:
elif e.args[0] in picamera_mmal_errors: error_sources.append(PICAMERA_ERROR_MAP[msg])
error_sources.append(ErrorSource(picamera_mmal_errors[e.args[0]]))
else: else:
error_sources.append(ErrorSource(e.args[0])) error_sources.append(ErrorSource("Unenumerated exception: " + msg))
return error_sources return error_sources