ecodash/database.go

111 lines
3 KiB
Go
Raw Normal View History

2022-10-15 17:58:44 +00:00
package main
import (
"errors"
2022-10-15 17:58:44 +00:00
"fmt"
"time"
)
type HistoryEntry struct {
Date int64
GreenEnergyPercentage float32
PolledSmartEnergySummation float32
2022-10-15 17:58:44 +00:00
}
type History []HistoryEntry
2022-10-15 17:58:44 +00:00
func (config Config) updateHistory() {
2022-10-15 17:58:44 +00:00
greenEnergyPercentage, err := config.historyAverageAndConvertToGreen(config.Sensors.FossilPercentage, time.Now())
if err != nil {
return
}
historyPolledSmartEnergySummation, err := config.historyDelta(config.Sensors.PolledSmartEnergySummation, time.Now())
if err != nil {
return
}
config.db.Exec("INSERT OR REPLACE INTO cache(time,green_energy_percentage,energy_consumption) VALUES (?,?,?);", dayStart(time.Now()).Unix(), greenEnergyPercentage.Value, historyPolledSmartEnergySummation.Value)
2022-10-15 17:58:44 +00:00
cached, err := config.readHistory()
2022-10-15 17:58:44 +00:00
if err != nil {
return
}
if len(cached) != 8 && time.Now().Sub(config.HomeAssistant.InstallationDate) > 8*time.Hour*24 {
err := config.refreshCacheFromPast(time.Now().Add(-8 * time.Hour * 24))
if err != nil {
fmt.Println("Error refreshing cache", err.Error())
return
}
}
}
func (config Config) refreshCacheFromInstall() error {
return config.refreshCacheFromPast(config.HomeAssistant.InstallationDate)
}
func (config Config) refreshCacheFromPast(pastTime time.Time) error {
// in order to avoid querying and storing each day's data from 0001-01-01 in future versions
if config.HomeAssistant.InstallationDate.IsZero() {
return errors.New("installation date not set")
}
greenEnergyPercentage, err := config.historyBulkAverageAndConvertToGreen(config.Sensors.FossilPercentage, pastTime, time.Now())
2022-10-15 17:58:44 +00:00
if err != nil {
return err
}
historyPolledSmartEnergySummation, err := config.historyBulkDelta(config.Sensors.PolledSmartEnergySummation, pastTime, time.Now())
if err != nil {
return err
2022-10-15 17:58:44 +00:00
}
for key, day := range greenEnergyPercentage {
var action2 string
if greenEnergyPercentage[key].Value != 0 && historyPolledSmartEnergySummation[key].Value != 0 {
action2 = "REPLACE"
} else {
action2 = "IGNORE"
}
stmt, err := config.db.Prepare("INSERT OR " + action2 + " INTO cache(time, green_energy_percentage, energy_consumption) values(?,?,?)")
if err != nil {
return err
}
_, err = stmt.Exec(day.DayTime.Unix(), greenEnergyPercentage[key].Value, historyPolledSmartEnergySummation[key].Value)
if err != nil {
return err
}
2022-10-15 17:58:44 +00:00
}
return nil
2022-10-15 17:58:44 +00:00
}
func (config Config) readHistory() (History, error) {
2022-10-15 17:58:44 +00:00
start := dayStart(time.Now()).AddDate(0, 0, -8)
rows, err := config.db.Query("SELECT time, green_energy_percentage, energy_consumption FROM cache WHERE time > ?", start.Unix())
2022-10-15 17:58:44 +00:00
if err != nil {
return History{}, err
2022-10-15 17:58:44 +00:00
}
defer rows.Close()
var ret History
2022-10-15 17:58:44 +00:00
for rows.Next() {
var (
date int64
greenEnergyPercentage float32
polledSmartEnergyConsumption float32
)
err = rows.Scan(&date, &greenEnergyPercentage, &polledSmartEnergyConsumption)
ret = append(ret, HistoryEntry{date, greenEnergyPercentage, polledSmartEnergyConsumption})
2022-10-15 17:58:44 +00:00
}
return ret, nil
2022-10-15 17:58:44 +00:00
}