annotate pw2log.py @ 3:ec3c8b63d8e7

Collect frequency correctly...
author Daniel O'Connor <darius@dons.net.au>
date Tue, 09 Feb 2021 22:10:25 +1030
parents 393a879db9eb
children 8990981c60a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #!/usr/bin/env python3
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 import configparser
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 import daemon
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 import daemon.pidfile
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 import datetime
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 import logging
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 from logging.handlers import RotatingFileHandler
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 import psycopg2
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 import requests
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 import sys
2
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
12 # https://github.com/jrester/tesla_powerwall
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
13 from tesla_powerwall import APIError, Powerwall
0
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 import time
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 # Standard in 3.7..
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 class NullContextManager(object):
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 def __init__(self, dummy_resource=None):
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 self.dummy_resource = dummy_resource
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 def __enter__(self):
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 return self.dummy_resource
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 def __exit__(self, *args):
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 pass
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 # Otherwise it's very noisy
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 logging.getLogger('tesla_powerwall').setLevel(logging.WARN)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 def main():
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 if len(sys.argv) != 2:
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 print('Bad usage', file = sys.stderr)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 print('\t%s conf.ini' % (sys.argv[0]), file = sys.stderr)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 sys.exit(1)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 cp = configparser.ConfigParser()
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 cp.read(sys.argv[1])
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 if not cp.has_section('db'):
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 print('Config file missing db section', file = sys.stderr)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 sys.exit(1)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 if not cp.has_option('db', 'dsn'):
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 print('db section missing dsn parameter', file = sys.stderr)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 sys.exit(1)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 if not cp.has_option('db', 'logtime'):
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 print('db section missing logtime parameter', file = sys.stderr)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 sys.exit(1)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 if not cp.has_section('pw'):
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 print('Config file missing pw section', file = sys.stderr)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 sys.exit(1)
2
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
49 for opt in ('ip', 'username', 'password'):
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
50 if not cp.has_option('pw', opt):
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
51 print('pw section missing %s parameter' % (opt,), file = sys.stderr)
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
52 sys.exit(1)
0
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 if cp.has_option('pw2log', 'logfile'):
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 logfile = cp.get('pw2log', 'logfile')
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 else:
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 logfile = None
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 if cp.has_option('pw2log', 'pidfile'):
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 pidfile = cp.get('pw2log', 'pidfile')
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 else:
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 pidfile = None
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 global logger
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 logger = logging.getLogger('pw2log')
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 logger.setLevel(logging.WARN)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 fmt = logging.Formatter('%(asctime)s: %(message)s', datefmt = '%Y/%m/%d %H:%M:%S')
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 if logfile == None:
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 ch = logging.StreamHandler()
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 ch.setFormatter(fmt)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 logger.addHandler(ch)
1
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
71 keepfh = None
0
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 else:
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 fh = RotatingFileHandler(logfile, maxBytes = 2000, backupCount = 10)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 fh.setFormatter(fmt)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 logger.addHandler(fh)
1
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
76 keepfhs = [fh.stream.fileno()] # XXX: gross
0
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 if pidfile == None:
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 ctx = NullContextManager()
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 else:
1
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
81 try:
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
82 #fh = open('/tmp/pw2errs.log', 'a')
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
83 fh = None
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
84 ctx = daemon.DaemonContext(pidfile = daemon.pidfile.PIDLockFile(pidfile), stdout = fh, stderr = fh, files_preserve = keepfhs)
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
85 except Exception as e:
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
86 logger.critical('Unable to get daemon context')
0
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87
1
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
88 try:
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
89 with ctx:
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
90 logger.critical('Starting')
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
91 try:
2
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
92 collectdata(cp.get('pw', 'ip'), cp.get('pw', 'username'), cp.get('pw', 'password'), cp.get('db', 'dsn'), cp.getint('db', 'logtime'))
1
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
93 except Exception as e:
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
94 logger.critical('Unable to collect data: ' + str(e))
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
95 except Exception as e:
7edf54ec37f2 Improve logging.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
96 logger.critical('Unable to enter daemon context: ' + str(e))
0
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97
2
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
98 def collectdata(pwip, username, password, dsn, logtime):
0
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 dbh = psycopg2.connect(dsn)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 cur = dbh.cursor()
2
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
101 pw = None
0
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 while True:
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 try:
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 # As per.. https://github.com/vloschiavo/powerwall2
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 # | | Load | Grid | Battery | Solar |
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 # |==========+==============+===================+======================+==================|
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 # | Positive | Supply house | Drawing from grid | Drawing from battery | Solar generation |
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 # | Negative | n/a | Feeding grid | Charging battery | n/a |
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 #
2
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
111 if not pw:
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
112 first = True
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
113 pw = Powerwall(pwip)
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
114 pw.login(username, password)
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
115 pw.detect_and_pin_version()
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
116 meters = pw.get_meters()
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
117 grid_volts = meters.site.avarage_voltage
3
ec3c8b63d8e7 Collect frequency correctly...
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
118 grid_freq = meters.site.frequency
2
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
119 grid_power = meters.site.instant_power
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
120 load_power = meters.load.instant_power
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
121 battery_power = meters.battery.instant_power
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
122 solar_power = meters.solar.instant_power
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
123 charge = pw.get_charge()
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
124 except APIError as e:
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
125 pw = None
0
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126 logger.error('Error communicating with Powerwall: ' + str(e))
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 time.sleep(300)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 continue
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 try:
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 cur.execute('INSERT INTO pw2 (date, grid_voltage, grid_freq, grid_power, load_power, battery_power, battery_charge, solar_power) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)',
2
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
131 (datetime.datetime.now(), grid_volts, grid_freq, grid_power, load_power, battery_power, charge, solar_power))
0
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 dbh.commit()
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 except psycopg2.OperationalError as e:
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134 logger.error('Reconnecting after database error:' + str(e))
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 time.sleep(60)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136 dbh = psycopg2.connect(dsn)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137 cur = dbh.cursor()
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 continue
2
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
139 if first:
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
140 logger.error('Logged OK')
393a879db9eb Update to latest tesla_powerwall code changes.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
141 first = False
0
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
142
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143 time.sleep(logtime)
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145 def createdb(dbh):
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 cur = dbh.cursor()
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 cur.execute('''
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148 CREATE TABLE pw2 (
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149 date TIMESTAMP WITH TIME ZONE PRIMARY KEY,
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 grid_voltage REAL,
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 grid_freq REAL,
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152 grid_power REAL,
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153 load_power REAL,
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 battery_power REAL,
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155 battery_charge REAL,
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 solar_power REAL
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157 );
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158 ''')
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
159 cur.execute('''
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 CREATE INDEX IF NOT EXISTS pw2_date_brin_idx ON pw2 USING brin (date);
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
161 ''')
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 if __name__ == '__main__':
a5a196b3ba63 Initial version of powerwall logger
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 main()