Adds code for pressure transmitter

This commit is contained in:
Patrick McDonagh
2018-04-24 11:58:53 -05:00
parent 2d93d78a7f
commit 50f64b41c2
2 changed files with 92 additions and 12 deletions

View File

@@ -0,0 +1,2 @@
class deviceBase(object):
pass

View File

@@ -12,10 +12,17 @@ pond_level = {
'value': 15.0,
'timestamp': 0
}
psi_reading = {
'value': 0.0,
'timestamp': 0
}
send_delta = 0.5
send_time = 300
temp_pl = pond_level['value']
temp_psi = psi_reading['value']
scaling_persist = persistence.load(filename="scaling_persist.json")
strapping_persist = persistence.load(filename="strapping_persist.json")
@@ -38,7 +45,7 @@ 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 = "1"
self.version = "2"
self.finished = threading.Event()
self.forceSend = False
threading.Thread.start(self)
@@ -48,6 +55,11 @@ class start(threading.Thread, deviceBase):
self.PL_EUMAX = 23.068
self.PL_EUMIN = 0.0
self.PSI_RAWMAX = 10.0
self.PSI_RAWMIN = 0.0
self.PSI_EUMAX = 600.0
self.PSI_EUMIN = 0.0
# Strapping table format
# {
# height: volume,
@@ -65,7 +77,7 @@ class start(threading.Thread, deviceBase):
def run(self):
"""Actually run the driver."""
global pond_level, temp_pl, send_delta, send_time
global pond_level, temp_pl, psi_reading, temp_psi, send_delta, send_time
try:
self.PL_RAWMAX = scaling_persist['raw_max']
@@ -73,9 +85,19 @@ 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 scaling parameters.")
print("No persistence data for pond level scaling parameters.")
except TypeError:
print("No persistence data for scaling parameters.")
print("No persistence data for pond level scaling parameters.")
try:
self.PSI_RAWMAX = scaling_persist['psi_raw_max']
self.PSI_RAWMIN = scaling_persist['psi_raw_min']
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.")
except TypeError:
print("No persistence data for pressure scaling parameters.")
try:
if len(strapping_persist.keys()) > 0:
@@ -100,25 +122,44 @@ class start(threading.Thread, deviceBase):
self.sendtodb("seteumax", self.PL_EUMAX, 0)
self.sendtodb("seteumin", self.PL_EUMIN, 0)
self.sendtodb("setpsirawmax", self.PSI_RAWMAX, 0)
self.sendtodb("setpsirawmin", self.PSI_RAWMIN, 0)
self.sendtodb("setpsieumax", self.PSI_EUMAX, 0)
self.sendtodb("setpsieumin", self.PSI_EUMIN, 0)
send_loops = 0
while True:
send_now = False
pl_send_now = False
psi_send_now = False
if self.forceSend:
print "FORCE SEND: TRUE"
send_now = True
pl_send_now = True
psi_send_now = True
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
if abs(temp_pl - pond_level['value']) > send_delta:
print("Sending {} due to value change".format(temp_pl))
send_now = True
print("Sending pond level {} due to value change".format(temp_pl))
pl_send_now = True
elif (time.time() - pond_level['timestamp']) > send_time:
print("Sending {} due to time limit".format(temp_pl))
send_now = True
print("Sending pond level {} due to time limit".format(temp_pl))
pl_send_now = True
if send_now:
# 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))
psi_send_now = True
elif (time.time() - psi_reading['timestamp']) > send_time:
print("Sending pressure psi {} due to time limit".format(temp_psi))
psi_send_now = True
if pl_send_now:
self.sendtodb('pond_level', temp_pl, 0)
pond_volume = 0.0
try:
@@ -130,6 +171,11 @@ class start(threading.Thread, deviceBase):
pond_level['value'] = temp_pl
pond_level['timestamp'] = time.time()
if psi_send_now:
self.sendtodb('pressure_psi', temp_psi, 0)
psi_reading['value'] = temp_psi
psi_reading['timestamp'] = time.time()
print("pondlevel driver still alive...")
if self.forceSend:
if send_loops > 2:
@@ -188,7 +234,11 @@ class start(threading.Thread, deviceBase):
'raw_max': self.PL_RAWMAX,
'raw_min': self.PL_RAWMIN,
'eu_max': self.PL_EUMAX,
'eu_min': self.PL_EUMIN
'eu_min': self.PL_EUMIN,
'psi_raw_max': self.PSI_RAWMAX,
'psi_raw_min': self.PSI_RAWMIN,
'psi_eu_max': self.PSI_EUMAX,
'psi_eu_min': self.PSI_EUMIN
}
persistence.store(scale_obj, filename="scaling_persist.json")
@@ -261,3 +311,31 @@ class start(threading.Thread, deviceBase):
self.sendtodb("seteumax", self.PL_EUMAX, 0)
self.store_scaling_data()
return(True)
def pondlevel_setpsirawmin(self, name, value):
"""Set the raw min pressure scaling value."""
self.PSI_RAWMIN = float(value)
self.sendtodb("setpsirawmin", self.PSI_RAWMIN, 0)
self.store_scaling_data()
return(True)
def pondlevel_setpsirawmax(self, name, value):
"""Set the raw max pressure scaling value."""
self.PSI_RAWMAX = float(value)
self.sendtodb("setpsirawmax", self.PSI_RAWMAX, 0)
self.store_scaling_data()
return(True)
def pondlevel_setpsieumin(self, name, value):
"""Set the psi min pressure scaling value."""
self.PSI_EUMIN = float(value)
self.sendtodb("setpsieumin", self.PSI_EUMIN, 0)
self.store_scaling_data()
return(True)
def pondlevel_setpsieumax(self, name, value):
"""Set the psi max pressure scaling value."""
self.PSI_EUMAX = float(value)
self.sendtodb("setpsieumax", self.PSI_EUMAX, 0)
self.store_scaling_data()
return(True)