comparison vanlogger.py @ 24:bacb798e6731

Remove giant code since the unit is dead. Add victron table (logging TBD)
author Daniel O'Connor <darius@dons.net.au>
date Wed, 25 Sep 2019 21:35:02 +0930
parents f77cc29d0b47
children 718b963b0dfa
comparison
equal deleted inserted replaced
13:3f22fa1f32d5 24:bacb798e6731
3 import sys 3 import sys
4 4
5 sys.path.append('/home/pi/logger') 5 sys.path.append('/home/pi/logger')
6 import datetime 6 import datetime
7 import epro.epro as epro 7 import epro.epro as epro
8 import giant.giant as giant 8 import json
9 import serial 9 import serial
10 import sqlite3 10 import sqlite3
11 import subprocess
12 import sys
11 13
12 def create(cur): 14 def create(cur):
13 cur.execute(''' 15 cur.execute('''
14 CREATE TABLE IF NOT EXISTS eprolog( 16 CREATE TABLE IF NOT EXISTS eprolog(
15 tstamp INTEGER NOT NULL, 17 tstamp INTEGER NOT NULL,
40 monitor_reset BOOLEAN NOT NULL 42 monitor_reset BOOLEAN NOT NULL
41 ); 43 );
42 ''') 44 ''')
43 45
44 cur.execute(''' 46 cur.execute('''
45 CREATE TABLE IF NOT EXISTS giantlog( 47 CREATE TABLE IF NOT EXISTS victron(
46 tstamp INTEGER NOT NULL, 48 tstamp INTEGER NOT NULL,
47 ac_act_power REAL NOT NULL, 49 ACIn_L1_volts REAL NOT NULL,
48 ac_app_power REAL NOT NULL, 50 ACIn_L1_freq REAL NOT NULL,
49 ac_frequency REAL NOT NULL, 51 ACIn_L1_curent REAL NOT NULL,
50 ac_volts REAL NOT NULL, 52 ACIn_active BOOLEAN NOT NULL,
51 batt_chr_curr REAL NOT NULL, 53 ACOut_L1_volts REAL NOT NULL,
52 batt_dis_curr REAL NOT NULL, 54 ACOut_L1_freq REAL NOT NULL,
53 battery_cap REAL NOT NULL, 55 ACOut_L1_curent REAL NOT NULL,
54 battery_volts REAL NOT NULL, 56 Battery_Voltage REAL NOT NULL,
55 batt_volt_ofs REAL NOT NULL, 57 Battery_Current REAL NOT NULL
56 bus_voltage REAL NOT NULL,
57 grid_frequency REAL NOT NULL,
58 grid_volts REAL NOT NULL,
59 hs_temperature REAL NOT NULL,
60 load_pct REAL NOT NULL,
61 pv1_chrg_pow REAL NOT NULL,
62 pv1_current REAL NOT NULL,
63 pv1_volts REAL NOT NULL,
64 scc1_volts REAL NOT NULL,
65 scc1_charging BOOLEAN NOT NULL,
66 switch BOOLEAN NOT NULL,
67 float_charge BOOLEAN NOT NULL,
68 ac_charging BOOLEAN NOT NULL,
69 sbu_prio BOOLEAN NOT NULL,
70 b_volt_steady BOOLEAN NOT NULL,
71 charging BOOLEAN NOT NULL
72 ); 58 );
73 ''') 59 ''')
60
61
74 62
75 def log_epro(p, cur): 63 def log_epro(p, cur):
76 # Check we have all the packets we need in the queue 64 # Check we have all the packets we need in the queue
77 msgtypes = set([x.msgtype for x in p.packets]) 65 msgtypes = set([x.msgtype for x in p.packets])
78 wantedtypes = set([ 66 wantedtypes = set([
132 break 120 break
133 121
134 row['tstamp'] = int(datetime.datetime.now().strftime('%s')) 122 row['tstamp'] = int(datetime.datetime.now().strftime('%s'))
135 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) 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)
136 124
137 def log_giant(gstat, cur):
138 row = {}
139 row['ac_act_power'] = gstat['ACActPower']
140 row['ac_app_power'] = gstat['ACAppPower']
141 row['ac_frequency'] = gstat['ACFreq']
142 row['ac_volts'] = gstat['ACVolts']
143 row['batt_chr_curr'] = gstat['BattChrCurr']
144 row['batt_dis_curr'] = gstat['BattDisCurr']
145 row['battery_cap'] = gstat['BattCap']
146 row['battery_volts'] = gstat['BattVolts']
147 row['batt_volt_ofs'] = gstat['BattVoltOfs']
148 row['bus_voltage'] = gstat['BusVolts']
149 row['grid_frequency'] = gstat['GridFreq']
150 row['grid_volts'] = gstat['GridVolts']
151 row['hs_temperature'] = gstat['HSTemp']
152 row['load_pct'] = gstat['LoadPct']
153 row['pv1_chrg_pow'] = gstat['PVChrgPow1']
154 row['pv1_current'] = gstat['PVCurr1']
155 row['pv1_volts'] = gstat['PVVolt1']
156 row['scc1_volts'] = gstat['SCC1Volt']
157 row['scc1_charging'] = gstat['Status']['SCC1Charging']
158 row['switch'] = gstat['Status']['Switch']
159 row['float_charge'] = gstat['Status']['FloatCharge']
160 if gstat['Status']['ChargeType'] == 'Both' or gstat['Status']['ChargeType'] == 'AC':
161 row['ac_charging'] = True
162 else:
163 row['ac_charging'] = False
164 row['sbu_prio'] = gstat['Status']['SBUPrio']
165 row['b_volt_steady'] = gstat['Status']['BattVoltSteady']
166 row['charging'] = gstat['Status']['Charging']
167
168 row['tstamp'] = int(datetime.datetime.now().strftime('%s'))
169 cur.execute('INSERT INTO giantlog VALUES(:tstamp, :ac_act_power, :ac_app_power, :ac_frequency, :ac_volts, :batt_chr_curr, :batt_dis_curr, :battery_cap, :battery_volts, :batt_volt_ofs, :bus_voltage, :grid_frequency, :grid_volts, :hs_temperature, :load_pct, :pv1_chrg_pow, :pv1_current, :pv1_volts, :scc1_volts, :scc1_charging, :switch, :float_charge, :ac_charging, :sbu_prio, :b_volt_steady, :charging)', row)
170
171 def main(): 125 def main():
126 print 'Started'
172 dbh = sqlite3.connect('/home/pi/vanlogger/log.db') 127 dbh = sqlite3.connect('/home/pi/vanlogger/log.db')
173 cur = dbh.cursor() 128 cur = dbh.cursor()
174 create(cur) 129 create(cur)
175 s = serial.Serial('/dev/ttyUSB0', 2400, parity='E') 130 #s = serial.Serial('/dev/ttyS0', 2400, parity='E')
131 s = serial.Serial('/dev/ttyS0', 2400)
176 s.timeout = 0.2 132 s.timeout = 0.2
177 133
178 p = epro.Processor() 134 p = epro.Processor()
179 ips = giant.GiantIPS()
180 135
181 then = None 136 then = None
137 lasteprolog = datetime.datetime.now()
182 while True: 138 while True:
139 if datetime.datetime.now() - lasteprolog > datetime.timedelta(hours = 1):
140 print('Stale ePro data')
141 sys.exit(1)
183 dolog = False 142 dolog = False
184 if then == None or datetime.datetime.now() - then > datetime.timedelta(seconds = 60): 143 if then == None or datetime.datetime.now() - then > datetime.timedelta(seconds = 60):
185 dolog = True 144 dolog = True
186 then = datetime.datetime.now() 145 then = datetime.datetime.now()
187 p.process(s.read(1024)) 146 p.process(s.read(1024))
188 if dolog: 147 if dolog:
148 lasteprolog = datetime.datetime.now()
189 log_epro(p, cur) 149 log_epro(p, cur)
190 dbh.commit() 150 dbh.commit()
191 151
192 gstat = None
193 try:
194 gstat = ips.getStatus()
195 except:
196 pass
197 if gstat != None and dolog:
198 log_giant(gstat, cur)
199 dbh.commit()
200 #print(gstat)
201
202 if __name__ == '__main__': 152 if __name__ == '__main__':
203 main() 153 main()