-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice-creator.py
More file actions
58 lines (51 loc) · 1.57 KB
/
Copy pathdevice-creator.py
File metadata and controls
58 lines (51 loc) · 1.57 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
"""
Datacake Python API Example on how to create device programmatically
"""
import requests
import time
# Replace with your API token
headers = {"Authorization": "Token YOURTOKENHERE"}
# Set device details here
number_of_sensors = 100
existing_product = "EXISTINGPRODUCTUUIDHERE"
workspace = "YOURWORKSPACEUUIDHERE"
serial_prefix = "level-"
name_prefix = "Fill Level Sensor "
# Function to call Datacake GraphQL query
def run_query(query):
request = requests.post('https://api.datacake.co/graphql/', json={'query': query}, headers=headers)
if request.status_code == 200:
return request.json()
else:
raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))
# Create devices
for i in range(1, number_of_sensors+1):
# Query for creating devices
query = """
mutation {{
createApiDevices(input:{{
workspace:"{workspace}",
plan:"free",
planCode:"CODE",
productKind:EXISTING,
existingProduct:"{product}",
devices:[
{{
name:"{name}",
serial:"{serial}"
}}
]
}}) {{
ok
devices {{
id
verboseName
serialNumber
}}
}}
}}
""".format(name=name_prefix+str(i), serial=serial_prefix+str(i), workspace=workspace, product=existing_product)
result = run_query(query) # Execute the query
print(result)
# Give Datacake API a bit of rest
time.sleep(0.25)