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

Added remote configuration for logs.conf file

This commit is contained in:
zbeacon
2020-01-24 12:36:55 +02:00
parent 68fea12bbb
commit d5f9eb1536
2 changed files with 42 additions and 18 deletions

View File

@@ -17,6 +17,8 @@ from simplejson import dumps, loads, dump
from yaml import safe_dump
from time import time, sleep
from logging import getLogger
from logging.config import fileConfig
from logging.handlers import MemoryHandler
from os import remove
from thingsboard_gateway.gateway.tb_client import TBClient
from thingsboard_gateway.gateway.tb_logger import TBLoggerHandler
@@ -31,6 +33,8 @@ class RemoteConfigurator:
self.__old_configuration = None
self.__apply_timeout = 10
self.__old_tb_client = None
self.__old_logs_configuration = self.__get_current_logs_configuration()
self.__new_logs_configuration = None
self.__old_connectors_configs = {}
self.__new_connectors_configs = {}
self.__old_general_configuration_file = config
@@ -47,6 +51,12 @@ class RemoteConfigurator:
self.__new_configuration = loads(decoded_configuration)
self.__old_connectors_configs = self.__gateway._connectors_configs
self.__new_general_configuration_file = self.__new_configuration.get("thingsboard")
self.__new_logs_configuration = b64decode(self.__new_general_configuration_file.pop("logs")).decode('UTF-8')
# if self.__old_logs_configuration == self.__new_logs_configuration:
# log.debug("Received logs configuration is the same.")
# else:
# log.debug("Received logs configuration is not the same. Updating loggers...")
self.__update_logs_configuration()
if self.__old_configuration != decoded_configuration:
log.info("Remote configuration received: \n %s", decoded_configuration)
self.__process_connectors_configuration()
@@ -198,3 +208,25 @@ class RemoteConfigurator:
self.__gateway.tb_client = self.__old_tb_client
self.__gateway.tb_client.connect()
self.__gateway.tb_client.unpause()
def __get_current_logs_configuration(self):
try:
with open(self.__gateway._config_dir + 'logs.conf', 'r') as logs:
current_logs_configuration = logs.read()
return current_logs_configuration
except Exception as e:
log.exception(e)
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)
fileConfig(logs_conf_file_path)
self.__gateway.main_handler = MemoryHandler(-1)
self.__gateway.remote_handler = TBLoggerHandler(self.__gateway)
self.__gateway.main_handler.setTarget(self.__gateway.remote_handler)
log.debug("Logs configuration has been updated.")
except Exception as e:
log.exception(e)

View File

@@ -158,25 +158,14 @@ class TBGatewayService:
if content is not None:
shared_attributes = content.get("shared")
client_attributes = content.get("client")
if shared_attributes is not None:
if self.__remote_configurator is not None and shared_attributes.get("configuration"):
try:
self.__remote_configurator.process_configuration(shared_attributes.get("configuration"))
self.__send_thread = Thread(target=self.__read_data_from_storage, daemon=True,
name="Send data to Thingsboard Thread")
self.__send_thread.start()
except Exception as e:
log.exception(e)
if client_attributes is not None:
log.debug("Client attributes received (%s).", ", ".join([attr for attr in client_attributes.keys()]))
if self.__remote_configurator is not None and content.get("configuration"):
new_configuration = shared_attributes.get("configuration") if shared_attributes is not None and shared_attributes.get("configuration") is not None else content.get("configuration")
if new_configuration is not None:
try:
self.__remote_configurator.process_configuration(content.get("configuration"))
self.__remote_configurator.process_configuration(new_configuration)
self.__send_thread = Thread(target=self.__read_data_from_storage, daemon=True,
name="Send data to Thingsboard Thread")
self.__send_thread.start()
self.__remote_configurator.send_current_configuration()
except Exception as e:
log.exception(e)
remote_logging_level = shared_attributes.get('RemoteLoggingLevel') if shared_attributes is not None else content.get("RemoteLoggingLevel")
@@ -186,8 +175,11 @@ class TBGatewayService:
elif remote_logging_level is not None:
if self.remote_handler.current_log_level != remote_logging_level or not self.remote_handler.activated:
self.remote_handler.activate(remote_logging_level)
if not self.remote_handler.activated:
log.info('Remote logging has being activated.')
log.info('Remote logging has being updated. Current logging level is: %s ', remote_logging_level)
if shared_attributes is not None:
log.debug("Shared attributes received (%s).", ", ".join([attr for attr in shared_attributes.keys()]))
if client_attributes is not None:
log.debug("Client attributes received (%s).", ", ".join([attr for attr in client_attributes.keys()]))
except Exception as e:
log.exception(e)
@@ -357,7 +349,7 @@ class TBGatewayService:
time.sleep(.01)
else:
time.sleep(.1)
self.__request_config_after_connect = False
# self.__request_config_after_connect = False
except Exception as e:
log.exception(e)
time.sleep(1)