205 lines
6.7 KiB
Python
205 lines
6.7 KiB
Python
__author__ = 'asc'
|
|
debug = True
|
|
from random import random
|
|
|
|
import configparser
|
|
import time
|
|
import logging
|
|
import base64
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class Camera:
|
|
def __init__(self,
|
|
low_quality_resolution=(320,240),
|
|
low_quality_compression_pct=5,
|
|
high_quality_resolution=(2592,1944),
|
|
high_quality_compression_pct=85,
|
|
debug = False,
|
|
**kwargs):
|
|
logger.info("Initializing camera", extra={'MISSION_TIME': "", 'MISSION_ID':""})
|
|
time.sleep(1)
|
|
self.low_quality_resolution = low_quality_resolution
|
|
self.low_quality_compression_pct = low_quality_compression_pct
|
|
self.high_quality_resolution = high_quality_resolution
|
|
self.high_quality_compression_pct = high_quality_compression_pct
|
|
self.kwargs=kwargs
|
|
self._debug = debug
|
|
if not self._debug:
|
|
import picamera
|
|
self._cam = picamera.PiCamera(resolution=high_quality_resolution)
|
|
# if "vflip" in kwargs.keys():
|
|
for k in kwargs.keys():
|
|
setattr(self._cam, k, kwargs.get(k))
|
|
|
|
logger.debug("Camera intialized", extra={'MISSION_TIME': "", 'MISSION_ID':""})
|
|
pass
|
|
|
|
@property
|
|
def status (self):
|
|
return (0, "Camera functioning properly")
|
|
|
|
def capture (self, name, no_low_quality=False, no_high_quality=False, **kwargs):
|
|
#todo image adjustments
|
|
img_hi = None
|
|
img_lo = None
|
|
r = {}
|
|
if not self._debug:
|
|
if not no_high_quality:
|
|
logger.debug('Taking high quality photo', extra={'MISSION_TIME': "", 'MISSION_ID':""})
|
|
self._cam.capture("temp_img_hi.jpg",
|
|
resize=self.high_quality_resolution,
|
|
quality=self.high_quality_compression_pct,
|
|
**kwargs
|
|
)
|
|
with open("temp_img_hi.jpg", 'rb') as f:
|
|
# img_hi = base64.b64encode(f.read())
|
|
img_hi = str(base64.b64encode(f.read()),'ascii')
|
|
r['hi']=("{}_hi.jpg".format(name), img_hi, "image/jpeg")
|
|
logger.debug('High quality photo taken', extra={'MISSION_TIME': "", 'MISSION_ID':""})
|
|
|
|
if not no_low_quality:
|
|
time.sleep(1)
|
|
logger.debug('Taking low quality photo (Resolution: {}, JPEG Quality: {}%)'.format(self.low_quality_resolution, self.low_quality_compression_pct))
|
|
self._cam.capture("temp_img_lo.jpg",
|
|
resize=self.low_quality_resolution,
|
|
quality=self.low_quality_compression_pct,
|
|
**kwargs
|
|
)
|
|
with open("temp_img_lo.jpg", 'rb') as f:
|
|
img_lo = str(base64.b64encode(f.read()),'ascii')
|
|
# img_lo = str(f.read())
|
|
r['lo']=("{}_lo.jpg".format(name), img_lo, "image/jpeg")
|
|
logger.debug('Low quality photo taken', extra={'MISSION_TIME': "", 'MISSION_ID':""})
|
|
else:
|
|
if not no_high_quality:
|
|
with open("temp_img_hi.jpg", 'rb') as f:
|
|
img_hi = str(base64.b64encode(f.read()),'ascii')
|
|
# img_hi = str(f.read(),'ascii')
|
|
r['hi']=("{}_hi.jpg".format(name), img_hi, "image/jpeg")
|
|
logger.info('High quality photo taken', extra={'MISSION_TIME': "", 'MISSION_ID':""})
|
|
|
|
if not no_low_quality:
|
|
with open("temp_img_lo.jpg", 'rb') as f:
|
|
img_lo = str(base64.b64encode(f.read()),'ascii')
|
|
# img_lo = str(f.read())
|
|
r['lo']=("{}_lo.jpg".format(name), img_lo, "image/jpeg")
|
|
logger.info('Low quality photo taken', extra={'MISSION_TIME': "", 'MISSION_ID':""})
|
|
|
|
return r
|
|
|
|
class Barometer:
|
|
def __init__(self, debug=False):
|
|
logger.debug("Intializing barometer", extra={'MISSION_TIME': "", 'MISSION_ID':""})
|
|
if not debug:
|
|
import Adafruit_BMP.BMP085 as BMP085
|
|
self.bmp = BMP085.BMP085()
|
|
logger.debug("Barometer intilized", extra={'MISSION_TIME': "", 'MISSION_ID':""})
|
|
pass
|
|
|
|
@property
|
|
def status (self):
|
|
return (0, "Barometer functioning properly")
|
|
|
|
@property
|
|
def temperature (self):
|
|
if self.status[0]:
|
|
return 'error'
|
|
|
|
temp = self.bmp.read_temperature()
|
|
return temp
|
|
|
|
@property
|
|
def pressure (self):
|
|
if (self.status[0]):
|
|
return 'error'
|
|
|
|
# press = 100000*random()
|
|
press = self.bmp.read_pressure()
|
|
|
|
return press
|
|
|
|
@property
|
|
def altitude (self):
|
|
if self.status[0]:
|
|
return 'error'
|
|
#TODO Set the altitude of your current location in meter
|
|
alt = self.bmp.read_altitude()
|
|
return alt
|
|
|
|
def read(self):
|
|
logger.debug("Reading from barometer", extra={'MISSION_TIME': "", 'MISSION_ID':""})
|
|
#refresh each instrument
|
|
if not debug:
|
|
alt = self.altitude
|
|
press = self.pressure
|
|
temp = self.temperature
|
|
else:
|
|
temp = random()*100
|
|
press = random()
|
|
alt = random()*100000
|
|
result = {"a":alt,
|
|
"t":temp,
|
|
"p":press,
|
|
}
|
|
logger.debug("Barometer reads {}".format(result), extra={'MISSION_TIME': "", 'MISSION_ID':""})
|
|
|
|
return result
|
|
|
|
class Gps:
|
|
def __init__(self):
|
|
logger.debug("Intializing GPS")
|
|
# self.bmp = BMP085.BMP085()
|
|
logger.debug("Gps intilized")
|
|
pass
|
|
|
|
@property
|
|
def status (self):
|
|
return (0, "Barometer functioning properly")
|
|
|
|
@property
|
|
def temperature (self):
|
|
if self.status[0]:
|
|
return 'error'
|
|
|
|
temp = self.bmp.read_temperature()
|
|
return temp
|
|
|
|
@property
|
|
def pressure (self):
|
|
if (self.status[0]):
|
|
return 'error'
|
|
|
|
# press = 100000*random()
|
|
press = self.bmp.read_pressure()
|
|
|
|
return press
|
|
|
|
@property
|
|
def altitude (self):
|
|
if self.status[0]:
|
|
return 'error'
|
|
#TODO Set the altitude of your current location in meter
|
|
alt = self.bmp.read_altitude()
|
|
return alt
|
|
|
|
def read(self):
|
|
logger.debug("Reading from barometer")
|
|
#refresh each instrument
|
|
alt = self.altitude
|
|
press = self.pressure
|
|
temp = self.temperature
|
|
result = {"a":alt,
|
|
"t":temp,
|
|
"p":press,
|
|
}
|
|
logger.debug("Gps reads {}".format(result))
|
|
|
|
return result
|
|
# class Gps:
|
|
|
|
|
|
|
|
|
|
|