small changes

This commit is contained in:
2020-09-22 12:34:33 -05:00
parent 206dd5fd4a
commit 392aa3b6ac
3 changed files with 100 additions and 45 deletions

View File

@@ -96,12 +96,16 @@ def write_tag(addr, tag, val, plc_type="CLX"):
def byteSwap32(array):
#array is a list of 2 dec numbers
newVal = ""
for i in array:
i = hex(i).replace('0x', '')
while len(i) < 4:
i = "0" + i
print i
newVal = i + newVal
try:
for i in array:
i = hex(i).replace('0x', '')
while len(i) < 4:
i = "0" + i
print i
newVal = i + newVal
except:
log.error("Issues with modbus read in Channel sending null")
return None
print newVal
return struct.unpack('!f', newVal.decode('hex'))[0]
@@ -217,20 +221,29 @@ class ModbusChannel(Channel):
try:
read_value = instrument.read_float(self.register_number,4,self.channel_size)
except Exception as e:
log.info(e)
return None
log.info("Error in read value: {}\nTrying one more time".format(e))
try:
read_value = instrument.read_float(self.register_number,4,self.channel_size)
except:
return None
elif self.data_type == "FLOATBS":
try:
read_value = byteSwap32(instrument.read_registers(self.register_number,2, 4))
except Exception as e:
log.info(e)
return None
log.info("Error in read value: {}\nTrying one more time".format(e))
try:
read_value = byteSwap32(instrument.read_registers(self.register_number,2,4))
except:
return None
elif self.data_type == "INTEGER" or self.data_type == "STRING":
try:
read_value = instrument.read_register(self.register_number, self.scaling, 4)
except Exception as e:
log.info(e)
return None
log.info("Error in read value: {}\nTrying one more time".format(e))
try:
read_value = instrument.read_register(self.register_number,self.scaling,4)
except:
return None
read_value = self.transform_fn(read_value)
return read_value

View File

@@ -8,7 +8,7 @@
"file4": "Tags.py"
},
"deviceName": "flowmonitor",
"releaseVersion": "21",
"releaseVersion": "22",
"driverFileName": "flowmonitor.py",
"driverId": "0140"
}

View File

