# HG changeset patch # User Daniel O'Connor # Date 1639381571 -37800 # Node ID a9df202d14b7d40f4ef95c12c39b5f4146832035 # Parent e86e839febca1c7f385e4b5936249cdabb810919 Use logging infra. Handle errors during startup nicely. diff -r e86e839febca -r a9df202d14b7 vanlogger.py --- a/vanlogger.py Mon Dec 06 10:48:02 2021 +1030 +++ b/vanlogger.py Mon Dec 13 18:16:11 2021 +1030 @@ -3,13 +3,26 @@ import sys import datetime +import dbus import json +import logging import serial import sqlite3 import subprocess import sys +import time import victron +logger = logging.getLogger('vanlogger') +logger.setLevel(logging.INFO) + +formatter = logging.Formatter('%(message)s') + +ch = logging.StreamHandler() +ch.setLevel(logging.INFO) +ch.setFormatter(formatter) +logger.addHandler(ch) + # Actual epro logging moved to eprodbus.py def create(cur): cur.execute(''' @@ -66,7 +79,7 @@ cur.execute('INSERT INTO victron (tstamp, ACIn_L1_volts, ACIn_L1_freq, ACIn_L1_current, ACIn_active, ACOut_L1_volts, ACOut_L1_freq, ACOut_L1_current, Battery_Voltage, Battery_Current) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', data) def main(): - print 'Started' + logger.error('Started') dbh = sqlite3.connect('/home/root/vanlogger/log.db') cur = dbh.cursor() create(cur) @@ -80,8 +93,13 @@ dolog = True then = datetime.datetime.now() if dolog: - log_victron(v, cur) - dbh.commit() + try: + log_victron(v, cur) + except (AttributeError, dbus.exceptions.DBusException) as e: + # We get various errors during startup so just log and keep going + logger.error('Error getting data: %s', str(e)) + dbh.commit() + time.sleep(30) if __name__ == '__main__': main()