diff --git a/thingsboard_gateway/config/mqtt.json b/thingsboard_gateway/config/mqtt.json index 4acb3d4ed..940c6b9ee 100644 --- a/thingsboard_gateway/config/mqtt.json +++ b/thingsboard_gateway/config/mqtt.json @@ -112,6 +112,7 @@ "converter": { "type": "custom", "extension": "CustomMqttUplinkConverter", + "cached": true, "extension-config": { "temperatureBytes": 2, "humidityBytes": 2, diff --git a/thingsboard_gateway/connectors/mqtt/mqtt_connector.py b/thingsboard_gateway/connectors/mqtt/mqtt_connector.py index ee573ba42..9a885ece6 100644 --- a/thingsboard_gateway/connectors/mqtt/mqtt_connector.py +++ b/thingsboard_gateway/connectors/mqtt/mqtt_connector.py @@ -124,6 +124,8 @@ def __init__(self, gateway, config, connector_type): self.__attribute_requests = [] self.__attribute_updates = [] + self.__cached_custom_converters = {} + mandatory_keys = { "mapping": ['topicFilter', 'converter'], "serverSideRpc": ['deviceNameFilter', 'methodFilter', 'requestTopicExpression', 'valueExpression'], @@ -355,15 +357,26 @@ def _on_connect(self, client, userdata, flags, result_code, *extra_params): self.__log.error('Converter type or extension class should be configured!') continue - # Find and load required class - module = TBModuleLoader.import_module(self._connector_type, converter_class_name) - if module: - self.__log.debug('Converter %s for topic %s - found!', converter_class_name, - mapping["topicFilter"]) - converter = module(mapping) + converter = None + need_cache = mapping["converter"].get("type") == "custom" and mapping["converter"].get("cached", False) + if need_cache: + converter = self.__cached_custom_converters.get(converter_class_name) + + if not converter: + # Find and load required class + module = TBModuleLoader.import_module(self._connector_type, converter_class_name) + if module: + self.__log.debug('Converter %s for topic %s - found!', converter_class_name, + mapping["topicFilter"]) + converter = module(mapping) + if need_cache: + self.__cached_custom_converters[converter_class_name] = converter + else: + self.__log.error("Cannot find converter for %s topic", mapping["topicFilter"]) + continue else: - self.__log.error("Cannot find converter for %s topic", mapping["topicFilter"]) - continue + self.__log.debug('Converter %s for topic %s - found in cache!', converter_class_name, + mapping["topicFilter"]) # Setup regexp topic acceptance list --------------------------------------------------------------- # Check if topic is shared subscription type