-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgiotto_helper.py
More file actions
63 lines (57 loc) · 2.28 KB
/
giotto_helper.py
File metadata and controls
63 lines (57 loc) · 2.28 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
import requests
'''
This is the GioTTO helper file which has the basic api calls
to the Building Depot/ GioTTO
'''
import time
import json
import time
import calendar
from json_setting import JsonSetting
class GiottoHelper:
def __init__(self, settingFilePath="./config.json"):
setting = JsonSetting(settingFilePath)
self.giotto_rest_api = setting.get('giotto_rest_api')
self.oauth = setting.get('oauth')
self.tag=setting.get('tag')
def getOauthToken(self):
headers = {'content-type': 'application/json'}
url = self.giotto_rest_api['server']
url += ':' + self.giotto_rest_api['oauth_port']
url += '/oauth/access_token/client_id='
url += self.oauth['id']
url += '/client_secret='
url += self.oauth['key']
result = requests.get(url, headers=headers)
if result.status_code == 200:
dic = result.json()
return dic['access_token']
else:
return ''
def sensorlist(self):
url = self.giotto_rest_api['server']
access_token=self.getOauthToken()
header = {"Authorization": "Bearer " + access_token, 'content-type':'application/json'}
url_sensor_list = url+":"+self.giotto_rest_api['oauth_port']+"/api/search"
data={"data":{"Tags":[self.tag]}}
response = requests.post(url_sensor_list,data=json.dumps(data),headers = header).json()
return response
def get_timeseries_data(self,sensor,mins):
url = self.giotto_rest_api['server']
sensorUUID = sensor
OauthToken = self.getOauthToken()
header = {"Authorization": "bearer " + OauthToken, 'content-type':'application/json'}
end_time = int(time.time()-86400)
#print end_time
start_time = int(end_time-(60)*mins)
url1 = url+":"+self.giotto_rest_api['port']+"/api/sensor/%s/timeseries?start_time=%s&end_time=%s" % (sensorUUID, start_time,end_time)
#print url1
response = requests.get(url1, headers = header)
#print response.text
if response.status_code == 200:
return response.json()
else:
return 'in get_timeseries_data in giotto_helper.py:: \\\
Please check the error corresponding to '+response.status_code
if __name__ == "__main__":
giotto_helper = GiottoHelper()