# Path: auv_control_pi/models.py
# class Configuration(SingletonModel):
# 
#     auv_id = models.UUIDField(blank=True, null=True)
#     auth_token = models.CharField(max_length=1024, blank=True)
#     update_frequency = models.DecimalField(blank=True, null=True,
#                                            max_digits=5, decimal_places=3)
#     left_motor_channel = models.IntegerField(default=0)
#     right_motor_channel = models.IntegerField(default=1)
#     trim = models.IntegerField(default=0)
#     name = models.CharField(max_length=255, blank=True)
#     address = models.CharField(max_length=255, blank=True)
#     crossbar_url = models.CharField(max_length=255, default='ws://localhost:8000/ws')
#     crossbar_realm = models.CharField(max_length=255, default='realm1')
# 
#     # auto pilot config vars
#     kP = models.FloatField(blank=True, default=1)
#     kI = models.FloatField(blank=True, default=0)
#     kD = models.FloatField(blank=True, default=0)
#     target_waypoint_distance = models.FloatField(blank=True, default=60)
#     pid_error_debounce = models.FloatField(blank=True, default=5)
# 
#     magbias_x = models.FloatField(blank=True, default=0)
#     magbias_y = models.FloatField(blank=True, default=0)
#     magbias_z = models.FloatField(blank=True, default=0)
#     declination = models.FloatField(blank=True, default=0)
#     board_offset = models.FloatField(blank=True, default=0)
# 
#     def __str__(self):
#         return 'AUV Configuration'
# 
#     class Meta:
#         verbose_name = "AUV Configuration"
# 
# Path: auv_control_pi/wamp.py
# class ApplicationSession(AutobahnApplicationSession):
#     name = ''
# 
#     @classmethod
#     def rpc_methods(cls, instance=None):
#         _cls = instance or cls
#         attrs = dir(_cls)
#         attrs.remove('rpc_methods')
#         methods = [getattr(_cls, attr) for attr in attrs if callable(getattr(_cls, attr))]
#         rpc_methods = [method for method in methods if getattr(method, 'is_rpc', False)]
#         return {method.rpc_uri: method for method in rpc_methods}
# 
#     @classmethod
#     def rpc_uris(cls, instance=None):
#         return cls.rpc_methods(instance).keys()
# 
#     @classmethod
#     def subcribtion_handlers(cls, instance=None):
#         _cls = instance or cls
#         attrs = dir(_cls)
#         attrs.remove('subcribtion_handlers')
#         methods = [getattr(_cls, attr) for attr in attrs if callable(getattr(_cls, attr))]
#         handlers = [method for method in methods if getattr(method, 'is_subcription', False)]
#         return {method.topic: method for method in handlers}
# 
#     def onConnect(self):
#         logger.info('Connecting to {} as {}'.format(self.config.realm, self.name))
#         self.join(realm=self.config.realm)
# 
#     async def onJoin(self, details):
#         logger.info('Joined realm as {}'.format(self.name))
#         for rpc_uri, method in self.rpc_methods(self).items():
#             logger.debug('Registering RPC: {}'.format(rpc_uri))
#             await self.register(method, rpc_uri)
# 
#         for topic, handler in self.subcribtion_handlers(self).items():
#             logger.debug('Subscribing To Topic: {}'.format(topic))
#             await self.subscribe(handler, topic)
# 
#         loop = asyncio.get_event_loop()
#         loop.create_task(self.update())
# 
# def rpc(rpc_uri=None):
#     if rpc_uri is None:
#         raise ValueError('Must provide rpc_uri')
# 
#     def outer(fnc):
#         @wraps(fnc)
#         def inner(*args, **kwargs):
#             return fnc(*args, **kwargs)
#         inner.is_rpc = True
#         inner.rpc_uri = rpc_uri
#         return inner
#     return outer


import asyncio
import logging
import shutil
import requests
from goprocam import GoProCamera
from datauri import DataURI
from ..models import Configuration
from ..wamp import ApplicationSession, rpc



logger = logging.getLogger(__name__)
config = Configuration.get_solo()


def download_image(url):
    resp = requests.get(url, stream=True)
    with open('img.jpg', 'wb') as f:
        resp.raw.decode_content = True
        shutil.copyfileobj(resp.raw, f)


