Adds comments to code
This commit is contained in:
@@ -24,14 +24,14 @@ INSERT_BLANK_FLOWDATA = """INSERT INTO flow_data (
|
||||
last_measured_timestamp)
|
||||
VALUES (1, 0.0, 0.0, 0.0, 0.0, 0);"""
|
||||
|
||||
CLEAR_FLOWDATA = """UPDATE flow_data SET
|
||||
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,
|
||||
last_measured_timestamp=0 WHERE id=1;"""
|
||||
|
||||
UPDATE_FLOWDATA = """UPDATE flow_data SET
|
||||
UPDATE_FLOWDATA = """UPDATE flow_data SET
|
||||
gal_totalizer_value=?,
|
||||
bbl_totalizer_value=?,
|
||||
gal_monthly_totalizer=?,
|
||||
@@ -53,7 +53,7 @@ INSERT_SCALINGDATA = """INSERT INTO scaling_data (
|
||||
gpm_min,
|
||||
gpm_max) VALUES (1, ?, ?, ?, ?);"""
|
||||
|
||||
UPDATE_SCALINGDATA = """UPDATE scaling_data SET
|
||||
UPDATE_SCALINGDATA = """UPDATE scaling_data SET
|
||||
raw_min=?,
|
||||
raw_max=?,
|
||||
gpm_min=?,
|
||||
@@ -61,8 +61,8 @@ UPDATE_SCALINGDATA = """UPDATE scaling_data SET
|
||||
|
||||
|
||||
|
||||
class Channel(object):
|
||||
"""Meshify channel structure."""
|
||||
class ChannelSimple(object):
|
||||
"""Simple Meshify channel structure."""
|
||||
|
||||
def __init__(self, meshify_name, senddelta_value, senddelta_time):
|
||||
"""Initialize the channel with variables."""
|
||||
@@ -146,49 +146,52 @@ class start(threading.Thread, deviceBase):
|
||||
# Configuration Parameters
|
||||
total_time_store_delta = 600 # seconds
|
||||
flow_time_store_delta = 600 # seconds
|
||||
|
||||
startup_wait_seconds = 30
|
||||
|
||||
|
||||
gpm_val = 0.0
|
||||
|
||||
gal_per_bbl = 42.0
|
||||
startup_wait_seconds = 30
|
||||
ip_check_after = 3600 # check public IP address after an hour
|
||||
|
||||
galtotal_ch = Channel('gal_total', 100.0, total_time_store_delta)
|
||||
bbltotal_ch = Channel('bbl_total', galtotal_ch.senddelta_value/gal_per_bbl, total_time_store_delta)
|
||||
|
||||
galtotalthismonth_ch = Channel('gal_total_thismonth', galtotal_ch.senddelta_value, total_time_store_delta)
|
||||
bbltotalthismonth_ch = Channel('bbl_total_thismonth', galtotalthismonth_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', gpmflow_ch.senddelta_value * 34.2857, flow_time_store_delta)
|
||||
|
||||
runstatus_ch = Channel('run_status', 0.5, 600)
|
||||
|
||||
# Initialization
|
||||
gpm_val = 0.0
|
||||
date_reset = False
|
||||
month_reset = True
|
||||
|
||||
month_reset = True # True because False was causing problems when starting up on the first of the month
|
||||
gal_totalizer_value = 0.0
|
||||
bbl_totalizer_value = 0.0
|
||||
gal_monthly_totalizer = 0.0
|
||||
bbl_monthly_totalizer = 0.0
|
||||
|
||||
# Channels
|
||||
galtotal_ch = ChannelSimple('gal_total', 100.0, total_time_store_delta)
|
||||
bbltotal_ch = ChannelSimple('bbl_total', galtotal_ch.senddelta_value/gal_per_bbl, total_time_store_delta)
|
||||
galtotalthismonth_ch = ChannelSimple('gal_total_thismonth', galtotal_ch.senddelta_value, total_time_store_delta)
|
||||
bbltotalthismonth_ch = ChannelSimple('bbl_total_thismonth', galtotalthismonth_ch.senddelta_value/gal_per_bbl, total_time_store_delta)
|
||||
gpmflow_ch = ChannelSimple('gpm_flow', 10.0, flow_time_store_delta)
|
||||
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.
|
||||
# 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))
|
||||
wait_loops += 1
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
##############################################
|
||||
# THIS IS THE ACTUAL DRIVER CODE
|
||||
# (executes after waiting for startup timer to complete)
|
||||
##############################################
|
||||
|
||||
# Determine public IP address and send to Meshify
|
||||
public_ip_address = get_public_ip_address()
|
||||
self.sendtodb('public_ip_address', public_ip_address, 0)
|
||||
ip_checked_time = time.time()
|
||||
ip_check_after = 3600 # send after an hour
|
||||
|
||||
|
||||
# Attempt to retrieve data stored in the database
|
||||
last_measured_timestamp = time.time()
|
||||
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('SELECT * FROM flow_data WHERE id = 1') # dummy query for checking database
|
||||
stored_data = cursor.fetchone()
|
||||
gal_totalizer_value = stored_data[1]
|
||||
bbl_totalizer_value = stored_data[2]
|
||||
@@ -196,15 +199,17 @@ class start(threading.Thread, deviceBase):
|
||||
bbl_monthly_totalizer = stored_data[4]
|
||||
last_measured_timestamp = stored_data[5]
|
||||
except sqlite3.OperationalError:
|
||||
# Caught if the table does not exist in the database.
|
||||
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()
|
||||
except IndexError:
|
||||
# Reset the database if the correct size data is not in the database.
|
||||
self.flowmonitor_resetdatabase(None, None)
|
||||
|
||||
try:
|
||||
cursor.execute('SELECT * FROM scaling_data WHERE id = 1')
|
||||
cursor.execute('SELECT * FROM scaling_data WHERE id = 1') # dummy query for checking database
|
||||
stored_data = cursor.fetchone()
|
||||
self.RAW_MIN = stored_data[1]
|
||||
self.RAW_MAX = stored_data[2]
|
||||
@@ -215,6 +220,7 @@ class start(threading.Thread, deviceBase):
|
||||
self.sendtodb("setgpmmin", self.GPM_MIN, 0)
|
||||
self.sendtodb("setgpmmax", self.GPM_MAX, 0)
|
||||
except (sqlite3.OperationalError, TypeError):
|
||||
# Caught if the table does not exist in the database.
|
||||
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))
|
||||
@@ -224,35 +230,39 @@ class start(threading.Thread, deviceBase):
|
||||
self.sendtodb("setgpmmax", self.GPM_MAX, 0)
|
||||
conn.commit()
|
||||
|
||||
# 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):
|
||||
gal_totalizer_value = 0.0
|
||||
bbl_totalizer_value = 0.0
|
||||
last_measured_timestamp = time.time()
|
||||
|
||||
|
||||
|
||||
# DRIVER LOOP
|
||||
while True:
|
||||
try:
|
||||
mcu_status = self.mcu.getDict()
|
||||
print(mcu_status)
|
||||
mcu_status = self.mcu.getDict() # Gets a dictionary of the IO states
|
||||
cloop_val = float(mcu_status['cloop'])
|
||||
|
||||
din1_val = 1 if mcu_status['din1'] == 'On' else 0
|
||||
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
|
||||
|
||||
bpd_val = (gpm_val / gal_per_bbl) * 60.0 * 24.0
|
||||
bpd_val = (gpm_val / gal_per_bbl) * 60.0 * 24.0 # Computes BPD from GPM
|
||||
|
||||
now = time.time()
|
||||
time_diff = now - last_measured_timestamp
|
||||
if time_diff > 0:
|
||||
# Volume flowed since last measuring
|
||||
gal_flow_delta = (time_diff / 60.0) * gpm_val
|
||||
bbl_flow_delta = (time_diff / 60.0) * (1.0 / 60.0) * (1.0 / 24.0) * bpd_val
|
||||
|
||||
# Increment totalizers
|
||||
gal_totalizer_value += gal_flow_delta
|
||||
bbl_totalizer_value += bbl_flow_delta
|
||||
gal_monthly_totalizer += gal_flow_delta
|
||||
@@ -260,13 +270,15 @@ class start(threading.Thread, deviceBase):
|
||||
|
||||
last_measured_timestamp = now
|
||||
|
||||
# Update the database with the most recent totalizer values.
|
||||
cursor.execute(UPDATE_FLOWDATA, (gal_totalizer_value, bbl_totalizer_value, gal_monthly_totalizer, bbl_monthly_totalizer, last_measured_timestamp))
|
||||
|
||||
conn.commit()
|
||||
|
||||
print('gpm: {}, bpd: {}, gal: {}, bbl:{}, month_gal:{}, month_bbl:{}'.format(
|
||||
gpm_val, bpd_val, gal_totalizer_value, bbl_totalizer_value, gal_monthly_totalizer, bbl_monthly_totalizer))
|
||||
|
||||
# Channel Checks:
|
||||
# check to see if the value needs to be sent to Meshify
|
||||
if galtotal_ch.check_if_send_needed(gal_totalizer_value, now):
|
||||
self.sendtodb(galtotal_ch.meshify_name, gal_totalizer_value, 0)
|
||||
galtotal_ch.update(gal_totalizer_value, now)
|
||||
@@ -295,30 +307,39 @@ class start(threading.Thread, deviceBase):
|
||||
self.sendtodb(runstatus_ch.meshify_name, din1_val, 0)
|
||||
runstatus_ch.update(din1_val, now)
|
||||
|
||||
# Check for the clock hitting midnight for resetting the daily totalizer value
|
||||
if time.localtime(now)[3] == 0 and not date_reset:
|
||||
self.sendtodb('gal_total_yesterday', gal_totalizer_value, 0)
|
||||
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()
|
||||
date_reset = True
|
||||
|
||||
# Once the hour goes to anything other than 0 (midnight), unset the Date Reset status
|
||||
if time.localtime(now)[3] != 0 and date_reset:
|
||||
date_reset = False
|
||||
|
||||
# Check for a new month for resetting the monthly totalizers
|
||||
if time.localtime(now)[2] == 1 and not month_reset:
|
||||
self.sendtodb('gal_total_lastmonth', gal_monthly_totalizer, 0)
|
||||
self.sendtodb('bbl_total_lastmonth', bbl_monthly_totalizer, 0)
|
||||
gal_monthly_totalizer = 0.0
|
||||
bbl_monthly_totalizer = 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()
|
||||
month_reset = True
|
||||
|
||||
# once it's no longer the 1st of the month, unset the Month Reset status
|
||||
if time.localtime(now)[2] != 1 and month_reset:
|
||||
month_reset = False
|
||||
|
||||
# Periodically check the public IP address to see if it has changed.
|
||||
if (now - ip_checked_time) > ip_check_after:
|
||||
test_public_ip = get_public_ip_address()
|
||||
if not test_public_ip == public_ip_address:
|
||||
@@ -331,6 +352,7 @@ class start(threading.Thread, deviceBase):
|
||||
time.sleep(5)
|
||||
|
||||
def flowmonitor_resetdatabase(self, name, value):
|
||||
"""Reset the database back to blank."""
|
||||
conn = sqlite3.connect('/root/python_firmware/drivers/flow-monitor.db')
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
@@ -348,6 +370,7 @@ class start(threading.Thread, deviceBase):
|
||||
return(True)
|
||||
|
||||
def flowmonitor_setrawmin(self, name, value):
|
||||
"""Set the raw min scaling value."""
|
||||
conn = sqlite3.connect('/root/python_firmware/drivers/flow-monitor.db')
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
@@ -363,6 +386,7 @@ class start(threading.Thread, deviceBase):
|
||||
return(True)
|
||||
|
||||
def flowmonitor_setrawmax(self, name, value):
|
||||
"""Set the raw max scaling value."""
|
||||
conn = sqlite3.connect('/root/python_firmware/drivers/flow-monitor.db')
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
@@ -378,6 +402,7 @@ class start(threading.Thread, deviceBase):
|
||||
return(True)
|
||||
|
||||
def flowmonitor_setgpmmin(self, name, value):
|
||||
"""Set the gpm min scaling value."""
|
||||
conn = sqlite3.connect('/root/python_firmware/drivers/flow-monitor.db')
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
@@ -393,6 +418,7 @@ class start(threading.Thread, deviceBase):
|
||||
return(True)
|
||||
|
||||
def flowmonitor_setgpmmax(self, name, value):
|
||||
"""Set the gpm max scaling value."""
|
||||
conn = sqlite3.connect('/root/python_firmware/drivers/flow-monitor.db')
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user