175 lines
4.7 KiB
Python
175 lines
4.7 KiB
Python
__author__ = 'asc'
|
|
DEBUG = True
|
|
import logging
|
|
import logging.handlers,logging.config
|
|
|
|
from instruments import Barometer, Camera
|
|
from datahandling import Datalogger, Datareporter
|
|
from system import System as System
|
|
from threading import Timer
|
|
from datahandling import Record
|
|
from mission import Mission
|
|
import random
|
|
import string
|
|
|
|
# class ContextFilter(logging.Filter):
|
|
# """
|
|
# This is a filter which injects contextual information into the log.
|
|
# """
|
|
# def filter(self, record):
|
|
# record.MYVAR = MYVAR
|
|
# return True
|
|
|
|
logging.config.fileConfig('logging.ini')
|
|
|
|
log = logging.getLogger('root')
|
|
log.addFilter(logging.Filter('root'))
|
|
# log.addFilter(ContextFilter())
|
|
#start-up
|
|
#log denotes write to local, report denotes sending to server
|
|
|
|
#loadconfig
|
|
from config import *
|
|
|
|
mission = Mission()
|
|
|
|
#todo test cell connection
|
|
reporter = Datareporter (
|
|
missiontime = mission,
|
|
use_lan = True,
|
|
url = url,
|
|
server_port = 5010,
|
|
data_path = data_path,
|
|
image_path = image_path,
|
|
ping_path = ping_path,
|
|
com_port_name=com_port_name,
|
|
baud_rate = baud_rate
|
|
)
|
|
|
|
t = Record({'zt':mission.timezero})
|
|
# mid = reporter.send(t) #reply of first message is the mission number
|
|
|
|
# mission.set_mid(mid)
|
|
|
|
notebook = Datalogger (
|
|
missiontime = mission,
|
|
text_path=None,
|
|
log_path=log_path,
|
|
photo_path=photo_path)
|
|
|
|
log.debug ("System started", extra={'MISSION_TIME': "", 'MISSION_ID':""})
|
|
|
|
#system check
|
|
system = System()
|
|
log.debug (system.stats, extra={'MISSION_TIME': "", 'MISSION_ID':""})
|
|
|
|
#set mission time
|
|
|
|
log.info("Mission {} started, time zero is {}".format(mission.name, mission.timezero), extra={'MISSION_TIME': mission.now(), 'MISSION_ID':mission.name})
|
|
|
|
|
|
#intiate mission
|
|
|
|
|
|
|
|
# log.info('Sent {} to server'.format(intiate))
|
|
|
|
#TODO test camera
|
|
camera = Camera (low_quality_compression_pct=low_quality_compression_pct,
|
|
low_quality_resolution=low_quality_resolution,
|
|
high_quality_compression_pct=high_quality_compression_pct,
|
|
high_quality_resolution=high_quality_resolution,
|
|
vflip=False,
|
|
hflip=False,
|
|
exposure_mode='sports',
|
|
debug=True
|
|
)
|
|
|
|
#todo test barometer
|
|
barometer = Barometer(debug=True)
|
|
|
|
#todo test GPS, log, report
|
|
|
|
#todo check for errors, throw exception if error
|
|
|
|
class scheduler ():
|
|
|
|
def __init__(self, interval, function, *args, **kwargs):
|
|
self._timer = None
|
|
self.interval = interval
|
|
self.function = function
|
|
self.args = args
|
|
self.kwargs = kwargs
|
|
self.is_running = False
|
|
self.start()
|
|
|
|
def _run(self):
|
|
self.is_running = False
|
|
self.start()
|
|
self.function(*self.args, **self.kwargs)
|
|
|
|
def start(self):
|
|
if not self.is_running:
|
|
self._timer = Timer(self.interval, self._run)
|
|
self._timer.start()
|
|
self.is_running = True
|
|
|
|
def stop(self):
|
|
self._timer.cancel()
|
|
self.is_running = False
|
|
|
|
def update_barometer_local():
|
|
global bar
|
|
bar = barometer.read()
|
|
bar.update({'mt':mission.now()})
|
|
record = Record(bar.copy(),'b')
|
|
log.info('Updating barometer...',extra={'MISSION_TIME': mission.now(), 'MISSION_ID':mission.name})
|
|
notebook.log(record)
|
|
|
|
def update_image_local():
|
|
global img
|
|
img = camera.capture(name=mission.now())
|
|
record = Record([img.get('hi'), img.get('lo')], 'i')
|
|
log.info('Updating image...', extra={'MISSION_TIME': mission.now(), 'MISSION_ID':mission.name})
|
|
notebook.log(record)
|
|
|
|
def submit_report():
|
|
global bar
|
|
global img
|
|
global counter
|
|
global transpondence
|
|
|
|
# reporter.create_transpondence()
|
|
|
|
if not transpondence:
|
|
log.info('Creating transpondence',extra={'MISSION_TIME': mission.now(), 'MISSION_ID':mission.name})
|
|
transpondence = Record()
|
|
|
|
if (counter % refresh_barometer_transmit) == 0:
|
|
log.info('Adding barometer data to transpondence',extra={'MISSION_TIME': mission.now(), 'MISSION_ID':mission.name})
|
|
transpondence.add(bar,'b')
|
|
|
|
if (counter % refresh_camera_transmit) == 0:
|
|
log.info('Adding image to transpondence',extra={'MISSION_TIME': mission.now(), 'MISSION_ID':mission.name})
|
|
transpondence.add(img.get('lo'),'i')
|
|
|
|
log.info('Sending transpondence', extra={'MISSION_TIME': mission.now(), 'MISSION_ID':mission.name})
|
|
transpondence = reporter.send(transpondence) #returns none on success, (bad practice?)
|
|
counter += 1
|
|
log.debug('Counter = {}'.format(counter), extra={'MISSION_TIME': mission.now(), 'MISSION_ID':mission.name})
|
|
|
|
counter=1
|
|
|
|
img = None
|
|
bar = None
|
|
transpondence = None
|
|
|
|
scheduler(1, update_barometer_local)
|
|
scheduler(refresh_camera_local, update_image_local)
|
|
scheduler(2, submit_report)
|
|
|
|
|
|
|
|
|
|
pass
|