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):
def __init__(self, config, name, queue):
def __init__(self, config, queue):
super().__init__()
self.config = config["opcua"][name]
self.config = config
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
self.killBill = False
self.killEvent = threading.Event()
async def opcUaRequesterInnerLoop(self):
while True:
async with Client(url=URL, timeout=10.0) as client:
for nodeSpec in NODE_SPECS:
while not self.killBill:
async with Client(url=self.url, timeout=10.0) as client:
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']}")
value = await node.read_value()
displayName = nodeSpec['d'] if ('d' in nodeSpec) else (await node.read_display_name()).Text
print(f"{displayName=} = {value=}")
self.queue.put(DataObject(SERVER, nodeSpec['ns'], displayName, value))
await asyncio.sleep(DELAY)
logger.debug(f"Got: {displayName=} = {value=}")
self.queue.put(DataObject(self.name, nodeSpec['ns'], displayName, value))
await asyncio.sleep(self.delay)
def run(self):
loop = asyncio.new_event_loop()

View File

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