From 04b48f75a302775774e355f72ce4b4ee0dac654a Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Fri, 4 Feb 2022 23:03:11 +0100 Subject: [PATCH] publish json data --- opcua2mqtt/DataObject.py | 10 ++++++++-- opcua2mqtt/OpcUaRequester.py | 3 ++- opcua2mqtt/config.json | 4 +++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/opcua2mqtt/DataObject.py b/opcua2mqtt/DataObject.py index 0653afd..b7628f2 100644 --- a/opcua2mqtt/DataObject.py +++ b/opcua2mqtt/DataObject.py @@ -1,5 +1,5 @@ import re - +import json class InvalidDataObjectException(Exception): def __init__(self, message): @@ -21,4 +21,10 @@ class DataObject(object): return self.serverName + '/' + str(self.nameSpaceIndex) + '/' + self.variableName def getPayload(self): - return self.value \ No newline at end of file + payload = { + "serverName": self.serverName, + "nameSpaceIndex": self.nameSpaceIndex, + "variableName": self.variableName, + "value": self.value + } + return json.dumps(payload) \ No newline at end of file diff --git a/opcua2mqtt/OpcUaRequester.py b/opcua2mqtt/OpcUaRequester.py index 27ec40e..d333652 100644 --- a/opcua2mqtt/OpcUaRequester.py +++ b/opcua2mqtt/OpcUaRequester.py @@ -16,6 +16,7 @@ class OpcUaRequester(threading.Thread): self.url = self.config['url'] self.nodes = self.config['nodes'] self.delay = self.config['delay'] + self.timeout = self.config['timeout'] # consider this flag in the localLoop self.killBill = False @@ -25,7 +26,7 @@ class OpcUaRequester(threading.Thread): async def opcUaRequesterInnerLoop(self): while not self.killBill: try: - async with Client(url=self.url, timeout=10.0) as client: + async with Client(url=self.url, timeout=self.timeout) as client: for nodeSpec in self.nodes: try: logger.debug(f"Trying {self.name} {self.url} ns={nodeSpec['ns']};{nodeSpec['n']}") diff --git a/opcua2mqtt/config.json b/opcua2mqtt/config.json index 46eb227..8b93b71 100644 --- a/opcua2mqtt/config.json +++ b/opcua2mqtt/config.json @@ -10,11 +10,12 @@ "url": "opc.tcp://172.16.3.60:4840", "name": "apl", "delay": 1.0, + "timeout": 1.0, "nodes": [ { "ns": 0, "n": "i=345", "d": "pv" }, { "ns": 0, "n": "i=348", "d": "sv" }, { "ns": 0, "n": "i=351", "d": "tv" }, - { "ns": 0, "n": "i=35400", "d": "qv" } + { "ns": 0, "n": "i=354", "d": "qv" } ] }, { @@ -22,6 +23,7 @@ "url": "opc.tcp://192.168.254.5:4863", "name": "sh", "delay": 1.0, + "timeout": 10.0, "nodes": [ { "ns": 1, "n": "s=A201CD124/MOT_01.AV_Out#Value", "d": "A201CD124" }, { "ns": 1, "n": "s=A201CJ003/PID_01.PV_Out#Value", "d": "A201CJ003" },