68 lines
1.5 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
deathBell = threading.Event()
def exceptHook(args):
global deathBell
logger.error("Exception in thread caught: {}".format(args))
deathBell.set()
logger.error("rang the death bell")
logger.info("opcua2mqtt bridge starting")
parser = argparse.ArgumentParser(description="example1")
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']:
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")
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")