mirror of
https://github.com/thingsboard/thingsboard-gateway
synced 2025-10-26 22:31:42 +08:00
Refactoring
This commit is contained in:
@@ -25,7 +25,7 @@ from thingsboard_gateway.gateway.tb_client import TBClient
|
||||
from thingsboard_gateway.gateway.tb_logger import TBLoggerHandler
|
||||
from thingsboard_gateway.tb_utility.tb_utility import TBUtility
|
||||
|
||||
log = getLogger("service")
|
||||
LOG = getLogger("service")
|
||||
|
||||
|
||||
class RemoteConfigurator:
|
||||
@@ -50,7 +50,7 @@ class RemoteConfigurator:
|
||||
if not self.in_process:
|
||||
self.in_process = True
|
||||
# while not self.__gateway._published_events.empty():
|
||||
# log.debug("Waiting for end of the data processing...")
|
||||
# LOG.debug("Waiting for end of the data processing...")
|
||||
# sleep(1)
|
||||
decoded_configuration = b64decode(configuration)
|
||||
self.__new_configuration = loads(decoded_configuration)
|
||||
@@ -58,7 +58,7 @@ class RemoteConfigurator:
|
||||
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').replace('}}', '\n')
|
||||
if self.__old_configuration != decoded_configuration:
|
||||
log.info("Remote configuration received: \n %s", decoded_configuration)
|
||||
LOG.info("Remote configuration received: \n %s", decoded_configuration)
|
||||
result = self.__process_connectors_configuration()
|
||||
self.in_process = False
|
||||
if result:
|
||||
@@ -67,13 +67,13 @@ class RemoteConfigurator:
|
||||
else:
|
||||
return False
|
||||
else:
|
||||
log.info("Remote configuration is the same.")
|
||||
LOG.info("Remote configuration is the same.")
|
||||
else:
|
||||
log.error("Remote configuration is already in processing")
|
||||
LOG.error("Remote configuration is already in processing")
|
||||
return False
|
||||
except Exception as e:
|
||||
self.in_process = False
|
||||
log.exception(e)
|
||||
LOG.exception(e)
|
||||
|
||||
def send_current_configuration(self):
|
||||
try:
|
||||
@@ -91,18 +91,18 @@ class RemoteConfigurator:
|
||||
self.__old_configuration = encoded_current_configuration
|
||||
self.__gateway.tb_client.client.send_attributes(
|
||||
{"current_configuration": encoded_current_configuration.decode("UTF-8")})
|
||||
log.debug('Current configuration has been sent to ThingsBoard: %s', json_current_configuration)
|
||||
LOG.debug('Current configuration has been sent to ThingsBoard: %s', json_current_configuration)
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
LOG.exception(e)
|
||||
|
||||
def __process_connectors_configuration(self):
|
||||
log.info("Processing remote connectors configuration...")
|
||||
LOG.info("Processing remote connectors configuration...")
|
||||
if self.__apply_new_connectors_configuration():
|
||||
self.__write_new_configuration_files()
|
||||
self.__apply_storage_configuration()
|
||||
if self.__safe_apply_connection_configuration():
|
||||
log.info("Remote configuration has been applied.")
|
||||
with open(self.__gateway._config_dir + "tb_gateway.yaml", "w") as general_configuration_file:
|
||||
LOG.info("Remote configuration has been applied.")
|
||||
with open(self.__gateway.get_config_path() + "tb_gateway.yaml", "w") as general_configuration_file:
|
||||
safe_dump(self.__new_general_configuration_file, general_configuration_file)
|
||||
self.__old_connectors_configs = {}
|
||||
self.__new_connectors_configs = {}
|
||||
@@ -115,9 +115,9 @@ class RemoteConfigurator:
|
||||
else:
|
||||
self.__update_logs_configuration()
|
||||
self.__old_general_configuration_file.pop("logs")
|
||||
with open(self.__gateway._config_dir + "tb_gateway.yaml", "w") as general_configuration_file:
|
||||
with open(self.__gateway.get_config_path() + "tb_gateway.yaml", "w") as general_configuration_file:
|
||||
safe_dump(self.__old_general_configuration_file, general_configuration_file)
|
||||
log.error("A remote general configuration applying has been failed.")
|
||||
LOG.error("A remote general configuration applying has been failed.")
|
||||
self.__old_connectors_configs = {}
|
||||
self.__new_connectors_configs = {}
|
||||
self.__new_logs_configuration = None
|
||||
@@ -137,7 +137,7 @@ class RemoteConfigurator:
|
||||
connector_class = TBUtility.check_and_import(connector["type"], self.__gateway._default_connectors.get(connector["type"], connector.get("class")))
|
||||
self.__gateway._implemented_connectors[connector["type"]] = connector_class
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
LOG.exception(e)
|
||||
|
||||
def __apply_new_connectors_configuration(self):
|
||||
try:
|
||||
@@ -146,9 +146,9 @@ class RemoteConfigurator:
|
||||
try:
|
||||
self.__gateway.available_connectors[connector_name].close()
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
LOG.exception(e)
|
||||
self.__gateway._connect_with_connectors()
|
||||
log.debug("New connectors configuration has been applied")
|
||||
LOG.debug("New connectors configuration has been applied")
|
||||
self.__old_connectors_configs = {}
|
||||
return True
|
||||
except Exception as e:
|
||||
@@ -157,7 +157,7 @@ class RemoteConfigurator:
|
||||
self.__gateway.available_connectors[connector_name].close()
|
||||
self.__gateway._load_connectors(self.__old_general_configuration_file)
|
||||
self.__gateway._connect_with_connectors()
|
||||
log.exception(e)
|
||||
LOG.exception(e)
|
||||
return False
|
||||
|
||||
def __write_new_configuration_files(self):
|
||||
@@ -168,10 +168,10 @@ class RemoteConfigurator:
|
||||
for connector_config_section in self.__new_connectors_configs[connector_type]:
|
||||
for connector_file in connector_config_section["config"]:
|
||||
connector_config = connector_config_section["config"][connector_file]
|
||||
with open(self.__gateway._config_dir + connector_file, "w") as config_file:
|
||||
with open(self.__gateway.get_config_path() + connector_file, "w") as config_file:
|
||||
dump(connector_config, config_file, sort_keys=True, indent=2)
|
||||
new_connectors_files.append(connector_file)
|
||||
log.debug("Saving new configuration for \"%s\" connector to file \"%s\"", connector_type,
|
||||
LOG.debug("Saving new configuration for \"%s\" connector to file \"%s\"", connector_type,
|
||||
connector_file)
|
||||
break
|
||||
self.__old_general_configuration_file["connectors"] = self.__new_general_configuration_file["connectors"]
|
||||
@@ -179,11 +179,11 @@ class RemoteConfigurator:
|
||||
for old_connector_config_section in self.__old_connectors_configs[old_connector_type]:
|
||||
for old_connector_file in old_connector_config_section["config"]:
|
||||
if old_connector_file not in new_connectors_files:
|
||||
remove(self.__gateway._config_dir + old_connector_file)
|
||||
log.debug("Remove old configuration file \"%s\" for \"%s\" connector ", old_connector_file,
|
||||
remove(self.__gateway.get_config_path() + old_connector_file)
|
||||
LOG.debug("Remove old configuration file \"%s\" for \"%s\" connector ", old_connector_file,
|
||||
old_connector_type)
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
LOG.exception(e)
|
||||
|
||||
def __safe_apply_connection_configuration(self):
|
||||
apply_start = time() * 1000
|
||||
@@ -200,14 +200,14 @@ class RemoteConfigurator:
|
||||
sleep(.1)
|
||||
if not connection_state:
|
||||
self.__revert_configuration()
|
||||
log.info("The gateway cannot connect to the ThingsBoard server with a new configuration.")
|
||||
LOG.info("The gateway cannot connect to the ThingsBoard server with a new configuration.")
|
||||
return False
|
||||
else:
|
||||
self.__old_tb_client.stop()
|
||||
self.__gateway.subscribe_to_required_topics()
|
||||
return True
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
LOG.exception(e)
|
||||
self.__revert_configuration()
|
||||
return False
|
||||
|
||||
@@ -215,56 +215,50 @@ class RemoteConfigurator:
|
||||
if self.__old_general_configuration_file["storage"] != self.__new_general_configuration_file["storage"]:
|
||||
self.__old_event_storage = self.__gateway._event_storage
|
||||
try:
|
||||
self.__gateway._event_storage = self.__gateway._event_storage_types[
|
||||
self.__new_general_configuration_file["storage"]["type"]](
|
||||
self.__new_general_configuration_file["storage"])
|
||||
storage_class = self.__gateway._event_storage_types[self.__new_general_configuration_file["storage"]["type"]]
|
||||
self.__gateway._event_storage = storage_class(self.__new_general_configuration_file["storage"])
|
||||
self.__old_event_storage = None
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
LOG.exception(e)
|
||||
self.__gateway._event_storage = self.__old_event_storage
|
||||
|
||||
def __revert_configuration(self):
|
||||
try:
|
||||
log.info("Remote general configuration will be restored.")
|
||||
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 = TBClient(self.__old_general_configuration_file["thingsboard"])
|
||||
self.__gateway.tb_client.connect()
|
||||
self.__gateway.subscribe_to_required_topics()
|
||||
log.debug("%s connection has been restored", str(self.__gateway.tb_client.client._client))
|
||||
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)
|
||||
LOG.exception("Exception on reverting configuration occurred:")
|
||||
LOG.exception(e)
|
||||
|
||||
def __get_current_logs_configuration(self):
|
||||
try:
|
||||
with open(self.__gateway._config_dir + 'logs.conf', 'r') as logs:
|
||||
with open(self.__gateway.get_config_path() + 'logs.conf', 'r') as logs:
|
||||
current_logs_configuration = logs.read()
|
||||
return current_logs_configuration
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
LOG.exception(e)
|
||||
|
||||
def __update_logs_configuration(self):
|
||||
global LOG
|
||||
try:
|
||||
global log
|
||||
log = getLogger('service')
|
||||
remote_handler_current_state = self.__gateway.remote_handler.activated
|
||||
remote_handler_current_level = self.__gateway.remote_handler.current_log_level
|
||||
logs_conf_file_path = self.__gateway._config_dir + 'logs.conf'
|
||||
LOG = getLogger('service')
|
||||
logs_conf_file_path = self.__gateway.get_config_path() + 'logs.conf'
|
||||
new_logging_level = findall(r'level=(.*)', self.__new_logs_configuration.replace("NONE", "NOTSET"))[-1]
|
||||
with open(logs_conf_file_path, 'w') as logs:
|
||||
logs.write(self.__new_logs_configuration.replace("NONE", "NOTSET")+"\r\n")
|
||||
# fileConfig(logs_conf_file_path)
|
||||
# self.__gateway.main_handler = MemoryHandler(-1)
|
||||
self.__gateway.main_handler.setLevel(new_logging_level)
|
||||
# self.__gateway.remote_handler = TBLoggerHandler(self.__gateway)
|
||||
self.__gateway.main_handler.setTarget(self.__gateway.remote_handler)
|
||||
if new_logging_level == "NOTSET":
|
||||
self.__gateway.remote_handler.deactivate()
|
||||
else:
|
||||
self.__gateway.remote_handler.activate(new_logging_level)
|
||||
log.debug("Logs configuration has been updated.")
|
||||
LOG.debug("Logs configuration has been updated.")
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
LOG.exception(e)
|
||||
|
||||
|
||||
@@ -251,7 +251,7 @@ class TBGatewayService:
|
||||
if not connector_name == self.name:
|
||||
if not TBUtility.validate_converted_data(data):
|
||||
log.error("Data from %s connector is invalid.", connector_name)
|
||||
return
|
||||
return None
|
||||
if data["deviceName"] not in self.get_devices():
|
||||
self.add_device(data["deviceName"],
|
||||
{"connector": self.available_connectors[connector_name]}, wait_for_publish=True, device_type=data["deviceType"])
|
||||
|
||||
Reference in New Issue
Block a user