From 70539d442e915fcac4be0f4d4a119f9ee546d8b8 Mon Sep 17 00:00:00 2001 From: Patrick McDonagh Date: Wed, 20 Sep 2017 15:58:14 -0500 Subject: [PATCH] Completes FLOWMON-1. Increments to V6 --- HTML/Scaling.html | 152 ++++++++++++++++++++++++++++++++++++++++++++++ config.txt | 2 +- flow-monitor.py | 122 ++++++++++++++++++++++++++++++++++--- 3 files changed, 266 insertions(+), 10 deletions(-) create mode 100644 HTML/Scaling.html diff --git a/HTML/Scaling.html b/HTML/Scaling.html new file mode 100644 index 0000000..1fb1a13 --- /dev/null +++ b/HTML/Scaling.html @@ -0,0 +1,152 @@ +
+
+
+

Raw Min

+
+
+ +
+ +
mA
+
+
+
+ " + data-techname="<%=channels["flowmonitor.setrawmin"].techName %>" + data-name="<%= channels["flowmonitor.setrawmin"].name%>" + data-nodechannelcurrentId="<%= channels["flowmonitor.setrawmin"].nodechannelcurrentId %>" + id="<%= channels["flowmonitor.setrawmin"].channelId %>" + class="btn btn-large btn-theme animated setstatic material-icons">send +
+
+
+ +
+
+

Raw Max

+
+
+ +
+ +
mA
+
+
+
+ " + data-techname="<%=channels["flowmonitor.setrawmax"].techName %>" + data-name="<%= channels["flowmonitor.setrawmax"].name%>" + data-nodechannelcurrentId="<%= channels["flowmonitor.setrawmax"].nodechannelcurrentId %>" + id="<%= channels["flowmonitor.setrawmax"].channelId %>" + class="btn btn-large btn-theme animated setstatic material-icons">send +
+
+
+ +
+
+

GPM Min

+
+
+ +
+ +
GPM
+
+
+
+ " + data-techname="<%=channels["flowmonitor.setgpmmin"].techName %>" + data-name="<%= channels["flowmonitor.setgpmmin"].name%>" + data-nodechannelcurrentId="<%= channels["flowmonitor.setgpmmin"].nodechannelcurrentId %>" + id="<%= channels["flowmonitor.setgpmmin"].channelId %>" + class="btn btn-large btn-theme animated setstatic material-icons">send +
+
+
+ +
+
+

GPM Max

