Completes MAXH2O-73 and MAXH2O-78

Adds scaling and GPM ignore limit.
Increments version in S3 to 9
This commit is contained in:
Patrick McDonagh
2017-12-01 10:59:26 -06:00
parent 17a52d9a0c
commit e4234bc2c3
6 changed files with 172 additions and 49 deletions

View File

@@ -1,3 +1,33 @@
<div class="row row-flex box-me">
<div class="col-md-6 text-center">
<a href="#"
data-confirm-message="Are you sure you want to do this?"
data-refreshpause="1"
data-command=""
data-staticsend=1
data-channelId="<%= channels["flowmonitor.startcmd"].channelId %>"
data-techname="<%=channels["flowmonitor.startcmd"].techName %>"
data-name="<%= channels["flowmonitor.startcmd"].name%>"
data-nodechannelcurrentId="<%= channels["flowmonitor.startcmd"].nodechannelcurrentId %>"
id="<%= channels["flowmonitor.startcmd"].channelId %>"
class="btn btn-large btn-theme animated setstatic">Start</a>
</div>
<div class="col-md-6 text-center">
<a href="#"
data-confirm-message="Are you sure you want to do this?"
data-refreshpause="1"
data-command=""
data-staticsend=1
data-channelId="<%= channels["flowmonitor.stopcmd"].channelId %>"
data-techname="<%=channels["flowmonitor.stopcmd"].techName %>"
data-name="<%= channels["flowmonitor.stopcmd"].name%>"
data-nodechannelcurrentId="<%= channels["flowmonitor.stopcmd"].nodechannelcurrentId %>"
id="<%= channels["flowmonitor.stopcmd"].channelId %>"
class="btn btn-large btn-theme animated setstatic">Stop</a>
</div>
</div>
<div class="row row-flex box-me">
<div class='col-xs-6 text-center box-me'>
<h2>GPM Flow</h2>
@@ -109,3 +139,10 @@
</div>
</div>
<style>
.text-center {
text-align: center;
}
</style>

View File

@@ -134,7 +134,41 @@
</form>
</div>
</div>
<div class="col-md-6 box-me entry-top-level" id="setgpmignorelimit">
<div style="margin-left:15px;">
<h2>GPM Ignore Limit</h2>
<form class="form-inline">
<div class="form-group" style="margin-bottom:15px;">
<label
class="sr-only"
for="<%= channels["flowmonitor.setgpmignorelimit"].channelId %>">GPM Max Value</label>
<div class="input-group">
<input
class="form-control channel_value"
type="number"
step="any"
value="<%=channels['flowmonitor.setgpmignorelimit'].value %>">
<div class="input-group-addon">GPM</div>
</div>
</div>
<hr />
<a
href="#"
data-confirm-message="Are you sure you want to do this?"
data-refreshpause="1"
data-command=""
data-staticsend=1.0
data-channelId="<%= channels["flowmonitor.setgpmignorelimit"].channelId %>"
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</a>
</form>
</div>
</div>
</div>
@@ -149,4 +183,4 @@ $('.channel_value').each(function(topLevel){
}
});
});
</script>
</script>

View File

@@ -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"
}

View File

@@ -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"
}

View File

@@ -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)
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

21
persistence.py Normal file
View File

@@ -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