mirror of
				https://github.com/thingsboard/thingsboard-gateway
				synced 2025-10-26 22:31:42 +08:00 
			
		
		
		
	Improvements for remote logging and fix for #236 (You can put only caCert intosecurity section of MQTT connector config)
This commit is contained in:
		
							parent
							
								
									aa1b8758cb
								
							
						
					
					
						commit
						7938457cb8
					
				|  | @ -29,6 +29,7 @@ class MqttConnector(Connector, Thread): | |||
|     def __init__(self, gateway, config, connector_type): | ||||
|         super().__init__() | ||||
|         self.__log = log | ||||
|         self.config = config | ||||
|         self.__connector_type = connector_type | ||||
|         self.statistics = {'MessagesReceived': 0, | ||||
|                            'MessagesSent': 0} | ||||
|  | @ -42,17 +43,17 @@ class MqttConnector(Connector, Thread): | |||
|         self.__sub_topics = {} | ||||
|         client_id = ''.join(random.choice(string.ascii_lowercase) for _ in range(23)) | ||||
|         self._client = Client(client_id) | ||||
|         self.setName(self.__broker.get("name", | ||||
|                                        'Mqtt Broker ' + ''.join(random.choice(string.ascii_lowercase) for _ in range(5)))) | ||||
|         if self.__broker["security"]["type"] == "basic": | ||||
|         self.setName(config.get("name", self.__broker.get("name", | ||||
|                                        'Mqtt Broker ' + ''.join(random.choice(string.ascii_lowercase) for _ in range(5))))) | ||||
|         if "username" in self.__broker["security"]: | ||||
|             self._client.username_pw_set(self.__broker["security"]["username"], | ||||
|                                          self.__broker["security"]["password"]) | ||||
|         elif self.__broker["security"]["type"] == "cert.PEM": | ||||
|         if "caCert" in self.__broker["security"]: | ||||
|             ca_cert = self.__broker["security"].get("caCert") | ||||
|             private_key = self.__broker["security"].get("privateKey") | ||||
|             cert = self.__broker["security"].get("cert") | ||||
|             if ca_cert is None or cert is None: | ||||
|                 self.__log.error("caCert and cert parameters must be in config if you need to use the SSL. Please add it and try again.") | ||||
|             if ca_cert is None: | ||||
|                 self.__log.error("caCert parameter must be in config if you need to use the SSL. Please add it and try again.") | ||||
|             else: | ||||
|                 try: | ||||
|                     self._client.tls_set(ca_certs=ca_cert, | ||||
|  |  | |||
|  | @ -60,7 +60,8 @@ class RemoteConfigurator: | |||
|                     current_configuration[connector].append(config_file[config]) | ||||
|         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")}) | ||||
|         self.__gateway.tb_client.client.send_attributes( | ||||
|             {"current_configuration": encoded_current_configuration.decode("UTF-8")}) | ||||
| 
 | ||||
|     def __process_connectors_configuration(self): | ||||
|         log.debug("Processing remote connectors configuration...") | ||||
|  | @ -70,7 +71,7 @@ class RemoteConfigurator: | |||
|             self.__write_new_configuration_files() | ||||
|         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: | ||||
|             with open(self.__gateway._config_dir + "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 = {} | ||||
|  | @ -86,13 +87,16 @@ class RemoteConfigurator: | |||
|     def __prepare_connectors_configuration(self): | ||||
|         self.__new_connectors_configs = {} | ||||
|         try: | ||||
|             for connector_type in {connector_type for connector_type in self.__new_configuration if "thingsboard" not in connector_type}: | ||||
|             for connector_type in {connector_type for connector_type in self.__new_configuration if | ||||
|                                    "thingsboard" not in connector_type}: | ||||
|                 connector_number = 0 | ||||
|                 for connector in self.__new_configuration[connector_type]: | ||||
|                     log.debug("Processing remote configuration for connector with type \"%s\" and config \"%s\".", connector_type, dumps(connector)) | ||||
|                     log.debug("Processing remote configuration for connector with type \"%s\" and config \"%s\".", | ||||
|                               connector_type, dumps(connector)) | ||||
|                     if not self.__new_connectors_configs.get(connector_type): | ||||
|                         self.__new_connectors_configs[connector_type] = [] | ||||
|                     self.__new_connectors_configs[connector_type].append({connector_type.lower().replace(" ", "")+str(connector_number)+".json": connector}) | ||||
|                     self.__new_connectors_configs[connector_type].append( | ||||
|                         {connector_type.lower().replace(" ", "") + str(connector_number) + ".json": connector}) | ||||
|                     connector_number += 1 | ||||
|             log.debug("Saved configuration for connectors: %s", ', '.join(con for con in self.__new_connectors_configs)) | ||||
| 
 | ||||
|  | @ -127,9 +131,9 @@ class RemoteConfigurator: | |||
|                 for connector_config_section in self.__new_connectors_configs[connector_type]: | ||||
|                     for connector_file in connector_config_section: | ||||
|                         connector_config = connector_config_section[connector_file] | ||||
|                         connector_name = connector_config["broker"]["name"] if connector_config.get("broker") else \ | ||||
|                                          connector_config["server"]["name"] if connector_config.get("server") else \ | ||||
|                                          connector_config["name"] if connector_config.get("name") else None | ||||
|                         connector_name = connector_config["name"] if connector_config.get("name") else \ | ||||
|                             connector_config["broker"]["name"] if connector_config.get("broker") else \ | ||||
|                             connector_config["server"]["name"] if connector_config.get("server") else None | ||||
|                         self.__new_general_configuration_file["connectors"].append( | ||||
|                             { | ||||
|                                 "name": connector_name, | ||||
|  | @ -140,18 +144,20 @@ class RemoteConfigurator: | |||
|                         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) | ||||
|                         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: | ||||
|                         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, old_connector_type) | ||||
|                         log.debug("Remove old configuration file \"%s\" for \"%s\" connector ", old_connector_file, | ||||
|                                   old_connector_type) | ||||
|         except Exception as e: | ||||
|             log.exception(e) | ||||
| 
 | ||||
|     def __safe_apply_connection_configuration(self): | ||||
|         apply_start = time()*1000 | ||||
|         apply_start = time() * 1000 | ||||
|         self.__old_tb_client = self.__gateway.tb_client | ||||
|         try: | ||||
|             self.__old_tb_client.pause() | ||||
|  | @ -170,7 +176,7 @@ class RemoteConfigurator: | |||
|         self.__gateway.tb_client = tb_client | ||||
|         try: | ||||
|             connection_state = False | ||||
|             while time()*1000-apply_start < self.__apply_timeout*1000 and not connection_state: | ||||
|             while time() * 1000 - apply_start < self.__apply_timeout * 1000 and not connection_state: | ||||
|                 connection_state = self.__gateway.tb_client.is_connected() | ||||
|                 sleep(.1) | ||||
|             if not connection_state: | ||||
|  | @ -179,10 +185,12 @@ class RemoteConfigurator: | |||
|                 return False | ||||
|             else: | ||||
|                 self.__old_tb_client.stop() | ||||
|                 self.__gateway.tb_client.client.gw_set_server_side_rpc_request_handler(self.__gateway._rpc_request_handler) | ||||
|                 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) | ||||
|                 self.__gateway.tb_client.client.gw_subscribe_to_all_attributes( | ||||
|                     self.__gateway._attribute_update_callback) | ||||
|                 return True | ||||
|         except Exception as e: | ||||
|             log.exception(e) | ||||
|  | @ -193,13 +201,14 @@ 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"]) | ||||
|                 self.__gateway._event_storage = self.__gateway._event_storage_types[ | ||||
|                     self.__new_general_configuration_file["storage"]["type"]]( | ||||
|                     self.__new_general_configuration_file["storage"]) | ||||
|                 self.__old_event_storage = None | ||||
|             except Exception as e: | ||||
|                 log.exception(e) | ||||
|                 self.__gateway._event_storage = self.__old_event_storage | ||||
| 
 | ||||
| 
 | ||||
|     def __revert_configuration(self): | ||||
|         log.info("Remote general configuration will be restored.") | ||||
|         self.__new_general_configuration_file = self.__old_general_configuration_file | ||||
|  | @ -208,5 +217,3 @@ class RemoteConfigurator: | |||
|         self.__gateway.tb_client = self.__old_tb_client | ||||
|         self.__gateway.tb_client.connect() | ||||
|         self.__gateway.tb_client.unpause() | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -32,6 +32,7 @@ from thingsboard_gateway.storage.file_event_storage import FileEventStorage | |||
| from thingsboard_gateway.gateway.tb_gateway_remote_configurator import RemoteConfigurator | ||||
| 
 | ||||
| log = logging.getLogger('tb_gateway.service') | ||||
| main_handler = logging.handlers.MemoryHandler(-1) | ||||
| 
 | ||||
| 
 | ||||
| class TBGatewayService: | ||||
|  | @ -42,8 +43,8 @@ class TBGatewayService: | |||
|             config = safe_load(config) | ||||
|             self._config_dir = path.dirname(path.abspath(config_file)) + '/' | ||||
|             logging.config.fileConfig(self._config_dir + "logs.conf") | ||||
|             global log | ||||
|             log = logging.getLogger('tb_gateway.service') | ||||
|             # global log | ||||
|             # log = logging.getLogger('tb_gateway.service') | ||||
|             self.available_connectors = {} | ||||
|             self.__connector_incoming_messages = {} | ||||
|             self.__connected_devices = {} | ||||
|  | @ -58,7 +59,8 @@ class TBGatewayService: | |||
|             self.tb_client.client.set_server_side_rpc_request_handler(self._rpc_request_handler) | ||||
|             self.tb_client.client.subscribe_to_all_attributes(self._attribute_update_callback) | ||||
|             self.tb_client.client.gw_subscribe_to_all_attributes(self._attribute_update_callback) | ||||
|             self.main_handler = logging.handlers.MemoryHandler(-1) | ||||
|             global main_handler | ||||
|             self.main_handler = main_handler | ||||
|             self.remote_handler = TBLoggerHandler(self) | ||||
|             self.main_handler.setTarget(self.remote_handler) | ||||
|             self._default_connectors = { | ||||
|  |  | |||
|  | @ -31,11 +31,8 @@ class TBLoggerHandler(logging.Handler): | |||
|                         ] | ||||
|         for logger in self.loggers: | ||||
|             log = logging.getLogger(logger) | ||||
|             log.setLevel(self.__current_log_level) | ||||
|             log.addHandler(self.__gateway.main_handler) | ||||
|             log.debug("Added remote handler to log %s", logger) | ||||
|         logging.getLogger("tb_gateway.tb_connection").addHandler(self.__gateway.main_handler) | ||||
|         logging.getLogger("tb_gateway.tb_connection").setLevel(logging.INFO) | ||||
| 
 | ||||
|     def activate(self, log_level=None): | ||||
|         try: | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 zbeacon
						zbeacon