Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import (
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/libp2p/zeroconf/v2"
"github.com/samber/lo"
"github.com/spf13/cast"
"github.com/spf13/cobra"
vpr "github.com/spf13/viper"
Expand Down Expand Up @@ -333,7 +332,7 @@ func configurableInstance[T any](typ string, conf *config.Config, newFromConf ne
}

var instance T
if err == nil {
if err == nil && !conf.Disable {
instance, err = newFromConf(ctx, typ, other)
if err != nil {
err = &DeviceError{cc.Name, fmt.Errorf("cannot create %s '%s': %w", typ, cc.Name, err)}
Expand Down Expand Up @@ -1232,9 +1231,12 @@ func configureSiteAndLoadpoints(conf *globalconfig.All) (*core.Site, error) {
errs = append(errs, &ClassError{ClassTariff, err})
}

loadpoints := lo.Map(config.Loadpoints().Devices(), func(dev config.Device[loadpoint.API], _ int) *core.Loadpoint {
return dev.Instance().(*core.Loadpoint)
})
var loadpoints []*core.Loadpoint
for _, dev := range config.Loadpoints().Devices() {
if inst := dev.Instance(); inst != nil {
loadpoints = append(loadpoints, inst.(*core.Loadpoint))
}
}

site, err := configureSite(conf.Site, loadpoints, tariffs)
if err != nil {
Expand Down Expand Up @@ -1338,9 +1340,12 @@ func configureLoadpoints(conf globalconfig.All) error {
return &DeviceError{cc.Name, err}
}

instance, err := core.NewLoadpointFromConfig(log, settings, static)
if err != nil {
err = &DeviceError{cc.Name, err}
var instance *core.Loadpoint
if !conf.Disable {
instance, err = core.NewLoadpointFromConfig(log, settings, static)
if err != nil {
err = &DeviceError{cc.Name, err}
}
}

dev := config.NewConfigurableDevice[loadpoint.API](&conf, instance)
Expand Down
4 changes: 4 additions & 0 deletions core/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ func NewSiteFromConfig(other map[string]any) (*Site, error) {
}

func (site *Site) Boot(log *util.Logger, loadpoints []*Loadpoint, tariffs *tariff.Tariffs) error {
if len(loadpoints) == 0 {
return errors.New("no loadpoint available")
Comment thread
andig marked this conversation as resolved.
Outdated
}

Comment thread
andig marked this conversation as resolved.
Outdated
site.loadpoints = loadpoints
site.tariffs = tariffs

Expand Down
8 changes: 7 additions & 1 deletion server/http_config_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,15 @@ func propsToMap(props config.Properties) (map[string]any, error) {
}

return lo.PickBy(res, func(k string, v any) bool {
if k == "Type" || v.(string) == "" {
if k == "Type" {
return false
}
switch val := v.(type) {
case string:
return val != ""
case bool:
return val
}
return true
}), nil
}
Expand Down
25 changes: 20 additions & 5 deletions server/http_config_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,34 @@ func TestConfigReqUnmarshal(t *testing.T) {
}

func TestConfigReqMarshalToMap(t *testing.T) {
props := config.Properties{
res, err := propsToMap(config.Properties{
Type: "type",
Title: "title",
Product: "product",
}

res, err := propsToMap(props)
})
require.NoError(t, err)

assert.Equal(t, map[string]any{
"deviceTitle": "title",
"deviceProduct": "product",
}, res)

// Disable=false is omitted (zero value)
res, err = propsToMap(config.Properties{
Type: "type",
Title: "title",
})
require.NoError(t, err)
assert.NotContains(t, res, "deviceDisable")

// Disable=true is included
res, err = propsToMap(config.Properties{
Type: "type",
Disable: true,
})
require.NoError(t, err)
assert.Equal(t, map[string]any{
"deviceDisable": true,
}, res)
}

type testStruct struct {
Expand Down
1 change: 1 addition & 0 deletions util/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {

type Properties struct {
Type string
Disable bool `json:"deviceDisable,omitempty" mapstructure:"deviceDisable"`
Title string `json:"deviceTitle,omitempty" mapstructure:"deviceTitle"`
Icon string `json:"deviceIcon,omitempty" mapstructure:"deviceIcon"`
Product string `json:"deviceProduct,omitempty" mapstructure:"deviceProduct"`
Expand Down
Loading