From 60eb92e3f9e93525ea2288e386bcdbecc9a0a9ca Mon Sep 17 00:00:00 2001 From: TShapinsky Date: Fri, 18 Aug 2023 14:45:14 -0600 Subject: [PATCH 1/3] add check for non-finite floats before writing to Alfalfa --- alfalfa_bacnet_bridge/alfalfa_bacnet_bridge.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/alfalfa_bacnet_bridge/alfalfa_bacnet_bridge.py b/alfalfa_bacnet_bridge/alfalfa_bacnet_bridge.py index e338756..5a7357a 100644 --- a/alfalfa_bacnet_bridge/alfalfa_bacnet_bridge.py +++ b/alfalfa_bacnet_bridge/alfalfa_bacnet_bridge.py @@ -1,4 +1,5 @@ from datetime import datetime +import math import sys import os from bacpypes.app import BIPSimpleApplication @@ -63,7 +64,7 @@ def __init__(self, host, site_id: SiteID) -> None: maxApduLengthAccepted=int(1024), segmentationSupported="segmentedBoth", vendorIdentifier=555, - vendorName=CharacterString("NREL"), + vendorName=CharacterString("NREL"), modelName=CharacterString("Alfalfa BACnet Bridge"), systemStatus=DeviceStatus(1), description=CharacterString("BACpypes (Python) based tool for exposing alfalfa models to real world BAS systems via BACnet"), @@ -127,13 +128,20 @@ def main_loop(): if point in inputs: current_value, value_type = object._highest_priority_value() if value_type is not None: - set_inputs[point] = current_value - object._had_value = True + if math.isfinite(current_value): + set_inputs[point] = current_value + object._had_value = True + else: + print(f"Got non-finite value {current_value} for point {point}") elif object._had_value: set_inputs[point] = None object._had_value = False if len(set_inputs) > 0: - self.client.set_inputs(self.site_id, set_inputs) + try: + self.client.set_inputs(self.site_id, set_inputs) + except Exception as e: + print(e) + deferred(main_loop) run() From 1e3f2ebb0dd49f03f6d7878d145e925326793340 Mon Sep 17 00:00:00 2001 From: TShapinsky Date: Thu, 24 Aug 2023 10:46:13 -0600 Subject: [PATCH 2/3] catch errors in watchdog --- alfalfa_bacnet_bridge/alfalfa_watchdog.py | 52 ++++++++++++----------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/alfalfa_bacnet_bridge/alfalfa_watchdog.py b/alfalfa_bacnet_bridge/alfalfa_watchdog.py index 0fe2584..a3f6515 100644 --- a/alfalfa_bacnet_bridge/alfalfa_watchdog.py +++ b/alfalfa_bacnet_bridge/alfalfa_watchdog.py @@ -51,32 +51,36 @@ async def main_loop(host: str, alfalfa_site: str, command: str): client = AlfalfaClient(host) old_site_id = None child_process:Popen = None - + while True: - site_id = get_site_id(client, alfalfa_site) - - if site_id != None and (site_id != old_site_id or not is_process_alive(child_process)): - logger.info(f"Found new site with ID: '{site_id}'") - status = client.status(site_id) - logger.info(f"Site status is: '{status}'") - if status == "running": - if is_process_alive(child_process): - logger.info(f"Killing old child process: '{child_process.pid}'") - child_process.kill() - elif child_process != None: - logger.info(f"Process '{child_process.pid}' died, restarting process") - child_process = Popen(["python", "-u", command, host, site_id]) - logger.info(f"Spawned new child process: '{child_process.pid}'") - old_site_id = site_id - - if site_id and is_process_alive(child_process) and client.status(site_id) != "running": - logger.info(f"Killing old child process: '{child_process.pid}'") - child_process.kill() - - elif site_id == None: - logger.info(f"No site found with identifier: '{alfalfa_site}'") - + try: + site_id = get_site_id(client, alfalfa_site) + + if site_id != None and (site_id != old_site_id or not is_process_alive(child_process)): + logger.info(f"Found new site with ID: '{site_id}'") + status = client.status(site_id) + logger.info(f"Site status is: '{status}'") + if status == "running": + if is_process_alive(child_process): + logger.info(f"Killing old child process: '{child_process.pid}'") + child_process.kill() + elif child_process != None: + logger.info(f"Process '{child_process.pid}' died, restarting process") + child_process = Popen(["python", "-u", command, host, site_id]) + logger.info(f"Spawned new child process: '{child_process.pid}'") + old_site_id = site_id + + if site_id and is_process_alive(child_process) and client.status(site_id) != "running": + logger.info(f"Killing old child process: '{child_process.pid}'") + child_process.kill() + + elif site_id == None: + logger.info(f"No site found with identifier: '{alfalfa_site}'") + + except Exception as e: + print(e) + await asyncio.sleep(5) if __name__ == "__main__": From c01d9c0d1bbb75f3495e76721a5127d8c2992b95 Mon Sep 17 00:00:00 2001 From: TShapinsky Date: Mon, 11 Sep 2023 12:00:05 -0600 Subject: [PATCH 3/3] add more try excepts around network calls in the main_loop --- alfalfa_bacnet_bridge/alfalfa_bacnet_bridge.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/alfalfa_bacnet_bridge/alfalfa_bacnet_bridge.py b/alfalfa_bacnet_bridge/alfalfa_bacnet_bridge.py index 5a7357a..b0923c7 100644 --- a/alfalfa_bacnet_bridge/alfalfa_bacnet_bridge.py +++ b/alfalfa_bacnet_bridge/alfalfa_bacnet_bridge.py @@ -112,10 +112,14 @@ def run(self): @recurring_function(1000) @bacpypes_debugging def main_loop(): - inputs = self.client.get_inputs(self.site_id) - outputs = self.client.get_outputs(self.site_id) - - sim_time = self.client.get_sim_time(self.site_id) + try: + inputs = self.client.get_inputs(self.site_id) + outputs = self.client.get_outputs(self.site_id) + + sim_time = self.client.get_sim_time(self.site_id) + except Exception as e: + print(e) + return self.device._date_time = sim_time set_inputs = {}