81 lines
1.8 KiB
Python
Raw Normal View History

2022-02-04 19:46:15 +01:00
from MqttPublish import MqttPublish
from OpcUaRequester import OpcUaRequester
from loguru import logger
import argparse
2022-02-04 20:25:32 +01:00
import json
2022-02-04 19:46:15 +01:00
import threading
import queue
2022-02-04 22:28:36 +01:00
import signal
2022-02-04 19:46:15 +01:00
deathBell = threading.Event()
def exceptHook(args):
global deathBell
logger.error("Exception in thread caught: {}".format(args))
deathBell.set()
logger.error("rang the death bell")
2022-02-04 22:28:36 +01:00
def terminateHook(sig, frame):
global deathBell
logger.error("SIGINT received")
deathBell.set()
logger.error("rang the death bell")
2022-02-04 19:46:15 +01:00
logger.info("opcua2mqtt bridge starting")
2022-02-04 23:08:53 +01:00
parser = argparse.ArgumentParser(description="opcua2mqtt")
2022-02-04 19:46:15 +01:00
parser.add_argument('--config', '-f',
help='Config file, default is $pwd/config.json',
required=False,
default='./config.json')
args = parser.parse_args()
2022-02-04 20:25:32 +01:00
with open(args.config) as f:
config = json.load(f)
2022-02-04 19:46:15 +01:00
queue = queue.Queue()
publishThread = MqttPublish(config, queue)
publishThread.start()
logger.info("MqttPublish started")
2022-02-04 20:25:32 +01:00
opcuaThreads = []
for o in config['opcua']:
2022-02-04 22:30:48 +01:00
if o['enabled'] != 'true':
continue
2022-02-04 20:25:32 +01:00
ot = OpcUaRequester(o, queue)
ot.start()
logger.info(f"OpcUaRequester thread for {o['name']} started")
opcuaThreads.append(ot)
2022-02-04 19:46:15 +01:00
threading.excepthook = exceptHook
logger.info("Threading excepthook set")
2022-02-04 22:28:36 +01:00
signal.signal(signal.SIGINT, terminateHook)
logger.info("SIGINT handler set")
2022-02-04 19:46:15 +01:00
logger.info("opcua2mqtt bridge is running")
deathBell.wait()
logger.error("opcua2mqtt bridge is dying")
publishThread.stop()
logger.error("publishThread stopped")
2022-02-04 20:25:32 +01:00
for ot in opcuaThreads:
ot.stop()
logger.error(f"opcua thread {ot.name} stopped")
2022-02-04 19:46:15 +01:00
publishThread.join()
logger.error("publishThread joined")
2022-02-04 20:25:32 +01:00
for ot in opcuaThreads:
ot.join()
logger.error(f"opcua thread {ot.name} joined")
2022-02-04 19:46:15 +01:00
logger.error("opcua2mqtt bridge is terminated")