Skip to content

Commit

Permalink
Merge pull request #14 from NREL/nonfinte_float_check
Browse files Browse the repository at this point in the history
Nonfinte float check
  • Loading branch information
TShapinsky authored Jan 30, 2024
2 parents 48ec2b1 + c01d9c0 commit 30eb90e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
28 changes: 20 additions & 8 deletions alfalfa_bacnet_bridge/alfalfa_bacnet_bridge.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
import math
import sys
import os
from bacpypes.app import BIPSimpleApplication
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -111,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 = {}
Expand All @@ -127,13 +132,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()
Expand Down
52 changes: 28 additions & 24 deletions alfalfa_bacnet_bridge/alfalfa_watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down

0 comments on commit 30eb90e

Please sign in to comment.