# Path: auv_control_pi/models.py
# class Configuration(SingletonModel):
# 
#     auv_id = models.UUIDField(blank=True, null=True)
#     auth_token = models.CharField(max_length=1024, blank=True)
#     update_frequency = models.DecimalField(blank=True, null=True,
#                                            max_digits=5, decimal_places=3)
#     left_motor_channel = models.IntegerField(default=0)
#     right_motor_channel = models.IntegerField(default=1)
#     trim = models.IntegerField(default=0)
#     name = models.CharField(max_length=255, blank=True)
#     address = models.CharField(max_length=255, blank=True)
#     crossbar_url = models.CharField(max_length=255, default='ws://localhost:8000/ws')
#     crossbar_realm = models.CharField(max_length=255, default='realm1')
# 
#     # auto pilot config vars
#     kP = models.FloatField(blank=True, default=1)
#     kI = models.FloatField(blank=True, default=0)
#     kD = models.FloatField(blank=True, default=0)
#     target_waypoint_distance = models.FloatField(blank=True, default=60)
#     pid_error_debounce = models.FloatField(blank=True, default=5)
# 
#     magbias_x = models.FloatField(blank=True, default=0)
#     magbias_y = models.FloatField(blank=True, default=0)
#     magbias_z = models.FloatField(blank=True, default=0)
#     declination = models.FloatField(blank=True, default=0)
#     board_offset = models.FloatField(blank=True, default=0)
# 
#     def __str__(self):
#         return 'AUV Configuration'
# 
#     class Meta:
#         verbose_name = "AUV Configuration"
# 
# Path: auv_control_pi/wamp.py
# class ApplicationSession(AutobahnApplicationSession):
#     name = ''
# 
#     @classmethod
#     def rpc_methods(cls, instance=None):
#         _cls = instance or cls
#         attrs = dir(_cls)
#         attrs.remove('rpc_methods')
#         methods = [getattr(_cls, attr) for attr in attrs if callable(getattr(_cls, attr))]
#         rpc_methods = [method for method in methods if getattr(method, 'is_rpc', False)]
#         return {method.rpc_uri: method for method in rpc_methods}
# 
#     @classmethod
#     def rpc_uris(cls, instance=None):
#         return cls.rpc_methods(instance).keys()
# 
#     @classmethod
#     def subcribtion_handlers(cls, instance=None):
#         _cls = instance or cls
#         attrs = dir(_cls)
#         attrs.remove('subcribtion_handlers')
#         methods = [getattr(_cls, attr) for attr in attrs if callable(getattr(_cls, attr))]
#         handlers = [method for method in methods if getattr(method, 'is_subcription', False)]
#         return {method.topic: method for method in handlers}
# 
#     def onConnect(self):
#         logger.info('Connecting to {} as {}'.format(self.config.realm, self.name))
#         self.join(realm=self.config.realm)
# 
#     async def onJoin(self, details):
#         logger.info('Joined realm as {}'.format(self.name))
#         for rpc_uri, method in self.rpc_methods(self).items():
#             logger.debug('Registering RPC: {}'.format(rpc_uri))
#             await self.register(method, rpc_uri)
# 
#         for topic, handler in self.subcribtion_handlers(self).items():
#             logger.debug('Subscribing To Topic: {}'.format(topic))
#             await self.subscribe(handler, topic)
# 
#         loop = asyncio.get_event_loop()
#         loop.create_task(self.update())
# 
# def rpc(rpc_uri=None):
#     if rpc_uri is None:
#         raise ValueError('Must provide rpc_uri')
# 
#     def outer(fnc):
#         @wraps(fnc)
#         def inner(*args, **kwargs):
#             return fnc(*args, **kwargs)
#         inner.is_rpc = True
#         inner.rpc_uri = rpc_uri
#         return inner
#     return outer


# Path: auv_control_pi/components/camera.py
import asyncio
import logging
import shutil
import requests
from goprocam import GoProCamera
from datauri import DataURI
from ..models import Configuration
from ..wamp import ApplicationSession, rpc




logger = logging.getLogger(__name__)
config = Configuration.get_solo()


def download_image(url):
    resp = requests.get(url, stream=True)
    with open('img.jpg', 'wb') as f:
        resp.raw.decode_content = True
        shutil.copyfileobj(resp.raw, f)


    img_uri = DataURI.from_file('img.jpg')