From 1468e0155bb21bd0d8d3f2ae2c4b799573eb9103 Mon Sep 17 00:00:00 2001 From: samson0v Date: Thu, 16 Nov 2023 10:08:44 +0200 Subject: [PATCH 1/2] Fixed XMPP Connector backup making --- thingsboard_gateway/connectors/xmpp/device.py | 17 +++++++++++++++ .../connectors/xmpp/xmpp_connector.py | 21 +++++++++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 thingsboard_gateway/connectors/xmpp/device.py diff --git a/thingsboard_gateway/connectors/xmpp/device.py b/thingsboard_gateway/connectors/xmpp/device.py new file mode 100644 index 000000000..c1f55d756 --- /dev/null +++ b/thingsboard_gateway/connectors/xmpp/device.py @@ -0,0 +1,17 @@ +from dataclasses import dataclass +from typing import List + + +@dataclass +class Device: + jid: str + device_name_expression: str + device_type_expression: str + attributes: List + timeseries: List + attribute_updates: List + server_side_rpc: List + converter = None + + def set_converter(self, converter): + self.converter = converter diff --git a/thingsboard_gateway/connectors/xmpp/xmpp_connector.py b/thingsboard_gateway/connectors/xmpp/xmpp_connector.py index b3824e69f..1f8c84e98 100644 --- a/thingsboard_gateway/connectors/xmpp/xmpp_connector.py +++ b/thingsboard_gateway/connectors/xmpp/xmpp_connector.py @@ -21,6 +21,7 @@ from time import sleep from thingsboard_gateway.connectors.connector import Connector +from thingsboard_gateway.connectors.xmpp.device import Device from thingsboard_gateway.tb_utility.tb_loader import TBModuleLoader from thingsboard_gateway.tb_utility.tb_utility import TBUtility from thingsboard_gateway.gateway.statistics_service import StatisticsService @@ -73,15 +74,23 @@ def __init__(self, gateway, config, connector_type): def _reformat_devices_config(self): for config in self._devices_config: try: - device_jid = config.pop('jid') + device_jid = config.get('jid') converter_name = config.pop('converter', DEFAULT_UPLINK_CONVERTER) converter = self._load_converter(converter_name) if not converter: continue - self._devices[device_jid] = config - self._devices[device_jid]['converter'] = converter(config, self.__log) + self._devices[device_jid] = Device( + jid=device_jid, + device_name_expression=config['deviceNameExpression'], + device_type_expression=config['deviceTypeExpression'], + attributes=config['attributes'], + timeseries=config['timeseries'], + attribute_updates=config['attributeUpdates'], + server_side_rpc=config['serverSideRpc'] + ) + self._devices[device_jid].set_converter(converter(config, self.__log)) except KeyError as e: self.__log.error('Invalid configuration %s with key error %s', config, e) continue @@ -161,7 +170,7 @@ def _process_messages(self): device_jid = msg.values['from'] device = self._devices.get(device_jid) if device: - converted_data = device['converter'].convert(device, msg.values['body']) + converted_data = device.converter.convert(device, msg.values['body']) if converted_data: XMPPConnector.DATA_TO_SEND.put(converted_data) @@ -220,7 +229,7 @@ def on_attributes_update(self, content): if not device_jid: self.__log.error('Device not found') - attr_updates = self._devices[device_jid].get('attributeUpdates', []) + attr_updates = self._devices[device_jid].attribute_updates for key, value in content['data'].items(): for attr_conf in attr_updates: if attr_conf['attributeOnThingsBoard'] == key: @@ -240,7 +249,7 @@ def server_side_rpc_handler(self, content): if not device_jid: self.__log.error('Device not found') - rpcs = self._devices[device_jid].get('serverSideRpc', []) + rpcs = self._devices[device_jid].server_side_rpc for rpc_conf in rpcs: if rpc_conf['methodRPC'] == content['data']['method']: data_to_send_tags = TBUtility.get_values(rpc_conf.get('valueExpression'), content['data'], From 048fa15a384a0f72a07b1456763d4b713fa16ae4 Mon Sep 17 00:00:00 2001 From: samson0v Date: Thu, 16 Nov 2023 10:11:20 +0200 Subject: [PATCH 2/2] Fixed XMPP Connector backup making --- thingsboard_gateway/connectors/xmpp/xmpp_connector.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/thingsboard_gateway/connectors/xmpp/xmpp_connector.py b/thingsboard_gateway/connectors/xmpp/xmpp_connector.py index 1f8c84e98..be231c17f 100644 --- a/thingsboard_gateway/connectors/xmpp/xmpp_connector.py +++ b/thingsboard_gateway/connectors/xmpp/xmpp_connector.py @@ -85,10 +85,10 @@ def _reformat_devices_config(self): jid=device_jid, device_name_expression=config['deviceNameExpression'], device_type_expression=config['deviceTypeExpression'], - attributes=config['attributes'], - timeseries=config['timeseries'], - attribute_updates=config['attributeUpdates'], - server_side_rpc=config['serverSideRpc'] + attributes=config.get('attributes', []), + timeseries=config.get('timeseries', []), + attribute_updates=config.get('attributeUpdates', []), + server_side_rpc=config.get('serverSideRpc', []) ) self._devices[device_jid].set_converter(converter(config, self.__log)) except KeyError as e: