-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetDataFromApiSensors.py
63 lines (44 loc) · 2.25 KB
/
getDataFromApiSensors.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
import requests
from requests.auth import HTTPDigestAuth
import json
from datetime import datetime
dataDict = {}
def getData(date, startTime, endTime) :
url = "http://apicapteur.westeurope.cloudapp.azure.com:8080/SensorThingsService/v1.0/Observations"
fil = "?$top=5000000&$filter=phenomenonTime ge "+date+"T"+startTime+":00.000Z and phenomenonTime le "+date+"T"+endTime+":00.000Z&$orderBy=phenomenonTime"
#2018-06-20T08:41:00.000Z and phenomenonTime le 2018-06-20T08:44:00.000Z "
url += fil
#myResponse = requests.get(url,auth=HTTPDigestAuth(raw_input("username: "), raw_input("Password: ")), verify=True)
print url
myResponse = requests.get(url)
hasNext = True
# For successful API call, response code will be 200 (OK)
while(hasNext) :
if (myResponse.ok) :
jData = json.loads(myResponse.content)
for key in jData["value"]:
observationId = key["result"]["subject"]["id"]
time = key["phenomenonTime"]
gps = key["result"]["location"]["geometry"]["coordinates"]
if observationId not in dataDict.keys() :
myResponseSensor = requests.get(key["[email protected]"])
jDataSensor = json.loads(myResponseSensor.content)
sensor = jDataSensor["name"]
dataDict[observationId] = {}
dataDict[observationId]["sensor"] = sensor
dataDict[observationId]["trajectory"] = []
dataDict[observationId]["trajectory"].append( [datetime.strptime(time,"%Y-%m-%dT%H:%M:%S.%fZ" ), [gps[0], gps[1]] ])
if "@iot.nextLink" in jData :
myResponse = requests.get(jData["@iot.nextLink" ])
else :
hasNext = False
else:
# If response code is not ok (200), print the resulting http error code with description
myResponse.raise_for_status()
return dataDict
def main():
dataDict = getData()
return dataDict
if __name__ == "__main__":
# Someone is launching this directly
main()