From e4234bc2c3c078e23247fcc42dd3a534eb7a3906 Mon Sep 17 00:00:00 2001 From: Patrick McDonagh Date: Fri, 1 Dec 2017 10:59:26 -0600 Subject: [PATCH] Completes MAXH2O-73 and MAXH2O-78 Adds scaling and GPM ignore limit. Increments version in S3 to 9 --- HTML/Overview.html | 37 ++++++++++++++++++ HTML/Scaling.html | 38 ++++++++++++++++++- config.txt | 19 +++++----- driverConfig.json | 11 +++--- flow-monitor.py | 95 ++++++++++++++++++++++++++++++---------------- persistence.py | 21 ++++++++++ 6 files changed, 172 insertions(+), 49 deletions(-) create mode 100644 persistence.py diff --git a/HTML/Overview.html b/HTML/Overview.html index ef07ee2..6c2baa4 100644 --- a/HTML/Overview.html +++ b/HTML/Overview.html @@ -1,3 +1,33 @@ +
+ + + +
+

GPM Flow

@@ -109,3 +139,10 @@
+ diff --git a/HTML/Scaling.html b/HTML/Scaling.html index 1fb1a13..099faf4 100644 --- a/HTML/Scaling.html +++ b/HTML/Scaling.html @@ -134,7 +134,41 @@ - + +
+
+

GPM Ignore Limit

