annotate agl.py @ 0:8d6ba11c1b76

Fetch & graphing code basically works.
author Daniel O'Connor <darius@dons.net.au>
date Fri, 08 Sep 2017 17:51:41 +0930
parents
children 6e3ca5bfda04
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #!/usr/bin/env python
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 import ConfigParser
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 import datetime
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 import exceptions
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 import json
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 import os
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 import pytz
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 import requests
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 import sqlite3
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 import sys
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 loginurl = 'https://command.aglsolar.com.au/api/v2/Account/LoginUser'
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 dataurl = 'https://command.aglsolar.com.au/api/v2/graph/b8e08afb-818f-4d2d-9d28-5afe8fc76a32'
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 # ?endDate=2017-08-23&granularity=Minute&metrics=read&startDate=2017-08-23&units=W'
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 logouturl = 'https://command.aglsolar.com.au/api/v2/Account/Logout'
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 class UTC(datetime.tzinfo):
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 def utcoffset(self, dt):
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 return datetime.timedelta(0)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 def dst(self, dt):
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 return datetime.timedelta(0)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 def tzname(self, dt):
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 return "UTC"
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 def main():
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 conf = ConfigParser.ConfigParser()
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 confname = os.environ['HOME'] + '/.agl.ini'
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 conf.read(confname)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 username = conf.get('DEFAULT', 'username')
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 password = conf.get('DEFAULT', 'password')
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 dbfn = conf.get('DEFAULT', 'db')
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 if conf.has_option('DEFAULT', 'token'):
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 token = conf.get('DEFAULT', 'token')
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 else:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 token = gettoken(username, password)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 conf.set('DEFAULT', 'token', token)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 conf.write(file(confname, 'w'))
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 if len(sys.argv) > 1:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 date = sys.argv[1]
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 else:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 date = datetime.datetime.now().strftime('%Y-%m-%d')
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 dbh = sqlite3.connect(dbfn, detect_types = sqlite3.PARSE_DECLTYPES)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 cur = dbh.cursor()
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 data = getdata(token, date, date)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 if data == None:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 token = gettoken(username, password)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 data = getdata(token, date, date)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 if data == None:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 print('Unable to fetch data')
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 updatedb(cur, data)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 dbh.commit()
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 def mkdb(cur):
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 cur.execute('''
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 CREATE TABLE IF NOT EXISTS agl (
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 t_stamp TIMESTAMP PRIMARY KEY,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 battery_charge NUMBER,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 battery_power NUMBER,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 power_consumed NUMBER,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 power_expected NUMBER,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 power_exported NUMBER,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 power_generated NUMBER,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 power_imported NUMBER,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 estimated_savings NUMBER,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 pv_forecast NUMBER,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 pv_gen_battery NUMBER,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 pv_gen_grid NUMBER,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 pv_gen_site NUMBER,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 site_cons_battery NUMBER,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 site_cons_grid NUMBER,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 site_cons_pv NUMBER
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 )''')
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 units = {
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 'battery_charge' : '%',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 'battery_power' : 'Watt',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 'power_consumed' : 'Watt',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 'power_expected' : 'Watt',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 'power_exported' : 'Watt',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 'power_generated' : 'Watt',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88 'power_imported' : 'Watt',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 'estimated_savings' : '$',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 'pv_forecast' : 'Watt',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 'pv_gen_battery' : 'Watt',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 'pv_gen_grid' : 'Watt',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 'pv_gen_site' : 'Watt',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94 'site_cons_battery' : 'Watt',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 'site_cons_grid' : 'Watt',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 'site_cons_pv' : 'Watt'
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 }
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 def graph(cur, cols, start, end):
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 import numpy
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 import matplotlib
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 import matplotlib.dates
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 import matplotlib.pylab
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 #matplotlib.rcParams['timezone'] = pytz.timezone('Australia/Adelaide')
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 colourlist = ['b','g','r','c','m','y','k']
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 yaxisunits1 = None
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 yaxisunits2 = None
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 ax1lines = []
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 ax2lines = []
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 colouridx = 0
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 for col in cols:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 unit = units[col]
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 if yaxisunits1 == None:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 yaxisunits1 = unit
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 if yaxisunits2 == None:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 if unit != yaxisunits1:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118 yaxisunits2 = unit
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 else:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 if unit != yaxisunits1 and unit != yaxisunits2:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 raise exceptions.Exception('Asked to graph >2 different units')
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 cur.execute('SELECT t_stamp, ' + reduce(lambda a, b: a + ', ' + b, cols) + ' FROM agl WHERE t_stamp > ? AND t_stamp < ? ORDER BY t_stamp',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 (start, end))
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125 ary = numpy.array(cur.fetchall())
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126 for idx in range(len(cols)):
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 if units[cols[idx]] == yaxisunits1:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 ax1lines.append([ary[:,0], ary[:,idx + 1], cols[idx], colourlist[colouridx]])
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 else:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 ax2lines.append([ary[:,0], ary[:,idx + 1], cols[idx], colourlist[colouridx]])
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131 colouridx += 1
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 fig = matplotlib.pylab.figure()
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134 ax1 = fig.add_subplot(111)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 ax1.set_ylabel(yaxisunits1)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137 for line in ax1lines:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 ax1.plot(line[0], line[1], label = line[2])
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
140 ax1.legend(loc = 'upper left')
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
141
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
142 if yaxisunits2 != None:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143 ax2 = ax1.twinx()
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 ax2.set_ylabel(yaxisunits2)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 for line in ax2lines:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 ax2.plot(line[0], line[1], label = line[2], color = line[3])
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148 ax2.legend(loc = 'upper right')
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 # Rotate X axis labels
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 for ax in fig.get_axes():
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152 ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%H:%M'))
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153 ax.xaxis.set_major_locator(matplotlib.dates.HourLocator(interval = 2))
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 ax.xaxis.set_minor_locator(matplotlib.dates.MinuteLocator(interval = 5))
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155 for label in ax.get_xticklabels():
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 label.set_ha('right')
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157 label.set_rotation(30)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
159 # Fudge margins to give more graph and less space
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 fig.subplots_adjust(left = 0.10, right = 0.88, top = 0.95, bottom = 0.15)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
161 matplotlib.pyplot.show()
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 def updatedb(cur, data):
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 mkdb(cur)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
165 for d in data['reads']['data']:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
166 ts = datetime.datetime.strptime(d['t_stamp'], '%Y-%m-%dT%H:%M:%SZ')
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
167 # Note we rename *energy* to *power* here to match what it actually means
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168 vals = [ts, d['battery_charge'], d['battery_energy'], d['energy_consumed'], d['energy_expected'], d['energy_exported'], d['energy_generated'],
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
169 d['energy_imported'], d['estimated_savings'], d['pv_forecast'], d['pv_generation']['battery_energy'],
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
170 d['pv_generation']['grid_energy'], d['pv_generation']['site_energy'], d['site_consumption']['battery_energy'],
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
171 d['site_consumption']['grid_energy'], d['site_consumption']['pv_energy']]
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172 skip = True
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173 for v in vals[1:]:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 if v != None:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
175 skip = False
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
176 break
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
177 if skip:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178 print('Skipping empty record at ' + str(ts))
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179 continue
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
180 cur.execute('INSERT OR IGNORE INTO agl(t_stamp, battery_charge, battery_power, power_consumed, power_expected, power_exported, power_generated, power_imported, estimated_savings, pv_forecast, pv_gen_battery, pv_gen_grid, pv_gen_site, site_cons_battery, site_cons_grid, site_cons_pv) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', vals)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182 def gettoken(username, password):
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183 authblob = json.encoder.JSONEncoder().encode({'email' : username, 'password' : password})
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
184 reply = requests.request('POST', loginurl, data = authblob, headers = {'Content-Type' : 'application/json'})
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185 if reply.status_code != 200:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186 return None
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
187 return json.decoder.JSONDecoder().decode(reply.content)['access_token']
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
189 def getdata(token, startdate, enddate):
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190 reply = requests.request('GET', dataurl, params = {
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191 'startDate' : startdate,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192 'endDate' : enddate,
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
193 'granularity' : 'Minute',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194 'metrics' : 'read',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
195 'units' : 'W',
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196 }, headers = { 'Authorization' : 'Bearer ' + token})
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
198 if reply.status_code != 200:
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
199 return None
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201 return json.decoder.JSONDecoder().decode(reply.content)
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
203 def logout(token):
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
204 reply = requests.request('GET', logouturl, headers = { 'Authorization' : 'Bearer ' + token})
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
205 return reply.status_code == 200
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
206
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
207 if __name__ == '__main__':
8d6ba11c1b76 Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
208 main()