changeset 30:a9df202d14b7

Use logging infra. Handle errors during startup nicely.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 13 Dec 2021 18:16:11 +1030
parents e86e839febca
children 91bfaba8f6b6
files vanlogger.py
diffstat 1 files changed, 21 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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()