2022-10-15 17:58:44 +00:00
package main
import (
2023-01-04 14:57:16 +00:00
"errors"
2022-10-15 17:58:44 +00:00
"fmt"
"time"
)
2023-01-29 20:16:04 +00:00
type HistoryEntry struct {
2022-12-07 16:54:46 +00:00
Date int64
GreenEnergyPercentage float32
PolledSmartEnergySummation float32
2022-10-15 17:58:44 +00:00
}
2023-01-29 20:16:04 +00:00
type History [ ] HistoryEntry
2022-10-15 17:58:44 +00:00
2023-01-29 20:16:04 +00:00
func ( config Config ) updateHistory ( ) {
2022-10-15 17:58:44 +00:00
2023-01-04 14:57:16 +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 {
2022-12-04 11:50:11 +00:00
return
}
2023-01-29 20:16:04 +00:00
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
2023-01-29 20:16:04 +00:00
cached , err := config . readHistory ( )
2022-10-15 17:58:44 +00:00
if err != nil {
return
}
2023-01-04 14:57:16 +00:00
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 {
2023-01-04 14:57:16 +00:00
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
}
2022-12-07 16:54:46 +00:00
for key , day := range greenEnergyPercentage {
2023-01-29 20:16:04 +00:00
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(?,?,?)" )
2022-12-07 16:54:46 +00:00
if err != nil {
2023-01-04 14:57:16 +00:00
return err
2022-12-07 16:54:46 +00:00
}
2023-01-29 20:16:04 +00:00
_ , 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
}
2023-01-04 14:57:16 +00:00
return nil
2022-10-15 17:58:44 +00:00
}
2023-01-29 20:16:04 +00:00
func ( config Config ) readHistory ( ) ( History , error ) {
2022-10-15 17:58:44 +00:00
2023-01-04 14:57:16 +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 {
2023-01-29 20:16:04 +00:00
return History { } , err
2022-10-15 17:58:44 +00:00
}
2022-12-07 16:54:46 +00:00
defer rows . Close ( )
2023-01-29 20:16:04 +00:00
var ret History
2022-10-15 17:58:44 +00:00
2022-12-07 16:54:46 +00:00
for rows . Next ( ) {
var (
date int64
greenEnergyPercentage float32
polledSmartEnergyConsumption float32
)
err = rows . Scan ( & date , & greenEnergyPercentage , & polledSmartEnergyConsumption )
2023-01-29 20:16:04 +00:00
ret = append ( ret , HistoryEntry { date , greenEnergyPercentage , polledSmartEnergyConsumption } )
2022-10-15 17:58:44 +00:00
}
2022-12-07 16:54:46 +00:00
return ret , nil
2022-10-15 17:58:44 +00:00
}