-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet.py
More file actions
65 lines (54 loc) · 1.9 KB
/
Get.py
File metadata and controls
65 lines (54 loc) · 1.9 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
"""
This code reads from the Thingful node and gets a list of all Sensors.
The code then reads through the list of sensors and counts the number
with a location of zero (at the moment assumes if x=0 y=0). Then displays
percentage that are zero locations.
Documentation for the GROW node is available at:
https://growobservatory.github.io/ThingfulNode/#tag/Locations
"""
import requests
import json
import sys
# ensure the key has been passed as argument
try:
api_key = sys.argv[1]
except:
sys.exit("The API key must be passed as argument when executing the script. Please run 'python Get.py <your-api-key>'.")
url = "https://grow.thingful.net/api/entity/locations/get"
headers = {"Authorization": "Bearer {0}".format(api_key)}
payload = json.dumps({'DataSourceCodes': ['Thingful.Connectors.GROWSensors'] })
response = requests.post(url, headers=headers, data=payload)
# parse json response if we can
try:
jResp = response.json()
except:
sys.exit("Unexpected Response. Status: {0}".format(response.status_code))
# exit if status code is not ok
if response.status_code != 200:
sys.exit("Unexpected response: {0}. Status: {1}. Message: {2}".format(response.reason, response.status_code, jResp['Exception']['Message']))
#print json.dumps(jResp,indent=4,sort_keys=True)
#for key in jResp.items():
# print key
# print
#print jResp["Locations"]
Locations = jResp["Locations"]
numSensor=len(Locations)
print("Number of Sensors : {0}".format(numSensor))
xCount=0
yCount=0
Count=0
for thing in Locations:
# print thing
th=Locations[thing]
x=th["X"]
y=th["Y"]
# print json.dumps(th,indent=4,sort_keys=True)
# print x,y
if (x==0):
xCount+=1
if (y==0):
yCount+=1
if ((x==0) and (y==0)):
Count+=1
Percent= float(xCount)/float(numSensor)*100
print("Blanks Total {0} | x-coord {1} | y-coord {2} | Percentage Blank {3}".format(Count, xCount, yCount, Percent))