import datetime
import time

from cnavbot import settings
from cnavbot.utils import logger, sentry
from cnavbot.messaging import messages, service


class Camera(service.Resource):
    topics = {
        'take_picture': settings.CAMERA_TOPIC,
    }

    def __init__(self, *args, **kwargs):
        super(Camera, self).__init__(*args, **kwargs)

        self.interval = kwargs.pop('interval', settings.CAMERA_INTERVAL)
        self.resolution = kwargs.pop('resolution', settings.CAMERA_RESOLUTION)
        self.camera = kwargs.pop('camera', settings.CAMERA)

    def run(self):
        with self.camera.PiCamera() as camera:
            camera.resolution = self.resolution
            while True:
                time.sleep(self.interval)
                self.take_picture(camera)

    def take_picture(self, camera):
        message = messages.FilePath(topic=self.topics['take_picture'])
        message.set_file_path(file_name=self.get_file_name())

        camera.capture(message.file_path)
        logger.debug("Picture taken: '{}'".format(message.file_path))

        self.publisher.send(message)

    @staticmethod
    def get_file_name():
        return "{}.jpg".format(datetime.datetime.utcnow().isoformat())


class Service(service.Service):
    name = 'camera'
    resource = Camera
    address = settings.LOCAL_CAMERA_PUBLISHER_ADDRESS
    port = settings.CAMERA_PORT_ADDRESS


@sentry
def start():
    return Service()


if __name__ == '__main__':
    start()

@@ -9,16 +9,26 @@
 atetime%0A
+import io%0A
 import t
@@ -208,28 +208,24 @@
        '
-take_
 picture
+s
 ': setti
@@ -247,16 +247,45 @@
 C,%0A    %7D
+%0A    capture_to_stream = True
 %0A%0A    de
@@ -767,16 +767,17 @@
 terval)%0A
+%0A
         
@@ -788,71 +788,221 @@
     
+if 
 self.
-take_picture(camera)%0A%0A    def take_picture(self, camera):%0A
+capture_to_stream:%0A                    message = messages.Base64()%0A                    destination = io.BytesIO()%0A                    capture_args = (destination, 'jpeg')%0A                else:%0A            
     
@@ -1037,43 +1037,22 @@
 ath(
-topic=self.topics%5B'take_picture'%5D)%0A
+)%0A            
     
@@ -1113,32 +1113,42 @@
 ())%0A
-%0A        camera.capture(
+                    destination = 
 mess
@@ -1160,17 +1160,16 @@
 ile_path
-)
 %0A       
@@ -1173,106 +1173,489 @@
     
-log
+            messa
 ge
-r
 .d
-ebug(%22Picture taken: '%7B%7D'%22.format(message.file_path))%0A%0A        self.publisher.send(message
+ata = destination%0A                    capture_args = (destination, )%0A%0A                self.take_picture(camera, *capture_args)%0A%0A                if self.capture_to_stream:%0A                    message.data = destination.read()%0A%0A                message.topic = self.topics%5B'pictures'%5D%0A                self.publisher.send(message)%0A%0A    def take_picture(self, camera, capture_args):%0A        logger.debug(%22Taking picture%22)%0A        return camera.capture(*capture_args
 )%0A%0A 

Add capturing pictures to stream by default