Skip to content

Commit d57aa03

Browse files
author
brentru
committed
message-queue handling to c2d, add optional return_response kwarg to _post for sending device messages
1 parent c677f4f commit d57aa03

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

adafruit_azureiot.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,30 @@ def _parse_http_status(status_code, status_reason):
7676
raise TypeError("Error {0}: {1}".format(status_code, status_reason))
7777

7878
def get_hub_message(self, device_id):
79-
"""Gets a message from a Microsoft Azure IoT Hub (Cloud-to-Device).
79+
"""Returns a message from a Microsoft Azure IoT Hub (Cloud-to-Device), or -1 if the message queue is empty.
8080
NOTE: HTTP Cloud-to-Device messages are throttled. Poll every 25 minutes, or more.
8181
:param int device_id: Device identifier.
8282
"""
83-
# GET device-bound notification
83+
reject_message = True
84+
# get a device-bound notification
8485
path = "{0}/devices/{1}/messages/deviceBound?api-version={2}".format(self._iot_hub_url,
8586
device_id, AZ_API_VER)
8687
data = self._get(path, is_c2d=True)
87-
reject_message = True
88-
# check for etag in header
89-
print(data)
88+
if data == 204: # device's message queue is empty
89+
return -1
9090
etag = data[1]['etag']
9191
if etag: # either complete or nack the message
9292
reject_message = False
93+
# prepare the device-bound completion URL
9394
etag = etag.strip('\'"')
94-
path_complete = "{0}.azure-devices.net/devices/{1}/messages/deviceBound/{2}?api-version={3}".format(self._iot_hub_url, device_id, etag, AZ_API_VER)
95-
print(path_complete)
95+
path_complete = "{0}/devices/{1}/messages/deviceBound/{2}?api-version={3}".format(self._iot_hub_url, device_id, etag, AZ_API_VER)
9696
if reject_message:
9797
path_complete += '&reject'
98-
self._delete(path_complete)
99-
print('deleted!')
98+
del_status = self._delete(path_complete, is_c2d=True)
99+
if del_status == 204:
100+
return data[0]
101+
return -1
102+
100103

101104
# Device Messaging
102105
def send_device_message(self, device_id, message):
@@ -106,7 +109,7 @@ def send_device_message(self, device_id, message):
106109
"""
107110
path = "{0}/devices/{1}/messages/events?api-version={2}".format(self._iot_hub_url,
108111
device_id, AZ_API_VER)
109-
self._post(path, message)
112+
self._post(path, message, return_response=False)
110113

111114
# Device Twin
112115
def get_device_twin(self, device_id):
@@ -155,7 +158,7 @@ def delete_device(self, device_id):
155158
self._delete(path)
156159

157160
# HTTP Helper Methods
158-
def _post(self, path, payload):
161+
def _post(self, path, payload, return_response=True):
159162
"""HTTP POST
160163
:param str path: Formatted Azure IOT Hub Path.
161164
:param str payload: JSON-formatted Data Payload.
@@ -165,32 +168,37 @@ def _post(self, path, payload):
165168
json=payload,
166169
headers=self._azure_header)
167170
self._parse_http_status(response.status_code, response.reason)
168-
return response.json()
171+
if return_response:
172+
return response.json()
173+
response.close()
169174

170175
def _get(self, path, is_c2d=False):
171176
"""HTTP GET
172177
:param str path: Formatted Azure IOT Hub Path.
173-
:param bool is_c2d: Cloud-to-device message request.
178+
:param bool is_c2d: Cloud-to-device get request.
174179
"""
180+
print(path)
175181
response = self._wifi.get(
176182
path,
177183
headers=self._azure_header)
178-
if is_c2d:
184+
if is_c2d: # check status of azure message queue
179185
if response.status_code == 200:
180186
return response.text, response.headers
181-
raise TypeError('No data within message queue')
187+
return response.status_code
182188
self._parse_http_status(response.status_code, response.reason)
183189
return response.json()
184190

185-
def _delete(self, path):
191+
def _delete(self, path, is_c2d=False):
186192
"""HTTP DELETE
187193
:param str path: Formatted Azure IOT Hub Path.
194+
:param bool is_c2d: Cloud-to-device delete request.
188195
"""
189196
response = self._wifi.delete(
190197
path,
191198
headers=self._azure_header)
192-
print(response.status_code, response.reason)
193199
self._parse_http_status(response.status_code, response.reason)
200+
if is_c2d: # check server response for complete message request
201+
return response.status_code
194202
return response.json()
195203

196204
def _patch(self, path, payload):

0 commit comments

Comments
 (0)