+
+
+ +
+ +
GPM
+
+
+
+ " + data-techname="<%=channels["flowmonitor.setgpmignorelimit"].techName %>" + data-name="<%= channels["flowmonitor.setgpmignorelimit"].name%>" + data-nodechannelcurrentId="<%= channels["flowmonitor.setgpmignorelimit"].nodechannelcurrentId %>" + id="<%= channels["flowmonitor.setgpmignorelimit"].channelId %>" + class="btn btn-large btn-theme animated setstatic material-icons">send +
+
+
+ @@ -149,4 +183,4 @@ $('.channel_value').each(function(topLevel){ } }); }); - \ No newline at end of file + diff --git a/config.txt b/config.txt index bc1dee6..0a927ee 100644 --- a/config.txt +++ b/config.txt @@ -1,10 +1,11 @@ { - -"driverFileName":"flow-monitor.py", -"deviceName":"flowmonitor", -"driverId":"0140", -"releaseVersion":"8", -"files": { - "file1":"flow-monitor.py", - "file2":"utilities.py"} -} + "files": { + "file3": "persistence.py", + "file2": "utilities.py", + "file1": "flow-monitor.py" + }, + "deviceName": "flowmonitor", + "driverId": "0180", + "releaseVersion": "9", + "driverFileName": "flow-monitor.py" +} \ No newline at end of file diff --git a/driverConfig.json b/driverConfig.json index d9d92c4..9695777 100644 --- a/driverConfig.json +++ b/driverConfig.json @@ -1,9 +1,10 @@ { - "name": "flowmonitor", - "driverFilename": "flow-monitor.py", + "s3BucketName": "flow-monitor", "additionalDriverFiles": [ - "utilities.py" + "utilities.py", + "persistence.py" ], - "version": 8, - "s3BucketName": "flow-monitor" + "name": "flowmonitor", + "version": 9, + "driverFilename": "flow-monitor.py" } \ No newline at end of file diff --git a/flow-monitor.py b/flow-monitor.py index 23791a3..31157e5 100644 --- a/flow-monitor.py +++ b/flow-monitor.py @@ -4,6 +4,7 @@ import time from datetime import datetime import sqlite3 from device_base import deviceBase +import persistence from utilities import get_public_ip_address CREATE_FLOWDATA_TABLE = """CREATE TABLE flow_data ( @@ -16,26 +17,26 @@ CREATE_FLOWDATA_TABLE = """CREATE TABLE flow_data ( );""" INSERT_BLANK_FLOWDATA = """INSERT INTO flow_data ( - id, - gal_totalizer_value, - bbl_totalizer_value, + id, + gal_totalizer_value, + bbl_totalizer_value, gal_monthly_totalizer, bbl_monthly_totalizer, - last_measured_timestamp) + last_measured_timestamp) VALUES (1, 0.0, 0.0, 0.0, 0.0, 0);""" CLEAR_FLOWDATA = """UPDATE flow_data SET - gal_totalizer_value=0.0, - bbl_totalizer_value=0.0, - gal_monthly_totalizer=0.0, - bbl_monthly_totalizer=0.0, + gal_totalizer_value=0.0, + bbl_totalizer_value=0.0, + gal_monthly_totalizer=0.0, + bbl_monthly_totalizer=0.0, last_measured_timestamp=0 WHERE id=1;""" UPDATE_FLOWDATA = """UPDATE flow_data SET - gal_totalizer_value=?, - bbl_totalizer_value=?, - gal_monthly_totalizer=?, - bbl_monthly_totalizer=?, + gal_totalizer_value=?, + bbl_totalizer_value=?, + gal_monthly_totalizer=?, + bbl_monthly_totalizer=?, last_measured_timestamp=? WHERE id=1""" CREATE_SCALINGDATA_TABLE = """CREATE TABLE scaling_data ( @@ -47,18 +48,19 @@ CREATE_SCALINGDATA_TABLE = """CREATE TABLE scaling_data ( );""" INSERT_SCALINGDATA = """INSERT INTO scaling_data ( - id, - raw_min, - raw_max, + id, + raw_min, + raw_max, gpm_min, gpm_max) VALUES (1, ?, ?, ?, ?);""" UPDATE_SCALINGDATA = """UPDATE scaling_data SET - raw_min=?, - raw_max=?, - gpm_min=?, + raw_min=?, + raw_max=?, + gpm_min=?, gpm_max=? WHERE id=1;""" +PERSIST = persistence.load() class ChannelSimple(object): @@ -128,6 +130,7 @@ class start(threading.Thread, deviceBase): self.RAW_MAX = 19.54 self.GPM_MIN = 0.0 self.GPM_MAX = 100.0 + self.GPM_IGNORE_LIMIT = 1.0 self.daemon = True self.version = "8" @@ -168,16 +171,16 @@ class start(threading.Thread, deviceBase): bpdflow_ch = ChannelSimple('bpd_flow', gpmflow_ch.senddelta_value * 34.2857, flow_time_store_delta) runstatus_ch = ChannelSimple('run_status', 0.5, 600) - # Startup timer. + # Startup timer. # Waits for connection to Meshify before attempting to send data wait_loops = 0 while wait_loops < startup_wait_seconds: - print("Waiting to start driver in {} seconds".format(startup_wait_seconds - wait_loops)) + print("Waiting to start flowmonitor driver in {} seconds".format(startup_wait_seconds - wait_loops)) wait_loops += 1 time.sleep(1) ############################################## - # THIS IS THE ACTUAL DRIVER CODE + # THIS IS THE ACTUAL DRIVER CODE # (executes after waiting for startup timer to complete) ############################################## @@ -185,7 +188,7 @@ class start(threading.Thread, deviceBase): public_ip_address = get_public_ip_address() self.sendtodb('public_ip_address', public_ip_address, 0) ip_checked_time = time.time() - + # Attempt to retrieve data stored in the database last_measured_timestamp = time.time() conn = sqlite3.connect('/root/python_firmware/drivers/flow-monitor.db') @@ -230,6 +233,11 @@ class start(threading.Thread, deviceBase): self.sendtodb("setgpmmax", self.GPM_MAX, 0) conn.commit() + try: + self.GPM_IGNORE_LIMIT = PERSIST['gpm_ignore_limit'] + except KeyError: + print("no persisted GPM Ignore Limit. Using default of {}".format(self.GPM_IGNORE_LIMIT)) + # on bootup, if the day has changed, clear the totalizers. # this would happen if the device is off when the time changes to Midnight. if not is_today(last_measured_timestamp): @@ -237,7 +245,6 @@ class start(threading.Thread, deviceBase): bbl_totalizer_value = 0.0 last_measured_timestamp = time.time() - # DRIVER LOOP while True: try: @@ -245,13 +252,9 @@ class start(threading.Thread, deviceBase): cloop_val = float(mcu_status['cloop']) din1_val = 1 if mcu_status['din1'] == 'On' else 0 # Check DIGITAL INPUT 1 for run status - if din1_val == 1: - gpm_val = scale(cloop_val, self.RAW_MIN, self.RAW_MAX, self.GPM_MIN, self.GPM_MAX) - if gpm_val < 0: - gpm_val = 0 - else: - # If the well is not running, drive the flow rate to 0. - gpm_val = 0.0 + gpm_val = scale(cloop_val, self.RAW_MIN, self.RAW_MAX, self.GPM_MIN, self.GPM_MAX) + if gpm_val < GPM_IGNORE_LIMIT: + gpm_val = 0 bpd_val = (gpm_val / gal_per_bbl) * 60.0 * 24.0 # Computes BPD from GPM @@ -313,7 +316,7 @@ class start(threading.Thread, deviceBase): self.sendtodb('bbl_total_yesterday', bbl_totalizer_value, 0) gal_totalizer_value = 0.0 bbl_totalizer_value = 0.0 - + # Update the database with cleared values cursor.execute(UPDATE_FLOWDATA, (gal_totalizer_value, bbl_totalizer_value, gal_monthly_totalizer, bbl_monthly_totalizer, last_measured_timestamp)) conn.commit() @@ -347,9 +350,25 @@ class start(threading.Thread, deviceBase): public_ip_address = test_public_ip ip_checked_time = now + if mcu_status['din2'] == 'On': + self.flowmonitor_startcmd(None, None) + + if mcu_status['din1'] == 'Off' and mcu_status['relay1'] == "On": + self.flowmonitor_stopcmd(None, None) + except Exception as e: print("problem in the driver: {}".format(e)) - time.sleep(5) + time.sleep(2) + + def flowmonitor_startcmd(self, name, value): + """Start the well.""" + self.mcu.relay1(str(1)) + return True + + def flowmonitor_stopcmd(self, name, value): + """Stop the well.""" + self.mcu.relay1(str(0)) + return True def flowmonitor_resetdatabase(self, name, value): """Reset the database back to blank.""" @@ -431,4 +450,14 @@ class start(threading.Thread, deviceBase): cursor.execute(CREATE_SCALINGDATA_TABLE) cursor.execute(INSERT_SCALINGDATA, (self.RAW_MIN, self.RAW_MAX, self.GPM_MIN, self.GPM_MAX)) conn.commit() - return(True) \ No newline at end of file + return(True) + + def flowmonitor_setgpmignorelimit(self, name, value): + """Set the GPM Ignore Limit.""" + try: + self.GPM_IGNORE_LIMIT = float(value) + self.sendtodb("setgpmignorelimit", self.GPM_IGNORE_LIMIT, 0) + return True + except Exception as e: + print("Error during flowmonitor_setgpmignorelimit: {}".format(e)) + return False diff --git a/persistence.py b/persistence.py new file mode 100644 index 0000000..ed65271 --- /dev/null +++ b/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