@@ -50,11 +50,12 @@ class IOT_Hub:
50
50
Provides access to a Microsoft Azure IoT Hub.
51
51
https://docs.microsoft.com/en-us/rest/api/iothub/
52
52
"""
53
- def __init__ (self , wifi_manager , iot_hub_name , sas_token ):
53
+ def __init__ (self , wifi_manager , iot_hub_name , sas_token , device_id ):
54
54
""" Creates an instance of an Azure IoT Hub Client.
55
55
:param wifi_manager: WiFiManager object from ESPSPI_WiFiManager.
56
56
:param str iot_hub_name: Name of your IoT Hub.
57
57
:param str sas_token: Azure IoT Hub SAS Token Identifier.
58
+ :param str device_id: Unique Azure IoT Device Identifier.
58
59
"""
59
60
_wifi_type = str (type (wifi_manager ))
60
61
if 'ESPSPI_WiFiManager' in _wifi_type :
@@ -63,6 +64,7 @@ def __init__(self, wifi_manager, iot_hub_name, sas_token):
63
64
raise TypeError ("This library requires a WiFiManager object." )
64
65
self ._iot_hub_url = "https://{0}.azure-devices.net" .format (iot_hub_name )
65
66
self ._sas_token = sas_token
67
+ self ._device_id = device_id
66
68
self ._azure_header = {"Authorization" :self ._sas_token }
67
69
68
70
@staticmethod
@@ -76,27 +78,27 @@ def _parse_http_status(status_code, status_reason):
76
78
raise TypeError ("Error {0}: {1}" .format (status_code , status_reason ))
77
79
78
80
# Cloud-to-Device Messaging
79
- def get_hub_message (self , device_id ):
81
+ def get_hub_message (self ):
80
82
"""Returns a message from a Microsoft Azure IoT Hub (Cloud-to-Device), or -1
81
83
if the message queue is empty.
82
84
NOTE: HTTP Cloud-to-Device messages are throttled. Poll every 25+ minutes.
83
- :param int device_id: Device identifier.
84
85
"""
85
86
reject_message = True
86
87
# get a device-bound notification
87
88
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 )
89
91
try :
90
92
data = self ._get (path , is_c2d = True )
91
93
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 .' )
93
95
if data == 204 : # device's message queue is empty
94
96
return - 1
95
97
etag = data [1 ]['etag' ]
96
98
if etag : # either complete or nack the message
97
99
reject_message = False
98
100
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 )
100
102
if reject_message :
101
103
path_complete += '&reject'
102
104
del_status = self ._delete (path_complete )
@@ -105,62 +107,54 @@ def get_hub_message(self, device_id):
105
107
return - 1
106
108
107
109
# Device-to-Cloud Messaging
108
- def send_device_message (self , device_id , message ):
110
+ def send_device_message (self , message ):
109
111
"""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.
112
113
"""
113
114
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 )
115
116
self ._post (path , message , return_response = False )
116
117
117
118
# 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.
121
121
"""
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 )
123
124
return self ._get (path )
124
125
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.
128
128
:param str properties: Device Twin Properties
129
129
(https://docs.microsoft.com/en-us/rest/api/iothub/service/updatetwin#twinproperties)
130
130
"""
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 )
132
133
return self ._patch (path , properties )
133
134
134
- def replace_device_twin (self , device_id , properties ):
135
+ def replace_device_twin (self , properties ):
135
136
"""Replaces tags and desired properties of a device twin.
136
- :param str device_id: Device Identifier.
137
137
:param str properties: Device Twin Properties.
138
138
"""
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 )
140
141
return self ._put (path , properties )
141
142
142
143
# IoT Hub Service
143
144
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.
145
146
"""
146
147
path = "{0}/devices/?api-version={1}" .format (self ._iot_hub_url , AZ_API_VER )
147
148
return self ._get (path )
148
149
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 .
152
153
"""
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 )
154
156
return self ._get (path )
155
157
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
-
164
158
# HTTP Helper Methods
165
159
def _post (self , path , payload , return_response = True ):
166
160
"""HTTP POST
0 commit comments