From 4fcc83760d9cbf6ef953b6c871e6a32bd5c59ef8 Mon Sep 17 00:00:00 2001 From: Nico Melone Date: Tue, 14 Dec 2021 16:12:02 -0600 Subject: [PATCH] fixed totalizer bug --- plcfreshwater/Tags.py | 2 +- plcfreshwater/config.txt | 2 +- plcfreshwater/plcfreshwater.py | 45 +++++++++++++-- .../reset lifetime/reset_lifetime.py | 55 +++++++++++++++++++ 4 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 plcfreshwater/reset lifetime/reset_lifetime.py diff --git a/plcfreshwater/Tags.py b/plcfreshwater/Tags.py index 1143bc4..b1d7386 100644 --- a/plcfreshwater/Tags.py +++ b/plcfreshwater/Tags.py @@ -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"}), diff --git a/plcfreshwater/config.txt b/plcfreshwater/config.txt index 367b3d4..dd1f95e 100644 --- a/plcfreshwater/config.txt +++ b/plcfreshwater/config.txt @@ -8,7 +8,7 @@ "file4": "Tags.py" }, "deviceName": "plcfreshwater", - "releaseVersion": "13", + "releaseVersion": "14", "driverFileName": "plcfreshwater.py", "driverId": "0100" } \ No newline at end of file diff --git a/plcfreshwater/plcfreshwater.py b/plcfreshwater/plcfreshwater.py index c04d261..91c2adb 100644 --- a/plcfreshwater/plcfreshwater.py +++ b/plcfreshwater/plcfreshwater.py @@ -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'] diff --git a/plcfreshwater/reset lifetime/reset_lifetime.py b/plcfreshwater/reset lifetime/reset_lifetime.py new file mode 100644 index 0000000..79aefc5 --- /dev/null +++ b/plcfreshwater/reset lifetime/reset_lifetime.py @@ -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")) \ No newline at end of file