Skip to content

Commit

Permalink
Merge pull request #1090 from engix-ltd/cached_mqtt_converters
Browse files Browse the repository at this point in the history
  • Loading branch information
imbeacon authored Apr 4, 2023
2 parents 24bfc23 + 4ddcf78 commit d58423a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions thingsboard_gateway/config/mqtt.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"converter": {
"type": "custom",
"extension": "CustomMqttUplinkConverter",
"cached": true,
"extension-config": {
"temperatureBytes": 2,
"humidityBytes": 2,
Expand Down
29 changes: 21 additions & 8 deletions thingsboard_gateway/connectors/mqtt/mqtt_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d58423a

Please sign in to comment.