@@ -84,21 +84,14 @@ def cloud_to_device_message_received(self, body: str, properties: dict) -> None:
84
84
:param dict properties: The propreties sent with the mesage
85
85
"""
86
86
87
- def device_twin_desired_updated (
88
- self , desired_property_name : str , desired_property_value , desired_version : int
89
- ) -> None :
87
+ def device_twin_desired_updated (self , desired_property_name : str , desired_property_value , desired_version : int ) -> None :
90
88
"""Called when the device twin desired properties are updated
91
89
:param str desired_property_name: The name of the desired property that was updated
92
90
:param desired_property_value: The value of the desired property that was updated
93
91
:param int desired_version: The version of the desired property that was updated
94
92
"""
95
93
96
- def device_twin_reported_updated (
97
- self ,
98
- reported_property_name : str ,
99
- reported_property_value ,
100
- reported_version : int ,
101
- ) -> None :
94
+ def device_twin_reported_updated (self , reported_property_name : str , reported_property_value , reported_version : int ,) -> None :
102
95
"""Called when the device twin reported values are updated
103
96
:param str reported_property_name: The name of the reported property that was updated
104
97
:param reported_property_value: The value of the reported property that was updated
@@ -114,17 +107,11 @@ class IoTMQTT:
114
107
def _gen_sas_token (self ) -> str :
115
108
token_expiry = int (time .time () + self ._token_expires )
116
109
uri = self ._hostname + "%2Fdevices%2F" + self ._device_id
117
- signed_hmac_sha256 = DeviceRegistration .compute_derived_symmetric_key (
118
- self ._key , uri + "\n " + str (token_expiry )
119
- )
110
+ signed_hmac_sha256 = DeviceRegistration .compute_derived_symmetric_key (self ._key , uri + "\n " + str (token_expiry ))
120
111
signature = parse .quote (signed_hmac_sha256 , "~()*!.'" )
121
- if signature .endswith (
122
- "\n "
123
- ): # somewhere along the crypto chain a newline is inserted
112
+ if signature .endswith ("\n " ): # somewhere along the crypto chain a newline is inserted
124
113
signature = signature [:- 1 ]
125
- token = "SharedAccessSignature sr={}&sig={}&se={}" .format (
126
- uri , signature , token_expiry
127
- )
114
+ token = "SharedAccessSignature sr={}&sig={}&se={}" .format (uri , signature , token_expiry )
128
115
return token
129
116
130
117
# Workaround for https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT/issues/25
@@ -160,12 +147,7 @@ def _create_mqtt_client(self) -> None:
160
147
161
148
# pylint: disable=C0103, W0613
162
149
def _on_connect (self , client , userdata , _ , rc ) -> None :
163
- self ._logger .info (
164
- "- iot_mqtt :: _on_connect :: rc = "
165
- + str (rc )
166
- + ", userdata = "
167
- + str (userdata )
168
- )
150
+ self ._logger .info ("- iot_mqtt :: _on_connect :: rc = " + str (rc ) + ", userdata = " + str (userdata ))
169
151
if rc == 0 :
170
152
self ._mqtt_connected = True
171
153
self ._auth_response_received = True
@@ -192,9 +174,7 @@ def _on_disconnect(self, client, userdata, rc) -> None:
192
174
self ._callback .connection_status_change (False )
193
175
194
176
def _on_publish (self , client , data , topic , msg_id ) -> None :
195
- self ._logger .info (
196
- "- iot_mqtt :: _on_publish :: " + str (data ) + " on topic " + str (topic )
197
- )
177
+ self ._logger .info ("- iot_mqtt :: _on_publish :: " + str (data ) + " on topic " + str (topic ))
198
178
199
179
# pylint: disable=W0703
200
180
def _handle_device_twin_update (self , msg : str , topic : str ) -> None :
@@ -205,12 +185,7 @@ def _handle_device_twin_update(self, msg: str, topic: str) -> None:
205
185
try :
206
186
twin = json .loads (msg )
207
187
except json .JSONDecodeError as e :
208
- self ._logger .error (
209
- "ERROR: JSON parse for Device Twin message object has failed. => "
210
- + msg
211
- + " => "
212
- + str (e )
213
- )
188
+ self ._logger .error ("ERROR: JSON parse for Device Twin message object has failed. => " + msg + " => " + str (e ))
214
189
return
215
190
216
191
if "reported" in twin :
@@ -220,15 +195,11 @@ def _handle_device_twin_update(self, msg: str, topic: str) -> None:
220
195
reported_version = reported ["$version" ]
221
196
reported .pop ("$version" )
222
197
else :
223
- self ._logger .error (
224
- "ERROR: Unexpected payload for reported twin update => " + msg
225
- )
198
+ self ._logger .error ("ERROR: Unexpected payload for reported twin update => " + msg )
226
199
return
227
200
228
201
for property_name , value in reported .items ():
229
- self ._callback .device_twin_reported_updated (
230
- property_name , value , reported_version
231
- )
202
+ self ._callback .device_twin_reported_updated (property_name , value , reported_version )
232
203
233
204
is_patch = "desired" not in twin
234
205
@@ -241,15 +212,11 @@ def _handle_device_twin_update(self, msg: str, topic: str) -> None:
241
212
desired_version = desired ["$version" ]
242
213
desired .pop ("$version" )
243
214
else :
244
- self ._logger .error (
245
- "ERROR: Unexpected payload for desired twin update => " + msg
246
- )
215
+ self ._logger .error ("ERROR: Unexpected payload for desired twin update => " + msg )
247
216
return
248
217
249
218
for property_name , value in desired .items ():
250
- self ._callback .device_twin_desired_updated (
251
- property_name , value , desired_version
252
- )
219
+ self ._callback .device_twin_desired_updated (property_name , value , desired_version )
253
220
254
221
def _handle_direct_method (self , msg : str , topic : str ) -> None :
255
222
index = topic .find ("$rid=" )
@@ -278,14 +245,7 @@ def _handle_direct_method(self, msg: str, topic: str) -> None:
278
245
ret_message = json .dumps (ret_json )
279
246
280
247
next_topic = "$iothub/methods/res/{}/?$rid={}" .format (ret_code , method_id )
281
- self ._logger .info (
282
- "C2D: => "
283
- + next_topic
284
- + " with data "
285
- + ret_message
286
- + " and name => "
287
- + method_name
288
- )
248
+ self ._logger .info ("C2D: => " + next_topic + " with data " + ret_message + " and name => " + method_name )
289
249
self ._send_common (next_topic , ret_message )
290
250
291
251
def _handle_cloud_to_device_message (self , msg : str , topic : str ) -> None :
@@ -318,18 +278,14 @@ def _on_message(self, client, msg_topic, payload) -> None:
318
278
topic = str (msg_topic )
319
279
320
280
if topic .startswith ("$iothub/" ):
321
- if topic .startswith (
322
- "$iothub/twin/PATCH/properties/desired/"
323
- ) or topic .startswith ("$iothub/twin/res/200/?$rid=" ):
281
+ if topic .startswith ("$iothub/twin/PATCH/properties/desired/" ) or topic .startswith ("$iothub/twin/res/200/?$rid=" ):
324
282
self ._handle_device_twin_update (str (msg ), topic )
325
283
elif topic .startswith ("$iothub/methods" ):
326
284
self ._handle_direct_method (str (msg ), topic )
327
285
else :
328
286
if not topic .startswith ("$iothub/twin/res/" ): # not twin response
329
287
self ._logger .error ("ERROR: unknown twin! - {}" .format (msg ))
330
- elif topic .startswith (
331
- "devices/{}/messages/devicebound" .format (self ._device_id )
332
- ):
288
+ elif topic .startswith ("devices/{}/messages/devicebound" .format (self ._device_id )):
333
289
self ._handle_cloud_to_device_message (str (msg ), topic )
334
290
else :
335
291
self ._logger .error ("ERROR: (unknown message) - {}" .format (msg ))
@@ -355,10 +311,7 @@ def _send_common(self, topic: str, data) -> None:
355
311
self ._logger .debug ("Data sent" )
356
312
break
357
313
except RuntimeError as runtime_error :
358
- self ._logger .info (
359
- "Could not send data, retrying after 0.5 seconds: "
360
- + str (runtime_error )
361
- )
314
+ self ._logger .info ("Could not send data, retrying after 0.5 seconds: " + str (runtime_error ))
362
315
retry = retry + 1
363
316
364
317
if retry >= 10 :
@@ -407,24 +360,18 @@ def __init__(
407
360
self ._hostname = hostname
408
361
self ._key = key
409
362
self ._token_expires = token_expires
410
- self ._username = "{}/{}/api-version={}" .format (
411
- self ._hostname , device_id , constants .IOTC_API_VERSION
412
- )
363
+ self ._username = "{}/{}/api-version={}" .format (self ._hostname , device_id , constants .IOTC_API_VERSION )
413
364
self ._passwd = self ._gen_sas_token ()
414
365
self ._logger = logger if logger is not None else logging .getLogger ("log" )
415
366
self ._is_subscribed_to_twins = False
416
367
417
368
def _subscribe_to_core_topics (self ):
418
369
self ._mqtts .subscribe ("devices/{}/messages/events/#" .format (self ._device_id ))
419
- self ._mqtts .subscribe (
420
- "devices/{}/messages/devicebound/#" .format (self ._device_id )
421
- )
370
+ self ._mqtts .subscribe ("devices/{}/messages/devicebound/#" .format (self ._device_id ))
422
371
self ._mqtts .subscribe ("$iothub/methods/#" )
423
372
424
373
def _subscribe_to_twin_topics (self ):
425
- self ._mqtts .subscribe (
426
- "$iothub/twin/PATCH/properties/desired/#"
427
- ) # twin desired property changes
374
+ self ._mqtts .subscribe ("$iothub/twin/PATCH/properties/desired/#" ) # twin desired property changes
428
375
self ._mqtts .subscribe ("$iothub/twin/res/#" ) # twin properties response
429
376
430
377
def connect (self ) -> bool :
@@ -440,10 +387,7 @@ def connect(self) -> bool:
440
387
while self ._auth_response_received is None :
441
388
self .loop ()
442
389
443
- self ._logger .info (
444
- " - iot_mqtt :: connect :: on_connect must be fired. Connected ? "
445
- + str (self .is_connected ())
446
- )
390
+ self ._logger .info (" - iot_mqtt :: connect :: on_connect must be fired. Connected ? " + str (self .is_connected ()))
447
391
if not self .is_connected ():
448
392
return False
449
393
@@ -500,9 +444,7 @@ def loop(self) -> None:
500
444
501
445
self ._mqtts .loop ()
502
446
503
- def send_device_to_cloud_message (
504
- self , message , system_properties : dict = None
505
- ) -> None :
447
+ def send_device_to_cloud_message (self , message , system_properties : dict = None ) -> None :
506
448
"""Send a device to cloud message from this device to Azure IoT Hub
507
449
:param message: The message data as a JSON string or a dictionary
508
450
:param system_properties: System properties to send with the message
@@ -538,7 +480,5 @@ def send_twin_patch(self, patch) -> None:
538
480
:raises RuntimeError: if the internet connection is not responding or is unable to connect
539
481
"""
540
482
self ._logger .info ("- iot_mqtt :: sendProperty :: " + str (patch ))
541
- topic = "$iothub/twin/PATCH/properties/reported/?$rid={}" .format (
542
- int (time .time ())
543
- )
483
+ topic = "$iothub/twin/PATCH/properties/reported/?$rid={}" .format (int (time .time ()))
544
484
self ._send_common (topic , patch )
0 commit comments