179 lines
4.8 KiB
Python
179 lines
4.8 KiB
Python
__author__ = 'asc'
|
|
debug = True
|
|
from random import random
|
|
import Adafruit_BMP.BMP085 as BMP085
|
|
import picamera
|
|
import configparser
|
|
import time
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class Barometer_Debug ():
|
|
def __init__(self):
|
|
pass
|
|
|
|
@property
|
|
def status (self):
|
|
return (0, "Barometer functioning properly")
|
|
|
|
@property
|
|
def temperature (self):
|
|
if self.status[0]:
|
|
return 'error'
|
|
|
|
if debug:
|
|
temp = random()*100
|
|
else:
|
|
raise Exception ('Not Debug')
|
|
return temp
|
|
|
|
@property
|
|
def pressure (self):
|
|
if (self.status[0]):
|
|
return 'error'
|
|
|
|
if debug:
|
|
press = random()
|
|
else:
|
|
raise Exception ('Not Debug')
|
|
return press
|
|
|
|
@property
|
|
def altitude (self):
|
|
if self.status[0]:
|
|
return 'error'
|
|
if debug:
|
|
alt = random()*100000
|
|
else:
|
|
raise Exception ('Not Debug')
|
|
|
|
return alt
|
|
|
|
class Camera_Debug():
|
|
|
|
def __init__(self):
|
|
pass
|
|
|
|
@property
|
|
def status (self):
|
|
return (0, "Camera functioning properly")
|
|
|
|
def capture (self):
|
|
#todo capture image
|
|
return ({"file":open("image2.jpg", 'rb')})
|
|
|
|
#camera debug 2 uses actual camera
|
|
class Camera_Debug2():
|
|
def __init__(self, low_quality_resolution=(320,240),
|
|
low_quality_compression_pct=5,
|
|
high_quality_resolution=(2592,1944),
|
|
high_quality_compression_pct=85,
|
|
**kwargs):
|
|
logger.debug("Initializing camera")
|
|
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._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")
|
|
pass
|
|
|
|
@property
|
|
def status (self):
|
|
return (0, "Camera functioning properly")
|
|
|
|
def capture (self, no_low_quality=False, no_high_quality=False, **kwargs):
|
|
#todo image adjustments
|
|
img_hi = None
|
|
img_lo = None
|
|
if not no_high_quality:
|
|
logger.debug('Taking high quality photo')
|
|
self._cam.capture("temp_img_hi.jpg",
|
|
resize=self.high_quality_resolution,
|
|
quality=self.high_quality_compression_pct,
|
|
# **kwargs
|
|
)
|
|
img_hi = open("temp_img_hi.jpg", 'rb')
|
|
with open("temp_img_hi.jpg", 'rb') as f:
|
|
img_hi = f.read()
|
|
logger.debug('High quality photo taken')
|
|
|
|
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 = f.read()
|
|
logger.debug('Low quality photo taken')
|
|
|
|
return ({"hi":img_hi,
|
|
"lo":img_lo})
|
|
|
|
#barometerdebug2 uses actual barometer
|
|
class Barometer_Debug2 ():
|
|
def __init__(self):
|
|
logger.debug("Intializing barometer")
|
|
self.bmp = BMP085.BMP085()
|
|
logger.debug("Barometer 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 = {"altitude":alt,
|
|
"temperature":temp,
|
|
"pressure":press,
|
|
}
|
|
logger.debug("Barometer reads {}".format(result))
|
|
|
|
return result
|
|
|
|
|
|
|
|
|