Skip to content

Commit

Permalink
Fixed TBClient reconnecting
Browse files Browse the repository at this point in the history
  • Loading branch information
samson0v committed Mar 15, 2024
1 parent 9474876 commit 89d8e60
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
9 changes: 8 additions & 1 deletion thingsboard_gateway/gateway/tb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ def connect(self, min_reconnect_delay=10):
self.__stopped = False
self.__min_reconnect_delay = min_reconnect_delay

def run(self):
keep_alive = self.__config.get("keep_alive", 120)
try:
while not self.client.is_connected() and not self.__stopped:
Expand All @@ -274,6 +273,7 @@ def run(self):
self.__logger.exception(e)
sleep(10)

def run(self):
while not self.__stopped:
try:
if not self.__stopped:
Expand All @@ -290,3 +290,10 @@ def get_config_folder_path(self):

def register_service_subscription_callback(self, subscribe_to_required_topics):
self.__service_subscription_callbacks.append(subscribe_to_required_topics)

@property
def config(self):
return self.__config

def is_stopped(self):
return self.__stopped
4 changes: 2 additions & 2 deletions thingsboard_gateway/gateway/tb_gateway_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,10 @@ def _watchers(self):
while not self.stopped:
cur_time = time() * 1000

if not self.tb_client.is_connected() and self.__subscribed_to_rpc_topics:
if not self.tb_client.client.is_connected() and self.__subscribed_to_rpc_topics:
self.__subscribed_to_rpc_topics = False

if self.tb_client.is_connected() and not self.__subscribed_to_rpc_topics:
if self.tb_client.is_connected() and not self.tb_client.is_stopped() and not self.__subscribed_to_rpc_topics:
for device in list(self.__saved_devices.keys()):
self.add_device(device,
{CONNECTOR_PARAMETER: self.__saved_devices[device][CONNECTOR_PARAMETER]},
Expand Down
44 changes: 29 additions & 15 deletions thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,26 +576,40 @@ def _handle_remote_logging_level_update(self, config):

# HANDLERS SUPPORT METHODS -----------------------------------------------------------------------------------------
def _apply_connection_config(self, config) -> bool:
apply_start = time() * 1000
old_tb_client = self._gateway.tb_client
old_tb_client_config_path = self._gateway.tb_client.get_config_folder_path()
old_tb_client_config = self._gateway.tb_client.config
connection_logger = getLogger('tb_connection')
try:
old_tb_client.disconnect()
self._gateway.tb_client.disconnect()
self._gateway.tb_client.stop()

connection_logger = getLogger('tb_connection')
new_tb_client = TBClient(config, old_tb_client.get_config_folder_path(), connection_logger)
while self._gateway.tb_client.client.is_connected():
sleep(1)

apply_start = time()

connection_state = False
use_new_config = True
while not connection_state:
for client in (new_tb_client, old_tb_client):
client.connect()
while time() * 1000 - apply_start >= 1000 and not connection_state:
connection_state = client.is_connected()
sleep(.1)

if connection_state:
self._gateway.tb_client = client
self._gateway.subscribe_to_required_topics()
return True
self._gateway.__subscribed_to_rpc_topics = False
new_tb_client = TBClient(config if use_new_config else old_tb_client_config, old_tb_client_config_path, connection_logger)
new_tb_client.connect()
while time() - apply_start <= 30 and not connection_state:
connection_state = new_tb_client.is_connected()
sleep(.1)

if connection_state:
self._gateway.tb_client = new_tb_client
self._gateway.__subscribed_to_rpc_topics = False
self._gateway.subscribe_to_required_topics()
return True
else:
new_tb_client.disconnect()
new_tb_client.stop()
while new_tb_client.client.is_connected():
sleep(1)
apply_start = time() * 1000
use_new_config = not use_new_config
except Exception as e:
LOG.exception(e)
self._revert_connection()
Expand Down

0 comments on commit 89d8e60

Please sign in to comment.