forked from massivebox/ecodash
MassiveBox
7a1214d492
- The program will now be cross-compiled and released for arm as well as x86 - What we previously called "cache" is not actually a cache, as it holds content that can't be always retrieved. Now we're stopping calling it a cache. - Improved history merging - Fixed the README to include the database as a volume, and to fix some errors
111 lines
3 KiB
Go
111 lines
3 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type HistoryEntry struct {
|
|
Date int64
|
|
GreenEnergyPercentage float32
|
|
PolledSmartEnergySummation float32
|
|
}
|
|
type History []HistoryEntry
|
|
|
|
func (config Config) updateHistory() {
|
|
|
|
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)
|
|
|
|
cached, err := config.readHistory()
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (config Config) readHistory() (History, 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 History{}, err
|
|
}
|
|
defer rows.Close()
|
|
var ret History
|
|
|
|
for rows.Next() {
|
|
var (
|
|
date int64
|
|
greenEnergyPercentage float32
|
|
polledSmartEnergyConsumption float32
|
|
)
|
|
err = rows.Scan(&date, &greenEnergyPercentage, &polledSmartEnergyConsumption)
|
|
ret = append(ret, HistoryEntry{date, greenEnergyPercentage, polledSmartEnergyConsumption})
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|