Mercurial > ~darius > hgwebdir.cgi > vanlogger
annotate vanlogger.py @ 20:d4356465dce1
Set limits for battery voltages to make graph comparison easier
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Tue, 27 Feb 2018 16:12:36 +1030 |
parents | f77cc29d0b47 |
children | 65be58857ed4 59cf68be7feb bacb798e6731 |
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 |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
11 |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
12 def create(cur): |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
13 cur.execute(''' |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
14 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
|
15 tstamp INTEGER NOT NULL, |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
16 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
|
17 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
|
18 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
|
19 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
|
20 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
|
21 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
|
22 battery_temp REAL, |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
23 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
|
24 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
|
25 e501 BOOLEAN NOT NULL, |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
26 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
|
27 light BOOLEAN NOT NULL, |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
28 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
|
29 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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 ); |
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 cur.execute(''' |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
45 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
|
46 tstamp INTEGER NOT NULL, |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 switch BOOLEAN NOT NULL, |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
67 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
|
68 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
|
69 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
|
70 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
|
71 charging BOOLEAN NOT NULL |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
72 ); |
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 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
|
76 # 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
|
77 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
|
78 wantedtypes = set([ |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
79 epro.MainVoltage.MSGTYPE, |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
80 epro.AmpHours.MSGTYPE, |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
81 epro.BatteryCurrent.MSGTYPE, |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
82 epro.StateOfCharge.MSGTYPE, |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
83 epro.TimeRemaining.MSGTYPE, |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
84 epro.BatteryTemperature.MSGTYPE, |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
85 epro.MonitorStatus.MSGTYPE, |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
86 epro.AuxVoltage.MSGTYPE, |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
87 ]) |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
88 if msgtypes < wantedtypes: |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
89 return |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
90 |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
91 row = {} |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
92 usedtypes = set() |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
93 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
|
94 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 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
|
103 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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 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
|
109 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
|
110 row['e501'] = pkt.e501compat |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
111 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
|
112 row['light'] = pkt.backlight |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
113 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 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
|
126 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
|
127 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
|
128 |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
129 usedtypes.add(pkt.msgtype) |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
130 if usedtypes >= wantedtypes: |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
131 p.packets = [] |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
132 break |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
133 |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
134 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
|
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) |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
136 |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
137 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
|
138 row = {} |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
139 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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 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
|
160 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
|
161 row['ac_charging'] = True |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
162 else: |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
163 row['ac_charging'] = False |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
164 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
|
165 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
|
166 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
|
167 |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
168 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
|
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) |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
170 |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
171 def main(): |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
172 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
|
173 cur = dbh.cursor() |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
174 create(cur) |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
175 s = serial.Serial('/dev/ttyUSB0', 2400, parity='E') |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
176 s.timeout = 0.2 |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
177 |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
178 p = epro.Processor() |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
179 ips = giant.GiantIPS() |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
180 |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
181 then = None |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
182 while True: |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
183 dolog = False |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
184 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
|
185 dolog = True |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
186 then = datetime.datetime.now() |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
187 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
|
188 if dolog: |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
189 log_epro(p, cur) |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
190 dbh.commit() |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
191 |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
192 gstat = None |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
193 try: |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
194 gstat = ips.getStatus() |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
195 except: |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
196 pass |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
197 if gstat != None and dolog: |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
198 log_giant(gstat, cur) |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
199 dbh.commit() |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
200 #print(gstat) |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
201 |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
202 if __name__ == '__main__': |
78148730d98d
Log ePro and Giant IPS status to sqlite DB
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
203 main() |