Skip to content

Commit

Permalink
Integrate with site planner
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Dec 12, 2022
1 parent 0d19ea0 commit ea3d067
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 26 deletions.
1 change: 1 addition & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type tariffConfig struct {
Currency string
Grid typedConfig
FeedIn typedConfig
Planner typedConfig
}

type networkConfig struct {
Expand Down
8 changes: 6 additions & 2 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions core/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 9 additions & 13 deletions tariff/electricitymaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 13 additions & 9 deletions tariff/tariffs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

0 comments on commit ea3d067

Please sign in to comment.