Adds driverConfig.json, persistence, scaling, etc.

This commit is contained in:
Patrick McDonagh
2017-11-27 22:20:10 -06:00
parent 7d39dd5e63
commit b87eb09d79
4 changed files with 264 additions and 14 deletions

View File

@@ -1,10 +1,11 @@
{
"name": "pondlevel",
"driverFilename": "pondlevel.py",
"driverId": "0130",
"name": "pondlevel",
"driverFilename": "pondlevel.py",
"driverId": "0130",
"additionalDriverFiles": [
"calibration_db.py"
],
"version": 1,
"calibration_db.py",
"persistence.py"
],
"version": 1,
"s3BucketName": "pondlevel"
}
}

View File

@@ -0,0 +1,21 @@
"""Data persistance functions."""
# if more advanced persistence is needed, use a sqlite database
import json
def load(filename="persist.json"):
"""Load persisted settings from the specified file."""
try:
with open(filename, 'r') as persist_file:
return json.load(persist_file)
except Exception:
return False
def store(persist_obj, filename="persist.json"):
"""Store the persisting settings into the specified file."""
try:
with open(filename, 'w') as persist_file:
return json.dump(persist_obj, persist_file)
except Exception:
return False

View File

@@ -4,8 +4,8 @@ import threading
from device_base import deviceBase
import calibration_db as db
import time
import random
import json
import persistence
_ = None
@@ -18,6 +18,16 @@ send_time = 300
temp_pl = pond_level['value']
persist_data = persistence.load()
def scale_to_eu(inp_measurement, raw_max, raw_min, eu_max, eu_min):
"""Scale the inp_measurement to engineering units."""
m = (eu_max - eu_min) / (raw_max - raw_min)
b = eu_max - raw_max * m
scaled = inp_measurement * m + b
return scaled
class start(threading.Thread, deviceBase):
"""Start class required by Meshify."""
@@ -33,6 +43,11 @@ class start(threading.Thread, deviceBase):
self.forceSend = False
threading.Thread.start(self)
self.PL_RAWMAX = 20.0
self.PL_RAWMIN = 4.0
self.PL_EUMAX = 23.068
self.PL_EUMIN = 0.0
# 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):
@@ -43,11 +58,26 @@ class start(threading.Thread, deviceBase):
"""Actually run the driver."""
global pond_level, temp_pl, send_delta, send_time
wait_sec = 10
try:
self.PL_RAWMAX = persist_data['raw_max']
self.PL_RAWMIN = persist_data['raw_min']
self.PL_EUMAX = persist_data['eu_max']
self.PL_EUMIN = persist_data['eu_min']
except KeyError:
print("No persistence data for scaling parameters.")
except TypeError:
print("No persistence data for scaling parameters.")
wait_sec = 30
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...")
self.sendtodb("setrawmax", self.PL_RAWMAX, 0)
self.sendtodb("setrawmin", self.PL_RAWMIN, 0)
self.sendtodb("seteumax", self.PL_EUMAX, 0)
self.sendtodb("seteumin", self.PL_EUMIN, 0)
send_loops = 0
while True:
send_now = False
@@ -55,11 +85,9 @@ class start(threading.Thread, deviceBase):
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()
mcu_status = self.mcu.getDict() # Gets a dictionary of the IO states
cloop_val = float(mcu_status['cloop'])
temp_pl = scale_to_eu(cloop_val, self.PL_RAWMAX, self.PL_RAWMIN, self.PL_EUMAX, self.PL_EUMIN)
if abs(temp_pl - pond_level['value']) > send_delta:
print("Sending {} due to value change".format(temp_pl))
@@ -103,3 +131,41 @@ class start(threading.Thread, deviceBase):
return True
except KeyError:
return "Misformed JSON"
def store_scaling_data(self):
"""Store scaling data in the persist file."""
scale_obj = {
'raw_max': self.PL_RAWMAX,
'raw_min': self.PL_RAWMIN,
'eu_max': self.PL_EUMAX,
'eu_min': self.PL_EUMIN
}
persistence.store(scale_obj)
def pondlevel_setrawmin(self, name, value):
"""Set the raw min scaling value."""
self.PL_RAWMIN = float(value)
self.sendtodb("setrawmin", self.PL_RAWMIN, 0)
self.store_scaling_data()
return(True)
def pondlevel_setrawmax(self, name, value):
"""Set the raw max scaling value."""
self.PL_RAWMAX = float(value)
self.sendtodb("setrawmax", self.PL_RAWMAX, 0)
self.store_scaling_data()
return(True)
def pondlevel_seteumin(self, name, value):
"""Set the gpm min scaling value."""
self.PL_EUMIN = float(value)
self.sendtodb("seteumin", self.PL_EUMIN, 0)
self.store_scaling_data()
return(True)
def pondlevel_seteumax(self, name, value):
"""Set the gpm max scaling value."""
self.PL_EUMAX = float(value)
self.sendtodb("seteumax", self.PL_EUMAX, 0)
self.store_scaling_data()
return(True)