Driver now creates database automatically if it does not exist

This commit is contained in:
Patrick McDonagh
2017-09-14 09:49:46 -05:00
parent ae0e6c4918
commit dd5e602525
4 changed files with 71 additions and 22 deletions

View File

@@ -5,10 +5,10 @@
<h3><%= node.vanityname %></h3> <h3><%= node.vanityname %></h3>
<h4> <h4>
<span data-updatevalue="run_status"> <span data-updatevalue="run_status">
<% if(channels["flowmonitor.run_status"].value === 1) {%> <% if(channels["flowmonitor.run_status"].value === "0") {%>
Running
<% } else { %>
Stopped Stopped
<% } else { %>
Running
<% } %> <% } %>
</span> </span>
</h4> </h4>

View File

@@ -31,11 +31,17 @@
<div class="col-xs-3"> <div class="col-xs-3">
<h2>Flow Rate</h2> <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>
<div class="col-xs-3"> <div class="col-xs-3">
<h2>Run Status</h2> <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>
</div> </div>

View File

@@ -3,7 +3,7 @@
"driverFileName":"flow-monitor.py", "driverFileName":"flow-monitor.py",
"deviceName":"flowmonitor", "deviceName":"flowmonitor",
"driverId":"0140", "driverId":"0140",
"releaseVersion":"1", "releaseVersion":"2",
"files": { "files": {
"file1":"flow-monitor.py"} "file1":"flow-monitor.py"}

View File

@@ -1,12 +1,30 @@
"""Driver for connecting Flow Monitor to Meshify.""" """Driver for connecting Flow Monitor to Meshify."""
import threading import threading
from device_base import deviceBase
import time import time
from datetime import datetime from datetime import datetime
import sqlite3 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.""" """Meshify channel structure."""
def __init__(self, meshify_name, senddelta_value, senddelta_time): def __init__(self, meshify_name, senddelta_value, senddelta_time):
@@ -63,7 +81,7 @@ class start(threading.Thread, deviceBase):
mqtt=mqtt, Nodes=Nodes) mqtt=mqtt, Nodes=Nodes)
self.daemon = True self.daemon = True
self.version = "1" self.version = "2"
self.finished = threading.Event() self.finished = threading.Event()
threading.Thread.start(self) threading.Thread.start(self)
@@ -84,7 +102,7 @@ class start(threading.Thread, deviceBase):
raw_max = 19.54 raw_max = 19.54
gpm_min = 0.0 gpm_min = 0.0
gpm_max = 600.0 gpm_max = 100.0
gal_per_bbl = 42.0 gal_per_bbl = 42.0
@@ -98,22 +116,33 @@ class start(threading.Thread, deviceBase):
date_reset = False date_reset = False
gal_totalizer_value = 0.0
bbl_totalizer_value = 0.0
last_measured_timestamp = 0
last_measured_timestamp = time.time() last_measured_timestamp = time.time()
conn = sqlite3.connect('/root/python_firmware/drivers/flow-monitor.db') conn = sqlite3.connect('/root/python_firmware/drivers/flow-monitor.db')
c = conn.cursor() cursor = conn.cursor()
c.execute('SELECT * FROM flow_data WHERE id = 1') try:
stored_data = c.fetchone() cursor.execute('SELECT * FROM flow_data WHERE id = 1')
stored_data = cursor.fetchone()
gal_totalizer_value = stored_data[1] gal_totalizer_value = stored_data[1]
bbl_totalizer_value = stored_data[2] bbl_totalizer_value = stored_data[2]
last_measured_timestamp = stored_data[3] 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): if not is_today(last_measured_timestamp):
gal_totalizer_value = 0 gal_totalizer_value = 0.0
bbl_totalizer_value = 0 bbl_totalizer_value = 0.0
last_measured_timestamp = time.time() last_measured_timestamp = time.time()
wait_loops = 0 wait_loops = 0
while wait_loops < 15: while wait_loops < 30:
print("Waiting to start driver in {} seconds".format(15 - wait_loops)) print("Waiting to start driver in {} seconds".format(30 - wait_loops))
wait_loops += 1 wait_loops += 1
time.sleep(1) time.sleep(1)
@@ -139,7 +168,7 @@ class start(threading.Thread, deviceBase):
bbl_totalizer_value += bbl_flow_delta bbl_totalizer_value += bbl_flow_delta
last_measured_timestamp = now 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)) (gal_totalizer_value, bbl_totalizer_value, last_measured_timestamp))
conn.commit() conn.commit()
@@ -170,7 +199,7 @@ class start(threading.Thread, deviceBase):
self.sendtodb('bbl_total_yesterday', bbl_totalizer_value, 0) self.sendtodb('bbl_total_yesterday', bbl_totalizer_value, 0)
gal_totalizer_value = 0.0 gal_totalizer_value = 0.0
bbl_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)) (gal_totalizer_value, bbl_totalizer_value, last_measured_timestamp))
conn.commit() conn.commit()
date_reset = True date_reset = True
@@ -181,3 +210,17 @@ class start(threading.Thread, deviceBase):
except Exception as e: except Exception as e:
print("problem in the driver: {}".format(e)) print("problem in the driver: {}".format(e))
time.sleep(5) 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)