annotate vanlogger.py @ 23:efe1954da8ca

merge
author Daniel O'Connor <darius@dons.net.au>
date Sun, 22 Jul 2018 12:56:29 +0930
parents 65be58857ed4 59cf68be7feb
children 718b963b0dfa
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 sys.path.append('/home/pi/logger')
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 import datetime
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 import epro.epro as epro
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 import giant.giant as giant
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
22
59cf68be7feb add trivial README
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
11 import sys
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 def create(cur):
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 cur.execute('''
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 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
16 tstamp INTEGER NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 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
18 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
19 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
20 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
21 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
22 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
23 battery_temp REAL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 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
25 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
26 e501 BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 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
28 light BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 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
30 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
31 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
32 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
33 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
34 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
35 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
36 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
37 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
38 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
39 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
40 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
41 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
42 );
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 cur.execute('''
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 CREATE TABLE IF NOT EXISTS giantlog(
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 tstamp INTEGER NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 ac_act_power REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 ac_app_power REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 ac_frequency REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 ac_volts REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 batt_chr_curr REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 batt_dis_curr REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 battery_cap REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 battery_volts REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 batt_volt_ofs REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 bus_voltage REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 grid_frequency REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 grid_volts REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 hs_temperature REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 load_pct REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 pv1_chrg_pow REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 pv1_current REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 pv1_volts REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 scc1_volts REAL NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 scc1_charging BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 switch BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 float_charge BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 ac_charging BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 sbu_prio BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 b_volt_steady BOOLEAN NOT NULL,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 charging BOOLEAN NOT NULL
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 );
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 ''')
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 def log_epro(p, cur):
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 # Check we have all the packets we need in the queue
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 msgtypes = set([x.msgtype for x in p.packets])
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 wantedtypes = set([
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 epro.MainVoltage.MSGTYPE,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 epro.AmpHours.MSGTYPE,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 epro.BatteryCurrent.MSGTYPE,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 epro.StateOfCharge.MSGTYPE,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 epro.TimeRemaining.MSGTYPE,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 epro.BatteryTemperature.MSGTYPE,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 epro.MonitorStatus.MSGTYPE,
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 epro.AuxVoltage.MSGTYPE,
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 if msgtypes < wantedtypes:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 return
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 row = {}
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 usedtypes = set()
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94 while len(p.packets) > 0:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 pkt = p.packets.pop() # Read latest packets first
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 if pkt.msgtype == epro.MainVoltage.MSGTYPE:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 row['main_voltage'] = pkt.volts
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 elif pkt.msgtype == epro.AmpHours.MSGTYPE:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 row['amp_hours'] = pkt.amphrs
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 elif pkt.msgtype == epro.BatteryCurrent.MSGTYPE:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 row['battery_curr'] = pkt.amps
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 elif pkt.msgtype == epro.StateOfCharge.MSGTYPE:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 row['state_of_charge'] = pkt.soc
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 elif pkt.msgtype == epro.TimeRemaining.MSGTYPE:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 row['time_remaining'] = pkt.time
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 elif pkt.msgtype == epro.BatteryTemperature.MSGTYPE:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 row['battery_temp'] = pkt.temp
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 elif pkt.msgtype == epro.MonitorStatus.MSGTYPE:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 row['auto_sync_volts'] = pkt.autosyncvolt
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 row['auto_sync_curr'] = pkt.autosyncamp
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 row['e501'] = pkt.e501compat
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 row['alarm_test'] = pkt.alarmtst
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 row['light'] = pkt.backlight
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 row['display_test'] = pkt.disptst
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 row['temp_sensor'] = pkt.tempsense
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 row['aux_hv'] = pkt.auxhv
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 row['aux_lv'] = pkt.auxlv
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118 row['installer_lock'] = pkt.lock
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 row['main_hv'] = pkt.mainhv
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 row['main_lv'] = pkt.mainlv
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 row['low_battery'] = pkt.lowbatalarm
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 row['battery_flat'] = pkt.batflat
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 row['battery_full'] = pkt.batfull
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 row['battery_charged'] = pkt.charged
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125 row['no_sync'] = pkt.nosync
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126 row['monitor_reset'] = pkt.monreset
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 elif pkt.msgtype == epro.AuxVoltage.MSGTYPE:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 row['aux_voltage'] = pkt.volts
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 usedtypes.add(pkt.msgtype)
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131 if usedtypes >= wantedtypes:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 p.packets = []
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 break
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 row['tstamp'] = int(datetime.datetime.now().strftime('%s'))
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
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)
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 def log_giant(gstat, cur):
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 row = {}
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
140 row['ac_act_power'] = gstat['ACActPower']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
141 row['ac_app_power'] = gstat['ACAppPower']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
142 row['ac_frequency'] = gstat['ACFreq']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143 row['ac_volts'] = gstat['ACVolts']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 row['batt_chr_curr'] = gstat['BattChrCurr']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145 row['batt_dis_curr'] = gstat['BattDisCurr']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 row['battery_cap'] = gstat['BattCap']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 row['battery_volts'] = gstat['BattVolts']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148 row['batt_volt_ofs'] = gstat['BattVoltOfs']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149 row['bus_voltage'] = gstat['BusVolts']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 row['grid_frequency'] = gstat['GridFreq']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 row['grid_volts'] = gstat['GridVolts']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152 row['hs_temperature'] = gstat['HSTemp']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153 row['load_pct'] = gstat['LoadPct']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 row['pv1_chrg_pow'] = gstat['PVChrgPow1']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155 row['pv1_current'] = gstat['PVCurr1']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 row['pv1_volts'] = gstat['PVVolt1']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157 row['scc1_volts'] = gstat['SCC1Volt']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158 row['scc1_charging'] = gstat['Status']['SCC1Charging']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
159 row['switch'] = gstat['Status']['Switch']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 row['float_charge'] = gstat['Status']['FloatCharge']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
161 if gstat['Status']['ChargeType'] == 'Both' or gstat['Status']['ChargeType'] == 'AC':
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162 row['ac_charging'] = True
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 else:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 row['ac_charging'] = False
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
165 row['sbu_prio'] = gstat['Status']['SBUPrio']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
166 row['b_volt_steady'] = gstat['Status']['BattVoltSteady']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
167 row['charging'] = gstat['Status']['Charging']
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
169 row['tstamp'] = int(datetime.datetime.now().strftime('%s'))
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
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)
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
171
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172 def main():
22
59cf68be7feb add trivial README
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
173 print 'Started'
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 dbh = sqlite3.connect('/home/pi/vanlogger/log.db')
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
175 cur = dbh.cursor()
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
176 create(cur)
22
59cf68be7feb add trivial README
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
177 s = serial.Serial('/dev/ttyS0', 2400, parity='E')
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178 s.timeout = 0.2
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
180 p = epro.Processor()
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181 ips = giant.GiantIPS()
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183 then = None
22
59cf68be7feb add trivial README
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
184 lasteprolog = datetime.datetime.now()
59cf68be7feb add trivial README
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
185 lastgiantlog = datetime.datetime.now()
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186 while True:
22
59cf68be7feb add trivial README
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
187 if datetime.datetime.now() - lasteprolog > datetime.timedelta(hours = 1):
59cf68be7feb add trivial README
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
188 print('Stale ePro data')
59cf68be7feb add trivial README
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
189 sys.exit(1)
59cf68be7feb add trivial README
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
190 if datetime.datetime.now() - lastgiantlog > datetime.timedelta(hours = 1):
59cf68be7feb add trivial README
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
191 print('Stale Giant data')
59cf68be7feb add trivial README
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
192 sys.exit(1)
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
193 dolog = False
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194 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
195 dolog = True
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196 then = datetime.datetime.now()
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197 p.process(s.read(1024))
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
198 if dolog:
22
59cf68be7feb add trivial README
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
199 lasteprolog = datetime.datetime.now()
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200 log_epro(p, cur)
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201 dbh.commit()
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
203 gstat = None
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
204 try:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
205 gstat = ips.getStatus()
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
206 except:
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
207 pass
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
208 if gstat != None and dolog:
22
59cf68be7feb add trivial README
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
209 lastgiantlog = datetime.datetime.now()
0
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
210 log_giant(gstat, cur)
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
211 dbh.commit()
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
212 #print(gstat)
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
213
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
214 if __name__ == '__main__':
78148730d98d Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215 main()