annotate vanlogger.py @ 33:7125b13d0528

Hide complaints from mkdir
author Daniel O'Connor <darius@dons.net.au>
date Mon, 13 Dec 2021 23:04:12 +1030
parents a9df202d14b7
children 5b03de9fb20b
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
30
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
6 import dbus
28
1da02c79b458 remove giant logging again due to merge screwup
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
7 import json
30
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
8 import logging
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 import serial
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 import sqlite3
28
1da02c79b458 remove giant logging again due to merge screwup
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
11 import subprocess
24
bacb798e6731 Remove giant code since the unit is dead.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
12 import sys
30
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
13 import time
29
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
14 import victron
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15
30
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
16 logger = logging.getLogger('vanlogger')
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
17 logger.setLevel(logging.INFO)
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
18
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
19 formatter = logging.Formatter('%(message)s')
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
20
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
21 ch = logging.StreamHandler()
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
22 ch.setLevel(logging.INFO)
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
23 ch.setFormatter(formatter)
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
24 logger.addHandler(ch)
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
25
29
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
26 # 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
27 def create(cur):
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 cur.execute('''
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 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
30 tstamp INTEGER NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 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
32 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
33 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
34 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
35 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
36 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
37 battery_temp REAL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 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
39 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
40 e501 BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 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
42 light BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 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
44 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
45 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
46 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
47 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
48 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
49 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
50 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
51 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
52 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
53 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
54 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
55 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
56 );
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 ''')
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 cur.execute('''
28
1da02c79b458 remove giant logging again due to merge screwup
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
60 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
61 tstamp INTEGER NOT NULL,
1da02c79b458 remove giant logging again due to merge screwup
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
62 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
63 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
64 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
65 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
66 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
67 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
68 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
69 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
70 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
71 );
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 ''')
29
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
73 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
74 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
75
29
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
76 def log_victron(v, cur):
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
77 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
78 data.extend(v.get_data())
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
79 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
80
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 def main():
30
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
82 logger.error('Started')
29
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
83 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
84 cur = dbh.cursor()
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 create(cur)
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86
29
e86e839febca Move epro logging into eprodbus.py
Daniel O'Connor <darius@dons.net.au>
parents: 28
diff changeset
87 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
88
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 then = None
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 while True:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 dolog = False
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 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
93 dolog = True
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94 then = datetime.datetime.now()
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 if dolog:
30
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
96 try:
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
97 log_victron(v, cur)
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
98 except (AttributeError, dbus.exceptions.DBusException) as e:
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
99 # We get various errors during startup so just log and keep going
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
100 logger.error('Error getting data: %s', str(e))
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101
30
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
102 dbh.commit()
a9df202d14b7 Use logging infra.
Daniel O'Connor <darius@dons.net.au>
parents: 29
diff changeset
103 time.sleep(30)
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 if __name__ == '__main__':
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 main()