Skip to content

Commit

Permalink
Fix for disconnecting device after connector stopped
Browse files Browse the repository at this point in the history
  • Loading branch information
imbeacon committed Feb 6, 2025
1 parent 05c2e91 commit db93fca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 8 additions & 2 deletions thingsboard_gateway/gateway/tb_gateway_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,9 @@ def __close_connectors(self):
close_start = monotonic()
while not self.available_connectors_by_id[current_connector].is_stopped():
self.available_connectors_by_id[current_connector].close()
if self.tb_client.is_connected():
for device in self.get_connector_devices(self.available_connectors_by_id[current_connector]):
self.del_device(device)
if monotonic() - close_start > 5:
log.error("Connector %s close timeout", current_connector)
break
Expand Down Expand Up @@ -987,11 +990,11 @@ def __update_connector_devices(self, connector):
self.update_device(device_name, 'connector', connector)

def clean_shared_attributes_cache_for_connector_devices(self, connector):
connector_devices = self.__get_connector_devices(connector)
connector_devices = self.get_connector_devices(connector)
for device_name in connector_devices:
self.__devices_shared_attributes.pop(device_name, None)

def __get_connector_devices(self, connector):
def get_connector_devices(self, connector):
return [device_name for device_name, device in self.__connected_devices.items() if device.get('connector') and device['connector'].get_id() == connector.get_id()]

def __cleanup_connectors(self):
Expand Down Expand Up @@ -1055,6 +1058,9 @@ def __connect_with_connectors(self):
log.debug("Error on loading connector %r", connector_name, exc_info=e)
if connector is not None and not connector.is_stopped():
connector.close()
if self.tb_client.is_connected():
for device in self.get_connector_devices(connector):
self.del_device(device)
else:
self.__grpc_connectors.update({connector_config['grpc_key']: connector_config})
if connector_type != 'grpc':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from tb_gateway_mqtt import TBGatewayMqttClient

from thingsboard_gateway.gateway.constants import CONFIG_VERSION_PARAMETER, REPORT_STRATEGY_PARAMETER, \
DEFAULT_REPORT_STRATEGY_CONFIG
DEFAULT_REPORT_STRATEGY_CONFIG, CONNECTOR_PARAMETER
from thingsboard_gateway.gateway.entities.report_strategy_config import ReportStrategyConfig
from thingsboard_gateway.gateway.tb_client import TBClient

Expand Down Expand Up @@ -675,6 +675,9 @@ def _handle_connector_configuration_update(self, config):
close_start = monotonic()
while not retrieved_connector.is_stopped(): # noqa
retrieved_connector.close()
if self._gateway.tb_client.is_connected():
for device in self._gateway.get_connector_devices(retrieved_connector):
self.del_device(device)
self._gateway.clean_shared_attributes_cache_for_connector_devices(retrieved_connector)
if monotonic() - close_start > 5:
self.__log.error('Connector %s not stopped in 5 seconds', connector_configuration['id']) # noqa
Expand All @@ -687,6 +690,9 @@ def _handle_connector_configuration_update(self, config):
close_start = monotonic()
while not retrieved_connector.is_stopped():
retrieved_connector.close()
if self._gateway.tb_client.is_connected():
for device in self._gateway.get_connector_devices(retrieved_connector):
self.del_device(device)
self._gateway.clean_shared_attributes_cache_for_connector_devices(retrieved_connector)
if monotonic() - close_start > 5:
self.__log.error('Connector %s not stopped in 5 seconds',
Expand Down

0 comments on commit db93fca

Please sign in to comment.