Adds public_ip_address, fixes pressure graph

This commit is contained in:
Patrick McDonagh
2018-07-03 16:51:51 -05:00
parent 261ceba0b4
commit 3688d62539
8 changed files with 213 additions and 53 deletions

View File

@@ -5,6 +5,8 @@ from device_base import deviceBase
import time
import json
import persistence
from utilities import get_public_ip_address
from file_logger import filelogger as log
_ = None
@@ -45,11 +47,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)
self.daemon = True
self.version = "3"
self.version = "4"
self.finished = threading.Event()
self.forceSend = False
threading.Thread.start(self)
self.public_ip_address = ""
self.public_ip_address_last_checked = 0
self.PL_RAWMAX = 20.0
self.PL_RAWMIN = 4.0
self.PL_EUMAX = 23.068
@@ -85,9 +90,9 @@ class start(threading.Thread, deviceBase):
self.PL_EUMAX = scaling_persist['eu_max']
self.PL_EUMIN = scaling_persist['eu_min']
except KeyError:
print("No persistence data for pond level scaling parameters.")
log.warning("No persistence data for pond level scaling parameters.")
except TypeError:
print("No persistence data for pond level scaling parameters.")
log.warning("No persistence data for pond level scaling parameters.")
try:
self.PSI_RAWMAX = scaling_persist['psi_raw_max']
@@ -95,27 +100,28 @@ class start(threading.Thread, deviceBase):
self.PSI_EUMAX = scaling_persist['psi_eu_max']
self.PSI_EUMIN = scaling_persist['psi_eu_min']
except KeyError:
print("No persistence data for pressure scaling parameters.")
log.warning("No persistence data for pressure scaling parameters.")
except TypeError:
print("No persistence data for pressure scaling parameters.")
log.warning("No persistence data for pressure scaling parameters.")
try:
if len(strapping_persist.keys()) > 0:
for k in strapping_persist.keys():
self.strapping_table[float(k)] = strapping_persist[k]
print("Using stored strapping table:\n{}".format(self.strapping_table))
log.info("Using stored strapping table:\n{}".format(self.strapping_table))
else:
print("No stored strapping table found.")
log.warning("No stored strapping table found.")
except Exception as e:
print("No stored strapping table. Got Error: {}".format(e))
log.warning("No stored strapping table. Got Error: {}".format(e))
wait_sec = 30
for i in range(0, wait_sec):
print("pondlevel driver will start in {} seconds".format(wait_sec - i))
log.info("pondlevel driver will start in {} seconds".format(wait_sec - i))
time.sleep(1)
print("BOOM! Starting pondlevel driver...")
log.info("BOOM! Starting pondlevel driver...")
# db.setup_calibration_table(drop_first=False)
self.send_calibration_points()
self._check_ip_address()
self.sendtodb("setrawmax", self.PL_RAWMAX, 0)
self.sendtodb("setrawmin", self.PL_RAWMIN, 0)
@@ -128,40 +134,39 @@ class start(threading.Thread, deviceBase):
self.sendtodb("setpsieumin", self.PSI_EUMIN, 0)
send_loops = 0
ip_check_after = 60
while True:
now = time.time()
pl_send_now = False
psi_send_now = False
if self.forceSend:
print "FORCE SEND: TRUE"
log.warning("FORCE SEND: TRUE")
pl_send_now = True
psi_send_now = True
try:
mcu_status = self.mcu.getDict() # Gets a dictionary of the IO states
cloop_val = float(mcu_status['cloop'])
analog1_val = float(mcu_status['analog1'])
temp_pl = scale_to_eu(cloop_val, self.PL_RAWMAX, self.PL_RAWMIN, self.PL_EUMAX, self.PL_EUMIN)
temp_psi = scale_to_eu(analog1_val, self.PSI_RAWMAX, self.PSI_RAWMIN, self.PSI_EUMAX, self.PSI_EUMIN)
except AttributeError as err:
log.error("Could not connect to MCU object, this device might not have an MCU: {}".format(err))
except KeyError as err:
log.error("Connected to MCU, but {} does not exist.".format(err))
mcu_status = self.mcu.getDict() # Gets a dictionary of the IO states
cloop_val = float(mcu_status['cloop'])
analog1_val = float(mcu_status['analog1'])
temp_pl = scale_to_eu(cloop_val, self.PL_RAWMAX, self.PL_RAWMIN, self.PL_EUMAX, self.PL_EUMIN)
temp_psi = scale_to_eu(analog1_val, self.PSI_RAWMAX, self.PSI_RAWMIN, self.PSI_EUMAX, self.PSI_EUMIN)
# Check if pond level needs to be sent
# 4/30/2018 -- COMMENTING OUT
# Pond level was sending irregular data
# -- Patrick McDonagh
# if abs(temp_pl - pond_level['value']) > send_delta:
# print("Sending pond level {} due to value change".format(temp_pl))
# pl_send_now = True
# 4/30/2018 -- END COMMENTING OUT
if (time.time() - pond_level['timestamp']) > send_time:
print("Sending pond level {} due to time limit".format(temp_pl))
if (now - pond_level['timestamp']) > send_time:
log.info("Sending pond level {} due to time limit".format(temp_pl))
pl_send_now = True
# Check if pressure needs to be sent
if abs(temp_psi - psi_reading['value']) > send_delta:
print("Sending pressure psi {} due to value change".format(temp_psi))
log.info("Sending pressure psi {} due to value change".format(temp_psi))
psi_send_now = True
if (time.time() - psi_reading['timestamp']) > send_time:
print("Sending pressure psi {} due to time limit".format(temp_psi))
if (now - psi_reading['timestamp']) > send_time:
log.info("Sending pressure psi {} due to time limit".format(temp_psi))
psi_send_now = True
if pl_send_now:
@@ -174,23 +179,36 @@ class start(threading.Thread, deviceBase):
self.sendtodb('pond_volume', pond_volume, 0)
pond_level['value'] = temp_pl
pond_level['timestamp'] = time.time()
pond_level['timestamp'] = now
if psi_send_now:
self.sendtodb('pressure_psi', temp_psi, 0)
psi_reading['value'] = temp_psi
psi_reading['timestamp'] = time.time()
psi_reading['timestamp'] = now
print("pondlevel driver still alive...")
if (now - self.public_ip_address_last_checked) > ip_check_after or self.forceSend:
self._check_ip_address()
log.info("pondlevel driver still alive...")
if self.forceSend:
if send_loops > 2:
print("Turning off forceSend")
log.info("Turning off forceSend")
self.forceSend = False
send_loops = 0
else:
send_loops += 1
time.sleep(10)
def _check_ip_address(self):
"""Check the public IP address and send to Meshify if changed."""
self.public_ip_address_last_checked = time.time()
test_public_ip = get_public_ip_address()
if not test_public_ip == self.public_ip_address:
self.sendtodb('public_ip_address', test_public_ip, 0)
self.public_ip_address = test_public_ip
def send_calibration_points(self):
"""Send calibration data from database to Meshify."""
strapping_table_list = []
@@ -212,14 +230,14 @@ class start(threading.Thread, deviceBase):
"""Add a JSON calibration point to the database."""
try:
new_point = json.loads(value.replace("'", '"'))
print("Trying to add Height: {}, volume: {}".format(new_point['height'], new_point['volume']))
log.info("Trying to add Height: {}, volume: {}".format(new_point['height'], new_point['volume']))
self.strapping_table[float(new_point['height'])] = float(new_point['volume'])
self.store_strapping_table()
# db.insert_calibration_data(new_point['height'], new_point['volume'])
self.send_calibration_points()
return True
except Exception as e:
print("EXCEPTION: {}".format(e))
log.error("EXCEPTION: {}".format(e))
def pondlevel_deletecalibrationpoint(self, name, value):
"""Delete a calibration point from the database."""
@@ -230,7 +248,7 @@ class start(threading.Thread, deviceBase):
self.send_calibration_points()
return True
except Exception as e:
print("Error deleting calibration point: {}".format(e))
log.error("Error deleting calibration point: {}".format(e))
return(e)
def store_scaling_data(self):
@@ -261,8 +279,8 @@ class start(threading.Thread, deviceBase):
for height_i in height_list:
strapping_table_list.append([float(height_i), float(self.strapping_table[height_i])])
print("Strapping Table Data\n===")
print(strapping_table_list)
# print("Strapping Table Data\n===")
# print(strapping_table_list)
cal_min_height = strapping_table_list[0][0]
cal_max_height = strapping_table_list[-1][0]
@@ -286,7 +304,7 @@ class start(threading.Thread, deviceBase):
return line_m * height + line_b
except Exception as e:
print("Error in get_volume_for_height: {}".format(e))
log.error("Error in get_volume_for_height: {}".format(e))
return 0.0
def pondlevel_setrawmin(self, name, value):