42
42
__version__ = "0.0.0-auto.0"
43
43
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AzureIoT.git"
44
44
45
- AZ_API_VER = "2018-06-30" # Azure URI API Version Identifier
46
- AZURE_HTTP_ERROR_CODES = [400 , 401 , 404 , 403 , 412 , 429 , 500 ] # Azure HTTP Status Codes
45
+ AZ_API_VER = "2018-06-30" # Azure URI API Version Identifier
46
+ AZURE_HTTP_ERROR_CODES = [400 , 401 , 404 , 403 , 412 , 429 , 500 ] # Azure HTTP Status Codes
47
+
47
48
48
49
class IOT_Hub :
49
50
"""
50
51
Provides access to a Microsoft Azure IoT Hub.
51
52
https://docs.microsoft.com/en-us/rest/api/iothub/
52
53
"""
54
+
53
55
def __init__ (self , wifi_manager , iot_hub_name , sas_token , device_id ):
54
56
""" Creates an instance of an Azure IoT Hub Client.
55
57
:param wifi_manager: WiFiManager object from ESPSPI_WiFiManager.
@@ -58,14 +60,14 @@ def __init__(self, wifi_manager, iot_hub_name, sas_token, device_id):
58
60
:param str device_id: Unique Azure IoT Device Identifier.
59
61
"""
60
62
_wifi_type = str (type (wifi_manager ))
61
- if ' ESPSPI_WiFiManager' in _wifi_type :
63
+ if " ESPSPI_WiFiManager" in _wifi_type :
62
64
self ._wifi = wifi_manager
63
65
else :
64
66
raise TypeError ("This library requires a WiFiManager object." )
65
67
self ._iot_hub_url = "https://{0}.azure-devices.net" .format (iot_hub_name )
66
68
self ._sas_token = sas_token
67
69
self ._device_id = device_id
68
- self ._azure_header = {"Authorization" :self ._sas_token }
70
+ self ._azure_header = {"Authorization" : self ._sas_token }
69
71
70
72
@property
71
73
def device_id (self ):
@@ -97,19 +99,20 @@ def get_hub_message(self):
97
99
"""
98
100
reject_message = True
99
101
# get a device-bound notification
100
- path = "{0}/devices/{1}/messages/deviceBound?api-version={2}" .format (self . _iot_hub_url ,
101
- self ._device_id ,
102
- AZ_API_VER )
102
+ path = "{0}/devices/{1}/messages/deviceBound?api-version={2}" .format (
103
+ self . _iot_hub_url , self ._device_id , AZ_API_VER
104
+ )
103
105
data = self ._get (path , is_c2d = True )
104
- if data == 204 : # device's message queue is empty
106
+ if data == 204 : # device's message queue is empty
105
107
return None
106
- etag = data [1 ][' etag' ]
107
- if etag : # either complete or nack the message
108
+ etag = data [1 ][" etag" ]
109
+ if etag : # either complete or nack the message
108
110
reject_message = False
109
111
path_complete = "{0}/devices/{1}/messages/deviceBound/{2}?api-version={3}" .format (
110
- self ._iot_hub_url , self ._device_id , etag .strip ('\' "' ), AZ_API_VER )
112
+ self ._iot_hub_url , self ._device_id , etag .strip ("'\" " ), AZ_API_VER
113
+ )
111
114
if reject_message :
112
- path_complete += ' &reject'
115
+ path_complete += " &reject"
113
116
del_status = self ._delete (path_complete )
114
117
if del_status == 204 :
115
118
return data [0 ]
@@ -120,33 +123,37 @@ def send_device_message(self, message):
120
123
"""Sends a device-to-cloud message.
121
124
:param string message: Message to send to Azure IoT.
122
125
"""
123
- path = "{0}/devices/{1}/messages/events?api-version={2}" .format (self ._iot_hub_url ,
124
- self ._device_id , AZ_API_VER )
126
+ path = "{0}/devices/{1}/messages/events?api-version={2}" .format (
127
+ self ._iot_hub_url , self ._device_id , AZ_API_VER
128
+ )
125
129
self ._post (path , message , return_response = False )
126
130
127
131
# Device Twin
128
132
def get_device_twin (self ):
129
133
"""Returns the device's device twin information in JSON format.
130
134
"""
131
- path = "{0}/twins/{1}?api-version={2}" .format (self ._iot_hub_url ,
132
- self ._device_id , AZ_API_VER )
135
+ path = "{0}/twins/{1}?api-version={2}" .format (
136
+ self ._iot_hub_url , self ._device_id , AZ_API_VER
137
+ )
133
138
return self ._get (path )
134
139
135
140
def update_device_twin (self , properties ):
136
141
"""Updates tags and desired properties of the device's device twin.
137
142
:param str properties: Device Twin Properties
138
143
(https://docs.microsoft.com/en-us/rest/api/iothub/service/updatetwin#twinproperties)
139
144
"""
140
- path = "{0}/twins/{1}?api-version={2}" .format (self ._iot_hub_url ,
141
- self ._device_id , AZ_API_VER )
145
+ path = "{0}/twins/{1}?api-version={2}" .format (
146
+ self ._iot_hub_url , self ._device_id , AZ_API_VER
147
+ )
142
148
return self ._patch (path , properties )
143
149
144
150
def replace_device_twin (self , properties ):
145
151
"""Replaces tags and desired properties of a device twin.
146
152
:param str properties: Device Twin Properties.
147
153
"""
148
- path = "{0}/twins/{1}?api-version-{2}" .format (self ._iot_hub_url ,
149
- self ._device_id , AZ_API_VER )
154
+ path = "{0}/twins/{1}?api-version-{2}" .format (
155
+ self ._iot_hub_url , self ._device_id , AZ_API_VER
156
+ )
150
157
return self ._put (path , properties )
151
158
152
159
# IoT Hub Service
@@ -160,8 +167,9 @@ def get_device(self):
160
167
"""Gets device information from the identity
161
168
registry of an IoT Hub.
162
169
"""
163
- path = "{0}/devices/{1}?api-version={2}" .format (self ._iot_hub_url ,
164
- self ._device_id , AZ_API_VER )
170
+ path = "{0}/devices/{1}?api-version={2}" .format (
171
+ self ._iot_hub_url , self ._device_id , AZ_API_VER
172
+ )
165
173
return self ._get (path )
166
174
167
175
# HTTP Helper Methods
@@ -170,10 +178,7 @@ def _post(self, path, payload, return_response=True):
170
178
:param str path: Formatted Azure IOT Hub Path.
171
179
:param str payload: JSON-formatted Data Payload.
172
180
"""
173
- response = self ._wifi .post (
174
- path ,
175
- json = payload ,
176
- headers = self ._azure_header )
181
+ response = self ._wifi .post (path , json = payload , headers = self ._azure_header )
177
182
self ._parse_http_status (response .status_code , response .reason )
178
183
if return_response :
179
184
return response .json ()
@@ -184,9 +189,7 @@ def _get(self, path, is_c2d=False):
184
189
:param str path: Formatted Azure IOT Hub Path.
185
190
:param bool is_c2d: Cloud-to-device get request.
186
191
"""
187
- response = self ._wifi .get (
188
- path ,
189
- headers = self ._azure_header )
192
+ response = self ._wifi .get (path , headers = self ._azure_header )
190
193
status_code = response .status_code
191
194
if is_c2d :
192
195
if status_code == 200 :
@@ -205,12 +208,10 @@ def _delete(self, path, etag=None):
205
208
:param str path: Formatted Azure IOT Hub Path.
206
209
"""
207
210
if etag :
208
- data_headers = {"Authorization" :self ._sas_token , "If-Match" :'"%s"' % etag }
211
+ data_headers = {"Authorization" : self ._sas_token , "If-Match" : '"%s"' % etag }
209
212
else :
210
213
data_headers = self ._azure_header
211
- response = self ._wifi .delete (
212
- path ,
213
- headers = data_headers )
214
+ response = self ._wifi .delete (path , headers = data_headers )
214
215
self ._parse_http_status (response .status_code , response .reason )
215
216
status_code = response .status_code
216
217
response .close ()
@@ -221,10 +222,7 @@ def _patch(self, path, payload):
221
222
:param str path: Formatted Azure IOT Hub Path.
222
223
:param str payload: JSON-formatted payload.
223
224
"""
224
- response = self ._wifi .patch (
225
- path ,
226
- json = payload ,
227
- headers = self ._azure_header )
225
+ response = self ._wifi .patch (path , json = payload , headers = self ._azure_header )
228
226
self ._parse_http_status (response .status_code , response .reason )
229
227
json_data = response .json ()
230
228
response .close ()
@@ -235,10 +233,7 @@ def _put(self, path, payload=None):
235
233
:param str path: Formatted Azure IOT Hub Path.
236
234
:param str payload: JSON-formatted payload.
237
235
"""
238
- response = self ._wifi .put (
239
- path ,
240
- json = payload ,
241
- headers = self ._azure_header )
236
+ response = self ._wifi .put (path , json = payload , headers = self ._azure_header )
242
237
self ._parse_http_status (response .status_code , response .reason )
243
238
json_data = response .json ()
244
239
response .close ()
0 commit comments