view vanlogger.py @ 29:e86e839febca

Move epro logging into eprodbus.py Index tstamp on tables
author Daniel O'Connor <darius@dons.net.au>
date Mon, 06 Dec 2021 10:48:02 +1030
parents 1da02c79b458
children a9df202d14b7
line wrap: on
line source

#!/usr/bin/env python

import sys

import datetime
import json
import serial
import sqlite3
import subprocess
import sys
import victron

# 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():
    print '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:
            log_victron(v, cur)
            dbh.commit()

if __name__ == '__main__':
    main()