forked from massivebox/ecodash
MassiveBox
52ba0ea4c1
- Now the cache isn't cleared and fetched from zero each update, but only when there's missing information for a day - Fixed a bug that prevented new users from saving changes in the config NOTE: Turns out that HomeAssistant's API only returns data for the last 10 days. Looks like we will have to improve the caching system to work around this.
103 lines
2.8 KiB
Go
103 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type CacheEntry struct {
|
|
Date int64
|
|
GreenEnergyPercentage float32
|
|
PolledSmartEnergySummation float32
|
|
}
|
|
type CacheData []CacheEntry
|
|
|
|
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)
|
|
|
|
cached, err := config.readCache()
|
|
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())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
historyPolledSmartEnergySummation, err := config.historyBulkDelta(config.Sensors.PolledSmartEnergySummation, pastTime, time.Now())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = config.db.Exec("DELETE FROM cache")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (config Config) readCache() (CacheData, error) {
|
|
|
|
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())
|
|
if err != nil {
|
|
return CacheData{}, err
|
|
}
|
|
defer rows.Close()
|
|
var ret CacheData
|
|
|
|
for rows.Next() {
|
|
var (
|
|
date int64
|
|
greenEnergyPercentage float32
|
|
polledSmartEnergyConsumption float32
|
|
)
|
|
err = rows.Scan(&date, &greenEnergyPercentage, &polledSmartEnergyConsumption)
|
|
ret = append(ret, CacheEntry{date, greenEnergyPercentage, polledSmartEnergyConsumption})
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|