energyd/pkg/energy/energy.go
2022-07-02 13:19:33 -04:00

82 lines
1.2 KiB
Go

package energy
import (
"context"
"fmt"
"math"
"time"
"github.com/amigan/energyd/pkg/isoclient"
)
type Energy struct {
iso isoclient.Client
}
type LoadProfile struct {
Current int
Peak int
Lowest int
PeakHr int
LowestHr int
PctPeak int
HoursAway int
}
func (e *Energy) Compute() {
ic := e.iso
load, err := ic.GetCurrentLoad()
if err != nil {
panic(err)
}
gphd, err := ic.GetHourlyLoadForecast()
if err != nil {
panic(err)
}
lp := LoadProfile{
Current: int(load.LoadMW),
LowestHr: -1,
}
for h, hf := range gphd.HourlyLoadForecast {
if int(hf.LoadMW) > lp.Peak {
lp.Peak = int(hf.LoadMW)
lp.PeakHr = h
}
if int(hf.LoadMW) < lp.Lowest || lp.LowestHr == -1 {
lp.LowestHr = h
lp.Lowest = int(hf.LoadMW)
}
}
hour := time.Now().Hour()
lp.PctPeak = int(float32(load.LoadMW / float32(lp.Peak)) * 100)
lp.HoursAway = int(math.Abs(float64(hour) - float64(lp.PeakHr)))
fmt.Printf("lp %#v\n", lp)
}
func (e *Energy) Go(ctx context.Context) {
tick := time.NewTicker(time.Minute*15)
e.Compute()
for {
select {
case <-tick.C:
e.Compute()
case <-ctx.Done():
return
}
}
}
func New(ic isoclient.Client) *Energy {
return &Energy{
iso: ic,
}
}