From ea3d067903c5c32a90bd455b3e16c9b509f96931 Mon Sep 17 00:00:00 2001 From: andig Date: Mon, 12 Dec 2022 18:37:42 +0100 Subject: [PATCH] Integrate with site planner --- cmd/config.go | 1 + cmd/setup.go | 8 ++++++-- core/site.go | 4 ++-- tariff/electricitymaps.go | 22 +++++++++------------- tariff/tariffs.go | 22 +++++++++++++--------- 5 files changed, 31 insertions(+), 26 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index e8aa4ca0d8..089239291a 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -113,6 +113,7 @@ type tariffConfig struct { Currency string Grid typedConfig FeedIn typedConfig + Planner typedConfig } type networkConfig struct { diff --git a/cmd/setup.go b/cmd/setup.go index 795806a216..246f13d82b 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -227,7 +227,7 @@ func configureMessengers(conf messagingConfig, cache *util.Cache) (chan push.Eve } func configureTariffs(conf tariffConfig) (tariff.Tariffs, error) { - var grid, feedin api.Tariff + var grid, feedin, planner api.Tariff var currencyCode currency.Unit = currency.EUR var err error @@ -243,11 +243,15 @@ func configureTariffs(conf tariffConfig) (tariff.Tariffs, error) { feedin, err = tariff.NewFromConfig(conf.FeedIn.Type, conf.FeedIn.Other) } + if err == nil && conf.Planner.Type != "" { + planner, err = tariff.NewFromConfig(conf.Planner.Type, conf.Planner.Other) + } + if err != nil { err = fmt.Errorf("failed configuring tariff: %w", err) } - tariffs := tariff.NewTariffs(currencyCode, grid, feedin) + tariffs := tariff.NewTariffs(currencyCode, grid, feedin, planner) return *tariffs, err } diff --git a/core/site.go b/core/site.go index 235c091f52..fcb462ee60 100644 --- a/core/site.go +++ b/core/site.go @@ -120,8 +120,8 @@ func NewSiteFromConfig( lp.coordinator = coordinator.NewAdapter(lp, site.coordinator) // planner - if gridTariff := site.tariffs.Grid; gridTariff != nil { - lp.planner = planner.New(lp.log, gridTariff) + if plannerTariff := site.tariffs.Planner; plannerTariff != nil { + lp.planner = planner.New(lp.log, plannerTariff) } if serverdb.Instance != nil { diff --git a/tariff/electricitymaps.go b/tariff/electricitymaps.go index 7249a91bc3..b34b4797a5 100644 --- a/tariff/electricitymaps.go +++ b/tariff/electricitymaps.go @@ -93,23 +93,19 @@ func (t *ElectricityMaps) Run() { } } -func (t *ElectricityMaps) CurrentPrice() (float64, error) { +func (t *ElectricityMaps) Rates() (api.Rates, error) { t.mux.Lock() defer t.mux.Unlock() - now := time.Now() - - for _, pi := range t.data { - ts := pi.Datetime - - if (ts.Before(now) || ts.Equal(now)) && ts.Add(time.Hour).After(now) { - return pi.CarbonIntensity, nil + res := make(api.Rates, 0, len(t.data)) + for _, r := range t.data { + ar := api.Rate{ + Start: r.Datetime, + End: r.Datetime.Add(time.Hour), + Price: r.CarbonIntensity, } + res = append(res, ar) } - return 0, errors.New("unable to find current slot") -} - -func (t *ElectricityMaps) IsCheap() (bool, error) { - return false, nil + return res, nil } diff --git a/tariff/tariffs.go b/tariff/tariffs.go index 0bcb72f485..08aef07374 100644 --- a/tariff/tariffs.go +++ b/tariff/tariffs.go @@ -6,15 +6,19 @@ import ( ) type Tariffs struct { - Currency currency.Unit - Grid api.Tariff - FeedIn api.Tariff + Currency currency.Unit + Grid, FeedIn, Planner api.Tariff } -func NewTariffs(currency currency.Unit, grid api.Tariff, feedin api.Tariff) *Tariffs { - t := Tariffs{} - t.Currency = currency - t.Grid = grid - t.FeedIn = feedin - return &t +func NewTariffs(currency currency.Unit, grid, feedin, planner api.Tariff) *Tariffs { + if planner == nil { + planner = grid + } + + return &Tariffs{ + Currency: currency, + Grid: grid, + FeedIn: feedin, + Planner: planner, + } }