Mercurial > ~darius > hgwebdir.cgi > agl
comparison agl.py @ 19:1d08f777533f default tip
Add installid parameter instead of hard coding it into a URL.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Tue, 03 Jul 2018 13:26:28 +0930 |
parents | 156ab071a9de |
children |
comparison
equal
deleted
inserted
replaced
18:156ab071a9de | 19:1d08f777533f |
---|---|
11 import sqlite3 | 11 import sqlite3 |
12 import sys | 12 import sys |
13 import tzlocal | 13 import tzlocal |
14 | 14 |
15 loginurl = 'https://command.aglsolar.com.au/api/v2/Account/LoginUser' | 15 loginurl = 'https://command.aglsolar.com.au/api/v2/Account/LoginUser' |
16 dataurl = 'https://command.aglsolar.com.au/api/v2/graph/b8e08afb-818f-4d2d-9d28-5afe8fc76a32' | 16 dataurl = 'https://command.aglsolar.com.au/api/v2/graph/' |
17 # ?endDate=2017-08-23&granularity=Minute&metrics=read&startDate=2017-08-23&units=W' | 17 # ?endDate=2017-08-23&granularity=Minute&metrics=read&startDate=2017-08-23&units=W' |
18 logouturl = 'https://command.aglsolar.com.au/api/v2/Account/Logout' | 18 logouturl = 'https://command.aglsolar.com.au/api/v2/Account/Logout' |
19 | 19 |
20 def valid_date(s): | 20 def valid_date(s): |
21 try: | 21 try: |
64 parser.error('Start must be before end') | 64 parser.error('Start must be before end') |
65 | 65 |
66 dbh = sqlite3.connect(dbfn, detect_types = sqlite3.PARSE_DECLTYPES) | 66 dbh = sqlite3.connect(dbfn, detect_types = sqlite3.PARSE_DECLTYPES) |
67 cur = dbh.cursor() | 67 cur = dbh.cursor() |
68 if args.update: | 68 if args.update: |
69 installid = conf.get('DEFAULT', 'installid') | |
69 date = start | 70 date = start |
70 while date < end: | 71 while date < end: |
71 if conf.has_option('DEFAULT', 'token'): | 72 if conf.has_option('DEFAULT', 'token'): |
72 token = conf.get('DEFAULT', 'token') | 73 token = conf.get('DEFAULT', 'token') |
73 else: | 74 else: |
74 token = gettoken(username, password) | 75 token = gettoken(username, password) |
75 conf.set('DEFAULT', 'token', token) | 76 conf.set('DEFAULT', 'token', token) |
76 conf.write(file(confname, 'w')) | 77 conf.write(file(confname, 'w')) |
77 | 78 |
78 data = getdata(token, date, date) | 79 data = getdata(token, installid, date, date) |
79 if data == None: | 80 if data == None: |
80 #print('Getting new token') | 81 #print('Getting new token') |
81 token = gettoken(username, password) | 82 token = gettoken(username, password) |
82 data = getdata(token, date, date) | 83 data = getdata(token, installid, date, date) |
83 if data == None: | 84 if data == None: |
84 print('Unable to fetch data') | 85 print('Unable to fetch data') |
85 updatedb(cur, data) | 86 updatedb(cur, data) |
86 dbh.commit() | 87 dbh.commit() |
87 date += datetime.timedelta(days = 1) | 88 date += datetime.timedelta(days = 1) |
216 xdeltas = xhours[1:] - xhours[0:-1] | 217 xdeltas = xhours[1:] - xhours[0:-1] |
217 calcd = {} | 218 calcd = {} |
218 for idx in range(len(cols)): | 219 for idx in range(len(cols)): |
219 col = cols[idx] | 220 col = cols[idx] |
220 ydata = ary[:,idx + 1] | 221 ydata = ary[:,idx + 1] |
222 | |
223 ydata = [0 if v is None else v for v in ydata] | |
221 if col in convs: | 224 if col in convs: |
222 ydata = convs[col](ydata) | 225 ydata = map(convs[col], ydata) |
223 if col in scale_limits: | 226 if col in scale_limits: |
224 scale_min = scale_limits[col][0] | 227 scale_min = scale_limits[col][0] |
225 scale_max = scale_limits[col][1] | 228 scale_max = scale_limits[col][1] |
226 else: | 229 else: |
227 scale_min = None | 230 scale_min = None |
326 reply = requests.request('POST', loginurl, data = authblob, headers = {'Content-Type' : 'application/json'}) | 329 reply = requests.request('POST', loginurl, data = authblob, headers = {'Content-Type' : 'application/json'}) |
327 if reply.status_code != 200: | 330 if reply.status_code != 200: |
328 return None | 331 return None |
329 return json.decoder.JSONDecoder().decode(reply.content)['access_token'] | 332 return json.decoder.JSONDecoder().decode(reply.content)['access_token'] |
330 | 333 |
331 def getdata(token, startdate, enddate): | 334 def getdata(token, installid, startdate, enddate): |
332 #print('getting ' + startdate.strftime('%Y-%m-%d')) | 335 #print('getting ' + startdate.strftime('%Y-%m-%d')) |
333 reply = requests.request('GET', dataurl, params = { | 336 reply = requests.request('GET', dataurl + installid, params = { |
334 'startDate' : startdate.strftime('%Y-%m-%d'), | 337 'startDate' : startdate.strftime('%Y-%m-%d'), |
335 'endDate' : enddate.strftime('%Y-%m-%d'), | 338 'endDate' : enddate.strftime('%Y-%m-%d'), |
336 'granularity' : 'Minute', | 339 'granularity' : 'Minute', |
337 'metrics' : 'read', | 340 'metrics' : 'read', |
338 'units' : 'W', | 341 'units' : 'W', |