From f14b83cd8ba63d772f71cca6d1adaf195ac68248 Mon Sep 17 00:00:00 2001 From: andig Date: Tue, 2 Jan 2024 11:45:15 +0100 Subject: [PATCH] Fix smart cost type not published (#11415) --- cmd/root.go | 2 +- cmd/setup.go | 62 ++++++++++++++++++++--------------------------- core/site.go | 18 ++++++-------- tariff/tariffs.go | 10 -------- 4 files changed, 34 insertions(+), 58 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index d7b61de9ef..264d86d82e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -147,7 +147,7 @@ func runRoot(cmd *cobra.Command, args []string) { go socketHub.Run(pipe.NewDropper(ignoreEmpty).Pipe(tee.Attach()), cache) // setup values channel - valueChan := make(chan util.Param, 1) + valueChan := make(chan util.Param, 64) go tee.Run(valueChan) // capture log messages for UI diff --git a/cmd/setup.go b/cmd/setup.go index 4014509537..432999a628 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -592,52 +592,42 @@ func configureMessengers(conf messagingConfig, valueChan chan util.Param, cache return messageChan, nil } -func configureTariffs(conf tariffConfig) (tariff.Tariffs, error) { - var grid, feedin, co2, planner api.Tariff - var currencyCode currency.Unit = currency.EUR - var err error +func configureTariff(name string, conf config.Typed, t *api.Tariff, wg *sync.WaitGroup) { + defer wg.Done() - if conf.Currency != "" { - currencyCode = currency.MustParseISO(conf.Currency) + if conf.Type == "" { + return } - if conf.Grid.Type != "" { - grid, err = tariff.NewFromConfig(conf.Grid.Type, conf.Grid.Other) - if err != nil { - grid = nil - log.ERROR.Printf("failed configuring grid tariff: %v", err) - } + res, err := tariff.NewFromConfig(conf.Type, conf.Other) + if err != nil { + log.ERROR.Printf("failed configuring %s tariff: %v", name, err) + return } - if conf.FeedIn.Type != "" { - feedin, err = tariff.NewFromConfig(conf.FeedIn.Type, conf.FeedIn.Other) - if err != nil { - feedin = nil - log.ERROR.Printf("failed configuring feed-in tariff: %v", err) - } - } + *t = res +} - if conf.Co2.Type != "" { - co2, err = tariff.NewFromConfig(conf.Co2.Type, conf.Co2.Other) - if err != nil { - co2 = nil - log.ERROR.Printf("failed configuring co2 tariff: %v", err) - } +func configureTariffs(conf tariffConfig) (*tariff.Tariffs, error) { + tariffs := tariff.Tariffs{ + Currency: currency.EUR, } - if conf.Planner.Type != "" { - planner, err = tariff.NewFromConfig(conf.Planner.Type, conf.Planner.Other) - if err != nil { - planner = nil - log.ERROR.Printf("failed configuring planner tariff: %v", err) - } else if planner.Type() == api.TariffTypeCo2 { - log.WARN.Printf("tariff configuration changed, use co2 instead of planner for co2 tariff") - } + if conf.Currency != "" { + tariffs.Currency = currency.MustParseISO(conf.Currency) } - tariffs := tariff.NewTariffs(currencyCode, grid, feedin, co2, planner) + var wg sync.WaitGroup + wg.Add(4) + + go configureTariff("grid", conf.Grid, &tariffs.Grid, &wg) + go configureTariff("feedin", conf.FeedIn, &tariffs.FeedIn, &wg) + go configureTariff("co2", conf.Co2, &tariffs.Co2, &wg) + go configureTariff("planner", conf.Planner, &tariffs.Planner, &wg) + + wg.Wait() - return *tariffs, nil + return &tariffs, nil } func configureDevices(conf globalConfig) error { @@ -668,7 +658,7 @@ func configureSiteAndLoadpoints(conf globalConfig) (*core.Site, error) { return configureSite(conf.Site, loadpoints, tariffs) } -func configureSite(conf map[string]interface{}, loadpoints []*core.Loadpoint, tariffs tariff.Tariffs) (*core.Site, error) { +func configureSite(conf map[string]interface{}, loadpoints []*core.Loadpoint, tariffs *tariff.Tariffs) (*core.Site, error) { site, err := core.NewSiteFromConfig(log, conf, loadpoints, tariffs) if err != nil { return nil, fmt.Errorf("failed configuring site: %w", err) diff --git a/core/site.go b/core/site.go index 40020cd7ec..a76807bd6a 100644 --- a/core/site.go +++ b/core/site.go @@ -82,8 +82,8 @@ type Site struct { bufferStartSoc float64 // start charging on battery above this Soc batteryDischargeControl bool // prevent battery discharge for fast and planned charging - tariffs tariff.Tariffs // Tariff loadpoints []*Loadpoint // Loadpoints + tariffs *tariff.Tariffs // Tariffs coordinator *coordinator.Coordinator // Vehicles prioritizer *prioritizer.Prioritizer // Power budgets stats *Stats // Stats @@ -111,7 +111,7 @@ func NewSiteFromConfig( log *util.Logger, other map[string]interface{}, loadpoints []*Loadpoint, - tariffs tariff.Tariffs, + tariffs *tariff.Tariffs, ) (*Site, error) { site := NewSite() if err := util.DecodeOther(other, site); err != nil { @@ -822,20 +822,16 @@ func (site *Site) prepare() { site.publish(keys.GridConfigured, site.gridMeter != nil) site.publish(keys.PvConfigured, len(site.pvMeters) > 0) site.publish(keys.BatteryConfigured, len(site.batteryMeters) > 0) - site.publish(keys.BufferSoc, site.bufferSoc) - site.publish(keys.BufferStartSoc, site.bufferStartSoc) - site.publish(keys.PrioritySoc, site.prioritySoc) + site.publish(keys.BatteryMode, site.batteryMode) site.publish(keys.ResidualPower, site.ResidualPower) - site.publish(keys.SmartCostLimit, site.smartCostLimit) - site.publish(keys.SmartCostType, nil) + + site.publish(keys.Currency, site.tariffs.Currency) site.publish(keys.SmartCostActive, false) if tariff := site.GetTariff(PlannerTariff); tariff != nil { site.publish(keys.SmartCostType, tariff.Type()) + } else { + site.publish(keys.SmartCostType, nil) } - site.publish(keys.Currency, site.tariffs.Currency) - - site.publish(keys.BatteryDischargeControl, site.batteryDischargeControl) - site.publish(keys.BatteryMode, site.batteryMode) if err := site.restoreSettings(); err != nil { site.log.ERROR.Println(err) diff --git a/tariff/tariffs.go b/tariff/tariffs.go index 4a3c4857be..ffeb024cf2 100644 --- a/tariff/tariffs.go +++ b/tariff/tariffs.go @@ -12,16 +12,6 @@ type Tariffs struct { Grid, FeedIn, Co2, Planner api.Tariff } -func NewTariffs(currency currency.Unit, grid, feedin, co2 api.Tariff, planner api.Tariff) *Tariffs { - return &Tariffs{ - Currency: currency, - Grid: grid, - FeedIn: feedin, - Co2: co2, - Planner: planner, - } -} - func currentPrice(t api.Tariff) (float64, error) { if t != nil { if rr, err := t.Rates(); err == nil {