Skip to content

Commit

Permalink
Remote configuration improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
imbeacon committed Jan 20, 2020
1 parent 6581394 commit 7913a1e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
21 changes: 14 additions & 7 deletions thingsboard_gateway/gateway/tb_gateway_remote_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def send_current_configuration(self):
current_configuration["thingsboard"] = self.__old_general_configuration_file
encoded_current_configuration = b64encode(dumps(current_configuration).encode())
self.__gateway.tb_client.client.send_attributes(
{"current_configuration": encoded_current_configuration.decode("UTF-8")})
{"current_configuration": encoded_current_configuration.decode("UTF-8")}).get()

def __process_connectors_configuration(self):
log.debug("Processing remote connectors configuration...")
Expand All @@ -87,6 +87,7 @@ def __process_connectors_configuration(self):
def __prepare_connectors_configuration(self):
self.__new_connectors_configs = {}
try:
self.__gateway._load_connectors(self.__new_configuration["thingsboard"])
for connector_type in {connector_type for connector_type in self.__new_configuration if
"thingsboard" not in connector_type}:
connector_number = 0
Expand All @@ -107,7 +108,10 @@ def __apply_new_connectors_configuration(self):
try:
self.__gateway._connectors_configs = self.__new_connectors_configs
for connector_name in self.__gateway.available_connectors:
self.__gateway.available_connectors[connector_name].close()
try:
self.__gateway.available_connectors[connector_name].close()
except Exception as e:
log.exception(e)
self.__gateway._connect_with_connectors()
log.debug("New connectors configuration has been applied")
self.__old_connectors_configs = {}
Expand Down Expand Up @@ -141,11 +145,14 @@ def __write_new_configuration_files(self):
# "configuration": connector_file
# }
# )
with open(self.__gateway._config_dir + 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,
connector_file)
connector_name = connector_config.get("name")
for conn in self.__new_general_configuration_file["connectors"]:

with open(self.__gateway._config_dir + conn.get("configuration"), "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,
connector_file)
for old_connector_type in self.__old_connectors_configs:
for old_connector_config_section in self.__old_connectors_configs[old_connector_type]:
for old_connector_file in old_connector_config_section:
Expand Down
19 changes: 11 additions & 8 deletions thingsboard_gateway/gateway/tb_gateway_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def _attributes_parse(self, content, *args):
self.__remote_configurator.process_configuration(content.get("configuration"))
except Exception as e:
log.exception(e)
remote_logging_level = shared_attributes.get('RemoteLoggingLevel', content.get("RemoteLoggingLevel"))
remote_logging_level = shared_attributes.get('RemoteLoggingLevel') if shared_attributes is not None else content.get("RemoteLoggingLevel")
if remote_logging_level == 'NONE':
self.remote_handler.deactivate()
log.info('Remote logging has being deactivated.')
Expand Down Expand Up @@ -224,16 +224,19 @@ def _connect_with_connectors(self):
for connector_type in self._connectors_configs:
for connector_config in self._connectors_configs[connector_type]:
for config_file in connector_config:
connector = None
try:
connector = self._implemented_connectors[connector_type](self, connector_config[config_file],
connector_type)
self.available_connectors[connector.get_name()] = connector
connector.open()
connector = None
try:
connector = self._implemented_connectors[connector_type](self, connector_config[config_file],
connector_type)
self.available_connectors[connector.get_name()] = connector
connector.open()
except Exception as e:
log.exception(e)
if connector is not None:
connector.close()
except Exception as e:
log.exception(e)
if connector is not None:
connector.close()

def __send_statistic(self):
self.tb_client.client.gw_send_telemetry()
Expand Down

0 comments on commit 7913a1e

Please sign in to comment.