Mercurial > ~darius > hgwebdir.cgi > vanlogger
comparison 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 |
comparison
equal
deleted
inserted
replaced
28:1da02c79b458 | 29:e86e839febca |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import sys | 3 import sys |
4 | 4 |
5 sys.path.append('/home/pi/logger') | |
6 import datetime | 5 import datetime |
7 import epro.epro as epro | |
8 import json | 6 import json |
9 import serial | 7 import serial |
10 import sqlite3 | 8 import sqlite3 |
11 import subprocess | 9 import subprocess |
12 import sys | 10 import sys |
11 import victron | |
13 | 12 |
13 # Actual epro logging moved to eprodbus.py | |
14 def create(cur): | 14 def create(cur): |
15 cur.execute(''' | 15 cur.execute(''' |
16 CREATE TABLE IF NOT EXISTS eprolog( | 16 CREATE TABLE IF NOT EXISTS eprolog( |
17 tstamp INTEGER NOT NULL, | 17 tstamp INTEGER NOT NULL, |
18 main_voltage REAL NOT NULL, | 18 main_voltage REAL NOT NULL, |
46 cur.execute(''' | 46 cur.execute(''' |
47 CREATE TABLE IF NOT EXISTS victron( | 47 CREATE TABLE IF NOT EXISTS victron( |
48 tstamp INTEGER NOT NULL, | 48 tstamp INTEGER NOT NULL, |
49 ACIn_L1_volts REAL NOT NULL, | 49 ACIn_L1_volts REAL NOT NULL, |
50 ACIn_L1_freq REAL NOT NULL, | 50 ACIn_L1_freq REAL NOT NULL, |
51 ACIn_L1_curent REAL NOT NULL, | 51 ACIn_L1_current REAL NOT NULL, |
52 ACIn_active BOOLEAN NOT NULL, | 52 ACIn_active BOOLEAN NOT NULL, |
53 ACOut_L1_volts REAL NOT NULL, | 53 ACOut_L1_volts REAL NOT NULL, |
54 ACOut_L1_freq REAL NOT NULL, | 54 ACOut_L1_freq REAL NOT NULL, |
55 ACOut_L1_curent REAL NOT NULL, | 55 ACOut_L1_current REAL NOT NULL, |
56 Battery_Voltage REAL NOT NULL, | 56 Battery_Voltage REAL NOT NULL, |
57 Battery_Current REAL NOT NULL | 57 Battery_Current REAL NOT NULL |
58 ); | 58 ); |
59 ''') | 59 ''') |
60 | 60 cur.execute('CREATE INDEX IF NOT EXISTS victron_tstamp_index ON victron (tstamp);') |
61 cur.execute('CREATE INDEX IF NOT EXISTS eprolog_tstamp_index ON eprolog (tstamp);') | |
61 | 62 |
62 | 63 def log_victron(v, cur): |
63 def log_epro(p, cur): | 64 data = [int(datetime.datetime.now().strftime('%s')), ] |
64 # Check we have all the packets we need in the queue | 65 data.extend(v.get_data()) |
65 msgtypes = set([x.msgtype for x in p.packets]) | 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) |
66 wantedtypes = set([ | |
67 epro.MainVoltage.MSGTYPE, | |
68 epro.AmpHours.MSGTYPE, | |
69 epro.BatteryCurrent.MSGTYPE, | |
70 epro.StateOfCharge.MSGTYPE, | |
71 epro.TimeRemaining.MSGTYPE, | |
72 epro.BatteryTemperature.MSGTYPE, | |
73 epro.MonitorStatus.MSGTYPE, | |
74 epro.AuxVoltage.MSGTYPE, | |
75 ]) | |
76 if msgtypes < wantedtypes: | |
77 return | |
78 | |
79 row = {} | |
80 usedtypes = set() | |
81 while len(p.packets) > 0: | |
82 pkt = p.packets.pop() # Read latest packets first | |
83 if pkt.msgtype == epro.MainVoltage.MSGTYPE: | |
84 row['main_voltage'] = pkt.volts | |
85 elif pkt.msgtype == epro.AmpHours.MSGTYPE: | |
86 row['amp_hours'] = pkt.amphrs | |
87 elif pkt.msgtype == epro.BatteryCurrent.MSGTYPE: | |
88 row['battery_curr'] = pkt.amps | |
89 elif pkt.msgtype == epro.StateOfCharge.MSGTYPE: | |
90 row['state_of_charge'] = pkt.soc | |
91 elif pkt.msgtype == epro.TimeRemaining.MSGTYPE: | |
92 row['time_remaining'] = pkt.time | |
93 elif pkt.msgtype == epro.BatteryTemperature.MSGTYPE: | |
94 row['battery_temp'] = pkt.temp | |
95 elif pkt.msgtype == epro.MonitorStatus.MSGTYPE: | |
96 row['auto_sync_volts'] = pkt.autosyncvolt | |
97 row['auto_sync_curr'] = pkt.autosyncamp | |
98 row['e501'] = pkt.e501compat | |
99 row['alarm_test'] = pkt.alarmtst | |
100 row['light'] = pkt.backlight | |
101 row['display_test'] = pkt.disptst | |
102 row['temp_sensor'] = pkt.tempsense | |
103 row['aux_hv'] = pkt.auxhv | |
104 row['aux_lv'] = pkt.auxlv | |
105 row['installer_lock'] = pkt.lock | |
106 row['main_hv'] = pkt.mainhv | |
107 row['main_lv'] = pkt.mainlv | |
108 row['low_battery'] = pkt.lowbatalarm | |
109 row['battery_flat'] = pkt.batflat | |
110 row['battery_full'] = pkt.batfull | |
111 row['battery_charged'] = pkt.charged | |
112 row['no_sync'] = pkt.nosync | |
113 row['monitor_reset'] = pkt.monreset | |
114 elif pkt.msgtype == epro.AuxVoltage.MSGTYPE: | |
115 row['aux_voltage'] = pkt.volts | |
116 | |
117 usedtypes.add(pkt.msgtype) | |
118 if usedtypes >= wantedtypes: | |
119 p.packets = [] | |
120 break | |
121 | |
122 row['tstamp'] = int(datetime.datetime.now().strftime('%s')) | |
123 cur.execute('INSERT INTO eprolog VALUES (:tstamp, :main_voltage, :aux_voltage, :battery_curr, :amp_hours, :state_of_charge, :time_remaining, :battery_temp, :auto_sync_volts, :auto_sync_curr, :e501, :alarm_test, :light, :display_test, :temp_sensor, :aux_hv, :aux_lv, :installer_lock, :main_hv, :main_lv, :low_battery, :battery_flat, :battery_full, :battery_charged, :no_sync, :monitor_reset)', row) | |
124 | 67 |
125 def main(): | 68 def main(): |
126 print 'Started' | 69 print 'Started' |
127 dbh = sqlite3.connect('/home/pi/vanlogger/log.db') | 70 dbh = sqlite3.connect('/home/root/vanlogger/log.db') |
128 cur = dbh.cursor() | 71 cur = dbh.cursor() |
129 create(cur) | 72 create(cur) |
130 #s = serial.Serial('/dev/ttyS0', 2400, parity='E') | |
131 s = serial.Serial('/dev/ttyS0', 2400) | |
132 s.timeout = 0.2 | |
133 | 73 |
134 p = epro.Processor() | 74 v = victron.Victron('com.victronenergy.vebus.ttyUSB1') |
135 | 75 |
136 then = None | 76 then = None |
137 lasteprolog = datetime.datetime.now() | |
138 while True: | 77 while True: |
139 if datetime.datetime.now() - lasteprolog > datetime.timedelta(hours = 1): | |
140 print('Stale ePro data') | |
141 sys.exit(1) | |
142 dolog = False | 78 dolog = False |
143 if then == None or datetime.datetime.now() - then > datetime.timedelta(seconds = 60): | 79 if then == None or datetime.datetime.now() - then > datetime.timedelta(seconds = 60): |
144 dolog = True | 80 dolog = True |
145 then = datetime.datetime.now() | 81 then = datetime.datetime.now() |
146 p.process(s.read(1024)) | |
147 if dolog: | 82 if dolog: |
148 lasteprolog = datetime.datetime.now() | 83 log_victron(v, cur) |
149 log_epro(p, cur) | |
150 dbh.commit() | 84 dbh.commit() |
151 | 85 |
152 if __name__ == '__main__': | 86 if __name__ == '__main__': |
153 main() | 87 main() |