-
Notifications
You must be signed in to change notification settings - Fork 0
/
netpie.py
486 lines (397 loc) · 17.4 KB
/
netpie.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# Import libraries
from simple import MQTTClient, MQTTException
from network import WLAN
import json
import urequests
__version__ = '1.0.0'
__author__ = 'Teeraphat Kullanankanjana'
# Class for NETPIE-related exceptions
class NETPIEException(Exception):
pass
# Main class
class NETPIE:
def __init__(self):
# Default MQTT broker information
self.hostname = "mqtt.netpie.io"
self.port = 1883
# Topic formats for messages and shadows
self.msg_topic = "@msg/"
self.shadow_topic = "@shadow/"
# Initialize client attributes
self.client = None
self.client_id = None
self.token = None
self.secret = None
# Set the client profile with authentication details
def set_profile(self, client_id, token=None, secret=None):
"""
Set the client profile with authentication details.
Args:
client_id (str): The Client ID for authentication.
token (str, optional): The authentication token. Defaults to None.
secret (str, optional): The authentication secret. Defaults to None.
Raises:
NETPIEException: If input types are invalid.
"""
# Validate input types
if not isinstance(client_id, str):
raise NETPIEException("The Client ID must be a string.")
if token is not None and not isinstance(token, str):
raise NETPIEException("The token must be a string.")
if secret is not None and not isinstance(secret, str):
raise NETPIEException("The secret must be a string.")
# Set client attributes
self.client_id = client_id
self.token = token
self.secret = secret
self.client = MQTTClient(self.client_id, self.hostname, user=self.token, password=self.secret)
# Connect to the MQTT broker
def connect(self, clean_session=True):
"""
Connect to the NETPIE
Args:
clean_session (bool, optional): Whether to start with a clean session. Defaults to True.
Raises:
NETPIEException: If authentication details are missing or connection fails.
"""
# Check for authentication details
if self.client_id is None:
raise NETPIEException("The Client ID must not be None before connecting.")
if self.token is None:
raise NETPIEException("The token must not be None before connecting.")
if self.secret is None:
raise NETPIEException("The secret must not be None before connecting.")
try:
# Attempt MQTT connection
self.client.connect(clean_session=clean_session)
except MQTTException as e:
if e.errno == 5:
raise NETPIEException("[MQTT Error 5] Not authorized.\nPlease check your Client ID, token, and secret.")
except OSError as e:
if e.errno == -202:
if not WLAN().isconnected():
raise NETPIEException("[Error -202] Network Error.\nPlease check your internet or network connection.")
def disconnect(self):
"""Disconnect from the NETPIE."""
self.client.disconnect()
def ping(self):
"""Ping the NETPIE."""
self.client.ping()
# Publish data to a specified topic
def publish(self, topic, data):
"""
Publish data to a specified topic.
Args:
topic (str): The topic to publish the data to.
data: The data to be published.
Raises:
NETPIEException: If there's an error publishing the data.
"""
try:
# Publish data using the MQTT client
self.client.publish(topic, data)
except OSError as e:
if e.errno == 104:
raise NETPIEException("[Error 104] Connection reset.\nPlease ensure that your data publishing speed is not too fast.")
# Publish shadow data
def publishShadow(self, unformat_data):
"""
Publish the data to Shadow Topic.
Args:
unformat_data (dict): The data to be published.
Raises:
NETPIEException: If the data format is invalid.
"""
if not isinstance(unformat_data, dict):
raise NETPIEException("The data must be a dictionary, but its type is {}".format(type(unformat_data)))
# Wrap data in shadow format
formatted_data = {"data": unformat_data}
topic = self.shadow_topic + "data/update"
# Publish formatted data
self.publish(topic, json.dumps(formatted_data))
# Publish a message to a specified topic
def publishMessage(self, topic, unformat_message):
"""
Publish a message to a specified topic.
Args:
topic (str): The topic to publish the message to.
unformat_message (str): The message to be published.
Raises:
NETPIEException: If topic or message format is invalid.
"""
if not isinstance(topic, str):
raise NETPIEException("The topic must be a string.")
if not topic.startswith("@msg/"):
topic = "@msg/" + topic
if isinstance(unformat_message, str):
# Publish the message
self.publish(topic, unformat_message)
else:
raise NETPIEException("The data must be a string.")
# Set a callback function for incoming messages
def set_callback(self, callback):
"""
Set a callback function for incoming messages.
Args:
callback (function): The callback function to be set.
Raises:
NETPIEException: If the provided callback is not callable.
"""
if callable(callback):
self.client.set_callback(callback)
else:
raise NETPIEException("You must provide a valid callback function. Try removing () after the function name.")
# Check for incoming messages
def check_message(self):
"""Check for incoming messages."""
self.client.check_msg()
# Subscribe to a topic
def subscribe(self, topic, qos=0):
"""
Subscribe to a topic.
Args:
topic (str): The topic to subscribe to.
qos (int, optional): Quality of Service level. Defaults to 0.
Raises:
NETPIEException: If the topic format is invalid.
"""
if isinstance(topic, str):
self.client.subscribe(topic, qos)
else:
raise NETPIEException("The topic must be a string.")
def is_connected(self):
"""
Check if the client is connected.
Returns:
bool: True if connected, False otherwise.
"""
try:
self.client.ping()
return True
except:
return False
def get_device_status(self, client_id=None, token=None):
"""
Get the device status using the NETPIE REST API.
Args:
client_id (str, optional): The Client ID for authentication. Defaults to None.
token (str, optional): The authentication token. Defaults to None.
Returns:
tuple: A tuple containing the HTTP response code and device status data.
"""
if client_id is not None and not isinstance(client_id, str):
raise NETPIEException("The client_id must be a string.")
if token is not None and not isinstance(token, str):
raise NETPIEException("The token must be a string.")
if client_id is None:
client_id = self.client_id
if token is None:
token = self.token
endpoint = "https://api.netpie.io/v2/device/status"
headers = {
"Authorization": "Device {}:{}".format(client_id, token)}
response = urequests.get(endpoint, headers=headers)
response_text = response.text
response_code = response.status_code
response.close()
if response_code == 200:
try:
status_data = json.loads(response_text)
return response_code, status_data
except json.JSONDecodeError as e:
raise NETPIEException("Error decoding JSON response: {}".format(e))
else:
return response_code, response_text
def get_device_shadow(self,client_id=None, token=None):
"""
Get the device shadow data using the NETPIE REST API.
Args:
client_id (str, optional): The Client ID for authentication. Defaults to None.
token (str, optional): The authentication token. Defaults to None.
Returns:
tuple: A tuple containing the HTTP response code and device shadow data.
Raises:
NETPIEException: If client_id or token is not a string or if there's an error with the API request.
"""
if client_id is not None and not isinstance(client_id, str):
raise NETPIEException("The client_id must be a string.")
if token is not None and not isinstance(token, str):
raise NETPIEException("The token must be a string.")
if client_id is None:
client_id = self.client_id
if token is None:
token = self.token
endpoint = "https://api.netpie.io/v2/device/shadow/data"
headers = {
"Authorization": f"Device {client_id}:{token}"
}
response = urequests.get(endpoint, headers=headers)
response_text = response.text
response_code = response.status_code
response.close()
if response_code == 200:
try:
status_data = json.loads(response_text)
return response_code, status_data
except json.JSONDecodeError as e:
raise NETPIEException("Error decoding JSON response: {}".format(e))
else:
return response_code, response_text
def write_device_shadow(self, data, client_id=None, token=None):
"""
Write the provided data to the device shadow using the NETPIE REST API.
Args:
data (dict): The data to be written to the device shadow. Should be in the format:
{
"field name 1": value1,
"field name 2": value2,
...,
"field name n": value n
}
client_id (str, optional): The Client ID for authentication. Defaults to None.
token (str, optional): The authentication token. Defaults to None.
Returns:
tuple: A tuple containing the HTTP response text and response code.
Raises:
NETPIEException: If data is not a dictionary or if there's an error with the API request.
"""
if not isinstance(data, dict):
raise NETPIEException("The data must be a dictionary.")
# Check if data is already formatted as expected
if "data" not in data:
formatted_data = {"data": data}
else:
formatted_data = data
if client_id is None:
client_id = self.client_id
if token is None:
token = self.token
endpoint = "https://api.netpie.io/v2/device/shadow/data"
headers = {
"Authorization": f"Device {client_id}:{token}",
"Content-Type": "application/json"}
response = urequests.post(endpoint, headers=headers, json=formatted_data)
response_text = response.text
response_code = response.status_code
response.close()
if response_code == 200:
try:
status_data = json.loads(response_text)
return response_code, status_data
except json.JSONDecodeError as e:
raise NETPIEException("Error decoding JSON response: {}".format(e))
else:
return response_code, response_text
def merge_device_shadow(self, data, client_id=None, token=None):
"""
Merge the provided data into the device shadow using the NETPIE REST API.
Args:
data (dict): The data to be merged into the device shadow. Should be in the format:
{
"field name 1": value1,
"field name 2": value2,
...,
"field name n": value n
}
client_id (str, optional): The Client ID for authentication. Defaults to None.
token (str, optional): The authentication token. Defaults to None.
Returns:
tuple: A tuple containing the HTTP response text and response code.
Raises:
NETPIEException: If data is not a dictionary or if there's an error with the API request.
"""
if not isinstance(data, dict):
raise NETPIEException("The data must be a dictionary.")
# Check if data is already formatted as expected
if "data" not in data:
formatted_data = {"data": data}
else:
formatted_data = data
if client_id is None:
client_id = self.client_id
if token is None:
token = self.token
endpoint = "https://api.netpie.io/v2/device/shadow/data"
headers = {
"Authorization": f"Device {client_id}:{token}",
"Content-Type": "application/json"}
response = urequests.put(endpoint, headers=headers, json=formatted_data)
response_text = response.text
response_code = response.status_code
response.close()
if response_code == 200:
try:
status_data = json.loads(response_text)
return response_code, status_data
except json.JSONDecodeError as e:
raise NETPIEException("Error decoding JSON response: {}".format(e))
else:
return response_code, response_text
def publish_private_message(self,topic,msg,client_id=None, token=None):
"""
Publishes a message to NETPIE's Private topic using the NETPIE REST API.
For the publisher, do not include the @private prefix in your topic; it will be filled in automatically.
For the subscriber,subscribe with @private/your_topic_name to receive the data.
Args:
topic (str): The topic to publish the message to.
msg (str): The message to be published.
client_id (str, optional): The Client ID for authentication. Defaults to None.
token (str, optional): The authentication token. Defaults to None.
Returns:
tuple: A tuple containing the HTTP response text and response code.
Raises:
NETPIEException: If topic or message format is invalid.
"""
if not isinstance(topic, str):
raise NETPIEException("The topic must be a string.")
if not isinstance(msg, str):
raise NETPIEException("The message must be a string.")
if topic.startswith("@private/"):
raise NETPIEException("Do not include the '@private/' prefix in the topic.")
if client_id is None:
client_id = self.client_id
if token is None:
token = self.token
endpoint = "https://api.netpie.io/v2/device/private/{}".format(topic)
headers = {
"Authorization": f"Device {client_id}:{token}",
"Content-Type": "text/plain"}
response = urequests.put(endpoint, headers=headers, data=msg)
response_text = response.text
response_code = response.status_code
response.close()
return response_text,response_code
def publish_device_message(self,topic,msg,client_id=None,token=None):
"""
Publishes data to NEPIE's Message topic using the NETPIE REST API.
For the publisher, do not include the @msg prefix in your topic; it will be filled in automatically.
For the subscriber,subscribe with @msg/your_topic_name to receive the data.
Args:
topic (str): The topic to publish the message to.
msg (str): The message to be published.
client_id (str, optional): The Client ID for authentication. Defaults to None.
token (str, optional): The authentication token. Defaults to None.
Returns:
tuple: A tuple containing the HTTP response text and response code.
Raises:
NETPIEException: If topic or message format is invalid.
"""
if not isinstance(topic, str):
raise NETPIEException("The topic must be a string.")
if not isinstance(msg, str):
raise NETPIEException("The message must be a string.")
if topic.startswith("@msg/"):
raise NETPIEException("Do not include the '@msg/' prefix in the topic.")
if client_id is None:
client_id = self.client_id
if token is None:
token = self.token
endpoint = "https://api.netpie.io/v2/device/message/{}".format(topic)
headers = {
"Authorization": f"Device {client_id}:{token}",
"Content-Type": "text/plain"}
response = urequests.put(endpoint, headers=headers, data=msg)
response_text = response.text
response_code = response.status_code
response.close()
return response_text,response_code