Skip to content

Commit

Permalink
Merge pull request #615 from Morg42/mqtt-2
Browse files Browse the repository at this point in the history
mqtt: add error handling for non-decodable utf-8 messages
  • Loading branch information
Morg42 authored Jan 20, 2024
2 parents ad62bba + d44b3be commit a3cfbfc
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions modules/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ def _callback_to_plugin(self, plugin_name, subscription_dict, topic, payload, qo
datatype = subscription_dict.get('payload_type', 'foo')
bool_values = subscription_dict.get('bool_values', None)
payload = self.cast_from_mqtt(datatype, payload, bool_values)

plugin = subscription_dict.get('callback', None)

subscription_found = False
Expand Down Expand Up @@ -563,13 +564,16 @@ def _on_mqtt_message(self, client, userdata, message):
for subscription in list(topic_dict):
self.logger.debug("_on_mqtt_message: subscription '{}': {}".format(subscription, topic_dict[subscription]))
subscriber_type = topic_dict[subscription].get('subscriber_type', None)
if subscriber_type == 'plugin':
subscription_found = self._callback_to_plugin(subscription, topic_dict[subscription], message.topic, message.payload, message.qos, message.retain)
elif subscriber_type == 'logic':
subscription_found = self._trigger_logic(topic_dict[subscription], message.topic, message.payload)
else:
self.logger.error("_on_mqtt_message: received topic for unknown subscriber_type '{}'".format(subscriber_type))

try:
if subscriber_type == 'plugin':
subscription_found = self._callback_to_plugin(subscription, topic_dict[subscription], message.topic, message.payload, message.qos, message.retain)
elif subscriber_type == 'logic':
subscription_found = self._trigger_logic(topic_dict[subscription], message.topic, message.payload)
else:
self.logger.error("_on_mqtt_message: received topic for unknown subscriber_type '{}'".format(subscriber_type))
except UnicodeDecodeError:
self.logger.warning(f"_on_mqtt_message: received ill-formed message with topic '{message.topic}', payload '{message.payload}', discarding")
return

if not subscription_found:
if not self._handle_broker_infos(message):
Expand Down

0 comments on commit a3cfbfc

Please sign in to comment.