Mercurial > ~darius > hgwebdir.cgi > agl
annotate agl.py @ 4:deb3adc4e086
Add graph saving option.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Mon, 11 Sep 2017 12:32:05 +0930 |
parents | 525a66486282 |
children | b42baa411817 |
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 |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
3 import argparse |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
4 import ConfigParser |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
5 import datetime |
1
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
6 import dateutil |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
7 import exceptions |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
8 import json |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
9 import os |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
10 import requests |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
11 import sqlite3 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
12 import sys |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
13 import tzlocal |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
14 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
15 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
|
16 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
|
17 # ?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
|
18 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
|
19 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
20 class UTC(datetime.tzinfo): |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
21 def utcoffset(self, dt): |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
22 return datetime.timedelta(0) |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
23 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
24 def dst(self, dt): |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
25 return datetime.timedelta(0) |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
26 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
27 def tzname(self, dt): |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
28 return "UTC" |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
29 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
30 |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
31 def valid_date(s): |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
32 try: |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
33 return datetime.datetime.strptime(s, "%Y-%m-%d") |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
34 except ValueError: |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
35 raise argparse.ArgumentTypeError("Not a valid date: '{0}'.".format(s)) |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
36 |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
37 def main(): |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
38 parser = argparse.ArgumentParser() |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
39 parser.add_argument('-u', '--update', help = 'Update data', action="store_true") |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
40 parser.add_argument('-g', '--graph', help = 'Produce graph', action="store_true") |
4
deb3adc4e086
Add graph saving option.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
41 parser.add_argument('-f', '--filename', help = 'Filename to save graph as', type = str) |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
42 parser.add_argument('-s', '--start', help = 'Start date for graph (YYYY-MM-DD)', type = valid_date) |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
43 parser.add_argument('-e', '--end', help = 'End date for graph (YYYY-MM-DD)', type = valid_date) |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
44 |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
45 args = parser.parse_args() |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
46 |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
47 conf = ConfigParser.ConfigParser() |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
48 confname = os.environ['HOME'] + '/.agl.ini' |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
49 conf.read(confname) |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
50 username = conf.get('DEFAULT', 'username') |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
51 password = conf.get('DEFAULT', 'password') |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
52 dbfn = conf.get('DEFAULT', 'db') |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
53 |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
54 if (args.start is None) ^ (args.end is None): |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
55 parser.error('Must specify start and end or neither') |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
56 |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
57 if not args.update and not args.graph: |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
58 parser.error('Nothing to do') |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
59 |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
60 start = args.start |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
61 if start is None: |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
62 start = datetime.date.today() |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
63 start = datetime.datetime(start.year, start.month, start.day) |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
64 |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
65 end = args.end |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
66 if end is None: |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
67 end = start + datetime.timedelta(days = 1) |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
68 end = datetime.datetime(end.year, end.month, end.day) |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
69 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
70 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
|
71 cur = dbh.cursor() |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
72 if args.update: |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
73 date = start |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
74 while date < end: |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
75 if conf.has_option('DEFAULT', 'token'): |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
76 token = conf.get('DEFAULT', 'token') |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
77 else: |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
78 token = gettoken(username, password) |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
79 conf.set('DEFAULT', 'token', token) |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
80 conf.write(file(confname, 'w')) |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
81 |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
82 data = getdata(token, date, date) |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
83 if data == None: |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
84 #print('Getting new token') |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
85 token = gettoken(username, password) |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
86 data = getdata(token, date, date) |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
87 if data == None: |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
88 print('Unable to fetch data') |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
89 updatedb(cur, data) |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
90 dbh.commit() |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
91 date += datetime.timedelta(days = 1) |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
92 |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
93 if args.graph: |
4
deb3adc4e086
Add graph saving option.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
94 graph(args.filename, cur, ['battery_charge', 'power_imported', 'power_exported', 'power_consumed', 'power_generated'], start, end) |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
95 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
96 def mkdb(cur): |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
97 cur.execute(''' |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
98 CREATE TABLE IF NOT EXISTS agl ( |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
99 t_stamp TIMESTAMP PRIMARY KEY, |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
100 battery_charge NUMBER, |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
101 battery_power NUMBER, |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
102 power_consumed NUMBER, |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
103 power_expected NUMBER, |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
104 power_exported NUMBER, |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
105 power_generated NUMBER, |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
106 power_imported NUMBER, |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
107 estimated_savings NUMBER, |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
108 pv_forecast NUMBER, |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
109 pv_gen_battery NUMBER, |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
110 pv_gen_grid NUMBER, |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
111 pv_gen_site NUMBER, |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
112 site_cons_battery NUMBER, |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
113 site_cons_grid NUMBER, |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
114 site_cons_pv NUMBER |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
115 )''') |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
116 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
117 units = { |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
118 'battery_charge' : '%', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
119 'battery_power' : 'Watt', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
120 'power_consumed' : 'Watt', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
121 'power_expected' : 'Watt', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
122 'power_exported' : 'Watt', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
123 'power_generated' : 'Watt', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
124 'power_imported' : 'Watt', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
125 'estimated_savings' : '$', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
126 'pv_forecast' : 'Watt', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
127 'pv_gen_battery' : 'Watt', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
128 'pv_gen_grid' : 'Watt', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
129 'pv_gen_site' : 'Watt', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
130 'site_cons_battery' : 'Watt', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
131 'site_cons_grid' : 'Watt', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
132 'site_cons_pv' : 'Watt' |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
133 } |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
134 |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
135 names = { |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
136 'battery_charge' : 'Battery Charge', |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
137 'battery_power' : 'Battery Power', |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
138 'power_consumed' : 'Power Consumed', |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
139 'power_expected' : 'Power Expected', |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
140 'power_exported' : 'Power Expected', |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
141 'power_generated' : 'Power Generated', |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
142 'power_imported' : 'Power Imported', |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
143 'estimated_savings' : 'Estimated Savings', |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
144 'pv_forecast' : 'PV Forecast', |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
145 'pv_gen_battery' : 'PV Generation Battery', |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
146 'pv_gen_grid' : 'PV Generation Grid', |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
147 'pv_gen_site' : 'PV Generation Site', |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
148 'site_cons_battery' : 'Site Consumption Batter', |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
149 'site_cons_grid' : 'Site Consumption Grid', |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
150 'site_cons_pv' : 'Site Consumption PV' |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
151 } |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
152 |
4
deb3adc4e086
Add graph saving option.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
153 def graph(fname, cur, cols, start, end): |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
154 import numpy |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
155 import matplotlib |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
156 import matplotlib.dates |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
157 |
1
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
158 colourlist = ['b','g','r','c','m','y','k'] |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
159 |
1
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
160 # Work out what axes we are using |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
161 yaxisunits1 = None |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
162 yaxisunits2 = None |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
163 ax1lines = [] |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
164 ax2lines = [] |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
165 colouridx = 0 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
166 for col in cols: |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
167 unit = units[col] |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
168 if yaxisunits1 == None: |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
169 yaxisunits1 = unit |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
170 if yaxisunits2 == None: |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
171 if unit != yaxisunits1: |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
172 yaxisunits2 = unit |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
173 else: |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
174 if unit != yaxisunits1 and unit != yaxisunits2: |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
175 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
|
176 |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
177 ltname = tzlocal.get_localzone().zone # Why is this so hard.. |
1
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
178 lt = dateutil.tz.gettz(ltname) |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
179 utc = dateutil.tz.gettz('UTC') |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
180 matplotlib.rcParams['timezone'] = ltname |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
181 |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
182 if start.tzinfo == None: |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
183 start = start.replace(tzinfo = lt) |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
184 if end.tzinfo == None: |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
185 end = end.replace(tzinfo = lt) |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
186 |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
187 start = start.astimezone(utc) |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
188 end = end.astimezone(utc) |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
189 |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
190 # Actually get the data |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
191 colstr = reduce(lambda a, b: a + ', ' + b, cols) |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
192 # Data is stored as naive datetime's which are in UTC so convert the requested time here |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
193 cur.execute('SELECT t_stamp, ' + colstr + ' FROM agl WHERE t_stamp > ? AND t_stamp < ? ORDER BY t_stamp', |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
194 (start, end)) |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
195 ary = numpy.array(cur.fetchall()) |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
196 if ary.shape[0] == 0: |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
197 print('No data') |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
198 return |
1
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
199 # Convert naive UTC to proper UTC then adjust to local time |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
200 xdata = map(lambda f: f.replace(tzinfo = utc).astimezone(lt), ary[:,0]) |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
201 for idx in range(len(cols)): |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
202 if units[cols[idx]] == yaxisunits1: |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
203 ax1lines.append([xdata, ary[:,idx + 1], names[cols[idx]], colourlist[colouridx]]) |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
204 else: |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
205 ax2lines.append([xdata, ary[:,idx + 1], names[cols[idx]], colourlist[colouridx]]) |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
206 colouridx += 1 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
207 |
4
deb3adc4e086
Add graph saving option.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
208 if fname == None: |
deb3adc4e086
Add graph saving option.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
209 import matplotlib.pylab |
deb3adc4e086
Add graph saving option.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
210 fig = matplotlib.pylab.figure() |
deb3adc4e086
Add graph saving option.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
211 else: |
deb3adc4e086
Add graph saving option.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
212 import matplotlib.backends.backend_agg |
deb3adc4e086
Add graph saving option.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
213 fig = matplotlib.figure.Figure(figsize = (8, 6), dpi = 75) |
deb3adc4e086
Add graph saving option.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
214 |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
215 ax1 = fig.add_subplot(111) |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
216 ax1.set_ylabel(yaxisunits1) |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
217 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
218 for line in ax1lines: |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
219 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
|
220 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
221 ax1.legend(loc = 'upper left') |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
222 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
223 if yaxisunits2 != None: |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
224 ax2 = ax1.twinx() |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
225 ax2.set_ylabel(yaxisunits2) |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
226 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
227 for line in ax2lines: |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
228 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
|
229 ax2.legend(loc = 'upper right') |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
230 |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
231 ndays = int(max(1, round(((end - start).total_seconds()) / 86400))) |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
232 for ax in fig.get_axes(): |
1
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
233 ax.set_xlim([start, end]) |
3
525a66486282
Fix mouse hover formatting.
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
234 ax.format_xdata = lambda d: matplotlib.dates.num2date(d).strftime('%d %b %H:%M') |
1
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
235 ax.xaxis.grid(True) |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
236 ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%d %b\n%H:%M')) |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
237 ax.xaxis.set_major_locator(matplotlib.dates.HourLocator(interval = 2 * ndays)) |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
238 ax.xaxis.set_minor_locator(matplotlib.dates.MinuteLocator(interval = 5 * ndays)) |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
239 for label in ax.get_xticklabels(): |
1
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
240 label.set_ha('center') |
6e3ca5bfda04
Improve plotting/timezone stuff.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
241 label.set_rotation(90) |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
242 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
243 # 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
|
244 fig.subplots_adjust(left = 0.10, right = 0.88, top = 0.95, bottom = 0.15) |
4
deb3adc4e086
Add graph saving option.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
245 if fname == None: |
deb3adc4e086
Add graph saving option.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
246 matplotlib.pyplot.show() |
deb3adc4e086
Add graph saving option.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
247 else: |
deb3adc4e086
Add graph saving option.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
248 canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig) # Sets canvas in fig too |
deb3adc4e086
Add graph saving option.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
249 fig.savefig(fname) |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
250 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
251 def updatedb(cur, data): |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
252 mkdb(cur) |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
253 for d in data['reads']['data']: |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
254 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
|
255 # 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
|
256 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
|
257 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
|
258 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
|
259 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
|
260 skip = True |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
261 for v in vals[1:]: |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
262 if v != None: |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
263 skip = False |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
264 break |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
265 if skip: |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
266 print('Skipping empty record at ' + str(ts)) |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
267 continue |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
268 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
|
269 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
270 def gettoken(username, password): |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
271 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
|
272 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
|
273 if reply.status_code != 200: |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
274 return None |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
275 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
|
276 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
277 def getdata(token, startdate, enddate): |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
278 #print('getting ' + startdate.strftime('%Y-%m-%d')) |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
279 reply = requests.request('GET', dataurl, params = { |
2
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
280 'startDate' : startdate.strftime('%Y-%m-%d'), |
2b7fb26f9114
- Make an actual usable command line program.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
281 'endDate' : enddate.strftime('%Y-%m-%d'), |
0
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
282 'granularity' : 'Minute', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
283 'metrics' : 'read', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
284 'units' : 'W', |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
285 }, headers = { 'Authorization' : 'Bearer ' + token}) |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
286 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
287 if reply.status_code != 200: |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
288 return None |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
289 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
290 return json.decoder.JSONDecoder().decode(reply.content) |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
291 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
292 def logout(token): |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
293 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
|
294 return reply.status_code == 200 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
295 |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
296 if __name__ == '__main__': |
8d6ba11c1b76
Fetch & graphing code basically works.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
297 main() |