+
+
+ +
+ +
GPM
+
+
+
+ " + data-techname="<%=channels["flowmonitor.setgpmmax"].techName %>" + data-name="<%= channels["flowmonitor.setgpmmax"].name%>" + data-nodechannelcurrentId="<%= channels["flowmonitor.setgpmmax"].nodechannelcurrentId %>" + id="<%= channels["flowmonitor.setgpmmax"].channelId %>" + class="btn btn-large btn-theme animated setstatic material-icons">send +
+
+
+ +
+ + + \ No newline at end of file diff --git a/config.txt b/config.txt index efd59ab..d3bd190 100644 --- a/config.txt +++ b/config.txt @@ -3,7 +3,7 @@ "driverFileName":"flow-monitor.py", "deviceName":"flowmonitor", "driverId":"0140", -"releaseVersion":"5", +"releaseVersion":"6", "files": { "file1":"flow-monitor.py"} diff --git a/flow-monitor.py b/flow-monitor.py index 9fd68aa..8607f36 100644 --- a/flow-monitor.py +++ b/flow-monitor.py @@ -24,6 +24,29 @@ CLEAR_FLOWDATA = """UPDATE flow_data SET bbl_totalizer_value=0.0, last_measured_timestamp=0 WHERE id=1;""" +CREATE_SCALINGDATA_TABLE = """CREATE TABLE scaling_data ( + id integer PRIMARY KEY, + raw_min float, + raw_max float, + gpm_min float, + gpm_max float + );""" + +INSERT_SCALINGDATA = """INSERT INTO scaling_data ( + id, + raw_min, + raw_max, + gpm_min, + gpm_max) VALUES (1, ?, ?, ?, ?);""" + +UPDATE_SCALINGDATA = """UPDATE scaling_data SET + raw_min=?, + raw_max=?, + gpm_min=?, + gpm_max=? WHERE id=1;""" + + + class Channel(object): """Meshify channel structure.""" @@ -79,9 +102,14 @@ class start(threading.Thread, deviceBase): deviceBase.__init__(self, name=name, number=number, mac=mac, Q=Q, mcu=mcu, companyId=companyId, offset=offset, mqtt=mqtt, Nodes=Nodes) + # Default Scaling Values for Flowmeter + self.RAW_MIN = 3.89 + self.RAW_MAX = 19.54 + self.GPM_MIN = 0.0 + self.GPM_MAX = 100.0 self.daemon = True - self.version = "5" + self.version = "6" self.finished = threading.Event() threading.Thread.start(self) @@ -100,11 +128,6 @@ class start(threading.Thread, deviceBase): startup_wait_seconds = 30 - raw_min = 3.89 - raw_max = 19.54 - - gpm_min = 0.0 - gpm_max = 100.0 gpm_val = 0.0 gal_per_bbl = 42.0 @@ -113,7 +136,7 @@ class start(threading.Thread, deviceBase): bbltotal_ch = Channel('bbl_total', galtotal_ch.senddelta_value/gal_per_bbl, total_time_store_delta) gpmflow_ch = Channel('gpm_flow', 10.0, flow_time_store_delta) - bpdflow_ch = Channel('bpd_flow', 5.0, flow_time_store_delta) + bpdflow_ch = Channel('bpd_flow', gpmflow_ch.senddelta_value * 34.2857, flow_time_store_delta) runstatus_ch = Channel('run_status', 0.5, 600) @@ -137,6 +160,27 @@ class start(threading.Thread, deviceBase): cursor.execute(INSERT_BLANK_FLOWDATA) conn.commit() + try: + cursor.execute('SELECT * FROM scaling_data WHERE id = 1') + stored_data = cursor.fetchone() + self.RAW_MIN = stored_data[1] + self.RAW_MAX = stored_data[2] + self.GPM_MIN = stored_data[3] + self.GPM_MAX = stored_data[4] + self.sendtodb("setrawmin", self.RAW_MIN, 0) + self.sendtodb("setrawmax", self.RAW_MAX, 0) + self.sendtodb("setgpmmin", self.GPM_MIN, 0) + self.sendtodb("setgpmmax", self.GPM_MAX, 0) + except (sqlite3.OperationalError, TypeError): + print("No table scaling_data in the database. I'll create it now.") + cursor.execute(CREATE_SCALINGDATA_TABLE) + cursor.execute(INSERT_SCALINGDATA, (self.RAW_MIN, self.RAW_MAX, self.GPM_MIN, self.GPM_MAX)) + self.sendtodb("setrawmin", self.RAW_MIN, 0) + self.sendtodb("setrawmax", self.RAW_MAX, 0) + self.sendtodb("setgpmmin", self.GPM_MIN, 0) + self.sendtodb("setgpmmax", self.GPM_MAX, 0) + conn.commit() + if not is_today(last_measured_timestamp): gal_totalizer_value = 0.0 bbl_totalizer_value = 0.0 @@ -156,7 +200,7 @@ class start(threading.Thread, deviceBase): din1_val = 1 if mcu_status['din1'] == 'On' else 0 if din1_val == 1: - gpm_val = scale(cloop_val, raw_min, raw_max, gpm_min, gpm_max) + 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: @@ -206,7 +250,7 @@ class start(threading.Thread, deviceBase): gal_totalizer_value = 0.0 bbl_totalizer_value = 0.0 cursor.execute('UPDATE flow_data SET gal_totalizer_value=?, bbl_totalizer_value=?, last_measured_timestamp=?', - (gal_totalizer_value, bbl_totalizer_value, last_measured_timestamp)) + (gal_totalizer_value, bbl_totalizer_value, last_measured_timestamp)) conn.commit() date_reset = True @@ -230,3 +274,63 @@ class start(threading.Thread, deviceBase): cursor.execute(INSERT_BLANK_FLOWDATA) conn.commit() return(True) + + def flowmonitor_setrawmin(self, name, value): + conn = sqlite3.connect('/root/python_firmware/drivers/flow-monitor.db') + cursor = conn.cursor() + try: + self.RAW_MIN = float(value) + cursor.execute(UPDATE_SCALINGDATA, (self.RAW_MIN, self.RAW_MAX, self.GPM_MIN, self.GPM_MAX)) + self.sendtodb("setrawmin", self.RAW_MIN, 0) + conn.commit() + except sqlite3.OperationalError: + print("No table flow_data in the database. I'll create it now.") + 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) + + def flowmonitor_setrawmax(self, name, value): + conn = sqlite3.connect('/root/python_firmware/drivers/flow-monitor.db') + cursor = conn.cursor() + try: + self.RAW_MAX = float(value) + self.sendtodb("setrawmax", self.RAW_MAX, 0) + cursor.execute(UPDATE_SCALINGDATA, (self.RAW_MIN, self.RAW_MAX, self.GPM_MIN, self.GPM_MAX)) + conn.commit() + except sqlite3.OperationalError: + print("No table flow_data in the database. I'll create it now.") + 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) + + def flowmonitor_setgpmmin(self, name, value): + conn = sqlite3.connect('/root/python_firmware/drivers/flow-monitor.db') + cursor = conn.cursor() + try: + self.GPM_MIN = float(value) + self.sendtodb("setgpmmin", self.GPM_MIN, 0) + cursor.execute(UPDATE_SCALINGDATA, (self.RAW_MIN, self.RAW_MAX, self.GPM_MIN, self.GPM_MAX)) + conn.commit() + except sqlite3.OperationalError: + print("No table flow_data in the database. I'll create it now.") + 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) + + def flowmonitor_setgpmmax(self, name, value): + conn = sqlite3.connect('/root/python_firmware/drivers/flow-monitor.db') + cursor = conn.cursor() + try: + self.GPM_MAX = float(value) + self.sendtodb("setgpmmax", self.GPM_MAX, 0) + cursor.execute(UPDATE_SCALINGDATA, (self.RAW_MIN, self.RAW_MAX, self.GPM_MIN, self.GPM_MAX)) + conn.commit() + except sqlite3.OperationalError: + print("No table flow_data in the database. I'll create it now.") + 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