fixed totalizer bug

This commit is contained in:
Nico Melone
2021-12-14 16:12:02 -06:00
parent 38c0657fcf
commit 4fcc83760d
4 changed files with 97 additions and 7 deletions

View File

@@ -2,7 +2,7 @@ from Channel import PLCChannel, ModbusChannel
from plcfreshwater import PLC_IP_ADDRESS
tags = [
PLCChannel(PLC_IP_ADDRESS, "scaled_flow_meter","Scaled_Flow_Meter","REAL", 3, 3600, plc_type="Micro800"),
PLCChannel(PLC_IP_ADDRESS, "scaled_flow_meter","Scaled_Flow_Meter","REAL", 10, 3600, plc_type="Micro800"),
PLCChannel(PLC_IP_ADDRESS, "scaled_pressure_transducer","Scaled_Pressure_Transducer","REAL", 3, 3600, plc_type="Micro800"),
PLCChannel(PLC_IP_ADDRESS, "raw_hand_input","Raw_Hand_Input","BOOL", 1, 7200, plc_type="Micro800", map_={0: "Off", 1: "On", None: "N/A"}),
PLCChannel(PLC_IP_ADDRESS, "raw_auto_input","Raw_Auto_Input","BOOL", 1, 7200, plc_type="Micro800", map_={0: "Off", 1: "On", None: "N/A"}),

View File

@@ -8,7 +8,7 @@
"file4": "Tags.py"
},
"deviceName": "plcfreshwater",
"releaseVersion": "13",
"releaseVersion": "14",
"driverFileName": "plcfreshwater.py",
"driverId": "0100"
}

View File

@@ -53,7 +53,7 @@ class start(threading.Thread, deviceBase):
mqtt=mqtt, Nodes=Nodes)
self.daemon = True
self.version = "13"
self.version = "14"
self.finished = threading.Event()
self.force_send = False
self.public_ip_address = ""
@@ -62,6 +62,9 @@ class start(threading.Thread, deviceBase):
self.plcip = ""
self.ping_counter = 0
self.plc_ping_status = 'Default'
self.flowing = False
self.totalizing = False
self.totals_counter = 0
threading.Thread.start(self)
# this is a required function for all drivers, its goal is to upload some piece of data
@@ -125,12 +128,17 @@ class start(threading.Thread, deviceBase):
if chan.mesh_name == "lifetime_flow_meter_gal":
self.totalize(val)
else:
if chan.mesh_name == "scaled_flow_meter":
if val > 0:
self.flowing = True
else:
self.flowing = False
if chan.check(val, self.force_send):
self.sendtodbDev(1, chan.mesh_name, chan.value, 0, 'plcfreshwater')
#time.sleep(TAG_DATAERROR_SLEEPTIME) # sleep to allow Micro800 to handle ENET requests
except Exception as e:
log.error("Something went wrong in read: {}".format(e))
self.check_totals_reset(self.flowing,self.totalizing)
# print("plcfreshwater driver still alive...")
try:
plc_ping = os.system("ping -c 1 " + IP_TABLE[self.mac] + " > /dev/null 2>&1")
@@ -179,17 +187,35 @@ class start(threading.Thread, deviceBase):
#and then check the response...
if response == 0:
print hostname, 'is up!'
print(hostname, 'is up!')
self.ping_counter = 0
else:
print hostname, 'is down!'
print(hostname, 'is down!')
self.ping_counter += 1
if self.ping_counter >= 3:
print("Rebooting because no internet detected")
os.system('reboot')
def check_totals_reset(self, flowing, totalizing):
if flowing and not totalizing:
self.totals_counter = self.totals_counter + 1
else:
self.totals_counter = 0
if self.totals_counter >= 3:
self.fix_totals()
self.totals_counter = 0
def fix_totals(self):
Daily_Holding = PERSIST["Daily Holding"] - PERSIST["Monthly Holding"]
resp = write_tag(self.plcip, "Lifetime_Flow_Meter_Gal", PERSIST["Lifetime"] - PERSIST["Monthly Holding"], plc_type="Micro800")
if resp == True:
PERSIST["Daily Holding"] = Daily_Holding
PERSIST["Monthly Holding"] = 0.0
persistence.store(PERSIST, 'totalizers_{}.json'.format(self.mac))
log.info("RESETTING TOTALIZERS!!!")
def plcfreshwater_sync(self, name, value):
"""Sync all data from the driver."""
self.force_send = True
@@ -225,6 +251,15 @@ class start(threading.Thread, deviceBase):
PERSIST['Daily Holding'] = val
PERSIST['Monthly Holding'] = val
try:
if val - PERSIST["Lifetime"] > 0:
self.totalizing = True
PERSIST['Lifetime'] = val
else:
self.totalizing = False
except:
log.error("Error while checking for totalizing")
try:
if val - PERSIST['Daily Holding'] - PERSIST['Todays'] > 500 or time.time() - PERSIST['Last Report'] > 3600 or self.force_send:
PERSIST['Todays'] = val - PERSIST['Daily Holding']

View File

@@ -0,0 +1,55 @@
from pycomm.ab_comm.clx import Driver as ClxDriver
from pycomm.cip.cip_base import CommError, DataError
import time
import sys
TAG_DATAERROR_SLEEPTIME = 5
def read_tag(addr, tag, plc_type="CLX"):
"""Read a tag from the PLC."""
direct = plc_type == "Micro800"
clx = ClxDriver()
try:
if clx.open(addr, direct_connection=direct):
try:
val = clx.read_tag(tag)
clx.close()
return val
except DataError as err:
clx.close()
time.sleep(TAG_DATAERROR_SLEEPTIME)
print("Data Error during readTag({}, {}): {}".format(addr, tag, err))
except CommError:
# err = c.get_status()
#clx.close()
print("Could not connect during readTag({}, {})".format(addr, tag))
except AttributeError as err:
clx.close()
print("AttributeError during readTag({}, {}): \n{}".format(addr, tag, err))
clx.close()
return False
def write_tag(addr, tag, val, plc_type="CLX"):
"""Write a tag value to the PLC."""
direct = plc_type == "Micro800"
clx = ClxDriver()
try:
if clx.open(addr, direct_connection=direct):
try:
initial_val = clx.read_tag(tag)
write_status = clx.write_tag(tag, val, initial_val[1])
clx.close()
return write_status
except DataError as err:
clx_err = clx.get_status()
clx.close()
print("--\nDataError during writeTag({}, {}, {}, plc_type={}) -- {}\n{}\n".format(addr, tag, val, plc_type, err, clx_err))
except CommError as err:
clx_err = clx.get_status()
print("--\nCommError during write_tag({}, {}, {}, plc_type={})\n{}\n--".format(addr, tag, val, plc_type, err))
#clx.close()
return False
resp = write_tag(sys.argv[1], "Lifetime_Flow_Meter_Gal", float(sys.argv[2]), "Micro800")
if resp:
print(read_tag(sys.argv[1], "Lifetime_Flow_Meter_Gal", "Micro800"))