@@ -93,7 +93,7 @@ class start(threading.Thread, deviceBase):
mqtt=mqtt, Nodes=Nodes)
self.daemon = True
self.version = "21"
self.version = "22"
self.lock = threading.Lock()
self.force_send = False
self.public_ip_address = ""
@@ -226,15 +226,21 @@ class start(threading.Thread, deviceBase):
val = psi_val
elif chan.mesh_name == "run_status":
val = runstatus
elif PERSIST["modbus"]:
val = chan.read()
if chan.mesh_name == "gpm_flow":
elif PERSIST["modbus"]:
try:
val = chan.read()
except:
log.error("Issue with modbus read in driver")
val = None
if chan.mesh_name == "gpm_flow" and val != None:
if val == 0:
runstatus = "Stopped"
elif val > PERSIST["lowflow"]:
runstatus = "Running"
else:
runstatus = "Running: Low Flow"
if PERSIST["gpm_or_bpd"] == "bpd":
val = ((val * 42.0) / 24.0) / 60.0
elif chan.mesh_name == "gpm_flow":
val = gpm_val
else:
@@ -442,27 +448,49 @@ class start(threading.Thread, deviceBase):
TOTALIZER['Todays'] = val - TOTALIZER['Daily Holding']
TOTALIZER['Current Months'] = val - TOTALIZER['Monthly Holding']
TOTALIZER['Lifetime'] = val
self.sendtodb('gal_total', TOTALIZER['Todays'], 0)
self.sendtodb('bbl_total', TOTALIZER['Todays']/42, 0)
self.sendtodb('gal_total_thismonth', TOTALIZER['Current Months'], 0)
self.sendtodb('bbl_total_thismonth', TOTALIZER['Current Months']/42, 0)
self.sendtodb('gal_total_yesterday', TOTALIZER['Yesterdays'], 0)
self.sendtodb('bbl_total_yesterday', TOTALIZER['Yesterdays']/42, 0)
if self.force_send:
self.sendtodb('gal_total_lastmonth', TOTALIZER['Previous Months'], 0)
self.sendtodb('bbl_total_lastmonth', TOTALIZER['Previous Months']/42, 0)
if PERSIST["gpm_or_bpd"] == "gpm":
self.sendtodb('gal_total', TOTALIZER['Todays'], 0)
self.sendtodb('bbl_total', TOTALIZER['Todays']/42, 0)
self.sendtodb('gal_total_thismonth', TOTALIZER['Current Months'], 0)
self.sendtodb('bbl_total_thismonth', TOTALIZER['Current Months']/42, 0)
self.sendtodb('gal_total_yesterday', TOTALIZER['Yesterdays'], 0)
self.sendtodb('bbl_total_yesterday', TOTALIZER['Yesterdays']/42, 0)
if self.force_send:
self.sendtodb('gal_total_lastmonth', TOTALIZER['Previous Months'], 0)
self.sendtodb('bbl_total_lastmonth', TOTALIZER['Previous Months']/42, 0)
else:
self.sendtodb('gal_total', TOTALIZER['Todays']*42, 0)
self.sendtodb('bbl_total', TOTALIZER['Todays'], 0)
self.sendtodb('gal_total_thismonth', TOTALIZER['Current Months'] * 42, 0)
self.sendtodb('bbl_total_thismonth', TOTALIZER['Current Months'], 0)
self.sendtodb('gal_total_yesterday', TOTALIZER['Yesterdays'] * 42, 0)
self.sendtodb('bbl_total_yesterday', TOTALIZER['Yesterdays'], 0)
if self.force_send:
self.sendtodb('gal_total_lastmonth', TOTALIZER['Previous Months'] * 42, 0)
self.sendtodb('bbl_total_lastmonth', TOTALIZER['Previous Months'], 0)
TOTALIZER['Last Report'] = time.time()
except:
if time.time() - TOTALIZER['Last Report'] > 3600 or self.force_send:
self.sendtodb('gal_total', TOTALIZER['Todays'], 0)
self.sendtodb('bbl_total', TOTALIZER['Todays']/42, 0)
self.sendtodb('gal_total_thismonth', TOTALIZER['Current Months'], 0)
self.sendtodb('bbl_total_thismonth', TOTALIZER['Current Months']/42, 0)
self.sendtodb('gal_total_yesterday', TOTALIZER['Yesterdays'], 0)
self.sendtodb('bbl_total_yesterday', TOTALIZER['Yesterdays']/42, 0)
if self.force_send:
self.sendtodb('gal_total_lastmonth', TOTALIZER['Previous Months'], 0)
self.sendtodb('bbl_total_lastmonth', TOTALIZER['Previous Months']/42, 0)
if PERSIST["gpm_or_bpd"] == "gpm":
self.sendtodb('gal_total', TOTALIZER['Todays'], 0)
self.sendtodb('bbl_total', TOTALIZER['Todays']/42, 0)
self.sendtodb('gal_total_thismonth', TOTALIZER['Current Months'], 0)
self.sendtodb('bbl_total_thismonth', TOTALIZER['Current Months']/42, 0)
self.sendtodb('gal_total_yesterday', TOTALIZER['Yesterdays'], 0)
self.sendtodb('bbl_total_yesterday', TOTALIZER['Yesterdays']/42, 0)
if self.force_send:
self.sendtodb('gal_total_lastmonth', TOTALIZER['Previous Months'], 0)
self.sendtodb('bbl_total_lastmonth', TOTALIZER['Previous Months']/42, 0)
else:
self.sendtodb('gal_total', TOTALIZER['Todays']*42, 0)
self.sendtodb('bbl_total', TOTALIZER['Todays'], 0)
self.sendtodb('gal_total_thismonth', TOTALIZER['Current Months'] * 42, 0)
self.sendtodb('bbl_total_thismonth', TOTALIZER['Current Months'], 0)
self.sendtodb('gal_total_yesterday', TOTALIZER['Yesterdays'] * 42, 0)
self.sendtodb('bbl_total_yesterday', TOTALIZER['Yesterdays'], 0)
if self.force_send:
self.sendtodb('gal_total_lastmonth', TOTALIZER['Previous Months'] * 42, 0)
self.sendtodb('bbl_total_lastmonth', TOTALIZER['Previous Months'], 0)
TOTALIZER['Last Report'] = time.time()
#If the current day doesn't equal the stored day roll the dailies over
@@ -478,12 +506,20 @@ class start(threading.Thread, deviceBase):
TOTALIZER['Daily Holding'] = val
TOTALIZER['Lifetime'] = val
TOTALIZER['Day'] = day
self.sendtodb('gal_total', TOTALIZER['Todays'], 0)
self.sendtodb('bbl_total', TOTALIZER['Todays']/42, 0)
self.sendtodb('total_fm_yesterday_gal', TOTALIZER['Yesterdays'], 0)
self.sendtodb('total_fm_yesterday_bbls', TOTALIZER['Yesterdays']/42, 0)
self.sendtodb('lifetime_flow_meter_gal', TOTALIZER['Lifetime'], 0)
self.sendtodb('lifetime_flow_meter_bbls', TOTALIZER['Lifetime']/42, 0)
if PERSIST["gpm_or_bpd"] == "gpm":
self.sendtodb('gal_total', TOTALIZER['Todays'], 0)
self.sendtodb('bbl_total', TOTALIZER['Todays']/42, 0)
self.sendtodb('total_fm_yesterday_gal', TOTALIZER['Yesterdays'], 0)
self.sendtodb('total_fm_yesterday_bbls', TOTALIZER['Yesterdays']/42, 0)
self.sendtodb('lifetime_flow_meter_gal', TOTALIZER['Lifetime'], 0)
self.sendtodb('lifetime_flow_meter_bbls', TOTALIZER['Lifetime']/42, 0)
else:
self.sendtodb('gal_total', TOTALIZER['Todays'] * 42, 0)
self.sendtodb('bbl_total', TOTALIZER['Todays'], 0)
self.sendtodb('total_fm_yesterday_gal', TOTALIZER['Yesterdays'] * 42, 0)
self.sendtodb('total_fm_yesterday_bbls', TOTALIZER['Yesterdays'], 0)
self.sendtodb('lifetime_flow_meter_gal', TOTALIZER['Lifetime'] * 42, 0)
self.sendtodb('lifetime_flow_meter_bbls', TOTALIZER['Lifetime'], 0)
TOTALIZER['Last Report'] = time.time()
#the day has rolled over if the month also rolls over
if not(month == TOTALIZER['Month']):
@@ -497,10 +533,16 @@ class start(threading.Thread, deviceBase):
TOTALIZER['Current Months'] = 0
TOTALIZER['Monthly Holding'] = val
TOTALIZER['Month'] = month
self.sendtodb('gal_total_thismonth', TOTALIZER['Current Months'], 0)
self.sendtodb('bbl_total_thismonth', TOTALIZER['Current Months']/42, 0)
self.sendtodb('gal_total_lastmonth', TOTALIZER['Previous Months'], 0)
self.sendtodb('bbl_total_lastmonth', TOTALIZER['Previous Months']/42, 0)
if PERSIST["gpm_or_bpd"] == "gpm":
self.sendtodb('gal_total_thismonth', TOTALIZER['Current Months'], 0)
self.sendtodb('bbl_total_thismonth', TOTALIZER['Current Months']/42, 0)
self.sendtodb('gal_total_lastmonth', TOTALIZER['Previous Months'], 0)
self.sendtodb('bbl_total_lastmonth', TOTALIZER['Previous Months']/42, 0)
else:
self.sendtodb('gal_total_thismonth', TOTALIZER['Current Months'] * 42, 0)
self.sendtodb('bbl_total_thismonth', TOTALIZER['Current Months'], 0)
self.sendtodb('gal_total_lastmonth', TOTALIZER['Previous Months'] * 42, 0)
self.sendtodb('bbl_total_lastmonth', TOTALIZER['Previous Months'], 0)
TOTALIZER['Last Report'] = time.time()
persistence.store(TOTALIZER, 'totalizers.json')