42
42
__version__ = "0.0.0-auto.0"
43
43
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AzureIoT.git"
44
44
45
- AZURE_API_VER = "2018-06-30" # Azure URI API Version Identifier
45
+ AZ_API_VER = "2018-06-30" # Azure URI API Version Identifier
46
46
AZURE_HTTP_ERROR_CODES = [400 , 401 , 404 , 403 , 412 , 429 , 500 ] # Azure HTTP Status Codes
47
47
48
48
class IOT_HUB :
@@ -76,23 +76,29 @@ def _parse_http_status(status_code, status_reason):
76
76
raise TypeError ("Error {0}: {1}" .format (status_code , status_reason ))
77
77
78
78
def get_hub_message (self , device_id ):
79
- """Gets a message from a Microsoft Azure IoT Hub (Cloud-to-Device).
80
- NOTE: HTTP Cloud-to-Device messages are throttled. Poll every 25 minutes, or more.
81
- :param int device_id: Device identifier.
82
- """
83
- # GET device-bound notification
84
- path = "{0}/devices/{1}/messages/deviceBound?api-version={2}" .format (self ._iot_hub_url , device_id , AZURE_API_VER )
85
- data = self ._get (path , is_c2d = True )
86
- # check for etag in header
87
- print (data )
88
- etag = data [1 ]['etag' ]
89
- if etag is not None :
90
- print ('etag: ' , etag )
91
- # DELETE https://fully-qualified-iothubname.azure-devices.net/devices/{id}/messages/deviceBound/{etag}?api-version=2018-06-30
92
- path_complete = "https://{0}.azure-devices.net/devices/{1}/messages/deviceBound/{2}?api-version=2018-06-30" .format (self ._iot_hub_url , device_id , etag )
93
- print (path_complete )
94
- self ._delete (path_complete )
95
- print ('deleted!' )
79
+ """Gets a message from a Microsoft Azure IoT Hub (Cloud-to-Device).
80
+ NOTE: HTTP Cloud-to-Device messages are throttled. Poll every 25 minutes, or more.
81
+ :param int device_id: Device identifier.
82
+ """
83
+ # GET device-bound notification
84
+ path = "{0}/devices/{1}/messages/deviceBound?api-version={2}" .format (self ._iot_hub_url ,
85
+ device_id , AZ_API_VER )
86
+ data = self ._get (path , is_c2d = True )
87
+ reject_message = True
88
+ # check for etag in header
89
+ print (data )
90
+ etag = data [1 ]['etag' ]
91
+ if etag : # either complete or nack the message
92
+ reject_message = False
93
+ etag = etag .strip ('\' "' )
94
+ path_complete = "https://{0}.azure-devices.net/devices/{1}/messages/ \
95
+ deviceBound/{2}?api-version={3}" .format (self ._iot_hub_url ,
96
+ device_id , etag , AZ_API_VER )
97
+ print (path_complete )
98
+ if reject_message :
99
+ path_complete += '&reject'
100
+ self ._delete (path_complete )
101
+ print ('deleted!' )
96
102
97
103
# Device Messaging
98
104
def send_device_message (self , device_id , message ):
@@ -101,15 +107,15 @@ def send_device_message(self, device_id, message):
101
107
:param string message: Message.
102
108
"""
103
109
path = "{0}/devices/{1}/messages/events?api-version={2}" .format (self ._iot_hub_url ,
104
- device_id , AZURE_API_VER )
110
+ device_id , AZ_API_VER )
105
111
self ._post (path , message )
106
112
107
113
# Device Twin
108
114
def get_device_twin (self , device_id ):
109
115
"""Returns a device twin
110
116
:param str device_id: Device Identifier.
111
117
"""
112
- path = "{0}/twins/{1}?api-version={2}" .format (self ._iot_hub_url , device_id , AZURE_API_VER )
118
+ path = "{0}/twins/{1}?api-version={2}" .format (self ._iot_hub_url , device_id , AZ_API_VER )
113
119
return self ._get (path )
114
120
115
121
def update_device_twin (self , device_id , properties ):
@@ -118,36 +124,36 @@ def update_device_twin(self, device_id, properties):
118
124
:param str properties: Device Twin Properties
119
125
(https://docs.microsoft.com/en-us/rest/api/iothub/service/updatetwin#twinproperties)
120
126
"""
121
- path = "{0}/twins/{1}?api-version={2}" .format (self ._iot_hub_url , device_id , AZURE_API_VER )
127
+ path = "{0}/twins/{1}?api-version={2}" .format (self ._iot_hub_url , device_id , AZ_API_VER )
122
128
return self ._patch (path , properties )
123
129
124
130
def replace_device_twin (self , device_id , properties ):
125
131
"""Replaces tags and desired properties of a device twin.
126
132
:param str device_id: Device Identifier.
127
133
:param str properties: Device Twin Properties.
128
134
"""
129
- path = "{0}/twins/{1}?api-version-{2}" .format (self ._iot_hub_url , device_id , AZURE_API_VER )
135
+ path = "{0}/twins/{1}?api-version-{2}" .format (self ._iot_hub_url , device_id , AZ_API_VER )
130
136
return self ._put (path , properties )
131
137
132
138
# IoT Hub Service
133
139
def get_devices (self ):
134
140
"""Enumerate devices from the identity registry of your IoT hub.
135
141
"""
136
- path = "{0}/devices/?api-version={1}" .format (self ._iot_hub_url , AZURE_API_VER )
142
+ path = "{0}/devices/?api-version={1}" .format (self ._iot_hub_url , AZ_API_VER )
137
143
return self ._get (path )
138
144
139
145
def get_device (self , device_id ):
140
146
"""Gets device information from the identity registry of an IoT hub.
141
147
:param str device_id: Device Identifier.
142
148
"""
143
- path = "{0}/devices/{1}?api-version={2}" .format (self ._iot_hub_url , device_id , AZURE_API_VER )
149
+ path = "{0}/devices/{1}?api-version={2}" .format (self ._iot_hub_url , device_id , AZ_API_VER )
144
150
return self ._get (path )
145
151
146
152
def delete_device (self , device_id ):
147
153
"""Deletes a specified device from the identity register of an IoT Hub.
148
154
:param str device_id: Device Identifier.
149
155
"""
150
- path = "{0}/devices/{1}?api-version={2}" .format (self ._iot_hub_url , device_id , AZURE_API_VER )
156
+ path = "{0}/devices/{1}?api-version={2}" .format (self ._iot_hub_url , device_id , AZ_API_VER )
151
157
self ._delete (path )
152
158
153
159
# HTTP Helper Methods
@@ -162,7 +168,6 @@ def _post(self, path, payload):
162
168
headers = self ._azure_header )
163
169
self ._parse_http_status (response .status_code , response .reason )
164
170
return response .json ()
165
- response .close ()
166
171
167
172
def _get (self , path , is_c2d = False ):
168
173
"""HTTP GET
@@ -178,7 +183,6 @@ def _get(self, path, is_c2d=False):
178
183
raise TypeError ('No data within message queue' )
179
184
self ._parse_http_status (response .status_code , response .reason )
180
185
return response .json ()
181
- response .close ()
182
186
183
187
def _delete (self , path ):
184
188
"""HTTP DELETE
@@ -190,7 +194,6 @@ def _delete(self, path):
190
194
print (response .status_code , response .reason )
191
195
self ._parse_http_status (response .status_code , response .reason )
192
196
return response .json ()
193
- response .close ()
194
197
195
198
def _patch (self , path , payload ):
196
199
"""HTTP PATCH
@@ -203,7 +206,6 @@ def _patch(self, path, payload):
203
206
headers = self ._azure_header )
204
207
self ._parse_http_status (response .status_code , response .reason )
205
208
return response .json ()
206
- response .close ()
207
209
208
210
def _put (self , path , payload = None ):
209
211
"""HTTP PUT
@@ -217,4 +219,3 @@ def _put(self, path, payload=None):
217
219
self ._parse_http_status (response .status_code , response .reason )
218
220
print ('Resp:' , response .status_code , response .reason )
219
221
return response .json ()
220
- response .close ()
0 commit comments