From 4451eddea26d963de3adab7559572d7c7c3ba2b1 Mon Sep 17 00:00:00 2001 From: samson0v Date: Wed, 25 Sep 2024 14:02:26 +0300 Subject: [PATCH] Fixed opcua subscription mode --- .../connectors/opcua/opcua_connector.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/thingsboard_gateway/connectors/opcua/opcua_connector.py b/thingsboard_gateway/connectors/opcua/opcua_connector.py index 21c55a5b..e0bd1545 100644 --- a/thingsboard_gateway/connectors/opcua/opcua_connector.py +++ b/thingsboard_gateway/connectors/opcua/opcua_connector.py @@ -174,7 +174,7 @@ async def __disconnect(self): self.__log.warning('%s could not be disconnected from OPC-UA Server: %s', self.name, e) async def __unsubscribe_from_node(self, device: Device, node): - node['valid'] = False + node.pop('valid', None) if node.get('node') is not None and self.__enable_subscriptions: subscription_id = device.nodes_data_change_subscriptions.get(node['node'].nodeid, {}).get('subscription') if subscription_id is not None: @@ -318,6 +318,7 @@ async def disconnect_if_connected(self): if self.__enable_subscriptions: for device in self.__device_nodes: device.subscription = None + device.nodes_data_change_subscriptions = {} await self.__unsubscribe_from_nodes() await self.__client.disconnect() except Exception: @@ -610,10 +611,6 @@ async def _load_devices_nodes(self): if node.get('valid') is None or (node.get('valid') and not self.__enable_subscriptions): if self.__enable_subscriptions and not subscription_exists and not self.__stopped: - if device.subscription is None: - device.subscription = await self.__client.create_subscription( - self.__sub_check_period_in_millis, SubHandler( - self.__sub_data_to_convert, self.__log)) if found_node.nodeid not in device.nodes_data_change_subscriptions: if found_node.nodeid not in self.__nodes_config_cache: self.__nodes_config_cache[found_node.nodeid] = [] @@ -622,6 +619,12 @@ async def _load_devices_nodes(self): "subscription": None, "key": node['key'], "section": section} + + if device.subscription is None: + device.subscription = await self.__client.create_subscription( + self.__sub_check_period_in_millis, SubHandler( + self.__sub_data_to_convert, self.__log)) + node['id'] = found_node.nodeid.to_string() node['valid'] = True @@ -665,10 +668,6 @@ async def _load_devices_nodes(self): subs.extend(subs_batch) for node, conf, subscription_id in zip(nodes_to_subscribe, conf, subs): device.nodes_data_change_subscriptions[node.nodeid]['subscription'] = subscription_id - else: - self.__log.error('Failed to subscribe on data change; client was disconnected!') - raise ConnectionError('Client was disconnected!') - except Exception as e: self.__log.exception("Error loading nodes: %s", e) raise e