comparison vanlogger.py @ 28:1da02c79b458

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