Files
spaceballoon/datahandling.py

86 lines
2.7 KiB
Python

__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)