1
0
mirror of https://github.com/thingsboard/thingsboard-gateway synced 2025-10-26 22:31:42 +08:00

Improvements for applying remote configuration

This commit is contained in:
zbeacon
2020-01-27 13:54:07 +02:00
parent d76b7976e5
commit f7fbbc805d
3 changed files with 32 additions and 18 deletions

View File

@@ -68,14 +68,17 @@ class TBClient(threading.Thread):
def _on_connect(self, client, userdata, flags, rc, *extra_params):
log.debug('TB client %s connected to ThingsBoard', str(client))
self.__is_connected = True
self.client._on_connect(client, userdata, flags, rc, *extra_params)
def _on_disconnect(self, client, userdata, rc):
log.info("TB client %s has been disconnected.", str(client))
self.unsubscribe('*')
self.__is_connected = False
self.client._on_disconnect(client, userdata, rc)
def stop(self):
self.disconnect()
# self.disconnect()
self.__stopped = True
def disconnect(self):
@@ -95,8 +98,10 @@ class TBClient(threading.Thread):
keep_alive = self.__config.get("keep_alive", 60)
try:
while not self.client.is_connected() and not self.__stopped:
log.debug("connecting to ThingsBoard")
if not self.__paused:
if self.__stopped:
break
log.debug("connecting to ThingsBoard")
try:
self.client.connect(tls=self.__tls,
ca_certs=self.__ca_cert,

View File

@@ -86,10 +86,10 @@ class RemoteConfigurator:
def __process_connectors_configuration(self):
log.debug("Processing remote connectors configuration...")
self.__apply_storage_configuration()
if self.__apply_new_connectors_configuration():
self.__write_new_configuration_files()
if self.__safe_apply_connection_configuration():
self.__apply_storage_configuration()
log.info("Remote configuration has been applied.")
with open(self.__gateway._config_dir + "tb_gateway.yaml", "w") as general_configuration_file:
safe_dump(self.__new_general_configuration_file, general_configuration_file)
@@ -168,19 +168,20 @@ class RemoteConfigurator:
apply_start = time() * 1000
self.__old_tb_client = self.__gateway.tb_client
try:
self.__old_tb_client.pause()
self.__old_tb_client.unsubscribe('*')
self.__old_tb_client.stop()
self.__old_tb_client.disconnect()
except Exception as e:
log.exception(e)
self.__revert_configuration()
return False
try:
tb_client = TBClient(self.__new_general_configuration_file["thingsboard"])
tb_client.connect()
self.__gateway.tb_client = TBClient(self.__new_general_configuration_file["thingsboard"])
self.__gateway.tb_client.connect()
except Exception as e:
log.exception(e)
self.__revert_configuration()
return False
self.__gateway.tb_client = tb_client
try:
connection_state = False
while time() * 1000 - apply_start < self.__apply_timeout * 1000 and not connection_state:
@@ -191,7 +192,6 @@ class RemoteConfigurator:
log.info("The gateway cannot connect to the ThingsBoard server with a new configuration.")
return False
else:
self.__old_tb_client.unsubscribe("*")
self.__old_tb_client.stop()
self.__gateway.tb_client.client.gw_set_server_side_rpc_request_handler(
self.__gateway._rpc_request_handler)
@@ -218,13 +218,21 @@ class RemoteConfigurator:
self.__gateway._event_storage = self.__old_event_storage
def __revert_configuration(self):
try:
log.info("Remote general configuration will be restored.")
self.__new_general_configuration_file = self.__old_general_configuration_file
self.__gateway.tb_client.disconnect()
self.__gateway.tb_client.stop()
self.__gateway.tb_client = self.__old_tb_client
self.__gateway.tb_client = TBClient(self.__old_general_configuration_file["thingsboard"])
self.__gateway.tb_client.connect()
self.__gateway.tb_client.unpause()
self.__gateway.tb_client.client.gw_set_server_side_rpc_request_handler(self.__gateway._rpc_request_handler)
self.__gateway.tb_client.client.set_server_side_rpc_request_handler(self.__gateway._rpc_request_handler)
self.__gateway.tb_client.client.subscribe_to_all_attributes(self.__gateway._attribute_update_callback)
self.__gateway.tb_client.client.gw_subscribe_to_all_attributes(self.__gateway._attribute_update_callback)
log.debug("%s connection has been restored", str(self.__gateway.tb_client.client._client))
except Exception as e:
log.exception("Exception on reverting configuration occurred:")
log.exception(e)
def __get_current_logs_configuration(self):
try:
@@ -237,8 +245,8 @@ class RemoteConfigurator:
def __update_logs_configuration(self):
try:
logs_conf_file_path = self.__gateway._config_dir + 'logs.conf'
with open(logs_conf_file_path, 'w') as logs:
logs.write(self.__new_logs_configuration+"\r\n")
# with open(logs_conf_file_path, 'w') as logs:
# logs.write(self.__new_logs_configuration+"\r\n")
fileConfig(logs_conf_file_path)
self.__gateway.main_handler = MemoryHandler(-1)
self.__gateway.remote_handler = TBLoggerHandler(self.__gateway)

View File

@@ -18,7 +18,7 @@ import logging.handlers
class TBLoggerHandler(logging.Handler):
def __init__(self, gateway):
self.current_log_level = 'DEBUG'
self.current_log_level = 'NOTSET'
super().__init__(logging.getLevelName(self.current_log_level))
self.__gateway = gateway
self.activated = False
@@ -55,4 +55,5 @@ class TBLoggerHandler(logging.Handler):
self.__gateway.send_to_storage(self.__gateway.name, {"deviceName": self.__gateway.name, "telemetry": [{'LOGS': record}]})
def deactivate(self):
self.current_log_level = 'NOTSET'
self.activated = False