-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add watchdog and fix tridium ingest issues
- Loading branch information
1 parent
2394119
commit b4208d1
Showing
7 changed files
with
117 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
ALFALFA_HOST=http://192.168.0.19 | ||
ALFALFA_SITE_ID=13607fa0-ad7a-11ed-a09f-a34350b09f8f | ||
ALFALFA_HOST=https://alfalfa.nrel.gov | ||
ALFALFA_SITE=bacnet | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from logging import getLogger | ||
import logging | ||
import os | ||
from subprocess import Popen, PIPE, STDOUT | ||
import sys | ||
from alfalfa_client import AlfalfaClient | ||
from requests import HTTPError | ||
from uuid import UUID | ||
from asyncio import run, create_task | ||
import asyncio | ||
|
||
def get_site_id(client: AlfalfaClient, alfalfa_site: str): | ||
try: | ||
return client.get_alias(alfalfa_site) | ||
except HTTPError: | ||
if is_valid_uuid(alfalfa_site): | ||
return alfalfa_site | ||
else: | ||
return None | ||
|
||
def is_valid_uuid(uuid_str: str, version="4"): | ||
try: | ||
uuid_obj = UUID(uuid_str, version=version) | ||
except ValueError: | ||
return False | ||
return str(uuid_obj) == uuid_str | ||
|
||
def is_process_alive(process: Popen): | ||
if process: | ||
process.poll() | ||
return process.returncode == None | ||
return False | ||
|
||
|
||
async def main_loop(host: str, alfalfa_site: str, command: str): | ||
|
||
logger = getLogger("ALFALFA WATCHDOG") | ||
logger.setLevel(logging.DEBUG) | ||
|
||
ch = logging.StreamHandler() | ||
ch.setLevel(logging.DEBUG) | ||
|
||
# create formatter | ||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | ||
|
||
# add formatter to ch | ||
ch.setFormatter(formatter) | ||
|
||
logger.addHandler(ch) | ||
|
||
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", command, host, site_id]) | ||
logger.info(f"Spawned new child process: '{child_process.pid}'") | ||
old_site_id = site_id | ||
elif site_id == None: | ||
logger.info(f"No site found with identifier: '{alfalfa_site}'") | ||
|
||
await asyncio.sleep(5) | ||
|
||
if __name__ == "__main__": | ||
alfalfa_site = os.getenv('ALFALFA_SITE') | ||
host = os.getenv('ALFALFA_HOST') | ||
command = sys.argv[1] | ||
run(main_loop(host, alfalfa_site, command)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
docker stop device | ||
docker rm device | ||
docker build -t alfalfa-bacnet-bridge . | ||
docker run -it --env-file=.env -p 47808:47808/udp --name="device" -h device alfalfa-bacnet-bridge poetry run python alfalfa_bacnet_bridge/alfalfa_bacnet_bridge.py | ||
docker run -it --env-file=.env -p 47808:47808/udp --name="device" -h device alfalfa-bacnet-bridge poetry run python alfalfa_bacnet_bridge/alfalfa_watchdog.py alfalfa_bacnet_bridge/alfalfa_bacnet_bridge.py |