From 36f821a5cd2808493c03b2ca74927b7b3aa3b922 Mon Sep 17 00:00:00 2001 From: samson0v Date: Tue, 25 Jan 2022 11:21:30 +0200 Subject: [PATCH 1/3] Fixed reconnection error for BLE Device --- thingsboard_gateway/connectors/ble/device.py | 27 +++++++------------- 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/thingsboard_gateway/connectors/ble/device.py b/thingsboard_gateway/connectors/ble/device.py index 31cc872c..0cadf9db 100644 --- a/thingsboard_gateway/connectors/ble/device.py +++ b/thingsboard_gateway/connectors/ble/device.py @@ -29,7 +29,7 @@ from platform import system from time import time, sleep import asyncio -from bleak import BleakClient, BleakError +from bleak import BleakClient from thingsboard_gateway.connectors.connector import log from thingsboard_gateway.tb_utility.tb_loader import TBModuleLoader @@ -74,7 +74,7 @@ class Device(Thread): 'serverSideRpc': config.get('serverSideRpc', []) } self.callback = config['callback'] - self.last_polled_time = 0 + self.last_polled_time = self.poll_period + 1 self.notifying_chars = [] @@ -104,9 +104,6 @@ class Device(Thread): self.stopped = True async def timer(self): - await self.__process_self() - self.last_polled_time = time() - while True: try: if time() - self.last_polled_time >= self.poll_period: @@ -114,8 +111,8 @@ class Device(Thread): await self.__process_self() else: await asyncio.sleep(.2) - except Exception as e: - log.exception(e) + except Exception: + log.error('Problem with connection') try: await self.client.disconnect() @@ -163,18 +160,12 @@ class Device(Thread): char_id = item['characteristicUUID'] if item['method'] == 'read': - try: - data = await self.client.read_gatt_char(char_id) - not_converted_data[section].append({'data': data, **item}) - except BleakError as e: - log.exception(e) + data = await self.client.read_gatt_char(char_id) + not_converted_data[section].append({'data': data, **item}) elif item['method'] == 'notify' and char_id not in self.notifying_chars: - try: - self.__set_char_handle(item, char_id) - self.notifying_chars.append(char_id) - await self.notify(char_id) - except BleakError as e: - log.error(e) + self.__set_char_handle(item, char_id) + self.notifying_chars.append(char_id) + await self.notify(char_id) if len(not_converted_data['telemetry']) > 0 or len(not_converted_data['attributes']) > 0: data_for_converter = { From 556e399a1cf037c23c4e0a33ccd9ca6668e0e51c Mon Sep 17 00:00:00 2001 From: samson0v Date: Tue, 25 Jan 2022 12:14:14 +0200 Subject: [PATCH 2/3] Fixed error handling for BLE connector --- thingsboard_gateway/connectors/ble/device.py | 30 +++++++++++++++---- .../connectors/ble/error_handler.py | 21 +++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 thingsboard_gateway/connectors/ble/error_handler.py diff --git a/thingsboard_gateway/connectors/ble/device.py b/thingsboard_gateway/connectors/ble/device.py index 0cadf9db..e5c44e0a 100644 --- a/thingsboard_gateway/connectors/ble/device.py +++ b/thingsboard_gateway/connectors/ble/device.py @@ -33,6 +33,7 @@ from bleak import BleakClient from thingsboard_gateway.connectors.connector import log from thingsboard_gateway.tb_utility.tb_loader import TBModuleLoader +from thingsboard_gateway.connectors.ble.error_handler import ErrorHandler MAC_ADDRESS_FORMAT = { 'Darwin': '-', @@ -111,8 +112,9 @@ class Device(Thread): await self.__process_self() else: await asyncio.sleep(.2) - except Exception: + except Exception as e: log.error('Problem with connection') + log.debug(e) try: await self.client.disconnect() @@ -160,12 +162,28 @@ class Device(Thread): char_id = item['characteristicUUID'] if item['method'] == 'read': - data = await self.client.read_gatt_char(char_id) - not_converted_data[section].append({'data': data, **item}) + try: + data = await self.client.read_gatt_char(char_id) + not_converted_data[section].append({'data': data, **item}) + except Exception as e: + error = ErrorHandler(e) + if error.is_char_not_found() or error.is_operation_not_supported(): + log.error(e) + pass + else: + raise e elif item['method'] == 'notify' and char_id not in self.notifying_chars: - self.__set_char_handle(item, char_id) - self.notifying_chars.append(char_id) - await self.notify(char_id) + try: + self.__set_char_handle(item, char_id) + self.notifying_chars.append(char_id) + await self.notify(char_id) + except Exception as e: + error = ErrorHandler(e) + if error.is_char_not_found() or error.is_operation_not_supported(): + log.error(e) + pass + else: + raise e if len(not_converted_data['telemetry']) > 0 or len(not_converted_data['attributes']) > 0: data_for_converter = { diff --git a/thingsboard_gateway/connectors/ble/error_handler.py b/thingsboard_gateway/connectors/ble/error_handler.py new file mode 100644 index 00000000..f04d8d4b --- /dev/null +++ b/thingsboard_gateway/connectors/ble/error_handler.py @@ -0,0 +1,21 @@ +class ErrorHandler: + def __init__(self, e): + self.e = e + + def is_char_not_found(self): + try: + if "could not be found!" in self.e.args[0]: + return True + except IndexError: + return False + + return False + + def is_operation_not_supported(self): + try: + if 'not permitted' in self.e.args[1] or 'not supported' in self.e.args[1]: + return True + except IndexError: + return False + + return False From e6c007fe7eea706448e8aaff807141a7dffc858c Mon Sep 17 00:00:00 2001 From: samson0v Date: Tue, 25 Jan 2022 14:36:14 +0200 Subject: [PATCH 3/3] Fixed path resolving for Modbus Compability Adapter --- .../modbus/backward_compability_adapter.py | 13 +++++-------- .../connectors/modbus/modbus_connector.py | 2 +- thingsboard_gateway/gateway/tb_gateway_service.py | 4 ++++ 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/thingsboard_gateway/connectors/modbus/backward_compability_adapter.py b/thingsboard_gateway/connectors/modbus/backward_compability_adapter.py index c38ac2ec..51a03ef6 100644 --- a/thingsboard_gateway/connectors/modbus/backward_compability_adapter.py +++ b/thingsboard_gateway/connectors/modbus/backward_compability_adapter.py @@ -11,8 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from os import path -from os.path import exists from simplejson import dumps @@ -22,13 +20,11 @@ from thingsboard_gateway.connectors.connector import log class BackwardCompatibilityAdapter: config_files_count = 1 CONFIG_PATH = None - if exists('config/'): - CONFIG_PATH = 'config/' - elif exists("/etc/thingsboard-gateway/config/".replace('/', path.sep)): - CONFIG_PATH = "/etc/thingsboard-gateway/config/".replace('/', path.sep) - def __init__(self, config): + def __init__(self, config, config_dir): self.__config = config + self.__config_dir = config_dir + BackwardCompatibilityAdapter.CONFIG_PATH = self.__config_dir self.__keys = ['host', 'port', 'type', 'method', 'timeout', 'byteOrder', 'wordOrder', 'retries', 'retryOnEmpty', 'retryOnInvalid', 'baudrate'] @@ -46,7 +42,8 @@ class BackwardCompatibilityAdapter: log.warning( 'You are using old configuration structure for Modbus connector. It will be DEPRECATED in the future ' - 'version! New config file "modbus_new.json" was generated in config/ folder. Please, use it.') + 'version! New config file "modbus_new.json" was generated in %s folder. Please, use it.', self.CONFIG_PATH) + log.warning('You have to manually connect the new generated config file to tb_gateway.yaml!') slaves = [] for device in self.__config['server'].get('devices', []): diff --git a/thingsboard_gateway/connectors/modbus/modbus_connector.py b/thingsboard_gateway/connectors/modbus/modbus_connector.py index 33277477..78471dac 100644 --- a/thingsboard_gateway/connectors/modbus/modbus_connector.py +++ b/thingsboard_gateway/connectors/modbus/modbus_connector.py @@ -94,7 +94,7 @@ class ModbusConnector(Connector, Thread): self.__gateway = gateway self._connector_type = connector_type - self.__backward_compatibility_adapter = BackwardCompatibilityAdapter(config) + self.__backward_compatibility_adapter = BackwardCompatibilityAdapter(config, gateway.config_dir) self.__config = self.__backward_compatibility_adapter.convert() self.setName(self.__config.get("name", 'Modbus Default ' + ''.join(choice(ascii_lowercase) for _ in range(5)))) diff --git a/thingsboard_gateway/gateway/tb_gateway_service.py b/thingsboard_gateway/gateway/tb_gateway_service.py index dc5ddb73..f4002d24 100644 --- a/thingsboard_gateway/gateway/tb_gateway_service.py +++ b/thingsboard_gateway/gateway/tb_gateway_service.py @@ -263,6 +263,10 @@ class TBGatewayService: log.info("The gateway has been stopped.") self.tb_client.stop() + @property + def config_dir(self): + return self._config_dir + def __close_connectors(self): for current_connector in self.available_connectors: try: