SQLite Initial Implementation
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This is the first, most basic implementation of a SQLite database for caching.
Future commits will make it much more optimized and able to efficiently store data for periods longer than 8 days.
This commit is contained in:
MassiveBox 2022-12-07 17:54:46 +01:00
parent 6dc8fa3750
commit e9125b783c
6 changed files with 56 additions and 33 deletions

View file

@ -2,10 +2,12 @@ package main
import (
"crypto/sha256"
"database/sql"
"encoding/json"
"errors"
"fmt"
"github.com/gofiber/fiber/v2"
_ "github.com/mattn/go-sqlite3"
"os"
"regexp"
"strings"
@ -13,6 +15,7 @@ import (
)
type Config struct {
db *sql.DB
HomeAssistant HomeAssistant `json:"home_assistant"`
Sensors Sensors `json:"sensors"`
Administrator Administrator `json:"administrator"`
@ -94,6 +97,22 @@ func loadConfig() (config Config, err error, isFirstRun bool) {
return Config{}, err, false
}
db, err := sql.Open("sqlite3", "./database.db")
if err != nil {
return Config{}, err, false
}
conf.db = db
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS "cache" (
"time" NUMERIC NOT NULL,
"green_energy_percentage" REAL NOT NULL,
"energy_consumption" REAL NOT NULL,
PRIMARY KEY("time")
);`)
if err != nil {
return Config{}, err, false
}
return conf, nil, false
}