Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
samson0v committed Sep 18, 2023
1 parent 6628db3 commit 52855ba
Show file tree
Hide file tree
Showing 20 changed files with 54 additions and 42 deletions.
2 changes: 1 addition & 1 deletion thingsboard_gateway/connectors/bacnet/bacnet_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion thingsboard_gateway/connectors/ble/ble_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion thingsboard_gateway/connectors/can/can_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion thingsboard_gateway/connectors/ftp/ftp_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions thingsboard_gateway/connectors/modbus/modbus_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion thingsboard_gateway/connectors/mqtt/mqtt_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion thingsboard_gateway/connectors/ocpp/ocpp_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion thingsboard_gateway/connectors/odbc/odbc_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion thingsboard_gateway/connectors/opcua/opcua_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion thingsboard_gateway/connectors/rest/rest_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion thingsboard_gateway/connectors/socket/socket_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion thingsboard_gateway/connectors/xmpp/xmpp_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions thingsboard_gateway/gateway/statistics_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import subprocess
from threading import Thread
from time import time, sleep
from platform import system as platform_system

import simplejson

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions thingsboard_gateway/gateway/tb_gateway_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '
Expand Down Expand Up @@ -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)

Expand Down
33 changes: 17 additions & 16 deletions thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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')
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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...')
Expand All @@ -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')
Expand All @@ -293,20 +294,20 @@ 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...')
LOG.exception(e)
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')
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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})
Expand Down
2 changes: 1 addition & 1 deletion thingsboard_gateway/tb_utility/tb_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!!!
"""
Expand Down

0 comments on commit 52855ba

Please sign in to comment.