-
Notifications
You must be signed in to change notification settings - Fork 5
/
DeviceClient.py
186 lines (142 loc) · 5.62 KB
/
DeviceClient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import hmac
import base64
import urllib.parse
import urllib.request
import time
class DeviceClient():
"""
Client for Azure IoT Hub REST API
https://msdn.microsoft.com/en-us/library/mt590785.aspx
"""
_API_VERSION = 'api-version=2016-11-14'
_HEADER_AUTHORIZATION = 'Authorization'
"""
iot_hub_name - name of your Azure IoT Hub
device_name - name of your device
key - security key for your device
"""
def __init__(self, iot_hub_name, device_name, key):
self._iot_hub_name = iot_hub_name
self._device_name = device_name
self._key = key
self._base_url = 'https://' + \
self._iot_hub_name + \
'.azure-devices.net/devices/' + \
self._device_name + \
'/messages/'
self._url_to_sign = self._iot_hub_name + \
'.azure-devices.net/devices/' + \
self._device_name
"""
Creates Shared Access Signature. Run before another funstions
timeout - expiration in seconds
"""
def create_sas(self, timeout):
urlToSign = urllib.parse.quote(self._url_to_sign, safe='')
# current time +10 minutes
timestamp = int(time.time()) + timeout
h = hmac.new(base64.b64decode(self._key),
msg = "{0}\n{1}".format(urlToSign, timestamp).encode('utf-8'),
digestmod = 'sha256')
self._sas = "SharedAccessSignature sr={0}&sig={1}&se={2}".format(urlToSign,
urllib.parse.quote(base64.b64encode(h.digest()), safe = ''),
timestamp)
return self._sas
"""
Sends message
message - message to be send
Returns HTTP response code. 204 is OK.
https://msdn.microsoft.com/en-us/library/mt590784.aspx
"""
def send(self, message):
headers = {
self._HEADER_AUTHORIZATION : self._sas,
'Content-Type' : 'application/json'
}
req = urllib.request.Request(self._base_url + 'events?' + self._API_VERSION,
message,
headers,
method = 'POST')
return_code = 0
with urllib.request.urlopen(req) as f:
return_code = f.code
return return_code
"""
Reads first message in queue
Returns:
message['headers'] - all response headers
message['etag'] - message id, you need this for complete, reject and abandon
message['body'] - message content
message['response_code'] - HTTP response code
https://msdn.microsoft.com/en-us/library/mt590786.aspx
"""
def read_message(self):
headers = {
self._HEADER_AUTHORIZATION : self._sas,
}
req = urllib.request.Request(self._base_url + 'devicebound?' + self._API_VERSION,
headers = headers,
method = 'GET')
message = {}
with urllib.request.urlopen(req) as f:
# Process headers
message['headers'] = f.info()
message['etag'] = ''
if f.info()['ETag'] != None:
message['etag'] = f.info()['ETag'].strip('"')
# message
message['body'] = f.read().decode('utf-8')
message['response_code'] = f.code
return message
"""
Completes a cloud-to-device message.
id - use message['etag'] from read_message function
Returns HTTP response code. 204 is OK.
https://msdn.microsoft.com/en-us/library/mt605155.aspx
"""
def complete_message(self, id):
headers = {
self._HEADER_AUTHORIZATION : self._sas,
}
req = urllib.request.Request(self._base_url + 'devicebound/' + id + '?' + self._API_VERSION,
headers = headers,
method = 'DELETE')
return_code = 0
with urllib.request.urlopen(req) as f:
return_code = f.code
return return_code
"""
Completes a cloud-to-device message.
id - use message['etag'] from read_message function
Returns HTTP response code. 204 is OK.
https://msdn.microsoft.com/en-us/library/mt590787.aspx
"""
def reject_message(self, id):
headers = {
self._HEADER_AUTHORIZATION : self._sas,
}
req = urllib.request.Request(self._base_url + 'devicebound/' + id + '?reject&' + self._API_VERSION,
headers = headers,
method = 'DELETE')
return_code = 0
with urllib.request.urlopen(req) as f:
return_code = f.code
return return_code
"""
Abandon a cloud-to-device message
id - use message['etag'] from read_message function
Returns HTTP response code. 204 is OK.
https://msdn.microsoft.com/en-us/library/mt590788.aspx
"""
def abandon_message(self, id):
headers = {
self._HEADER_AUTHORIZATION : self._sas,
}
req = urllib.request.Request(self._base_url + 'devicebound/' + id + '/abandon?' + self._API_VERSION,
'',
headers,
method = 'POST')
return_code = 0
with urllib.request.urlopen(req) as f:
return_code = f.code
return return_code