Driver now creates database automatically if it does not exist
This commit is contained in:
@@ -5,10 +5,10 @@
|
||||
<h3><%= node.vanityname %></h3>
|
||||
<h4>
|
||||
<span data-updatevalue="run_status">
|
||||
<% if(channels["flowmonitor.run_status"].value === 1) {%>
|
||||
Running
|
||||
<% } else { %>
|
||||
<% if(channels["flowmonitor.run_status"].value === "0") {%>
|
||||
Stopped
|
||||
<% } else { %>
|
||||
Running
|
||||
<% } %>
|
||||
</span>
|
||||
</h4>
|
||||
|
||||
@@ -31,11 +31,17 @@
|
||||
|
||||
<div class="col-xs-3">
|
||||
<h2>Flow Rate</h2>
|
||||
<p><span data-valueupdate="gpm_flow"><%= round(channels["flowmonitor.gpm_flow"].value * 100) / 100 %></span> GPM</p>
|
||||
<p><span data-valueupdate="gpm_flow"><%= Math.round(channels["flowmonitor.gpm_flow"].value * 100) / 100 %></span> GPM</p>
|
||||
</div>
|
||||
|
||||
<div class="col-xs-3">
|
||||
<h2>Run Status</h2>
|
||||
<p><span data-valueupdate="run_status"><%= channels["flowmonitor.run_status"].value %></span></p>
|
||||
<p><span data-valueupdate="run_status">
|
||||
<% if(channels["flowmonitor.run_status"].value === "0") {%>
|
||||
Stopped
|
||||
<% } else { %>
|
||||
Running
|
||||
<% } %>
|
||||
</span></p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"driverFileName":"flow-monitor.py",
|
||||
"deviceName":"flowmonitor",
|
||||
"driverId":"0140",
|
||||
"releaseVersion":"1",
|
||||
"releaseVersion":"2",
|
||||
"files": {
|
||||
"file1":"flow-monitor.py"}
|
||||
|
||||
|
||||
@@ -1,12 +1,30 @@
|
||||
"""Driver for connecting Flow Monitor to Meshify."""
|
||||
import threading
|
||||
from device_base import deviceBase
|
||||
import time
|
||||
from datetime import datetime
|
||||
import sqlite3
|
||||
from device_base import deviceBase
|
||||
|
||||
CREATE_FLOWDATA_TABLE = """CREATE TABLE flow_data (
|
||||
id integer PRIMARY KEY,
|
||||
gal_totalizer_value float,
|
||||
bbl_totalizer_value float,
|
||||
last_measured_timestamp integer
|
||||
);"""
|
||||
|
||||
class Channel:
|
||||
INSERT_BLANK_FLOWDATA = """INSERT INTO flow_data (
|
||||
id,
|
||||
gal_totalizer_value,
|
||||
bbl_totalizer_value,
|
||||
last_measured_timestamp)
|
||||
VALUES (1, 0.0, 0.0, 0);"""
|
||||
|
||||
CLEAR_FLOWDATA = """UPDATE flow_data SET
|
||||
gal_totalizer_value=0.0,
|
||||
bbl_totalizer_value=0.0,
|
||||
last_measured_timestamp=0 WHERE id=1;"""
|
||||
|
||||
class Channel(object):
|
||||
"""Meshify channel structure."""
|
||||
|
||||
def __init__(self, meshify_name, senddelta_value, senddelta_time):
|
||||
@@ -63,7 +81,7 @@ class start(threading.Thread, deviceBase):
|
||||
mqtt=mqtt, Nodes=Nodes)
|
||||
|
||||
self.daemon = True
|
||||
self.version = "1"
|
||||
self.version = "2"
|
||||
self.finished = threading.Event()
|
||||
threading.Thread.start(self)
|
||||
|
||||
@@ -84,7 +102,7 @@ class start(threading.Thread, deviceBase):
|
||||
raw_max = 19.54
|
||||
|
||||
gpm_min = 0.0
|
||||
gpm_max = 600.0
|
||||
gpm_max = 100.0
|
||||
|
||||
gal_per_bbl = 42.0
|
||||
|
||||
@@ -98,22 +116,33 @@ class start(threading.Thread, deviceBase):
|
||||
|
||||
date_reset = False
|
||||
|
||||
gal_totalizer_value = 0.0
|
||||
bbl_totalizer_value = 0.0
|
||||
last_measured_timestamp = 0
|
||||
|
||||
last_measured_timestamp = time.time()
|
||||
conn = sqlite3.connect('/root/python_firmware/drivers/flow-monitor.db')
|
||||
c = conn.cursor()
|
||||
c.execute('SELECT * FROM flow_data WHERE id = 1')
|
||||
stored_data = c.fetchone()
|
||||
gal_totalizer_value = stored_data[1]
|
||||
bbl_totalizer_value = stored_data[2]
|
||||
last_measured_timestamp = stored_data[3]
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute('SELECT * FROM flow_data WHERE id = 1')
|
||||
stored_data = cursor.fetchone()
|
||||
gal_totalizer_value = stored_data[1]
|
||||
bbl_totalizer_value = stored_data[2]
|
||||
last_measured_timestamp = stored_data[3]
|
||||
except sqlite3.OperationalError:
|
||||
print("No table flow_data in the database. I'll create it now.")
|
||||
cursor.execute(CREATE_FLOWDATA_TABLE)
|
||||
cursor.execute(INSERT_BLANK_FLOWDATA)
|
||||
conn.commit()
|
||||
|
||||
if not is_today(last_measured_timestamp):
|
||||
gal_totalizer_value = 0
|
||||
bbl_totalizer_value = 0
|
||||
gal_totalizer_value = 0.0
|
||||
bbl_totalizer_value = 0.0
|
||||
last_measured_timestamp = time.time()
|
||||
|
||||
wait_loops = 0
|
||||
while wait_loops < 15:
|
||||
print("Waiting to start driver in {} seconds".format(15 - wait_loops))
|
||||
while wait_loops < 30:
|
||||
print("Waiting to start driver in {} seconds".format(30 - wait_loops))
|
||||
wait_loops += 1
|
||||
time.sleep(1)
|
||||
|
||||
@@ -139,7 +168,7 @@ class start(threading.Thread, deviceBase):
|
||||
bbl_totalizer_value += bbl_flow_delta
|
||||
last_measured_timestamp = now
|
||||
|
||||
c.execute('UPDATE flow_data SET gal_totalizer_value=?, bbl_totalizer_value=?, last_measured_timestamp=?',
|
||||
cursor.execute('UPDATE flow_data SET gal_totalizer_value=?, bbl_totalizer_value=?, last_measured_timestamp=?',
|
||||
(gal_totalizer_value, bbl_totalizer_value, last_measured_timestamp))
|
||||
conn.commit()
|
||||
|
||||
@@ -170,7 +199,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
|
||||
c.execute('UPDATE flow_data SET gal_totalizer_value=?, bbl_totalizer_value=?, last_measured_timestamp=?',
|
||||
cursor.execute('UPDATE flow_data SET gal_totalizer_value=?, bbl_totalizer_value=?, last_measured_timestamp=?',
|
||||
(gal_totalizer_value, bbl_totalizer_value, last_measured_timestamp))
|
||||
conn.commit()
|
||||
date_reset = True
|
||||
@@ -181,3 +210,17 @@ class start(threading.Thread, deviceBase):
|
||||
except Exception as e:
|
||||
print("problem in the driver: {}".format(e))
|
||||
time.sleep(5)
|
||||
|
||||
def flowmonitor_resetdatabase(self, name, value):
|
||||
conn = sqlite3.connect('/root/python_firmware/drivers/flow-monitor.db')
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute('SELECT * FROM flow_data WHERE id = 1')
|
||||
cursor.execute(CLEAR_FLOWDATA)
|
||||
conn.commit()
|
||||
except sqlite3.OperationalError:
|
||||
print("No table flow_data in the database. I'll create it now.")
|
||||
cursor.execute(CREATE_FLOWDATA_TABLE)
|
||||
cursor.execute(INSERT_BLANK_FLOWDATA)
|
||||
conn.commit()
|
||||
return(True)
|
||||
|
||||
Reference in New Issue
Block a user