comparison eprodbus.py @ 22:02ba26698696

Change to run as a service by serial-starter. - Remove file logging. - Take TTY from argv - Just blow up if the serial port read fails, we will die and get resurrected if the TTY comes back.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 13 Dec 2021 15:51:09 +1030
parents 625064987b7c
children 64d0d5aaf329
comparison
equal deleted inserted replaced
21:625064987b7c 22:02ba26698696
21 logger = logging.getLogger('eprodbus') 21 logger = logging.getLogger('eprodbus')
22 logger.setLevel(logging.INFO) 22 logger.setLevel(logging.INFO)
23 23
24 formatter = logging.Formatter('%(asctime)s - %(message)s') 24 formatter = logging.Formatter('%(asctime)s - %(message)s')
25 25
26 #fh = logging.FileHandler('/var/log/eprodbus.log')
27 #fh.setLevel(logging.DEBUG)
28 #fh.setFormatter(formatter)
29
30 ch = logging.StreamHandler() 26 ch = logging.StreamHandler()
31 ch.setLevel(logging.INFO) 27 ch.setLevel(logging.INFO)
32 ch.setFormatter(formatter) 28 ch.setFormatter(formatter)
33 29
34 #logger.addHandler(fh)
35 logger.addHandler(ch) 30 logger.addHandler(ch)
36 31
37 logger.info(__file__ + " is starting up") 32 logger.info(__file__ + " is starting up")
38 33
39 dbpath = '/home/root/vanlogger/log.db' 34 dbpath = '/home/root/vanlogger/log.db'
40 port = 'ttyepro'
41 servicename = 'com.victronenergy.battery.' + port
42 instance = 0 35 instance = 0
43 36
44 class eProUpdater: 37 class eProUpdater:
45 def __init__(self, dbusservice, s, dbh): 38 def __init__(self, dbusservice, s, dbh):
46 self.log_queue = [] 39 self.log_queue = []
50 self.dbh = dbh 43 self.dbh = dbh
51 gobject.io_add_watch(s.fileno(), gobject.IO_IN, lambda fd, userdata: exit_on_error(self.read_serial, fd, userdata)) 44 gobject.io_add_watch(s.fileno(), gobject.IO_IN, lambda fd, userdata: exit_on_error(self.read_serial, fd, userdata))
52 gobject.timeout_add(60000, exit_on_error, self.log_epro) 45 gobject.timeout_add(60000, exit_on_error, self.log_epro)
53 46
54 def read_serial(self, fd, userdata): 47 def read_serial(self, fd, userdata):
55 try: 48 data = self.s.read(1024)
56 data = self.s.read(1024)
57 except Exception as e:
58 logger.error('Failed to read from serial port: %s', str(e))
59 return False
60 49
61 logger.debug('Read %d bytes from serial port', len(data)) 50 logger.debug('Read %d bytes from serial port', len(data))
62 self.p.process(data) 51 self.p.process(data)
63 52
64 while len(self.p.packets) > 0: 53 while len(self.p.packets) > 0:
160 149
161 def doexit(signum, frame): 150 def doexit(signum, frame):
162 sys.exit(1) 151 sys.exit(1)
163 152
164 def main(): 153 def main():
154 port = sys.argv[1]
155 servicename = 'com.victronenergy.battery.' + port
165 # Add signal handler to exit, otherwise we have to press ctrl-c twice to quit 156 # Add signal handler to exit, otherwise we have to press ctrl-c twice to quit
166 signal.signal(signal.SIGINT, doexit) 157 signal.signal(signal.SIGINT, doexit)
167 158
168 DBusGMainLoop(set_as_default = True) 159 DBusGMainLoop(set_as_default = True)
169 160