Add envoy support to solar logger

This commit is contained in:
2022-08-15 13:14:16 +02:00
parent 642170172a
commit 35e6ec08d7
16 changed files with 355 additions and 309 deletions

View File

@@ -1,12 +0,0 @@
# Scripts
This directory contains the following 2 scripts:
- `createdb.sh` is used to create the SQL database file used by the logger
- `migratedb.sh` is used to fill the newly created SQL database with the old file based logging content
The created database exists out of 3 rows, ignoring the `RowId` column:
- `DateTimeUtc` is an `INTEGER` and represents the UTC date and time in the Unix epoch format
- `Watts` is an `INTEGER` and represents the power output at the time of logging
- `KilowattHour` is a `REAL` and represents the cumulative power generated that day
Try running each script without any arguments for more help or read their source code.

18
script/solar/create.sql Normal file
View File

@@ -0,0 +1,18 @@
BEGIN TRANSACTION;
-- Create Zever tables
CREATE TABLE ZeverLogs (Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Date TEXT NOT NULL, TimeUtc TEXT NOT NULL, CurrentWatts INTEGER NOT NULL, TotalWatts BIGINT NOT NULL);
CREATE INDEX idx_ZeverLogs_Date ON ZeverLogs (Date);
CREATE INDEX idx_ZeverLogs_TimeUtc ON ZeverLogs (TimeUtc);
CREATE INDEX idx_ZeverLogs_TotalWatts ON ZeverLogs (TotalWatts);
CREATE TABLE ZeverSummary (Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Date TEXT NOT NULL UNIQUE, TotalWatts BIGINT NOT NULL);
CREATE UNIQUE INDEX idx_ZeverSummary_Date on ZeverSummary (Date);
-- Create Envoy table
CREATE TABLE EnvoyLogs (Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Date TEXT NOT NULL, TimeUtc TEXT NOT NULL, CurrentWatts INTEGER NOT NULL, TotalWatts BIGINT NOT NULL, Inverters INTEGER NOT NULL);
CREATE INDEX idx_EnvoyLogs_Date ON EnvoyLogs (Date);
CREATE INDEX idx_EnvoyLogs_TimeUtc ON EnvoyLogs (TimeUtc);
CREATE INDEX idx_EnvoyLogs_TotalWatts ON EnvoyLogs (TotalWatts);
COMMIT;

View File

@@ -1,30 +0,0 @@
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Usage: $0 [/path/to/db/database].db";
exit 1;
fi
echo "Creating database file $1...";
touch "$1";
if ! [ -f "$1" ]; then
echo "Cannot open or create database file $1, aborting.";
exit 1;
fi
TABLE_NAME="SolarPanelOutput";
echo "Creating table $TABLE_NAME...";
sqlite3 $1 "CREATE TABLE IF NOT EXISTS $TABLE_NAME (Date TEXT NOT NULL, TimeUtc TEXT NOT NULL, Watts INTEGER NOT NULL, KilowattHour REAL NOT NULL);";
echo "Creating indexes on table $TABLE_NAME...";
sqlite3 $1 "CREATE INDEX IF NOT EXISTS idx_Date ON $TABLE_NAME (Date);"
sqlite3 $1 "CREATE INDEX IF NOT EXISTS idx_TimeUtc ON $TABLE_NAME (TimeUtc);"
TABLE2_NAME="SolarPanelSummary";
echo "Creating table $TABLE2_NAME...";
sqlite3 $1 "CREATE TABLE IF NOT EXISTS $TABLE2_NAME (Date TEXT NOT NULL UNIQUE, Kilowatthour REAL NOT NULL);"
echo "Creating indexes on table $TABLE2_NAME";
sqlite3 $1 "CREATE UNIQUE INDEX IF NOT EXISTS idx_Date_$TABLE2_NAME on $TABLE2_NAME (Date);"

31
script/solar/migrate.sql Normal file
View File

@@ -0,0 +1,31 @@
BEGIN TRANSACTION;
-- Alter metric columns
ALTER TABLE SolarPanelOutput RENAME COLUMN Watts TO CurrentWatts;
ALTER TABLE SolarPanelOutput ADD COLUMN TotalWatts BIGINT;
UPDATE SolarPanelOutput SET TotalWatts = CAST(ROUND(KilowattHour * 1000) AS BIGINT);
ALTER TABLE SolarPanelOutput DROP COLUMN KilowattHour;
ALTER TABLE SolarPanelSummary ADD COLUMN TotalWatts BIGINT;
UPDATE SolarPanelSummary SET TotalWatts = CAST(ROUND(Kilowatthour * 1000) AS BIGINT);
ALTER TABLE SolarPanelSummary DROP COLUMN Kilowatthour;
-- Optimize indice
CREATE INDEX idx_TotalWatts ON SolarPanelOutput (TotalWatts);
-- Fix date bug
UPDATE SolarPanelOutput SET Date = '2018-02-02' WHERE Date = '18-02-02';
-- Remove bogus data
DELETE FROM SolarPanelOutput WHERE Date = '2018-02-02' AND TimeUtc = '08:15:10';
DELETE FROM SolarPanelOutput WHERE Date = '2018-01-02' AND TimeUtc = '13:31:03';
-- Copy data
INSERT INTO ZeverLogs (Date, TimeUtc, CurrentWatts, TotalWatts) SELECT * FROM SolarPanelOutput;
INSERT INTO ZeverSummary (Date, TotalWatts) SELECT * FROM SolarPanelSummary;
-- Delete old table
DROP TABLE SolarPanelOutput;
DROP TABLE SolarPanelSummary;
COMMIT;

View File

@@ -1,41 +0,0 @@
#!/bin/bash
TABLE_NAME="SolarPanelOutput";
if [ "$#" -ne 2 ]; then
echo "Usage: $0 [/log/file/directory/] [/path/to/db/database].db";
fi
if ! [ -d "$1" ]; then
echo "Error opening log file directory $1, aborting.";
exit 1;
fi
if ! [ -f "$2" ]; then
echo "Error opening database file $2, aborting.";
exit 1;
fi
if [ "${$1}" != */ ]; then
$1 = "$1/";
fi
for DIRECTORY in $1*/; do
if [ -d "$DIRECTORY" ]; then
echo "Checking out directory $DIRECTORY...";
for FILE in $DIRECTORY*; do
if [ -f "$FILE" ]; then
echo "Processing file $FILE...";
while read line; do
PROCESSED_LINE="$(echo "$line" | tr -s ' ')";
ISODATE="$(echo "$PROCESSED_LINE" | cut -d ' ' -f1)";
WATT="$(echo "$PROCESSED_LINE" | cut -d ' ' -f2)";
KILOWATTHR="$(echo "$PROCESSED_LINE" | cut -d ' ' -f3)";
EPOCHDATE="$(date -d"$ISODATE" +%s)"
sqlite3 $2 "INSERT INTO $TABLE_NAME VALUES($EPOCHDATE, $WATT, $KILOWATTHR);";
done < "$FILE"
fi
done;
fi
done