annotate eprodbus.py @ 21:625064987b7c

Wrap gobject callbacks in exit_on_error to make more robust
author Daniel O'Connor <darius@dons.net.au>
date Thu, 09 Dec 2021 23:52:54 +1030
parents 8d48ca5294d3
children 02ba26698696
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #!/usr/bin/env python
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 # Read enerdrive ePro packets from serial port and update DBus with them
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 # Also logs to an sqlite3 DB every 60 seconds
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 import datetime
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 from dbus.mainloop.glib import DBusGMainLoop
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 import epro
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 import gobject
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 import logging
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 import os
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 import serial
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 import signal
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 import sqlite3
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 import sys
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'velib_python'))
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 from vedbus import VeDbusService
21
625064987b7c Wrap gobject callbacks in exit_on_error to make more robust
Daniel O'Connor <darius@dons.net.au>
parents: 20
diff changeset
19 from ve_utils import exit_on_error
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20
15
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
21 logger = logging.getLogger('eprodbus')
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
22 logger.setLevel(logging.INFO)
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
23
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
24 formatter = logging.Formatter('%(asctime)s - %(message)s')
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
25
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
26 #fh = logging.FileHandler('/var/log/eprodbus.log')
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
27 #fh.setLevel(logging.DEBUG)
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
28 #fh.setFormatter(formatter)
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
29
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
30 ch = logging.StreamHandler()
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
31 ch.setLevel(logging.INFO)
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
32 ch.setFormatter(formatter)
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
33
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
34 #logger.addHandler(fh)
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
35 logger.addHandler(ch)
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
36
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
37 logger.info(__file__ + " is starting up")
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38
16
3338e666256b Move DB path to top of the file with the other knobs.
Daniel O'Connor <darius@dons.net.au>
parents: 15
diff changeset
39 dbpath = '/home/root/vanlogger/log.db'
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 port = 'ttyepro'
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 servicename = 'com.victronenergy.battery.' + port
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 instance = 0
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 class eProUpdater:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 def __init__(self, dbusservice, s, dbh):
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 self.log_queue = []
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 self.dbusservice = dbusservice
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 self.p = epro.Processor()
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 self.s = s
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 self.dbh = dbh
21
625064987b7c Wrap gobject callbacks in exit_on_error to make more robust
Daniel O'Connor <darius@dons.net.au>
parents: 20
diff changeset
51 gobject.io_add_watch(s.fileno(), gobject.IO_IN, lambda fd, userdata: exit_on_error(self.read_serial, fd, userdata))
625064987b7c Wrap gobject callbacks in exit_on_error to make more robust
Daniel O'Connor <darius@dons.net.au>
parents: 20
diff changeset
52 gobject.timeout_add(60000, exit_on_error, self.log_epro)
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 def read_serial(self, fd, userdata):
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 try:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 data = self.s.read(1024)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 except Exception as e:
15
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
58 logger.error('Failed to read from serial port: %s', str(e))
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 return False
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60
15
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
61 logger.debug('Read %d bytes from serial port', len(data))
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 self.p.process(data)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 while len(self.p.packets) > 0:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 # Process oldest packets first
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 p = self.p.packets.pop(0)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 self.log_queue.append(p)
15
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
68 logger.debug('%s', str(p))
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 if type(p) == epro.StateOfCharge:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 self.dbusservice['/Soc'] = p.soc
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 elif type(p) == epro.MainVoltage:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 self.dbusservice['/Dc/0/Voltage'] = p.volts
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 elif type(p) == epro.BatteryCurrent:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 self.dbusservice['/Dc/0/Current'] = p.amps
19
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
75 elif type(p) == epro.BatteryTemperature:
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
76 self.dbusservice['/Dc/0/Temperature'] = p.temp
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 elif type(p) == epro.AmpHours:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 self.dbusservice['/ConsumedAmphours'] = p.amphrs
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 elif type(p) == epro.TimeRemaining:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 # ePro reports in minutes, Venus expects seconds
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 self.dbusservice['/TimeToGo'] = p.time * 60
19
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
82 elif type(p) == epro.AuxVoltage:
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
83 self.dbusservice['/Dc/1/Voltage'] = p.volts
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
84 elif type(p) == epro.MonitorStatus:
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
85 self.dbusservice['/Alarms/LowVoltage'] = 2 if p.mainlv else 0
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
86 self.dbusservice['/Alarms/HighVoltage'] = 2 if p.mainhv else 0
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
87 self.dbusservice['/Alarms/LowStarterVoltage'] = 2 if p.auxlv else 0
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
88 self.dbusservice['/Alarms/HighStarterVoltage'] = 2 if p.auxhv else 0
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
89 self.dbusservice['/Alarms/LowSoc'] = 2 if p.batflat else 0
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
90 self.dbusservice['/Settings/HasTemperature'] = 1 if p.tempsense else 0
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 return True
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 def log_epro(self):
15
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
94 logger.debug('Logging epro data')
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 # Check we have all the packets we need in the queue
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 msgtypes = set([x.msgtype for x in self.log_queue])
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 wantedtypes = set([
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 epro.MainVoltage.MSGTYPE,
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 epro.AmpHours.MSGTYPE,
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 epro.BatteryCurrent.MSGTYPE,
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 epro.StateOfCharge.MSGTYPE,
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 epro.TimeRemaining.MSGTYPE,
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 epro.BatteryTemperature.MSGTYPE,
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 epro.MonitorStatus.MSGTYPE,
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 epro.AuxVoltage.MSGTYPE,
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 ])
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 if msgtypes < wantedtypes:
15
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
108 logger.debug('Didn\'t get all packet types required to log')
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 return
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 row = {}
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 usedtypes = set()
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 while len(self.log_queue) > 0:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 pkt = self.log_queue.pop() # Read latest packets first
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 if pkt.msgtype == epro.MainVoltage.MSGTYPE:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 row['main_voltage'] = pkt.volts
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 elif pkt.msgtype == epro.AmpHours.MSGTYPE:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118 row['amp_hours'] = pkt.amphrs
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 elif pkt.msgtype == epro.BatteryCurrent.MSGTYPE:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 row['battery_curr'] = pkt.amps
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 elif pkt.msgtype == epro.StateOfCharge.MSGTYPE:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 row['state_of_charge'] = pkt.soc
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 elif pkt.msgtype == epro.TimeRemaining.MSGTYPE:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 row['time_remaining'] = pkt.time
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125 elif pkt.msgtype == epro.BatteryTemperature.MSGTYPE:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126 row['battery_temp'] = pkt.temp
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 elif pkt.msgtype == epro.MonitorStatus.MSGTYPE:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 row['auto_sync_volts'] = pkt.autosyncvolt
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 row['auto_sync_curr'] = pkt.autosyncamp
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 row['e501'] = pkt.e501compat
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131 row['alarm_test'] = pkt.alarmtst
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 row['light'] = pkt.backlight
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 row['display_test'] = pkt.disptst
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134 row['temp_sensor'] = pkt.tempsense
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 row['aux_hv'] = pkt.auxhv
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136 row['aux_lv'] = pkt.auxlv
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137 row['installer_lock'] = pkt.lock
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 row['main_hv'] = pkt.mainhv
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 row['main_lv'] = pkt.mainlv
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
140 row['low_battery'] = pkt.lowbatalarm
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
141 row['battery_flat'] = pkt.batflat
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
142 row['battery_full'] = pkt.batfull
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143 row['battery_charged'] = pkt.charged
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 row['no_sync'] = pkt.nosync
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145 row['monitor_reset'] = pkt.monreset
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 elif pkt.msgtype == epro.AuxVoltage.MSGTYPE:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 row['aux_voltage'] = pkt.volts
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149 usedtypes.add(pkt.msgtype)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 if usedtypes >= wantedtypes:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 self.log_queue = []
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152 break
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153
15
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
154 logger.info('Got all packets, logging')
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155 cur = self.dbh.cursor()
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 row['tstamp'] = int(datetime.datetime.now().strftime('%s'))
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157 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)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158 self.dbh.commit()
18
9c2e08444598 Make sure timer is called more than once.
Daniel O'Connor <darius@dons.net.au>
parents: 17
diff changeset
159 return True
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160
17
3067bb98b482 Fix signal handling by using the correct function signature.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
161 def doexit(signum, frame):
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162 sys.exit(1)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 def main():
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
165 # Add signal handler to exit, otherwise we have to press ctrl-c twice to quit
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
166 signal.signal(signal.SIGINT, doexit)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
167
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168 DBusGMainLoop(set_as_default = True)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
169
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
170 dbusservice = VeDbusService(servicename)
20
8d48ca5294d3 Use add_mandatory_paths rather than rolling our own.
Daniel O'Connor <darius@dons.net.au>
parents: 19
diff changeset
171 dbusservice.add_mandatory_paths(__file__, '1.0', 'Serial ' + port, instance, 0, 'Enerdrive ePro', '1.0', '1.0', 1)
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172 dbusservice.add_path('/Dc/0/Voltage', value = None)
19
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
173 dbusservice.add_path('/Dc/0/Temperature', value = None)
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 dbusservice.add_path('/Dc/0/Current', value = None)
19
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
175 dbusservice.add_path('/Dc/1/Voltage', value = None)
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
176 dbusservice.add_path('/Soc', value = None)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
177 dbusservice.add_path('/TimeToGo', value = None)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178 dbusservice.add_path('/ConsumedAmphours', value = None)
19
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
179 dbusservice.add_path('/Alarms/LowVoltage', value = 0)
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
180 dbusservice.add_path('/Alarms/HighVoltage', value = 0)
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
181 dbusservice.add_path('/Alarms/LowStarterVoltage', value = 0)
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
182 dbusservice.add_path('/Alarms/HighStarterVoltage', value = 0)
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
183 dbusservice.add_path('/Alarms/LowSoc', value = 0)
3a3dba401b4a Hook up more stuff..
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
184 dbusservice.add_path('/Settings/HasTemperature', value = 0)
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186 s = serial.Serial('/dev/' + port, 2400, parity = 'E')
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
187 s.timeout = 0.1
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188
16
3338e666256b Move DB path to top of the file with the other knobs.
Daniel O'Connor <darius@dons.net.au>
parents: 15
diff changeset
189 dbh = sqlite3.connect(dbpath)
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191 updater = eProUpdater(dbusservice, s, dbh)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192
15
08b61687b75f Use logger better
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
193 logger.info('Starting main loop')
9
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194 mainloop = gobject.MainLoop()
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
195 mainloop.run()
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197 if __name__ == '__main__':
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
198 main()