Skip to content

Commit 3eed9fc

Browse files
author
Jim Bennett
committed
Added initial support for topic-based subscriptions
1 parent 5dbbfa7 commit 3eed9fc

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

adafruit_azureiot/iot_mqtt.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def _on_publish(self, client, data, topic, msg_id) -> None:
172172
self._logger.info("- iot_mqtt :: _on_publish :: " + str(data) + " on topic " + str(topic))
173173

174174
# pylint: disable=W0703
175-
def _handle_device_twin_update(self, msg: str, topic: str) -> None:
175+
def _handle_device_twin_update(self, client, topic: str, msg: str) -> None:
176176
self._logger.debug("- iot_mqtt :: _echo_desired :: " + topic)
177177
twin = None
178178
desired = None
@@ -244,7 +244,7 @@ def _handle_direct_method(self, client, topic: str, msg: str) -> None:
244244
self._logger.info("C2D: => " + next_topic + " with data " + ret_message + " and name => " + method_name)
245245
self._send_common(next_topic, ret_message)
246246

247-
def _handle_cloud_to_device_message(self, msg: str, topic: str) -> None:
247+
def _handle_cloud_to_device_message(self, client, topic: str, msg: str) -> None:
248248
parts = topic.split("&")[1:]
249249

250250
properties = {}
@@ -274,19 +274,7 @@ def _on_message(self, client, msg_topic, payload) -> None:
274274
except:
275275
topic = str(msg_topic)
276276

277-
if topic.startswith("$iothub/"):
278-
if topic.startswith("$iothub/twin/PATCH/properties/desired/") or topic.startswith("$iothub/twin/res/200/?$rid="):
279-
self._handle_device_twin_update(str(msg), topic)
280-
elif topic.startswith("$iothub/methods"):
281-
pass
282-
# self._handle_direct_method(str(msg), topic)
283-
else:
284-
if not topic.startswith("$iothub/twin/res/"): # not twin response
285-
self._logger.error("ERROR: unknown twin! - {}".format(msg))
286-
elif topic.startswith("devices/{}/messages/devicebound".format(self._device_id)):
287-
self._handle_cloud_to_device_message(str(msg), topic)
288-
else:
289-
self._logger.error("ERROR: (unknown message) - {}".format(msg))
277+
self._logger.error("ERROR: (unknown message) - {} on topic {}".format(msg, topic))
290278

291279
def _send_common(self, topic: str, data) -> None:
292280
# Convert data to a string
@@ -364,15 +352,19 @@ def __init__(
364352
self._is_subscribed_to_twins = False
365353

366354
def _subscribe_to_core_topics(self):
367-
self._mqtts.subscribe("devices/{}/messages/events/#".format(self._device_id))
368-
self._mqtts.subscribe("devices/{}/messages/devicebound/#".format(self._device_id))
355+
device_bound_topic = "devices/{}/messages/devicebound/#".format(self._device_id)
356+
self._mqtts.add_topic_callback(device_bound_topic, self._handle_cloud_to_device_message)
357+
self._mqtts.subscribe(device_bound_topic)
369358

370359
self._mqtts.add_topic_callback("$iothub/methods/#", self._handle_direct_method)
371-
# self._mqtts.subscribe("$iothub/methods/#")
360+
self._mqtts.subscribe("$iothub/methods/#")
372361

373362
def _subscribe_to_twin_topics(self):
363+
self._mqtts.add_topic_callback("$iothub/twin/PATCH/properties/desired/#", self._handle_device_twin_update)
374364
self._mqtts.subscribe("$iothub/twin/PATCH/properties/desired/#") # twin desired property changes
375-
self._mqtts.subscribe("$iothub/twin/res/#") # twin properties response
365+
366+
self._mqtts.add_topic_callback("$iothub/twin/res/200/#", self._handle_device_twin_update)
367+
self._mqtts.subscribe("$iothub/twin/res/200/#") # twin properties response
376368

377369
def connect(self) -> bool:
378370
"""Connects to the MQTT broker

0 commit comments

Comments
 (0)