ecodash/cache.go

103 lines
2.8 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 CacheEntry struct {
Date int64
GreenEnergyPercentage float32
PolledSmartEnergySummation float32
2022-10-15 17:58:44 +00:00
}
type CacheData []CacheEntry
2022-10-15 17:58:44 +00:00
func (config Config) updateCache() {
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 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.readCache()
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
}
_, err = config.db.Exec("DELETE FROM cache")
if err != nil {
return err
2022-10-15 17:58:44 +00:00
}
for key, day := range greenEnergyPercentage {
_, err := config.db.Exec("INSERT INTO cache(time,green_energy_percentage,energy_consumption) VALUES (?,?,?);", 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) readCache() (CacheData, 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 CacheData{}, err
2022-10-15 17:58:44 +00:00
}
defer rows.Close()
var ret CacheData
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, CacheEntry{date, greenEnergyPercentage, polledSmartEnergyConsumption})
2022-10-15 17:58:44 +00:00
}
return ret, nil
2022-10-15 17:58:44 +00:00
}