-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevice_config.py
More file actions
84 lines (65 loc) · 2.07 KB
/
device_config.py
File metadata and controls
84 lines (65 loc) · 2.07 KB
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
import requests
import time
from craw import spider
def get_token_sys():
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
}
data = '{"username":"tenant@thingsboard.org", "password":"tenant"}'
response = requests.post('http://localhost:8080/api/auth/login', headers=headers, data=data)
t = str(response.content)
t = t.split('"')
token = str(t[3])
return token
def create_device(name):
token = 'Bearer ' + get_token_sys()
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Authorization': token,
}
data = '{ \n "additionalInfo": null, \n "createdTime": 0, \n "name": "' + str(
name) + '",\n "type": "default" \n }'
response = requests.post('http://localhost:8080/api/device', headers=headers, data=data)
t = str(response.content)
t = t.split('"')
device_id = t[9]
device_name = t[-8]
g = open("devices.txt", "a")
g.write(device_name + ",")
g.write(device_id + ",")
g.close()
print(device_id)
return str(device_id)
def get_credentials(device):
token = 'Bearer ' + get_token_sys()
headers = {
'Accept': 'application/json',
'X-Authorization': token,
}
response = requests.get('http://localhost:8080/api/device/' + str(device) + '/credentials', headers=headers)
t = str(response.content)
t = t.split('"')
credential = str(t[-4])
g = open("credentials.txt", "a")
g.write(credential + ",")
g.close
return credential
def create_base_devices():
list_code = spider()
credentials = []
time.sleep(2)
for i in range(5):
item = list_code[i]
print("Create device with id: {}".format(i))
device = create_device(item)
time.sleep(0.5)
print("Device created.")
time.sleep(0.5)
print("Geting credencials.")
cred = get_credentials(device)
credentials.append(cred)
print("Credencials of the device: {}".format(cred))
time.sleep(2)
return credentials