@@ -172,7 +172,7 @@ def _on_publish(self, client, data, topic, msg_id) -> None:
172
172
self ._logger .info ("- iot_mqtt :: _on_publish :: " + str (data ) + " on topic " + str (topic ))
173
173
174
174
# 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 :
176
176
self ._logger .debug ("- iot_mqtt :: _echo_desired :: " + topic )
177
177
twin = None
178
178
desired = None
@@ -244,7 +244,7 @@ def _handle_direct_method(self, client, topic: str, msg: str) -> None:
244
244
self ._logger .info ("C2D: => " + next_topic + " with data " + ret_message + " and name => " + method_name )
245
245
self ._send_common (next_topic , ret_message )
246
246
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 :
248
248
parts = topic .split ("&" )[1 :]
249
249
250
250
properties = {}
@@ -274,19 +274,7 @@ def _on_message(self, client, msg_topic, payload) -> None:
274
274
except :
275
275
topic = str (msg_topic )
276
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
- 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 ))
290
278
291
279
def _send_common (self , topic : str , data ) -> None :
292
280
# Convert data to a string
@@ -364,15 +352,19 @@ def __init__(
364
352
self ._is_subscribed_to_twins = False
365
353
366
354
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 )
369
358
370
359
self ._mqtts .add_topic_callback ("$iothub/methods/#" , self ._handle_direct_method )
371
- # self._mqtts.subscribe("$iothub/methods/#")
360
+ self ._mqtts .subscribe ("$iothub/methods/#" )
372
361
373
362
def _subscribe_to_twin_topics (self ):
363
+ self ._mqtts .add_topic_callback ("$iothub/twin/PATCH/properties/desired/#" , self ._handle_device_twin_update )
374
364
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
376
368
377
369
def connect (self ) -> bool :
378
370
"""Connects to the MQTT broker
0 commit comments