2022-06-25 22:13:35 -04:00
|
|
|
package energy
|
|
|
|
|
|
|
|
import (
|
2022-07-02 13:19:33 -04:00
|
|
|
"context"
|
2022-06-25 22:13:35 -04:00
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/amigan/energyd/pkg/isoclient"
|
|
|
|
)
|
|
|
|
|
2022-07-02 13:19:33 -04:00
|
|
|
type Energy struct {
|
|
|
|
iso isoclient.Client
|
|
|
|
}
|
|
|
|
|
2022-06-25 22:13:35 -04:00
|
|
|
type LoadProfile struct {
|
|
|
|
Current int
|
|
|
|
Peak int
|
|
|
|
Lowest int
|
|
|
|
PeakHr int
|
|
|
|
LowestHr int
|
|
|
|
|
|
|
|
PctPeak int
|
|
|
|
HoursAway int
|
|
|
|
}
|
|
|
|
|
2022-07-02 13:19:33 -04:00
|
|
|
func (e *Energy) Compute() {
|
|
|
|
ic := e.iso
|
|
|
|
|
2022-06-25 22:13:35 -04:00
|
|
|
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)
|
2022-07-02 13:19:33 -04:00
|
|
|
}
|
2022-06-25 22:13:35 -04:00
|
|
|
|
2022-07-02 13:19:33 -04:00
|
|
|
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,
|
|
|
|
}
|
2022-06-25 22:13:35 -04:00
|
|
|
}
|