From b87eb09d79cc59ee2ef5ce9e0be2815e1fb98b69 Mon Sep 17 00:00:00 2001 From: Patrick McDonagh Date: Mon, 27 Nov 2017 22:20:10 -0600 Subject: [PATCH] Adds driverConfig.json, persistence, scaling, etc. --- html_templates/Scaling.html | 162 ++++++++++++++++++++++++++++++++ python_driver/driverConfig.json | 15 +-- python_driver/persistence.py | 21 +++++ python_driver/pondlevel.py | 80 ++++++++++++++-- 4 files changed, 264 insertions(+), 14 deletions(-) create mode 100644 html_templates/Scaling.html create mode 100644 python_driver/persistence.py diff --git a/html_templates/Scaling.html b/html_templates/Scaling.html new file mode 100644 index 0000000..29f104e --- /dev/null +++ b/html_templates/Scaling.html @@ -0,0 +1,162 @@ + + +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ + + + + diff --git a/python_driver/driverConfig.json b/python_driver/driverConfig.json index f69cb03..f73b9f4 100644 --- a/python_driver/driverConfig.json +++ b/python_driver/driverConfig.json @@ -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" -} \ No newline at end of file +} diff --git a/python_driver/persistence.py b/python_driver/persistence.py new file mode 100644 index 0000000..ed65271 --- /dev/null +++ b/python_driver/persistence.py @@ -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 diff --git a/python_driver/pondlevel.py b/python_driver/pondlevel.py index 061ccf4..31f2e63 100644 --- a/python_driver/pondlevel.py +++ b/python_driver/pondlevel.py @@ -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)