Skip to content

Commit d169190

Browse files
author
brentru
committed
refactor: device_id in init, move new self._device_id into all calls, remove device_id from functions, pylint
1 parent 012db47 commit d169190

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

adafruit_azureiot.py

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ class IOT_Hub:
5050
Provides access to a Microsoft Azure IoT Hub.
5151
https://docs.microsoft.com/en-us/rest/api/iothub/
5252
"""
53-
def __init__(self, wifi_manager, iot_hub_name, sas_token):
53+
def __init__(self, wifi_manager, iot_hub_name, sas_token, device_id):
5454
""" Creates an instance of an Azure IoT Hub Client.
5555
:param wifi_manager: WiFiManager object from ESPSPI_WiFiManager.
5656
:param str iot_hub_name: Name of your IoT Hub.
5757
:param str sas_token: Azure IoT Hub SAS Token Identifier.
58+
:param str device_id: Unique Azure IoT Device Identifier.
5859
"""
5960
_wifi_type = str(type(wifi_manager))
6061
if 'ESPSPI_WiFiManager' in _wifi_type:
@@ -63,6 +64,7 @@ def __init__(self, wifi_manager, iot_hub_name, sas_token):
6364
raise TypeError("This library requires a WiFiManager object.")
6465
self._iot_hub_url = "https://{0}.azure-devices.net".format(iot_hub_name)
6566
self._sas_token = sas_token
67+
self._device_id = device_id
6668
self._azure_header = {"Authorization":self._sas_token}
6769

6870
@staticmethod
@@ -76,27 +78,27 @@ def _parse_http_status(status_code, status_reason):
7678
raise TypeError("Error {0}: {1}".format(status_code, status_reason))
7779

7880
# Cloud-to-Device Messaging
79-
def get_hub_message(self, device_id):
81+
def get_hub_message(self):
8082
"""Returns a message from a Microsoft Azure IoT Hub (Cloud-to-Device), or -1
8183
if the message queue is empty.
8284
NOTE: HTTP Cloud-to-Device messages are throttled. Poll every 25+ minutes.
83-
:param int device_id: Device identifier.
8485
"""
8586
reject_message = True
8687
# get a device-bound notification
8788
path = "{0}/devices/{1}/messages/deviceBound?api-version={2}".format(self._iot_hub_url,
88-
device_id, AZ_API_VER)
89+
self._device_id,
90+
AZ_API_VER)
8991
try:
9092
data = self._get(path, is_c2d=True)
9193
except RuntimeError:
92-
raise RuntimeError('HTTP C2D Messages are HEAVILY throttled, poll every 25 min.')
94+
raise RuntimeError('HTTP C2D messages are HEAVILY throttled - poll every 25 mins.')
9395
if data == 204: # device's message queue is empty
9496
return -1
9597
etag = data[1]['etag']
9698
if etag: # either complete or nack the message
9799
reject_message = False
98100
path_complete = "{0}/devices/{1}/messages/deviceBound/{2}?api-version={3}".format(
99-
self._iot_hub_url, device_id, etag.strip('\'"'), AZ_API_VER)
101+
self._iot_hub_url, self._device_id, etag.strip('\'"'), AZ_API_VER)
100102
if reject_message:
101103
path_complete += '&reject'
102104
del_status = self._delete(path_complete)
@@ -105,62 +107,54 @@ def get_hub_message(self, device_id):
105107
return -1
106108

107109
# Device-to-Cloud Messaging
108-
def send_device_message(self, device_id, message):
110+
def send_device_message(self, message):
109111
"""Sends a device-to-cloud message.
110-
:param string device_id: Device Identifier.
111-
:param string message: Message.
112+
:param string message: Message to send to Azure IoT.
112113
"""
113114
path = "{0}/devices/{1}/messages/events?api-version={2}".format(self._iot_hub_url,
114-
device_id, AZ_API_VER)
115+
self._device_id, AZ_API_VER)
115116
self._post(path, message, return_response=False)
116117

117118
# Device Twin
118-
def get_device_twin(self, device_id):
119-
"""Returns a device twin
120-
:param str device_id: Device Identifier.
119+
def get_device_twin(self):
120+
"""Returns the device's device twin information in JSON format.
121121
"""
122-
path = "{0}/twins/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZ_API_VER)
122+
path = "{0}/twins/{1}?api-version={2}".format(self._iot_hub_url,
123+
self._device_id, AZ_API_VER)
123124
return self._get(path)
124125

125-
def update_device_twin(self, device_id, properties):
126-
"""Updates tags and desired properties of a device twin.
127-
:param str device_id: Device Identifier.
126+
def update_device_twin(self, properties):
127+
"""Updates tags and desired properties of the device's device twin.
128128
:param str properties: Device Twin Properties
129129
(https://docs.microsoft.com/en-us/rest/api/iothub/service/updatetwin#twinproperties)
130130
"""
131-
path = "{0}/twins/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZ_API_VER)
131+
path = "{0}/twins/{1}?api-version={2}".format(self._iot_hub_url,
132+
self._device_id, AZ_API_VER)
132133
return self._patch(path, properties)
133134

134-
def replace_device_twin(self, device_id, properties):
135+
def replace_device_twin(self, properties):
135136
"""Replaces tags and desired properties of a device twin.
136-
:param str device_id: Device Identifier.
137137
:param str properties: Device Twin Properties.
138138
"""
139-
path = "{0}/twins/{1}?api-version-{2}".format(self._iot_hub_url, device_id, AZ_API_VER)
139+
path = "{0}/twins/{1}?api-version-{2}".format(self._iot_hub_url,
140+
self._device_id, AZ_API_VER)
140141
return self._put(path, properties)
141142

142143
# IoT Hub Service
143144
def get_devices(self):
144-
"""Enumerate devices from the identity registry of your IoT Hub.
145+
"""Enumerate devices from the identity registry of the IoT Hub.
145146
"""
146147
path = "{0}/devices/?api-version={1}".format(self._iot_hub_url, AZ_API_VER)
147148
return self._get(path)
148149

149-
def get_device(self, device_id):
150-
"""Gets device information from the identity registry of an IoT Hub.
151-
:param str device_id: Device Identifier.
150+
def get_device(self):
151+
"""Gets device information from the identity
152+
registry of an IoT Hub.
152153
"""
153-
path = "{0}/devices/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZ_API_VER)
154+
path = "{0}/devices/{1}?api-version={2}".format(self._iot_hub_url,
155+
self._device_id, AZ_API_VER)
154156
return self._get(path)
155157

156-
def delete_device(self, device_id, device_etag):
157-
"""Deletes a specified device from the identity register of an IoT Hub.
158-
:param str device_id: Device Identifier.
159-
:param str device_etag: Device Identity Tag.
160-
"""
161-
path = "{0}/devices/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZ_API_VER)
162-
self._delete(path, etag=device_etag)
163-
164158
# HTTP Helper Methods
165159
def _post(self, path, payload, return_response=True):
166160
"""HTTP POST

0 commit comments

Comments
 (0)