From 29a5cfa9c26e3f877ea8e386af896081912f85ad Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 14 Jun 2018 00:34:30 +0200 Subject: [PATCH] Refactored camera.pi backend. Support for start/stop recording control via plugin action. Introduced support for taking pictures too --- platypush/backend/camera/pi.py | 39 +++++++++++++++++++++------- platypush/plugins/camera/__init__.py | 0 platypush/plugins/camera/pi.py | 25 ++++++++++++++++++ 3 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 platypush/plugins/camera/__init__.py create mode 100644 platypush/plugins/camera/pi.py diff --git a/platypush/backend/camera/pi.py b/platypush/backend/camera/pi.py index 65f0cdf6..77f270db 100644 --- a/platypush/backend/camera/pi.py +++ b/platypush/backend/camera/pi.py @@ -2,6 +2,8 @@ import socket import time import picamera +from threading import Event + from platypush.backend import Backend @@ -12,6 +14,7 @@ class CameraPiBackend(Backend): video_stabilization=False, ISO=0, exposure_compensation=0, exposure_mode='auto', meter_mode='average', awb_mode='auto', image_effect='none', color_effects=None, rotation=0, + start_recording_on_startup=True, crop=(0.0, 0.0, 1.0, 1.0), **kwargs): """ See https://www.raspberrypi.org/documentation/usage/camera/python/README.md for a detailed reference about the Pi camera options """ @@ -19,6 +22,7 @@ class CameraPiBackend(Backend): super().__init__(**kwargs) self.listen_port = listen_port + self.start_recording_on_startup = start_recording_on_startup self.server_socket = socket.socket() self.server_socket.bind(('0.0.0.0', self.listen_port)) self.server_socket.listen(0) @@ -41,30 +45,47 @@ class CameraPiBackend(Backend): self.camera.color_effects = color_effects self.camera.rotation = rotation self.camera.crop = crop + self.start_recording_event = Event() + self.stop_recording_event = Event() + + if self.start_recording_on_startup: + self.start_recording_event.set() self.logger.info('Initialized Pi camera backend') - def send_message(self, msg): - pass + def take_picture(self, image_file): + self.logger.info('Capturing camera snapshot to {}'.format(image_file)) + self.camera.capture(image_file) + self.logger.info('Captured camera snapshot to {}'.format(image_file)) + + def start_recording(self): + if not self.start_recording_event.is_set(): + self.start_recording_event.set() + self.stop_recording_event.clear() + + def stop_recording(self): + if not self.stop_recording_event.is_set(): + self.stop_recording_event.set() + self.start_recording_event.clear() def run(self): super().run() while True: + self.start_recording_event.wait() + self.logger.start('Starting camera recording') + connection = self.server_socket.accept()[0].makefile('wb') try: self.camera.start_recording(connection, format='h264') - while True: - self.camera.wait_recording(60) + self.camera.stop_recording_event.wait() + self.logger.info('Stopping camera recording') except ConnectionError as e: pass finally: - try: - self.camera.stop_recording() - connection.close() - except: - pass + connection.close() + self.camera.stop_recording() # vim:sw=4:ts=4:et: diff --git a/platypush/plugins/camera/__init__.py b/platypush/plugins/camera/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/platypush/plugins/camera/pi.py b/platypush/plugins/camera/pi.py new file mode 100644 index 00000000..653baa8b --- /dev/null +++ b/platypush/plugins/camera/pi.py @@ -0,0 +1,25 @@ +from platypush.context import get_backend +from platypush.message.response import Response + +from platypush.plugins import Plugin + + +class CameraPiPlugin(Plugin): + def start_recording(self): + camera = get_backend('camera.pi') + camera.start_recording() + return Response(output={'status':'ok'}) + + def stop_recording(self): + camera = get_backend('camera.pi') + camera.stop_recording() + return Response(output={'status':'ok'}) + + def take_picture(self, image_file): + camera = get_backend('camera.pi') + camera.take_picture(image_file) + return Response(output={'image_file':image_file}) + + +# vim:sw=4:ts=4:et: +