This commit is contained in:
Daniel 2022-07-02 13:19:33 -04:00
parent ed8e7bd27c
commit e9aa9b7d27
3 changed files with 38 additions and 5 deletions

View file

@ -1,9 +1,11 @@
package main package main
import ( import (
"context"
"flag" "flag"
"log" "log"
"os" "os"
"os/signal"
"runtime/pprof" "runtime/pprof"
"github.com/amigan/energyd/internal/config" "github.com/amigan/energyd/internal/config"
@ -27,5 +29,11 @@ func main() {
ic := isoclient.New(cfg) ic := isoclient.New(cfg)
energy.Compute(ic) ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
ener := energy.New(ic)
go ener.Go(ctx)
<-ctx.Done()
log.Printf("shutting down\n")
} }

View file

@ -20,8 +20,8 @@ func Config() *Conf {
} }
return &Conf{ return &Conf{
ISOUser: viper.Get("isouser").(string), ISOUser: viper.Get("iso.user").(string),
ISOPassword: viper.Get("isopassword").(string), ISOPassword: viper.Get("iso.password").(string),
} }
} }

View file

@ -1,6 +1,7 @@
package energy package energy
import ( import (
"context"
"fmt" "fmt"
"math" "math"
"time" "time"
@ -8,6 +9,10 @@ import (
"github.com/amigan/energyd/pkg/isoclient" "github.com/amigan/energyd/pkg/isoclient"
) )
type Energy struct {
iso isoclient.Client
}
type LoadProfile struct { type LoadProfile struct {
Current int Current int
Peak int Peak int
@ -19,7 +24,9 @@ type LoadProfile struct {
HoursAway int HoursAway int
} }
func Compute(ic isoclient.Client) { func (e *Energy) Compute() {
ic := e.iso
load, err := ic.GetCurrentLoad() load, err := ic.GetCurrentLoad()
if err != nil { if err != nil {
panic(err) panic(err)
@ -53,5 +60,23 @@ func Compute(ic isoclient.Client) {
lp.PctPeak = int(float32(load.LoadMW / float32(lp.Peak)) * 100) lp.PctPeak = int(float32(load.LoadMW / float32(lp.Peak)) * 100)
lp.HoursAway = int(math.Abs(float64(hour) - float64(lp.PeakHr))) lp.HoursAway = int(math.Abs(float64(hour) - float64(lp.PeakHr)))
fmt.Printf("lp %#v\n", lp) 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,
}
} }