first commit 10/20/2015
This commit is contained in:
85
datahandling.py
Normal file
85
datahandling.py
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
__author__ = 'asc'
|
||||||
|
import os
|
||||||
|
import datetime
|
||||||
|
from urllib import request
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
|
||||||
|
SCRIPTDIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
REPORTTOURL = "http://10.0.1.4:5010/report"
|
||||||
|
REPORTIMAGETOURL = "http://10.0.1.4:5010/photo"
|
||||||
|
|
||||||
|
class Datalogger_Debug ():
|
||||||
|
def __init__(self, path=SCRIPTDIR):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def log(self, message, type="text"):
|
||||||
|
if type == "text":
|
||||||
|
print ("%s - Log Message: %s"% (str(datetime.datetime.now()), message))
|
||||||
|
elif type == "image":
|
||||||
|
print ("%s - Log Image: %s"% (str(datetime.datetime.now()), message))
|
||||||
|
else:
|
||||||
|
raise Exception
|
||||||
|
|
||||||
|
class Datareporter_Debug ():
|
||||||
|
def __init__(self, path=SCRIPTDIR):
|
||||||
|
#TODO communication
|
||||||
|
pass
|
||||||
|
|
||||||
|
@property
|
||||||
|
def status (self):
|
||||||
|
return (0, "Data reporter functioning properly")
|
||||||
|
|
||||||
|
def send(self, message, type="text"):
|
||||||
|
if type == "text":
|
||||||
|
#TODO send text
|
||||||
|
print ("%s - Sent Message: %s"% (str(datetime.datetime.now()), message))
|
||||||
|
elif type == "image":
|
||||||
|
#todo send image
|
||||||
|
print ("%s - Sent Image: %s"% (str(datetime.datetime.now()), message))
|
||||||
|
else:
|
||||||
|
#todo error handling
|
||||||
|
raise Exception
|
||||||
|
|
||||||
|
class Datareporter_Debug2 ():
|
||||||
|
def __init__(self, path=SCRIPTDIR):
|
||||||
|
#TODO communication
|
||||||
|
pass
|
||||||
|
|
||||||
|
@property
|
||||||
|
def status (self):
|
||||||
|
#TODO status check
|
||||||
|
try:
|
||||||
|
check = json.loads(request.urlopen(REPORTTOURL).read().decode()).get('status')
|
||||||
|
pass
|
||||||
|
if not check:
|
||||||
|
return (0, "Data reporter functioning properly")
|
||||||
|
else:
|
||||||
|
return (1, check)
|
||||||
|
except Exception as e:
|
||||||
|
return (1, "Data reporter error: %s" % e)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def send(self, message, type="text"):
|
||||||
|
try:
|
||||||
|
if type == "text":
|
||||||
|
#TODO send text
|
||||||
|
print ("%s - Sent Message: %s"% (str(datetime.datetime.now()), message))
|
||||||
|
elif type == "image":
|
||||||
|
#todo send image
|
||||||
|
response = requests.post(REPORTIMAGETOURL, files={'file': message})
|
||||||
|
print ("%s - Sent Image: %s"% (str(datetime.datetime.now()), message))
|
||||||
|
elif type == "data":
|
||||||
|
#add date to message
|
||||||
|
message['sent']=str(datetime.datetime.now())
|
||||||
|
req = request.Request(REPORTTOURL)
|
||||||
|
req.add_header('Content-Type', 'application/json')
|
||||||
|
response = request.urlopen(req,json.dumps(message).encode())
|
||||||
|
the_page = response.read()
|
||||||
|
|
||||||
|
return 0
|
||||||
|
except Exception as e:
|
||||||
|
#todo error handling
|
||||||
|
return (0, "Reporter error: %s" % e)
|
||||||
|
|
||||||
101
instruments.py
Normal file
101
instruments.py
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
__author__ = 'asc'
|
||||||
|
debug = True
|
||||||
|
from random import random
|
||||||
|
|
||||||
|
|
||||||
|
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("image.jpg", 'rb')})
|
||||||
|
|
||||||
|
|
||||||
|
#barometerdebug2 uses actual barometer
|
||||||
|
# class Barometer_Debug2 ():
|
||||||
|
# import Adafruit_BMP.BMP085 as BMP085
|
||||||
|
# def __init__(self):
|
||||||
|
# self.bmp = BMP085.BMP085()
|
||||||
|
# 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()
|
||||||
|
# # alt = 100000*random()
|
||||||
|
# return alt
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
127
main.py
Normal file
127
main.py
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
__author__ = 'asc'
|
||||||
|
DEBUG = True
|
||||||
|
|
||||||
|
if DEBUG:
|
||||||
|
from instruments import Barometer_Debug as Barometer
|
||||||
|
from instruments import Camera_Debug as Camera
|
||||||
|
from datahandling import Datalogger_Debug as Datalogger
|
||||||
|
from datahandling import Datareporter_Debug2 as Datareporter
|
||||||
|
from system import System_Debug as System
|
||||||
|
import threading
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
# else:
|
||||||
|
# from instruments import Barometer, Camera
|
||||||
|
# from datahandling import Datalogger, Datareporter
|
||||||
|
# from system import System
|
||||||
|
# import threading
|
||||||
|
|
||||||
|
#start-up
|
||||||
|
#log denotes write to local, report denotes sending to server
|
||||||
|
|
||||||
|
#TODO startup message
|
||||||
|
|
||||||
|
logger = Datalogger ()
|
||||||
|
logger.log ("System Startup")
|
||||||
|
logger.log("Log Intiated")
|
||||||
|
|
||||||
|
#system check
|
||||||
|
system = System()
|
||||||
|
system_status = system.status
|
||||||
|
|
||||||
|
logger.log (system_status[1])
|
||||||
|
|
||||||
|
#todo test cell connection, log, report
|
||||||
|
reporter = Datareporter()
|
||||||
|
reporter_status = reporter.status
|
||||||
|
|
||||||
|
logger.log (reporter_status[1])
|
||||||
|
reporter.send(reporter_status[1])
|
||||||
|
reporter.send(system_status[1])
|
||||||
|
|
||||||
|
#TODO test camera, log, report
|
||||||
|
camera = Camera ()
|
||||||
|
camera_status = camera.status
|
||||||
|
logger.log (camera_status[1])
|
||||||
|
reporter.send(camera_status[1])
|
||||||
|
|
||||||
|
#todo test barometer, log, report
|
||||||
|
barometer = Barometer()
|
||||||
|
barometer_status = barometer.status
|
||||||
|
logger.log (barometer_status[1])
|
||||||
|
reporter.send(barometer_status[1])
|
||||||
|
|
||||||
|
#todo test GPS, log, report
|
||||||
|
|
||||||
|
#check for errors, throw exception if error
|
||||||
|
if(system_status[0] or reporter_status[0] or camera_status[0]):
|
||||||
|
raise Exception ('Error')
|
||||||
|
|
||||||
|
if DEBUG:
|
||||||
|
#rate refresh hi-res camera images, saved locally, in seconds
|
||||||
|
refresh_camera_local = 2
|
||||||
|
refresh_camera_transmit = 5
|
||||||
|
|
||||||
|
refresh_barometer_local = 1
|
||||||
|
refresh_barometer_transmit = 5
|
||||||
|
|
||||||
|
refresh_gps_local = 2
|
||||||
|
refresh_gps_transmit = 5
|
||||||
|
|
||||||
|
else:
|
||||||
|
refresh_camera_local = 10
|
||||||
|
refresh_camera_transmit= 60
|
||||||
|
|
||||||
|
refresh_barometer_local = 10
|
||||||
|
refresh_barometer_transmit= 60
|
||||||
|
|
||||||
|
refresh_gps_local = 10
|
||||||
|
refresh_gps_transmit= 60
|
||||||
|
|
||||||
|
def update_barometer():
|
||||||
|
#refresh each instrument
|
||||||
|
alt = barometer.altitude
|
||||||
|
press = barometer.pressure
|
||||||
|
temp = barometer.temperature
|
||||||
|
|
||||||
|
#log instrument info
|
||||||
|
logger.log({"altitude":alt,
|
||||||
|
"temperature":temp,
|
||||||
|
"pressure":press,
|
||||||
|
"sent":datetime.datetime.now()
|
||||||
|
})
|
||||||
|
|
||||||
|
#report instrument info
|
||||||
|
reporter.send({"altitude":alt,
|
||||||
|
"temperature": temp,
|
||||||
|
"pressure":press,
|
||||||
|
"sent":datetime.datetime.now()
|
||||||
|
},type="data")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def update_camera():
|
||||||
|
image = camera.capture()
|
||||||
|
#log image
|
||||||
|
logger.log(image.get('file'), type="image")
|
||||||
|
#report image
|
||||||
|
reporter.send(image.get('file'), type="image")
|
||||||
|
pass
|
||||||
|
|
||||||
|
def scheduler (interval, worker_func, iterations = 0):
|
||||||
|
if iterations != 1:
|
||||||
|
threading.Timer (
|
||||||
|
interval,
|
||||||
|
scheduler, [interval, worker_func, 0 if iterations == 0 else iterations-1]
|
||||||
|
).start ()
|
||||||
|
|
||||||
|
worker_func ()
|
||||||
|
|
||||||
|
#while 1:
|
||||||
|
|
||||||
|
update_camera()
|
||||||
|
#todo break apart log and report refresh
|
||||||
|
scheduler(refresh_barometer_local, update_barometer, 20)
|
||||||
|
scheduler(refresh_camera_local, update_camera, 1)
|
||||||
|
|
||||||
|
pass
|
||||||
12
system.py
Normal file
12
system.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
__author__ = 'asc'
|
||||||
|
|
||||||
|
class System_Debug():
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
#TODO add system initialization
|
||||||
|
pass
|
||||||
|
|
||||||
|
@property
|
||||||
|
def status (self):
|
||||||
|
#TODO status check
|
||||||
|
return (0, "System functioning properly")
|
||||||
21
test.py
Normal file
21
test.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import csv, requests
|
||||||
|
from instruments import Camera_Debug as Camera
|
||||||
|
from datahandling import Datareporter_Debug2 as Datareporter
|
||||||
|
|
||||||
|
REPORTIMAGETOURL = "http://10.0.1.4:5010/photo"
|
||||||
|
|
||||||
|
LOG_FILE = "log2.txt"
|
||||||
|
|
||||||
|
data = {"temp":1,"press":3,"altitude":2,"cheetas":"just enough"}
|
||||||
|
|
||||||
|
camera = Camera ()
|
||||||
|
reporter = Datareporter ()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
image = camera.capture()
|
||||||
|
|
||||||
|
response = requests.post(REPORTIMAGETOURL, files={'file': image.get('file')})
|
||||||
|
#report image
|
||||||
|
reporter.send(image.get('file'), type="image")
|
||||||
|
pass
|
||||||
12
test_retrieve_data.py
Normal file
12
test_retrieve_data.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
__author__ = 'asc'
|
||||||
|
|
||||||
|
import json
|
||||||
|
from urllib import request
|
||||||
|
|
||||||
|
|
||||||
|
req = request.Request("http://home.ascorrea.com:5010/retrieve-report")
|
||||||
|
# req.add_header('Content-Type', 'application/json')
|
||||||
|
response = request.urlopen(req)
|
||||||
|
r = response.read()
|
||||||
|
json.loads(r.decode()).get('data')
|
||||||
|
pass
|
||||||
Reference in New Issue
Block a user