annotate vanlogger.py @ 29:e86e839febca

Move epro logging into eprodbus.py Index tstamp on tables
author Daniel O'Connor <darius@dons.net.au>
date Mon, 06 Dec 2021 10:48:02 +1030
parents 1da02c79b458
children a9df202d14b7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #!/usr/bin/env python
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 import sys
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 import datetime
28
1da02c79b458 remove giant logging again due to merge screwup
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
6 import json
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 import serial
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 import sqlite3
28
1da02c79b458 remove giant logging again due to merge screwup
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
9 import subprocess
24
bacb798e6731 Remove giant code since the unit is dead.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
10 import sys
29
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
11 import victron
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12
29
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
13 # Actual epro logging moved to eprodbus.py
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 def create(cur):
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 cur.execute('''
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 CREATE TABLE IF NOT EXISTS eprolog(
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 tstamp INTEGER NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 main_voltage REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 aux_voltage REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 battery_curr REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 amp_hours REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 state_of_charge REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 time_remaining REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 battery_temp REAL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 auto_sync_volts BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 auto_sync_curr BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 e501 BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 alarm_test BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 light BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 display_test BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 temp_sensor BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 aux_hv BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 aux_lv BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 installer_lock BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 main_hv BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 main_lv BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 low_battery BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 battery_flat BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 battery_full BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 battery_charged BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 no_sync BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 monitor_reset BOOLEAN NOT NULL
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 );
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 ''')
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 cur.execute('''
28
1da02c79b458 remove giant logging again due to merge screwup
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
47 CREATE TABLE IF NOT EXISTS victron(
1da02c79b458 remove giant logging again due to merge screwup
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
48 tstamp INTEGER NOT NULL,
1da02c79b458 remove giant logging again due to merge screwup
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
49 ACIn_L1_volts REAL NOT NULL,
1da02c79b458 remove giant logging again due to merge screwup
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
50 ACIn_L1_freq REAL NOT NULL,
29
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
51 ACIn_L1_current REAL NOT NULL,
28
1da02c79b458 remove giant logging again due to merge screwup
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
52 ACIn_active BOOLEAN NOT NULL,
1da02c79b458 remove giant logging again due to merge screwup
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
53 ACOut_L1_volts REAL NOT NULL,
1da02c79b458 remove giant logging again due to merge screwup
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
54 ACOut_L1_freq REAL NOT NULL,
29
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
55 ACOut_L1_current REAL NOT NULL,
28
1da02c79b458 remove giant logging again due to merge screwup
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
56 Battery_Voltage REAL NOT NULL,
1da02c79b458 remove giant logging again due to merge screwup
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
57 Battery_Current REAL NOT NULL
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 );
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 ''')
29
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
60 cur.execute('CREATE INDEX IF NOT EXISTS victron_tstamp_index ON victron (tstamp);')
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
61 cur.execute('CREATE INDEX IF NOT EXISTS eprolog_tstamp_index ON eprolog (tstamp);')
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62
29
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
63 def log_victron(v, cur):
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
64 data = [int(datetime.datetime.now().strftime('%s')), ]
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
65 data.extend(v.get_data())
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
66 cur.execute('INSERT INTO victron (tstamp, ACIn_L1_volts, ACIn_L1_freq, ACIn_L1_current, ACIn_active, ACOut_L1_volts, ACOut_L1_freq, ACOut_L1_current, Battery_Voltage, Battery_Current) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', data)
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 def main():
24
bacb798e6731 Remove giant code since the unit is dead.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
69 print 'Started'
29
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
70 dbh = sqlite3.connect('/home/root/vanlogger/log.db')
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 cur = dbh.cursor()
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 create(cur)
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73
29
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
74 v = victron.Victron('com.victronenergy.vebus.ttyUSB1')
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 then = None
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 while True:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 dolog = False
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 if then == None or datetime.datetime.now() - then > datetime.timedelta(seconds = 60):
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 dolog = True
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 then = datetime.datetime.now()
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 if dolog:
29
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
83 log_victron(v, cur)
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 dbh.commit()
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 if __name__ == '__main__':
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 main()