view vanlogger.py @ 34:5b03de9fb20b default tip

Log that we logged some data.
author Daniel O'Connor <darius@dons.net.au>
date Tue, 14 Dec 2021 13:10:27 +1030
parents a9df202d14b7
children
line wrap: on
line source

#!/usr/bin/env python

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('''
CREATE TABLE IF NOT EXISTS eprolog(
    tstamp			INTEGER NOT NULL,
    main_voltage	REAL NOT NULL,
    aux_voltage		REAL NOT NULL,
    battery_curr	REAL NOT NULL,
    amp_hours		REAL NOT NULL,
    state_of_charge	REAL NOT NULL,
    time_remaining	REAL NOT NULL,
    battery_temp	REAL,
    auto_sync_volts	BOOLEAN NOT NULL,
    auto_sync_curr	BOOLEAN NOT NULL,
    e501			BOOLEAN NOT NULL,
    alarm_test		BOOLEAN NOT NULL,
    light			BOOLEAN NOT NULL,
    display_test	BOOLEAN NOT NULL,
    temp_sensor		BOOLEAN NOT NULL,
    aux_hv			BOOLEAN NOT NULL,
    aux_lv			BOOLEAN NOT NULL,
    installer_lock	BOOLEAN NOT NULL,
    main_hv			BOOLEAN NOT NULL,
    main_lv			BOOLEAN NOT NULL,
	low_battery 	BOOLEAN NOT NULL,
    battery_flat	BOOLEAN NOT NULL,
    battery_full	BOOLEAN NOT NULL,
    battery_charged	BOOLEAN NOT NULL,
    no_sync			BOOLEAN NOT NULL,
    monitor_reset	BOOLEAN NOT NULL
);
''')

    cur.execute('''
CREATE TABLE IF NOT EXISTS victron(
    tstamp		INTEGER NOT NULL,
    ACIn_L1_volts	REAL NOT NULL,
    ACIn_L1_freq	REAL NOT NULL,
    ACIn_L1_current	REAL NOT NULL,
    ACIn_active		BOOLEAN NOT NULL,
    ACOut_L1_volts	REAL NOT NULL,
    ACOut_L1_freq	REAL NOT NULL,
    ACOut_L1_current REAL NOT NULL,
    Battery_Voltage	REAL NOT NULL,
    Battery_Current	REAL NOT NULL
);
''')
    cur.execute('CREATE INDEX IF NOT EXISTS victron_tstamp_index ON victron (tstamp);')
    cur.execute('CREATE INDEX IF NOT EXISTS eprolog_tstamp_index ON eprolog (tstamp);')

def log_victron(v, cur):
    data = [int(datetime.datetime.now().strftime('%s')), ]
    data.extend(v.get_data())
    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():
    logger.error('Started')
    dbh = sqlite3.connect('/home/root/vanlogger/log.db')
    cur = dbh.cursor()
    create(cur)

    v = victron.Victron('com.victronenergy.vebus.ttyUSB1')

    then = None
    while True:
        dolog = False
        if then == None or datetime.datetime.now() - then > datetime.timedelta(seconds = 60):
            dolog = True
            then = datetime.datetime.now()
        if dolog:
            try:
                log_victron(v, cur)
                logger.info('Logging data')
            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()