Skip to content

Commit

Permalink
added camera connection check
Browse files Browse the repository at this point in the history
  • Loading branch information
exddc committed Jun 24, 2024
1 parent 96f431a commit df38193
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions agent/video_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,13 @@ def _toggle_video(self, command):
def _start_video_stream(self):
"""Start the video stream."""
LOGGER.info("Starting video stream")
if not self._check_camera_status():
LOGGER.error("Cannot start video stream.")
return

self._picamera.start_recording(MJPEGEncoder(), FileOutput(OUTPUT))
try:
self._streaming = True
__server = StreamingServer(self._address, StreamingHandler)
__server.serve_forever()
# pylint: disable=broad-except
Expand All @@ -156,6 +161,22 @@ def _stop_video_stream(self):
finally:
self._streaming = False

def _check_camera_status(self):
"""Check the camera status."""
try:
self._picamera.capture_file("test_image.jpg")

if os.path.exists("test_image.jpg"):
os.remove("test_image.jpg")
LOGGER.debug("Camera connected and working.")
return True

LOGGER.error("Camera connected, but failed to capture image.")
return False
except Picamera2.PiCameraError as e:
LOGGER.error("Camera not connected: %s", e)
return False

def stop(self):
"""Stop the agent."""
self._stop_video_stream()
Expand Down

0 comments on commit df38193

Please sign in to comment.