done so far

This commit is contained in:
Wolfgang Hottgenroth
2022-02-04 20:25:32 +01:00
parent 41c31249cf
commit 51bc396c39
2 changed files with 31 additions and 18 deletions

View File

@ -6,27 +6,33 @@ from DataObject import DataObject
class OpcUaRequester(threading.Thread): class OpcUaRequester(threading.Thread):
def __init__(self, config, name, queue): def __init__(self, config, queue):
super().__init__() super().__init__()
self.config = config["opcua"][name] self.config = config
self.queue = queue self.queue = queue
self.name = self.config['name']
self.url = self.config['url']
self.nodes = self.config['nodes']
self.delay = self.config['delay']
# consider this flag in the localLoop # consider this flag in the localLoop
self.killBill = False self.killBill = False
self.killEvent = threading.Event() self.killEvent = threading.Event()
async def opcUaRequesterInnerLoop(self): async def opcUaRequesterInnerLoop(self):
while True: while not self.killBill:
async with Client(url=URL, timeout=10.0) as client: async with Client(url=self.url, timeout=10.0) as client:
for nodeSpec in NODE_SPECS: for nodeSpec in self.nodes:
logger.debug(f"Trying {self.name} {self.url} ns={nodeSpec['ns']};{nodeSpec['n']}")
node = client.get_node(f"ns={nodeSpec['ns']};{nodeSpec['n']}") node = client.get_node(f"ns={nodeSpec['ns']};{nodeSpec['n']}")
value = await node.read_value() value = await node.read_value()
displayName = nodeSpec['d'] if ('d' in nodeSpec) else (await node.read_display_name()).Text displayName = nodeSpec['d'] if ('d' in nodeSpec) else (await node.read_display_name()).Text
print(f"{displayName=} = {value=}") logger.debug(f"Got: {displayName=} = {value=}")
self.queue.put(DataObject(SERVER, nodeSpec['ns'], displayName, value)) self.queue.put(DataObject(self.name, nodeSpec['ns'], displayName, value))
await asyncio.sleep(DELAY) await asyncio.sleep(self.delay)
def run(self): def run(self):
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()

View File

@ -2,7 +2,7 @@ from MqttPublish import MqttPublish
from OpcUaRequester import OpcUaRequester from OpcUaRequester import OpcUaRequester
from loguru import logger from loguru import logger
import argparse import argparse
import configparser import json
import threading import threading
import queue import queue
@ -24,8 +24,8 @@ parser.add_argument('--config', '-f',
default='./config.json') default='./config.json')
args = parser.parse_args() args = parser.parse_args()
config = configparser.ConfigParser() with open(args.config) as f:
config.read(args.config) config = json.load(f)
queue = queue.Queue() queue = queue.Queue()
@ -34,9 +34,12 @@ publishThread = MqttPublish(config, queue)
publishThread.start() publishThread.start()
logger.info("MqttPublish started") logger.info("MqttPublish started")
opcuaThread = OpcUaRequester(config, queue) opcuaThreads = []
opcuaThread.start() for o in config['opcua']:
logger.info("OpcUaRequest started") ot = OpcUaRequester(o, queue)
ot.start()
logger.info(f"OpcUaRequester thread for {o['name']} started")
opcuaThreads.append(ot)
threading.excepthook = exceptHook threading.excepthook = exceptHook
logger.info("Threading excepthook set") logger.info("Threading excepthook set")
@ -49,12 +52,16 @@ logger.error("opcua2mqtt bridge is dying")
publishThread.stop() publishThread.stop()
logger.error("publishThread stopped") logger.error("publishThread stopped")
opcuaThread.stop()
logger.error("opcuaThread stopped") for ot in opcuaThreads:
ot.stop()
logger.error(f"opcua thread {ot.name} stopped")
publishThread.join() publishThread.join()
logger.error("publishThread joined") logger.error("publishThread joined")
opcuaThread.join()
logger.error("opcuaThread joined") for ot in opcuaThreads:
ot.join()
logger.error(f"opcua thread {ot.name} joined")
logger.error("opcua2mqtt bridge is terminated") logger.error("opcua2mqtt bridge is terminated")