Skip to content

Commit 0c36033

Browse files
author
brentru
committed
working on get_hub_message, d2c is funky
1 parent 7bafda5 commit 0c36033

File tree

1 file changed

+91
-44
lines changed

1 file changed

+91
-44
lines changed

adafruit_azureiot.py

Lines changed: 91 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -67,54 +67,34 @@ def __init__(self, wifi_manager, iot_hub_name, sas_token):
6767

6868
@staticmethod
6969
def _parse_http_status(status_code, status_reason):
70-
"""Parses HTTP Status, throws error based on Azure IoT Common Error Codes.
70+
"""Parses status code, throws error based on Azure IoT Common Error Codes.
7171
:param int status_code: HTTP status code.
7272
:param str status_reason: Description of HTTP status.
7373
"""
7474
for error in AZURE_HTTP_ERROR_CODES:
7575
if error == status_code:
7676
raise TypeError("Error {0}: {1}".format(status_code, status_reason))
7777

78-
# HTTP Request Methods
79-
def _post(self, path, payload):
80-
response = self._wifi.post(
81-
path,
82-
json=payload,
83-
headers=self._azure_header)
84-
self._parse_http_status(response.status_code, response.reason)
85-
86-
def _get(self, path):
87-
response = self._wifi.get(
88-
path,
89-
headers=self._azure_header)
90-
self._parse_http_status(response.status_code, response.reason)
91-
return response.json()
92-
93-
def _delete(self, path):
94-
response = self._wifi.delete(
95-
path,
96-
headers=self._azure_header)
97-
self._parse_http_status(response.status_code, response.reason)
98-
return response.json()
99-
100-
def _patch(self, path, payload):
101-
response = self._wifi.patch(
102-
path,
103-
json=payload,
104-
headers=self._azure_header)
105-
self._parse_http_status(response.status_code, response.reason)
106-
return response.json()
107-
108-
def _put(self, path, payload):
109-
response = self._wifi.put(
110-
path,
111-
json=payload,
112-
headers=self._azure_header)
113-
self._parse_http_status(response.status_code, response.reason)
114-
return response.json()
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!')
11596

11697
# Device Messaging
117-
# D2C: Device-to-Cloud
11898
def send_device_message(self, device_id, message):
11999
"""Sends a device-to-cloud message.
120100
:param string device_id: Device Identifier.
@@ -124,8 +104,6 @@ def send_device_message(self, device_id, message):
124104
device_id, AZURE_API_VER)
125105
self._post(path, message)
126106

127-
# TODO: Cloud-to-Device Communication
128-
129107
# Device Twin
130108
def get_device_twin(self, device_id):
131109
"""Returns a device twin
@@ -153,21 +131,90 @@ def replace_device_twin(self, device_id, properties):
153131

154132
# IoT Hub Service
155133
def get_devices(self):
156-
"""Retrieve devices from the identity registry of your IoT hub.
134+
"""Enumerate devices from the identity registry of your IoT hub.
157135
"""
158136
path = "{0}/devices/?api-version={1}".format(self._iot_hub_url, AZURE_API_VER)
159137
return self._get(path)
160138

161139
def get_device(self, device_id):
162-
"""Retrieves a device from the identity registry of an IoT hub.
140+
"""Gets device information from the identity registry of an IoT hub.
163141
:param str device_id: Device Identifier.
164142
"""
165143
path = "{0}/devices/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZURE_API_VER)
166144
return self._get(path)
167145

168146
def delete_device(self, device_id):
169-
"""Deletes a specified device_id from the identity register of an IoT Hub.
147+
"""Deletes a specified device from the identity register of an IoT Hub.
170148
:param str device_id: Device Identifier.
171149
"""
172150
path = "{0}/devices/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZURE_API_VER)
173151
self._delete(path)
152+
153+
# HTTP Helper Methods
154+
def _post(self, path, payload):
155+
"""HTTP POST
156+
:param str path: Formatted Azure IOT Hub Path.
157+
:param str payload: JSON-formatted Data Payload.
158+
"""
159+
response = self._wifi.post(
160+
path,
161+
json=payload,
162+
headers=self._azure_header)
163+
self._parse_http_status(response.status_code, response.reason)
164+
return response.json()
165+
response.close()
166+
167+
def _get(self, path, is_c2d=False):
168+
"""HTTP GET
169+
:param str path: Formatted Azure IOT Hub Path.
170+
:param bool is_c2d: Cloud-to-device message request.
171+
"""
172+
response = self._wifi.get(
173+
path,
174+
headers=self._azure_header)
175+
if is_c2d:
176+
if response.status_code == 200:
177+
return response.text, response.headers
178+
raise TypeError('No data within message queue')
179+
self._parse_http_status(response.status_code, response.reason)
180+
return response.json()
181+
response.close()
182+
183+
def _delete(self, path):
184+
"""HTTP DELETE
185+
:param str path: Formatted Azure IOT Hub Path.
186+
"""
187+
response = self._wifi.delete(
188+
path,
189+
headers=self._azure_header)
190+
print(response.status_code, response.reason)
191+
self._parse_http_status(response.status_code, response.reason)
192+
return response.json()
193+
response.close()
194+
195+
def _patch(self, path, payload):
196+
"""HTTP PATCH
197+
:param str path: Formatted Azure IOT Hub Path.
198+
:param str payload: JSON-formatted payload.
199+
"""
200+
response = self._wifi.patch(
201+
path,
202+
json=payload,
203+
headers=self._azure_header)
204+
self._parse_http_status(response.status_code, response.reason)
205+
return response.json()
206+
response.close()
207+
208+
def _put(self, path, payload=None):
209+
"""HTTP PUT
210+
:param str path: Formatted Azure IOT Hub Path.
211+
:param str payload: JSON-formatted payload.
212+
"""
213+
response = self._wifi.put(
214+
path,
215+
json=payload,
216+
headers=self._azure_header)
217+
self._parse_http_status(response.status_code, response.reason)
218+
print('Resp:', response.status_code, response.reason)
219+
return response.json()
220+
response.close()

0 commit comments

Comments
 (0)