@@ -76,27 +76,30 @@ 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).
79
+ """Returns a message from a Microsoft Azure IoT Hub (Cloud-to-Device), or -1 if the message queue is empty .
80
80
NOTE: HTTP Cloud-to-Device messages are throttled. Poll every 25 minutes, or more.
81
81
:param int device_id: Device identifier.
82
82
"""
83
- # GET device-bound notification
83
+ reject_message = True
84
+ # get a device-bound notification
84
85
path = "{0}/devices/{1}/messages/deviceBound?api-version={2}" .format (self ._iot_hub_url ,
85
86
device_id , AZ_API_VER )
86
87
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
90
90
etag = data [1 ]['etag' ]
91
91
if etag : # either complete or nack the message
92
92
reject_message = False
93
+ # prepare the device-bound completion URL
93
94
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 )
96
96
if reject_message :
97
97
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
+
100
103
101
104
# Device Messaging
102
105
def send_device_message (self , device_id , message ):
@@ -106,7 +109,7 @@ def send_device_message(self, device_id, message):
106
109
"""
107
110
path = "{0}/devices/{1}/messages/events?api-version={2}" .format (self ._iot_hub_url ,
108
111
device_id , AZ_API_VER )
109
- self ._post (path , message )
112
+ self ._post (path , message , return_response = False )
110
113
111
114
# Device Twin
112
115
def get_device_twin (self , device_id ):
@@ -155,7 +158,7 @@ def delete_device(self, device_id):
155
158
self ._delete (path )
156
159
157
160
# HTTP Helper Methods
158
- def _post (self , path , payload ):
161
+ def _post (self , path , payload , return_response = True ):
159
162
"""HTTP POST
160
163
:param str path: Formatted Azure IOT Hub Path.
161
164
:param str payload: JSON-formatted Data Payload.
@@ -165,32 +168,37 @@ def _post(self, path, payload):
165
168
json = payload ,
166
169
headers = self ._azure_header )
167
170
self ._parse_http_status (response .status_code , response .reason )
168
- return response .json ()
171
+ if return_response :
172
+ return response .json ()
173
+ response .close ()
169
174
170
175
def _get (self , path , is_c2d = False ):
171
176
"""HTTP GET
172
177
: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.
174
179
"""
180
+ print (path )
175
181
response = self ._wifi .get (
176
182
path ,
177
183
headers = self ._azure_header )
178
- if is_c2d :
184
+ if is_c2d : # check status of azure message queue
179
185
if response .status_code == 200 :
180
186
return response .text , response .headers
181
- raise TypeError ( 'No data within message queue' )
187
+ return response . status_code
182
188
self ._parse_http_status (response .status_code , response .reason )
183
189
return response .json ()
184
190
185
- def _delete (self , path ):
191
+ def _delete (self , path , is_c2d = False ):
186
192
"""HTTP DELETE
187
193
:param str path: Formatted Azure IOT Hub Path.
194
+ :param bool is_c2d: Cloud-to-device delete request.
188
195
"""
189
196
response = self ._wifi .delete (
190
197
path ,
191
198
headers = self ._azure_header )
192
- print (response .status_code , response .reason )
193
199
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
194
202
return response .json ()
195
203
196
204
def _patch (self , path , payload ):
0 commit comments