annotate eprodbus.py @ 9:446cfe74827b

Add program to report epro status to DBus for Venus tools. Move sqlite3 logging to here as well
author Daniel O'Connor <darius@dons.net.au>
date Sun, 05 Dec 2021 16:19:31 +1030
parents
children 08b61687b75f
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
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 logging.basicConfig(format = '%(asctime)s %(message)s', level = logging.DEBUG)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 logging.info(__file__ + " is starting up")
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 port = 'ttyepro'
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 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
25 instance = 0
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 class eProUpdater:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 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
29 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
30 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
31 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
32 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
33 self.dbh = dbh
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 gobject.io_add_watch(s.fileno(), gobject.IO_IN, self.read_serial)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 gobject.timeout_add(60000, self.log_epro)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 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
38 try:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 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
40 except Exception as e:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 logging.error('Failed to read from serial port: %s', str(e))
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 return False
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 logging.debug('Read %d bytes from serial port', len(data))
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 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
46
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 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
48 # 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
49 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
50 self.log_queue.append(p)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 logging.debug('%s', str(p))
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 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
53 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
54 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
55 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
56 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
57 self.dbusservice['/Dc/0/Current'] = p.amps
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 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
59 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
60 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
61 # 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
62 self.dbusservice['/TimeToGo'] = p.time * 60
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 return True
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 def log_epro(self):
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 logging.debug('Logging epro data')
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 # 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
68 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
69 wantedtypes = set([
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 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
71 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
72 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
73 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
74 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
75 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
76 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
77 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
78 ])
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 if msgtypes < wantedtypes:
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 logging.debug('Didn\'t get all packet types required to log')
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 return
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 row = {}
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 usedtypes = set()
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 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
86 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
87 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
88 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
89 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
90 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
91 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
92 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
93 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
94 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
95 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
96 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 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
107 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
108 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
109 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
110 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
111 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
112 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
113 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
114 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
115 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
116 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
117 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
118 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
119 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
120
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 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
122 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
123 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
124 break
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126 logging.info('Got all packets, logging')
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 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
128 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
129 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
130 self.dbh.commit()
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 def doexit():
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 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
134
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 def main():
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136 # 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
137 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
138
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 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
140
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
141 dbusservice = VeDbusService(servicename)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
142 dbusservice.add_path('/Connected', value = True)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143 dbusservice.add_path('/ProductName', value = 'Enerdrive ePro')
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 dbusservice.add_path('/Mgmt/Connection', value = '/dev/' + port)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145 dbusservice.add_path('/DeviceInstance', value = instance)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 dbusservice.add_path('/ProductId', value = 'unknown')
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 dbusservice.add_path('/Dc/0/Voltage', value = None)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148 dbusservice.add_path('/Dc/0/Current', value = None)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149 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
150 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
151 dbusservice.add_path('/ConsumedAmphours', value = None)
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153 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
154 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
155
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 dbh = sqlite3.connect('/home/root/vanlogger/log.db')
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158 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
159
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 logging.info('Starting main loop')
446cfe74827b Add program to report epro status to DBus for Venus tools.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
161 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
162 mainloop.run()
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 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
165 main()