From 53483d239b5c78ef7e92a250526b3407be964958 Mon Sep 17 00:00:00 2001 From: samson0v Date: Fri, 23 Feb 2024 10:42:23 +0200 Subject: [PATCH] Fixed reading two same nodes for OPC-UA connector --- .../connectors/opcua/opcua_connector.py | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/thingsboard_gateway/connectors/opcua/opcua_connector.py b/thingsboard_gateway/connectors/opcua/opcua_connector.py index 3099d3e38..8968d9059 100644 --- a/thingsboard_gateway/connectors/opcua/opcua_connector.py +++ b/thingsboard_gateway/connectors/opcua/opcua_connector.py @@ -419,6 +419,7 @@ def __search_nodes_and_subscribe(self, device_info): config_path = TBUtility.get_value(information["path"], get_tag=True) information_path = self._check_path(config_path, device_info["deviceNode"]) information["path"] = '${%s}' % information_path + information_key = information['key'] information_nodes = [] self.__search_node(device_info["deviceNode"], information_path, result=information_nodes) if len(information_nodes) == 0: @@ -448,18 +449,17 @@ def __search_nodes_and_subscribe(self, device_info): else: converter = device_info["uplink_converter"] - self.subscribed[information_node] = {"converter": converter, - "information": information, - "information_type": information_type} + self.subscribed[information_key] = {"converter": converter, + "information_node": information_node, + "information": information, + "information_type": information_type} # Use Node name if param "key" not found in config if not information.get('key'): information['key'] = information_node.get_browse_name().Name - self.subscribed[information_node]['key'] = information['key'] + self.subscribed[information_key]['key'] = information['key'] changed_key = True - information_key = information['key'] - self._log.debug("Node for %s \"%s\" with path: %s - FOUND! Current values is: %s", information_type, information_key, @@ -749,14 +749,18 @@ def __init__(self, connector: OpcUaConnector): def datachange_notification(self, node, val, data): try: - self.connector._log.debug("Python: New data change event on node %s, with val: %s and data %s", node, val, str(data)) - subscription = self.connector.subscribed[node] - converted_data = subscription["converter"].convert( - (subscription["information"], subscription["information_type"]), val, data, key=subscription.get('key')) - self.connector.statistics['MessagesReceived'] = self.connector.statistics['MessagesReceived'] + 1 - self.connector.data_to_send.append(converted_data) - self.connector.statistics['MessagesSent'] = self.connector.statistics['MessagesSent'] + 1 - self.connector._log.debug("[SUBSCRIPTION] Data to ThingsBoard: %s", converted_data) + self.connector._log.debug("Python: New data change event on node %s, with val: %s and data %s", node, val, + str(data)) + subscriptions = list( + filter(lambda node_info: node_info["information_node"] == node, self.connector.subscribed.values())) + for subscription in subscriptions: + converted_data = subscription["converter"].convert( + (subscription["information"], subscription["information_type"]), val, data, + key=subscription.get('key')) + self.connector.statistics['MessagesReceived'] = self.connector.statistics['MessagesReceived'] + 1 + self.connector.data_to_send.append(converted_data) + self.connector.statistics['MessagesSent'] = self.connector.statistics['MessagesSent'] + 1 + self.connector._log.debug("[SUBSCRIPTION] Data to ThingsBoard: %s", converted_data) except KeyError: self.connector.scan_nodes_from_config() except Exception as e: