Skip to content

Commit 6735960

Browse files
authored
Merge pull request #18 from jimbobbennett/topic_subscribe
Updated to use topic specific subscriptions
2 parents 7f30d82 + b2b6035 commit 6735960

File tree

2 files changed

+20
-40
lines changed

2 files changed

+20
-40
lines changed

adafruit_azureiot/iot_mqtt.py

+12-39
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ def _create_mqtt_client(self) -> None:
132132

133133
# set actions to take throughout connection lifecycle
134134
self._mqtts.on_connect = self._on_connect
135-
self._mqtts.on_message = self._on_message
136135
self._mqtts.on_log = self._on_log
137136
self._mqtts.on_publish = self._on_publish
138137
self._mqtts.on_disconnect = self._on_disconnect
@@ -172,7 +171,7 @@ def _on_publish(self, client, data, topic, msg_id) -> None:
172171
self._logger.info("- iot_mqtt :: _on_publish :: " + str(data) + " on topic " + str(topic))
173172

174173
# pylint: disable=W0703
175-
def _handle_device_twin_update(self, msg: str, topic: str) -> None:
174+
def _handle_device_twin_update(self, client, topic: str, msg: str) -> None:
176175
self._logger.debug("- iot_mqtt :: _echo_desired :: " + topic)
177176
twin = None
178177
desired = None
@@ -213,7 +212,7 @@ def _handle_device_twin_update(self, msg: str, topic: str) -> None:
213212
for property_name, value in desired.items():
214213
self._callback.device_twin_desired_updated(property_name, value, desired_version)
215214

216-
def _handle_direct_method(self, msg: str, topic: str) -> None:
215+
def _handle_direct_method(self, client, topic: str, msg: str) -> None:
217216
index = topic.find("$rid=")
218217
method_id = 1
219218
method_name = "None"
@@ -244,7 +243,7 @@ def _handle_direct_method(self, msg: str, topic: str) -> None:
244243
self._logger.info("C2D: => " + next_topic + " with data " + ret_message + " and name => " + method_name)
245244
self._send_common(next_topic, ret_message)
246245

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

250249
properties = {}
@@ -255,38 +254,6 @@ def _handle_cloud_to_device_message(self, msg: str, topic: str) -> None:
255254
self._callback.cloud_to_device_message_received(msg, properties)
256255
gc.collect()
257256

258-
# pylint: disable=W0702, R0912
259-
def _on_message(self, client, msg_topic, payload) -> None:
260-
topic = ""
261-
msg = None
262-
263-
self._logger.info("- iot_mqtt :: _on_message")
264-
265-
if payload is not None:
266-
try:
267-
msg = payload.decode("utf-8")
268-
except:
269-
msg = str(payload)
270-
271-
if msg_topic is not None:
272-
try:
273-
topic = msg_topic.decode("utf-8")
274-
except:
275-
topic = str(msg_topic)
276-
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-
self._handle_direct_method(str(msg), topic)
282-
else:
283-
if not topic.startswith("$iothub/twin/res/"): # not twin response
284-
self._logger.error("ERROR: unknown twin! - {}".format(msg))
285-
elif topic.startswith("devices/{}/messages/devicebound".format(self._device_id)):
286-
self._handle_cloud_to_device_message(str(msg), topic)
287-
else:
288-
self._logger.error("ERROR: (unknown message) - {}".format(msg))
289-
290257
def _send_common(self, topic: str, data) -> None:
291258
# Convert data to a string
292259
if isinstance(data, dict):
@@ -363,13 +330,19 @@ def __init__(
363330
self._is_subscribed_to_twins = False
364331

365332
def _subscribe_to_core_topics(self):
366-
self._mqtts.subscribe("devices/{}/messages/events/#".format(self._device_id))
367-
self._mqtts.subscribe("devices/{}/messages/devicebound/#".format(self._device_id))
333+
device_bound_topic = "devices/{}/messages/devicebound/#".format(self._device_id)
334+
self._mqtts.add_topic_callback(device_bound_topic, self._handle_cloud_to_device_message)
335+
self._mqtts.subscribe(device_bound_topic)
336+
337+
self._mqtts.add_topic_callback("$iothub/methods/#", self._handle_direct_method)
368338
self._mqtts.subscribe("$iothub/methods/#")
369339

370340
def _subscribe_to_twin_topics(self):
341+
self._mqtts.add_topic_callback("$iothub/twin/PATCH/properties/desired/#", self._handle_device_twin_update)
371342
self._mqtts.subscribe("$iothub/twin/PATCH/properties/desired/#") # twin desired property changes
372-
self._mqtts.subscribe("$iothub/twin/res/#") # twin properties response
343+
344+
self._mqtts.add_topic_callback("$iothub/twin/res/200/#", self._handle_device_twin_update)
345+
self._mqtts.subscribe("$iothub/twin/res/200/#") # twin properties response
373346

374347
def connect(self) -> bool:
375348
"""Connects to the MQTT broker

docs/conf.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@
2121
# Uncomment the below if you use native CircuitPython modules such as
2222
# digitalio, micropython and busio. List the modules you use. Without it, the
2323
# autodoc module docs will fail to generate with a warning.
24-
autodoc_mock_imports = ["adafruit_binascii", "adafruit_logging", "adafruit_requests", "adafruit_hashlib", "adafruit_ntp"]
24+
autodoc_mock_imports = [
25+
"adafruit_binascii",
26+
"adafruit_logging",
27+
"adafruit_requests",
28+
"adafruit_hashlib",
29+
"adafruit_ntp",
30+
"adafruit_minimqtt",
31+
]
2532

2633

2734
intersphinx_mapping = {

0 commit comments

Comments
 (0)