diff --git a/thingsboard_gateway/connectors/bacnet/bacnet_connector.py b/thingsboard_gateway/connectors/bacnet/bacnet_connector.py index 19df68838..9a437eb07 100644 --- a/thingsboard_gateway/connectors/bacnet/bacnet_connector.py +++ b/thingsboard_gateway/connectors/bacnet/bacnet_connector.py @@ -109,7 +109,7 @@ def run(self): def close(self): self.__stopped = True self.__connected = False - self._log.__del__() + self._log.reset() stop() def get_name(self): diff --git a/thingsboard_gateway/connectors/ble/ble_connector.py b/thingsboard_gateway/connectors/ble/ble_connector.py index 3cd57024b..2009e1231 100644 --- a/thingsboard_gateway/connectors/ble/ble_connector.py +++ b/thingsboard_gateway/connectors/ble/ble_connector.py @@ -97,7 +97,7 @@ def run(self): def close(self): self.__stopped = True self.__log.info('%s has been stopped.', self.get_name()) - self.__log.__del__() + self.__log.reset() def get_name(self): return self.name diff --git a/thingsboard_gateway/connectors/can/can_connector.py b/thingsboard_gateway/connectors/can/can_connector.py index fb86ba845..050011891 100644 --- a/thingsboard_gateway/connectors/can/can_connector.py +++ b/thingsboard_gateway/connectors/can/can_connector.py @@ -99,7 +99,7 @@ def close(self): if not self.__stopped: self.__stopped = True self._log.debug("[%s] Stopping", self.get_name()) - self._log.__del__() + self._log.reset() def get_name(self): return self.name diff --git a/thingsboard_gateway/connectors/ftp/ftp_connector.py b/thingsboard_gateway/connectors/ftp/ftp_connector.py index 3141ef7da..016dbc1c6 100644 --- a/thingsboard_gateway/connectors/ftp/ftp_connector.py +++ b/thingsboard_gateway/connectors/ftp/ftp_connector.py @@ -192,7 +192,7 @@ def __send_data(self, converted_data): def close(self): self.__stopped = True - self.__log.__del__() + self.__log.reset() def get_name(self): return self.name diff --git a/thingsboard_gateway/connectors/modbus/backward_compability_adapter.py b/thingsboard_gateway/connectors/modbus/backward_compability_adapter.py index 07b2e0625..d5d4981b4 100644 --- a/thingsboard_gateway/connectors/modbus/backward_compability_adapter.py +++ b/thingsboard_gateway/connectors/modbus/backward_compability_adapter.py @@ -20,8 +20,12 @@ class BackwardCompatibilityAdapter: config_files_count = 1 CONFIG_PATH = None - def __init__(self, config, config_dir): - self._log = logging.getLogger('BackwardCompatibilityAdapter') + def __init__(self, config, config_dir, logger=None): + if logger: + self._log = logger + else: + self._log = logging.getLogger('BackwardCompatibilityAdapter') + self.__config = config self.__config_dir = config_dir BackwardCompatibilityAdapter.CONFIG_PATH = self.__config_dir diff --git a/thingsboard_gateway/connectors/modbus/modbus_connector.py b/thingsboard_gateway/connectors/modbus/modbus_connector.py index 59464781b..6a82c9361 100644 --- a/thingsboard_gateway/connectors/modbus/modbus_connector.py +++ b/thingsboard_gateway/connectors/modbus/modbus_connector.py @@ -105,11 +105,13 @@ def __init__(self, gateway, config, connector_type): super().__init__() self.__gateway = gateway self._connector_type = connector_type - self.__backward_compatibility_adapter = BackwardCompatibilityAdapter(config, gateway.get_config_path()) + self.__log = init_logger(self.__gateway, config.get('name', self.name), + config.get('logLevel', 'INFO')) + self.__backward_compatibility_adapter = BackwardCompatibilityAdapter(config, gateway.get_config_path(), + logger=self.__log) self.__config = self.__backward_compatibility_adapter.convert() self.setName(self.__config.get("name", 'Modbus Default ' + ''.join(choice(ascii_lowercase) for _ in range(5)))) - self.__log = init_logger(self.__gateway, self.__config.get('name', self.name), - self.__config.get('logLevel', 'INFO')) + self.__connected = False self.__stopped = False self.daemon = True @@ -293,7 +295,7 @@ def close(self): if reactor.running: ServerStop() self.__log.info('%s has been stopped.', self.get_name()) - self.__log.__del__() + self.__log.reset() def get_name(self): return self.name diff --git a/thingsboard_gateway/connectors/mqtt/mqtt_connector.py b/thingsboard_gateway/connectors/mqtt/mqtt_connector.py index 7b63ceadf..b60a0cf15 100644 --- a/thingsboard_gateway/connectors/mqtt/mqtt_connector.py +++ b/thingsboard_gateway/connectors/mqtt/mqtt_connector.py @@ -319,7 +319,7 @@ def close(self): self.__log.exception(e) self._client.loop_stop() self.__log.info('%s has been stopped.', self.get_name()) - self.__log.__del__() + self.__log.reset() def get_name(self): return self.name diff --git a/thingsboard_gateway/connectors/ocpp/ocpp_connector.py b/thingsboard_gateway/connectors/ocpp/ocpp_connector.py index 27b39429e..48eed137d 100644 --- a/thingsboard_gateway/connectors/ocpp/ocpp_connector.py +++ b/thingsboard_gateway/connectors/ocpp/ocpp_connector.py @@ -212,7 +212,7 @@ def close(self): sleep(.2) self._log.info('%s has been stopped.', self.get_name()) - self._log.__del__() + self._log.reset() async def _close_cp_connections(self): for cp in self._connected_charge_points: diff --git a/thingsboard_gateway/connectors/odbc/odbc_connector.py b/thingsboard_gateway/connectors/odbc/odbc_connector.py index 8dd4be78a..e22a85f79 100644 --- a/thingsboard_gateway/connectors/odbc/odbc_connector.py +++ b/thingsboard_gateway/connectors/odbc/odbc_connector.py @@ -92,7 +92,7 @@ def close(self): if not self.__stopped: self.__stopped = True self._log.debug("[%s] Stopping", self.get_name()) - self._log.__del__() + self._log.reset() def get_name(self): return self.name diff --git a/thingsboard_gateway/connectors/opcua/opcua_connector.py b/thingsboard_gateway/connectors/opcua/opcua_connector.py index 2d077baca..5f15b4c58 100644 --- a/thingsboard_gateway/connectors/opcua/opcua_connector.py +++ b/thingsboard_gateway/connectors/opcua/opcua_connector.py @@ -237,7 +237,7 @@ def close(self): pass self.__connected = False self._log.info('%s has been stopped.', self.get_name()) - self._log.__del__() + self._log.reset() def get_name(self): return self.name diff --git a/thingsboard_gateway/connectors/opcua_asyncio/opcua_connector.py b/thingsboard_gateway/connectors/opcua_asyncio/opcua_connector.py index b057f5e57..d826794f4 100644 --- a/thingsboard_gateway/connectors/opcua_asyncio/opcua_connector.py +++ b/thingsboard_gateway/connectors/opcua_asyncio/opcua_connector.py @@ -97,7 +97,7 @@ def close(self): self.__stopped = True self.__connected = False self.__log.info('%s has been stopped.', self.get_name()) - self.__log.__del__() + self.__log.reset() async def __reset_node(self, node): node['valid'] = False diff --git a/thingsboard_gateway/connectors/request/request_connector.py b/thingsboard_gateway/connectors/request/request_connector.py index 45fc9e414..0e30d155b 100644 --- a/thingsboard_gateway/connectors/request/request_connector.py +++ b/thingsboard_gateway/connectors/request/request_connector.py @@ -314,7 +314,7 @@ def open(self): def close(self): self.__stopped = True - self._log.__del__() + self._log.reset() def get_config(self): return self.__config diff --git a/thingsboard_gateway/connectors/rest/rest_connector.py b/thingsboard_gateway/connectors/rest/rest_connector.py index a0d8e6d50..b6d88e20c 100644 --- a/thingsboard_gateway/connectors/rest/rest_connector.py +++ b/thingsboard_gateway/connectors/rest/rest_connector.py @@ -175,7 +175,7 @@ def run(self): def close(self): self.__stopped = True self._connected = False - self.__log.__del__() + self.__log.reset() def get_name(self): return self.name diff --git a/thingsboard_gateway/connectors/socket/socket_connector.py b/thingsboard_gateway/connectors/socket/socket_connector.py index 3155e3cb4..5da9a6d8a 100644 --- a/thingsboard_gateway/connectors/socket/socket_connector.py +++ b/thingsboard_gateway/connectors/socket/socket_connector.py @@ -287,7 +287,7 @@ def close(self): self.__stopped = True self._connected = False self.__connections = {} - self.__log.__del__() + self.__log.reset() def get_name(self): return self.name diff --git a/thingsboard_gateway/connectors/xmpp/xmpp_connector.py b/thingsboard_gateway/connectors/xmpp/xmpp_connector.py index 68507b14a..b3824e69f 100644 --- a/thingsboard_gateway/connectors/xmpp/xmpp_connector.py +++ b/thingsboard_gateway/connectors/xmpp/xmpp_connector.py @@ -192,7 +192,7 @@ def close(self): self.__stopped = True self._connected = False self.__log.info('%s has been stopped.', self.get_name()) - self.__log.__del__() + self.__log.reset() def get_name(self): return self.name diff --git a/thingsboard_gateway/extensions/serial/custom_serial_connector.py b/thingsboard_gateway/extensions/serial/custom_serial_connector.py index 5b79b7668..ea0a21fa0 100644 --- a/thingsboard_gateway/extensions/serial/custom_serial_connector.py +++ b/thingsboard_gateway/extensions/serial/custom_serial_connector.py @@ -158,7 +158,7 @@ def close(self): # Close connect function, usually used if exception handled in self.__gateway.del_device(self.__devices[device]["device_config"]["name"]) if self.__devices[device]['serial'].isOpen(): self.__devices[device]['serial'].close() - self._log.__del__() + self._log.reset() def on_attributes_update(self, content): # Function used for processing attribute update requests from ThingsBoard self._log.debug(content) diff --git a/thingsboard_gateway/gateway/statistics_service.py b/thingsboard_gateway/gateway/statistics_service.py index ceb51c061..f157be77e 100644 --- a/thingsboard_gateway/gateway/statistics_service.py +++ b/thingsboard_gateway/gateway/statistics_service.py @@ -2,6 +2,7 @@ import subprocess from threading import Thread from time import time, sleep +from platform import system as platform_system import simplejson @@ -61,9 +62,14 @@ def run(self) -> None: data_to_send = {} for attribute in self._config: try: - process = subprocess.run(['/bin/sh', '-c', attribute['command']], stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - encoding='utf-8', timeout=attribute['timeout']) + if platform_system() == 'Windows': + process = subprocess.run(attribute['command'], stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding='utf-8', timeout=attribute['timeout']) + else: + process = subprocess.run(['/bin/sh', '-c', attribute['command']], stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding='utf-8', timeout=attribute['timeout']) except Exception as e: self._log.warning("Statistic parameter %s raise the exception: %s", attribute['attributeOnGateway'], e) diff --git a/thingsboard_gateway/gateway/tb_gateway_service.py b/thingsboard_gateway/gateway/tb_gateway_service.py index 3cbf76e96..ec7312a37 100644 --- a/thingsboard_gateway/gateway/tb_gateway_service.py +++ b/thingsboard_gateway/gateway/tb_gateway_service.py @@ -335,7 +335,7 @@ def __load_general_config(config_file): except Exception as e: log.exception('Failed to load configuration file:\n %s', e) else: - log.warning('YAML configuration will be deprecated in the future version. ' + log.warning('YAML configuration is deprecated. ' 'Please, use JSON configuration instead.') log.warning( 'See default configuration on ' @@ -631,7 +631,6 @@ def __process_remote_configuration(self, new_configuration): if new_configuration is not None and self.__remote_configurator is not None: try: self.__remote_configurator.process_config_request(new_configuration) - # self.__remote_configurator.send_current_configuration() except Exception as e: log.exception(e) diff --git a/thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py b/thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py index 2fb2cc9c2..aff55ff16 100644 --- a/thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py +++ b/thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py @@ -88,10 +88,10 @@ def connectors_configuration(self): def _get_active_connectors(self): return [connector['name'] for connector in self.connectors_configuration] - def _get_tb_gateway_general_config_for_save(self): + def _get_general_config_in_local_format(self): """ - Method used only for saving data to conf files - !!!Don't use it for sending data to TB (use `_get_tb_gateway_general_config_for_remote` instead)!!! + Method returns general configuration in format that should be used only for local files + !!!Don't use it for sending data to TB (use `_get_general_config_in_remote_format` instead)!!! """ connectors_config = [ @@ -105,10 +105,10 @@ def _get_tb_gateway_general_config_for_save(self): 'connectors': connectors_config } - def _get_tb_gateway_general_config_for_remote(self): + def _get_general_config_in_remote_format(self): """ - Method used only for saving data to TB - !!!Don't use it for saving data to conf files (use `_get_tb_gateway_general_config_for_save`)!!! + Method returns general configuration in format that should be used only for sending configuration to the server. + !!!Don't use it for saving data to conf files (use `_get_general_config_in_local_format`)!!! """ stat_conf_path = self.general_configuration['statistics'].get('configuration') @@ -137,7 +137,7 @@ def send_current_configuration(self): LOG.debug('Sending all configurations (init)') self._gateway.tb_client.client.send_attributes( - {'general_configuration': self._get_tb_gateway_general_config_for_remote()}) + {'general_configuration': self._get_general_config_in_remote_format()}) self._gateway.tb_client.client.send_attributes({'storage_configuration': self.storage_configuration}) self._gateway.tb_client.client.send_attributes({'grpc_configuration': self.grpc_configuration}) self._gateway.tb_client.client.send_attributes( @@ -168,6 +168,7 @@ def _load_logs_configuration(self): return load(logs) except Exception as e: LOG.exception(e) + return {} def process_config_request(self, config): if not self.in_process: @@ -265,7 +266,7 @@ def _handle_general_configuration_update(self, config): self._cleanup() with open(self._gateway.get_config_path() + "tb_gateway.json", "w", encoding="UTF-8") as file: - file.writelines(dumps(self._get_tb_gateway_general_config_for_save(), indent=' ')) + file.writelines(dumps(self._get_general_config_in_local_format(), indent=' ')) def _handle_storage_configuration_update(self, config): LOG.debug('Processing storage configuration update...') @@ -281,7 +282,7 @@ def _handle_storage_configuration_update(self, config): else: self.storage_configuration = config with open(self._gateway.get_config_path() + "tb_gateway.json", "w", encoding="UTF-8") as file: - file.writelines(dumps(self._get_tb_gateway_general_config_for_save(), indent=' ')) + file.writelines(dumps(self._get_general_config_in_local_format(), indent=' ')) self._gateway.tb_client.client.send_attributes({'storage_configuration': self.storage_configuration}) LOG.info('Processed storage configuration update successfully') @@ -293,7 +294,7 @@ def _handle_grpc_configuration_update(self, config): self._gateway.init_grpc_service(config) for connector_name in self._gateway.available_connectors: self._gateway.available_connectors[connector_name].close() - self._gateway.load_connectors(self._get_tb_gateway_general_config_for_save()) + self._gateway.load_connectors(self._get_general_config_in_local_format()) self._gateway.connect_with_connectors() except Exception as e: LOG.error('Something went wrong with applying the new GRPC configuration. Reverting...') @@ -301,12 +302,12 @@ def _handle_grpc_configuration_update(self, config): self._gateway.init_grpc_service(self.grpc_configuration) for connector_name in self._gateway.available_connectors: self._gateway.available_connectors[connector_name].close() - self._gateway.load_connectors(self._get_tb_gateway_general_config_for_save()) + self._gateway.load_connectors(self._get_general_config_in_local_format()) self._gateway.connect_with_connectors() else: self.grpc_configuration = config with open(self._gateway.get_config_path() + "tb_gateway.json", "w", encoding="UTF-8") as file: - file.writelines(dumps(self._get_tb_gateway_general_config_for_save(), indent=' ')) + file.writelines(dumps(self._get_general_config_in_local_format(), indent=' ')) self._gateway.tb_client.client.send_attributes({'grpc_configuration': self.grpc_configuration}) LOG.info('Processed GRPC configuration update successfully') @@ -354,7 +355,7 @@ def _handle_active_connectors_update(self, config): self._delete_connectors_from_config(config) with open(self._gateway.get_config_path() + 'tb_gateway.json', 'w') as file: - file.writelines(dumps(self._get_tb_gateway_general_config_for_save(), indent=' ')) + file.writelines(dumps(self._get_general_config_in_local_format(), indent=' ')) self._active_connectors = config self._gateway.tb_client.client.send_attributes({'active_connectors': config}) @@ -396,9 +397,9 @@ def _handle_connector_configuration_update(self, config): self.connectors_configuration.append(connector_configuration) with open(self._gateway.get_config_path() + 'tb_gateway.json', 'w') as file: - file.writelines(dumps(self._get_tb_gateway_general_config_for_save(), indent=' ')) + file.writelines(dumps(self._get_general_config_in_local_format(), indent=' ')) - self._gateway.load_connectors(self._get_tb_gateway_general_config_for_save()) + self._gateway.load_connectors(self._get_general_config_in_local_format()) self._gateway.connect_with_connectors() else: found_connector = found_connectors[0] @@ -439,7 +440,7 @@ def _handle_connector_configuration_update(self, config): connector_configuration = found_connector self._gateway.available_connectors[connector_configuration['name']].close() - self._gateway.load_connectors(self._get_tb_gateway_general_config_for_save()) + self._gateway.load_connectors(self._get_general_config_in_local_format()) self._gateway.connect_with_connectors() self._gateway.tb_client.client.send_attributes({config['name']: config}) diff --git a/thingsboard_gateway/tb_utility/tb_logger.py b/thingsboard_gateway/tb_utility/tb_logger.py index 58f6f9f9f..4fb2e56ad 100644 --- a/thingsboard_gateway/tb_utility/tb_logger.py +++ b/thingsboard_gateway/tb_utility/tb_logger.py @@ -56,7 +56,7 @@ def __init__(self, name, gateway=None, level=logging.NOTSET): self._errors_sender_thread = Thread(name='Log Errors Sender', daemon=True, target=self._send_errors) self._errors_sender_thread.start() - def __del__(self): + def reset(self): """ !!!Need to be called manually in the connector 'close' method!!! """