Rewrite electricity-logger to use an sqlite3 database

This commit is contained in:
2022-06-25 22:17:46 +02:00
parent 458d824dc8
commit 5b09b06bcf
62 changed files with 5937 additions and 3 deletions

12
script/solar/README.md Normal file
View File

@@ -0,0 +1,12 @@
# 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.

30
script/solar/createdb.sh Executable file
View File

@@ -0,0 +1,30 @@
#!/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);"

41
script/solar/migratedb.sh Executable file
View File

@@ -0,0 +1,41 @@
#!/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