Reorganize and add calibration database

This commit is contained in:
Patrick McDonagh
2017-11-16 17:55:49 -06:00
parent caed9a07fb
commit 7d39dd5e63
15 changed files with 231 additions and 10 deletions

105
python_driver/pondlevel.py Normal file
View File

@@ -0,0 +1,105 @@
"""Driver for connecting Pond Level to Meshify."""
import threading
from device_base import deviceBase
import calibration_db as db
import time
import random
import json
_ = None
pond_level = {
'value': 15.0,
'timestamp': 0
}
send_delta = 0.5
send_time = 300
temp_pl = pond_level['value']
class start(threading.Thread, deviceBase):
"""Start class required by Meshify."""
def __init__(self, name=None, number=None, mac=None, Q=None, mcu=None, companyId=None, offset=None, mqtt=None, Nodes=None):
"""Initialize the driver."""
threading.Thread.__init__(self)
deviceBase.__init__(self, name=name, number=number, mac=mac, Q=Q, mcu=mcu, companyId=companyId, offset=offset, mqtt=mqtt, Nodes=Nodes)
self.daemon = True
self.version = "1"
self.finished = threading.Event()
self.forceSend = False
threading.Thread.start(self)
# this is a required function for all drivers, its goal is to upload some piece of data
# about your device so it can be seen on the web
def register(self):
"""Register the driver."""
self.sendtodb("log", "BOOM! Booted.", 0)
def run(self):
"""Actually run the driver."""
global pond_level, temp_pl, send_delta, send_time
wait_sec = 10
for i in range(0, wait_sec):
print("pondlevel driver will start in {} seconds".format(wait_sec - i))
time.sleep(1)
print("BOOM! Starting pondlevel driver...")
send_loops = 0
while True:
send_now = False
if self.forceSend:
print "FORCE SEND: TRUE"
send_now = True
up_down = 1
if bool(random.getrandbits(1)):
up_down = -1
temp_pl = pond_level['value'] + up_down * random.random()
if abs(temp_pl - pond_level['value']) > send_delta:
print("Sending {} due to value change".format(temp_pl))
send_now = True
elif (time.time() - pond_level['timestamp']) > send_time:
print("Sending {} due to time limit".format(temp_pl))
send_now = True
if send_now:
self.sendtodb('pond_level', temp_pl, 0)
pond_level['value'] = temp_pl
pond_level['timestamp'] = time.time()
print("pondlevel driver still alive...")
if self.forceSend:
if send_loops > 2:
print("Turning off forceSend")
self.forceSend = False
send_loops = 0
else:
send_loops += 1
time.sleep(10)
def send_calibration_points(self):
"""Send calibration data from database to Meshify."""
cal_data = db.get_calibration_data()
self.sendtodb('pondlevel', json.dumps(cal_data), 0)
def pondlevel_sync(self, name, value):
"""Sync all data from the driver."""
self.forceSend = True
self.sendtodb("log", "synced", 0)
return True
def pondlevel_addcalibrationpoint(self, name, value):
"""Add a JSON calibration point to the database."""
try:
new_point = json.loads(value)
db.insert_calibration_data(new_point['height'], new_point['volume'])
self.send_calibration_points()
return True
except KeyError:
return "Misformed JSON"