Mercurial > ~darius > hgwebdir.cgi > epro
comparison eprodbus.py @ 15:08b61687b75f
Use logger better
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Mon, 06 Dec 2021 11:27:11 +1030 |
parents | 446cfe74827b |
children | 3338e666256b |
comparison
equal
deleted
inserted
replaced
14:60ead9b5fc1b | 15:08b61687b75f |
---|---|
15 import sys | 15 import sys |
16 | 16 |
17 sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'velib_python')) | 17 sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'velib_python')) |
18 from vedbus import VeDbusService | 18 from vedbus import VeDbusService |
19 | 19 |
20 logging.basicConfig(format = '%(asctime)s %(message)s', level = logging.DEBUG) | 20 logger = logging.getLogger('eprodbus') |
21 logging.info(__file__ + " is starting up") | 21 logger.setLevel(logging.INFO) |
22 | |
23 formatter = logging.Formatter('%(asctime)s - %(message)s') | |
24 | |
25 #fh = logging.FileHandler('/var/log/eprodbus.log') | |
26 #fh.setLevel(logging.DEBUG) | |
27 #fh.setFormatter(formatter) | |
28 | |
29 ch = logging.StreamHandler() | |
30 ch.setLevel(logging.INFO) | |
31 ch.setFormatter(formatter) | |
32 | |
33 #logger.addHandler(fh) | |
34 logger.addHandler(ch) | |
35 | |
36 logger.info(__file__ + " is starting up") | |
22 | 37 |
23 port = 'ttyepro' | 38 port = 'ttyepro' |
24 servicename = 'com.victronenergy.battery.' + port | 39 servicename = 'com.victronenergy.battery.' + port |
25 instance = 0 | 40 instance = 0 |
26 | 41 |
36 | 51 |
37 def read_serial(self, fd, userdata): | 52 def read_serial(self, fd, userdata): |
38 try: | 53 try: |
39 data = self.s.read(1024) | 54 data = self.s.read(1024) |
40 except Exception as e: | 55 except Exception as e: |
41 logging.error('Failed to read from serial port: %s', str(e)) | 56 logger.error('Failed to read from serial port: %s', str(e)) |
42 return False | 57 return False |
43 | 58 |
44 logging.debug('Read %d bytes from serial port', len(data)) | 59 logger.debug('Read %d bytes from serial port', len(data)) |
45 self.p.process(data) | 60 self.p.process(data) |
46 | 61 |
47 while len(self.p.packets) > 0: | 62 while len(self.p.packets) > 0: |
48 # Process oldest packets first | 63 # Process oldest packets first |
49 p = self.p.packets.pop(0) | 64 p = self.p.packets.pop(0) |
50 self.log_queue.append(p) | 65 self.log_queue.append(p) |
51 logging.debug('%s', str(p)) | 66 logger.debug('%s', str(p)) |
52 if type(p) == epro.StateOfCharge: | 67 if type(p) == epro.StateOfCharge: |
53 self.dbusservice['/Soc'] = p.soc | 68 self.dbusservice['/Soc'] = p.soc |
54 elif type(p) == epro.MainVoltage: | 69 elif type(p) == epro.MainVoltage: |
55 self.dbusservice['/Dc/0/Voltage'] = p.volts | 70 self.dbusservice['/Dc/0/Voltage'] = p.volts |
56 elif type(p) == epro.BatteryCurrent: | 71 elif type(p) == epro.BatteryCurrent: |
61 # ePro reports in minutes, Venus expects seconds | 76 # ePro reports in minutes, Venus expects seconds |
62 self.dbusservice['/TimeToGo'] = p.time * 60 | 77 self.dbusservice['/TimeToGo'] = p.time * 60 |
63 return True | 78 return True |
64 | 79 |
65 def log_epro(self): | 80 def log_epro(self): |
66 logging.debug('Logging epro data') | 81 logger.debug('Logging epro data') |
67 # Check we have all the packets we need in the queue | 82 # Check we have all the packets we need in the queue |
68 msgtypes = set([x.msgtype for x in self.log_queue]) | 83 msgtypes = set([x.msgtype for x in self.log_queue]) |
69 wantedtypes = set([ | 84 wantedtypes = set([ |
70 epro.MainVoltage.MSGTYPE, | 85 epro.MainVoltage.MSGTYPE, |
71 epro.AmpHours.MSGTYPE, | 86 epro.AmpHours.MSGTYPE, |
75 epro.BatteryTemperature.MSGTYPE, | 90 epro.BatteryTemperature.MSGTYPE, |
76 epro.MonitorStatus.MSGTYPE, | 91 epro.MonitorStatus.MSGTYPE, |
77 epro.AuxVoltage.MSGTYPE, | 92 epro.AuxVoltage.MSGTYPE, |
78 ]) | 93 ]) |
79 if msgtypes < wantedtypes: | 94 if msgtypes < wantedtypes: |
80 logging.debug('Didn\'t get all packet types required to log') | 95 logger.debug('Didn\'t get all packet types required to log') |
81 return | 96 return |
82 | 97 |
83 row = {} | 98 row = {} |
84 usedtypes = set() | 99 usedtypes = set() |
85 while len(self.log_queue) > 0: | 100 while len(self.log_queue) > 0: |
121 usedtypes.add(pkt.msgtype) | 136 usedtypes.add(pkt.msgtype) |
122 if usedtypes >= wantedtypes: | 137 if usedtypes >= wantedtypes: |
123 self.log_queue = [] | 138 self.log_queue = [] |
124 break | 139 break |
125 | 140 |
126 logging.info('Got all packets, logging') | 141 logger.info('Got all packets, logging') |
127 cur = self.dbh.cursor() | 142 cur = self.dbh.cursor() |
128 row['tstamp'] = int(datetime.datetime.now().strftime('%s')) | 143 row['tstamp'] = int(datetime.datetime.now().strftime('%s')) |
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) | 144 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) |
130 self.dbh.commit() | 145 self.dbh.commit() |
131 | 146 |
155 | 170 |
156 dbh = sqlite3.connect('/home/root/vanlogger/log.db') | 171 dbh = sqlite3.connect('/home/root/vanlogger/log.db') |
157 | 172 |
158 updater = eProUpdater(dbusservice, s, dbh) | 173 updater = eProUpdater(dbusservice, s, dbh) |
159 | 174 |
160 logging.info('Starting main loop') | 175 logger.info('Starting main loop') |
161 mainloop = gobject.MainLoop() | 176 mainloop = gobject.MainLoop() |
162 mainloop.run() | 177 mainloop.run() |
163 | 178 |
164 if __name__ == '__main__': | 179 if __name__ == '__main__': |
165 main() | 180 main() |