Skip to content

Commit

Permalink
Merge pull request #15 from NREL/alfalfa_api_refactor
Browse files Browse the repository at this point in the history
Point to api refactored client
  • Loading branch information
TShapinsky authored Jun 28, 2024
2 parents 30eb90e + c0a41c5 commit 4da0479
Show file tree
Hide file tree
Showing 4 changed files with 1,054 additions and 926 deletions.
50 changes: 33 additions & 17 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 logging
import math
import sys
import os
Expand All @@ -10,7 +11,7 @@
from bacpypes.local.object import AnalogValueCmdObject
from bacpypes.task import recurring_function
from alfalfa_client.alfalfa_client import AlfalfaClient
from alfalfa_client.alfalfa_client import SiteID
from alfalfa_client.alfalfa_client import RunID
from bacpypes.service.device import DeviceCommunicationControlServices
from bacpypes.service.object import ReadWritePropertyMultipleServices
from bacpypes.primitivedata import CharacterString, Date, Time
Expand All @@ -19,6 +20,21 @@
_debug = 0
_log = ModuleLogger(globals())

logger = logging.getLogger("AlfalfaBACnetBridge")
logger.setLevel(logging.DEBUG)
log_formatter = logging.Formatter('%(asctime)s - %(threadName)s - %(levelname)s - %(message)s')

stdout_handler = logging.StreamHandler()
stdout_handler.setLevel(logging.DEBUG)
stdout_handler.setFormatter(log_formatter)

file_handler = logging.FileHandler('bridge.log')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(log_formatter)

logger.addHandler(stdout_handler)
logger.addHandler(file_handler)

@bacpypes_debugging
class AlfalfaBACnetApplication(BIPSimpleApplication,
ReadWritePropertyMultipleServices,
Expand Down Expand Up @@ -53,10 +69,10 @@ def ReadProperty(self, propid, arrayIndex=None):

class AlfalfaBACnetBridge():

def __init__(self, host, site_id: SiteID) -> None:
def __init__(self, host, run_id: RunID) -> None:
self.client = AlfalfaClient(host)

self.site_id = site_id
self.run_id = run_id

self.device = AlfalfaBACnetDevice(
objectName=os.getenv("ALFALFA_DEVICE_NAME","AlfalfaProxy"),
Expand All @@ -78,9 +94,9 @@ def __init__(self, host, site_id: SiteID) -> None:
self.points = {}

def setup_points(self):
inputs = self.client.get_inputs(self.site_id)
inputs = self.client.get_inputs(self.run_id)
inputs.sort()
outputs = self.client.get_outputs(self.site_id)
outputs = self.client.get_outputs(self.run_id)
output_names = list(outputs.keys())
output_names.sort()

Expand All @@ -89,18 +105,18 @@ def setup_points(self):
for input in inputs:
if input in outputs:
self.points[input] = LocalAnalogValueObject(objectName=input, objectIdentifier=("analogValue", index), sim_value=outputs[input])
print(f"Creating BIDIRECTIONAL point: '{input}'")
logger.info(f"Creating BIDIRECTIONAL point: '{input}'")
else:
self.points[input] = AnalogValueCmdObject(objectName=input, objectIdentifier=("analogValue", index))
print(f"Creating INPUT point: '{input}'")
logger.info(f"Creating INPUT point: '{input}'")
self.points[input]._had_value = False
index += 1

for output in output_names:
if output in self.points:
continue
self.points[output] = AnalogInputObject(objectName=output, objectIdentifier=("analogInput", index), presentValue=outputs[output])
print(f"Creating OUTPUT point: '{output}'")
logger.info(f"Creating OUTPUT point: '{output}'")
index += 1

for point in self.points.values():
Expand All @@ -113,12 +129,12 @@ def run(self):
@bacpypes_debugging
def main_loop():
try:
inputs = self.client.get_inputs(self.site_id)
outputs = self.client.get_outputs(self.site_id)
inputs = self.client.get_inputs(self.run_id)
outputs = self.client.get_outputs(self.run_id)

sim_time = self.client.get_sim_time(self.site_id)
sim_time = self.client.get_sim_time(self.run_id)
except Exception as e:
print(e)
logger.error(e)
return
self.device._date_time = sim_time

Expand All @@ -136,23 +152,23 @@ def main_loop():
set_inputs[point] = current_value
object._had_value = True
else:
print(f"Got non-finite value {current_value} for point {point}")
logger.warn(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:
try:
self.client.set_inputs(self.site_id, set_inputs)
self.client.set_inputs(self.run_id, set_inputs)
except Exception as e:
print(e)
logger.error(e)


deferred(main_loop)
run()

if __name__ == "__main__":
site_id = sys.argv[2]
run_id = sys.argv[2]
host = sys.argv[1]
bridge = AlfalfaBACnetBridge(host, site_id)
bridge = AlfalfaBACnetBridge(host, run_id)
bridge.setup_points()
bridge.run()
6 changes: 3 additions & 3 deletions alfalfa_bacnet_bridge/alfalfa_watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def main_loop(host: str, alfalfa_site: str, command: str):
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 status == "RUNNING":
if is_process_alive(child_process):
logger.info(f"Killing old child process: '{child_process.pid}'")
child_process.kill()
Expand All @@ -71,15 +71,15 @@ async def main_loop(host: str, alfalfa_site: str, command: str):
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":
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)
logger.error(e)

await asyncio.sleep(5)

Expand Down
Loading

0 comments on commit 4da0479

Please sign in to comment.