From 8c7ef0cb5ecf61453a36c531665f41a97d3ac679 Mon Sep 17 00:00:00 2001 From: andig Date: Mon, 30 Dec 2024 15:50:11 +0100 Subject: [PATCH 1/8] Add charger provider --- provider/enable.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 provider/enable.go diff --git a/provider/enable.go b/provider/enable.go new file mode 100644 index 0000000000..982298cc9c --- /dev/null +++ b/provider/enable.go @@ -0,0 +1,61 @@ +package provider + +import ( + "context" + "fmt" + + "github.com/evcc-io/evcc/api" + "github.com/evcc-io/evcc/charger" + "github.com/evcc-io/evcc/util" + "github.com/evcc-io/evcc/util/config" +) + +type enableProvider struct { + charger api.Charger +} + +func init() { + registry.AddCtx("map", NewMapFromConfig) +} + +// NewEnableFromConfig creates type conversion provider +func NewEnableFromConfig(ctx context.Context, other map[string]interface{}) (Provider, error) { + var cc struct { + Charger config.Typed + } + + if err := util.DecodeOther(other, &cc); err != nil { + return nil, err + } + + if cc.Charger.Type == "" { + return nil, fmt.Errorf("missing charger") + } + + charger, err := charger.NewFromConfig(ctx, cc.Charger.Type, cc.Charger.Other) + if err != nil { + return nil, err + } + + o := &enableProvider{ + charger: charger, + } + + return o, nil +} + +var _ BoolProvider = (*enableProvider)(nil) + +func (o *enableProvider) BoolGetter() (func() (bool, error), error) { + return func() (bool, error) { + return o.charger.Enabled() + }, nil +} + +var _ SetBoolProvider = (*enableProvider)(nil) + +func (o *enableProvider) BoolSetter(param string) (func(bool) error, error) { + return func(val bool) error { + return o.charger.Enable(val) + }, nil +} From ebb9d34eeae272f9f004bc67b5e56a7a80fb33b9 Mon Sep 17 00:00:00 2001 From: andig Date: Mon, 30 Dec 2024 15:51:10 +0100 Subject: [PATCH 2/8] wip --- provider/{enable.go => charger.go} | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) rename provider/{enable.go => charger.go} (58%) diff --git a/provider/enable.go b/provider/charger.go similarity index 58% rename from provider/enable.go rename to provider/charger.go index 982298cc9c..5f7e6612aa 100644 --- a/provider/enable.go +++ b/provider/charger.go @@ -10,16 +10,16 @@ import ( "github.com/evcc-io/evcc/util/config" ) -type enableProvider struct { +type chargerProvider struct { charger api.Charger } func init() { - registry.AddCtx("map", NewMapFromConfig) + registry.AddCtx("charger", NewChargerEnableFromConfig) } -// NewEnableFromConfig creates type conversion provider -func NewEnableFromConfig(ctx context.Context, other map[string]interface{}) (Provider, error) { +// NewChargerEnableFromConfig creates type conversion provider +func NewChargerEnableFromConfig(ctx context.Context, other map[string]interface{}) (Provider, error) { var cc struct { Charger config.Typed } @@ -37,24 +37,24 @@ func NewEnableFromConfig(ctx context.Context, other map[string]interface{}) (Pro return nil, err } - o := &enableProvider{ + o := &chargerProvider{ charger: charger, } return o, nil } -var _ BoolProvider = (*enableProvider)(nil) +var _ BoolProvider = (*chargerProvider)(nil) -func (o *enableProvider) BoolGetter() (func() (bool, error), error) { +func (o *chargerProvider) BoolGetter() (func() (bool, error), error) { return func() (bool, error) { return o.charger.Enabled() }, nil } -var _ SetBoolProvider = (*enableProvider)(nil) +var _ SetBoolProvider = (*chargerProvider)(nil) -func (o *enableProvider) BoolSetter(param string) (func(bool) error, error) { +func (o *chargerProvider) BoolSetter(param string) (func(bool) error, error) { return func(val bool) error { return o.charger.Enable(val) }, nil From f246c8f442e3c3eeb86e4e6d74586037cc6740ff Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 3 Jan 2025 17:50:24 +0100 Subject: [PATCH 3/8] wip --- charger/config.go | 18 +++--------------- charger/config/config.go | 27 +++++++++++++++++++++++++++ provider/charger.go | 2 +- 3 files changed, 31 insertions(+), 16 deletions(-) create mode 100644 charger/config/config.go diff --git a/charger/config.go b/charger/config.go index 6449c3d780..05b95ae912 100644 --- a/charger/config.go +++ b/charger/config.go @@ -2,14 +2,12 @@ package charger import ( "context" - "fmt" - "strings" "github.com/evcc-io/evcc/api" - reg "github.com/evcc-io/evcc/util/registry" + "github.com/evcc-io/evcc/charger/config" ) -var registry = reg.New[api.Charger]("charger") +var registry = config.Registry // Types returns the list of types func Types() []string { @@ -18,15 +16,5 @@ func Types() []string { // NewFromConfig creates charger from configuration func NewFromConfig(ctx context.Context, typ string, other map[string]interface{}) (api.Charger, error) { - factory, err := registry.Get(strings.ToLower(typ)) - if err != nil { - return nil, err - } - - v, err := factory(ctx, other) - if err != nil { - err = fmt.Errorf("cannot create charger type '%s': %w", typ, err) - } - - return v, err + return config.NewFromConfig(ctx, typ, other) } diff --git a/charger/config/config.go b/charger/config/config.go new file mode 100644 index 0000000000..33a88b06a0 --- /dev/null +++ b/charger/config/config.go @@ -0,0 +1,27 @@ +package config + +import ( + "context" + "fmt" + "strings" + + "github.com/evcc-io/evcc/api" + reg "github.com/evcc-io/evcc/util/registry" +) + +var Registry = reg.New[api.Charger]("charger") + +// NewFromConfig creates charger from configuration +func NewFromConfig(ctx context.Context, typ string, other map[string]interface{}) (api.Charger, error) { + factory, err := Registry.Get(strings.ToLower(typ)) + if err != nil { + return nil, err + } + + v, err := factory(ctx, other) + if err != nil { + err = fmt.Errorf("cannot create charger type '%s': %w", typ, err) + } + + return v, err +} diff --git a/provider/charger.go b/provider/charger.go index 5f7e6612aa..f3460c0b11 100644 --- a/provider/charger.go +++ b/provider/charger.go @@ -5,7 +5,7 @@ import ( "fmt" "github.com/evcc-io/evcc/api" - "github.com/evcc-io/evcc/charger" + charger "github.com/evcc-io/evcc/charger/config" "github.com/evcc-io/evcc/util" "github.com/evcc-io/evcc/util/config" ) From d0ab15dde49e27c2fe6999fad74efcd7bd2ffebe Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 3 Jan 2025 18:11:39 +0100 Subject: [PATCH 4/8] wip --- provider/charger.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/provider/charger.go b/provider/charger.go index f3460c0b11..224962c854 100644 --- a/provider/charger.go +++ b/provider/charger.go @@ -21,18 +21,18 @@ func init() { // NewChargerEnableFromConfig creates type conversion provider func NewChargerEnableFromConfig(ctx context.Context, other map[string]interface{}) (Provider, error) { var cc struct { - Charger config.Typed + Config config.Typed } if err := util.DecodeOther(other, &cc); err != nil { return nil, err } - if cc.Charger.Type == "" { + if cc.Config.Type == "" { return nil, fmt.Errorf("missing charger") } - charger, err := charger.NewFromConfig(ctx, cc.Charger.Type, cc.Charger.Other) + charger, err := charger.NewFromConfig(ctx, cc.Config.Type, cc.Config.Other) if err != nil { return nil, err } From 64c4d41153e2680d2626648a3df565831c6aafb6 Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 3 Jan 2025 18:46:30 +0100 Subject: [PATCH 5/8] wip --- provider/charger.go | 22 ++++++++++++++++++++++ provider/const.go | 12 +----------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/provider/charger.go b/provider/charger.go index 224962c854..e8214c2374 100644 --- a/provider/charger.go +++ b/provider/charger.go @@ -8,6 +8,7 @@ import ( charger "github.com/evcc-io/evcc/charger/config" "github.com/evcc-io/evcc/util" "github.com/evcc-io/evcc/util/config" + "github.com/spf13/cast" ) type chargerProvider struct { @@ -44,6 +45,27 @@ func NewChargerEnableFromConfig(ctx context.Context, other map[string]interface{ return o, nil } +var _ IntProvider = (*chargerProvider)(nil) + +func (o *chargerProvider) IntGetter() (func() (int64, error), error) { + return func() (int64, error) { + v, err := o.charger.Enabled() + return cast.ToInt64(v), err + }, nil +} + +var _ SetIntProvider = (*chargerProvider)(nil) + +func (o *chargerProvider) IntSetter(param string) (func(int64) error, error) { + return func(val int64) error { + b, err := cast.ToBoolE(val) + if err != nil { + return err + } + return o.charger.Enable(b) + }, nil +} + var _ BoolProvider = (*chargerProvider)(nil) func (o *chargerProvider) BoolGetter() (func() (bool, error), error) { diff --git a/provider/const.go b/provider/const.go index 638e048cf1..692f15dc87 100644 --- a/provider/const.go +++ b/provider/const.go @@ -33,19 +33,9 @@ func NewConstFromConfig(ctx context.Context, other map[string]interface{}) (Prov return nil, err } - pipe, err := pipeline.New(nil, cc.Settings) - if err != nil { - return nil, err - } - - b, err := pipe.Process([]byte(cc.Value)) - if err != nil { - return nil, err - } - p := &constProvider{ ctx: ctx, - str: string(b), + str: cc.Value, set: cc.Set, } From f14cedc5129a97913f5a5ecd75678d395f3d6c5d Mon Sep 17 00:00:00 2001 From: andig Date: Sat, 4 Jan 2025 14:53:50 +0100 Subject: [PATCH 6/8] wip --- provider/charger.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/provider/charger.go b/provider/charger.go index e8214c2374..2496bc777b 100644 --- a/provider/charger.go +++ b/provider/charger.go @@ -45,6 +45,15 @@ func NewChargerEnableFromConfig(ctx context.Context, other map[string]interface{ return o, nil } +var _ FloatProvider = (*chargerProvider)(nil) + +func (o *chargerProvider) FloatGetter() (func() (float64, error), error) { + return func() (float64, error) { + v, err := o.charger.Enabled() + return cast.ToFloat64(v), err + }, nil +} + var _ IntProvider = (*chargerProvider)(nil) func (o *chargerProvider) IntGetter() (func() (int64, error), error) { From d1ebbed4f4f4bc26741530555719d60bc145b15c Mon Sep 17 00:00:00 2001 From: andig Date: Sat, 4 Jan 2025 16:56:43 +0100 Subject: [PATCH 7/8] wip --- provider/{charger.go => switch-charger.go} | 35 ++++++++++------------ 1 file changed, 15 insertions(+), 20 deletions(-) rename provider/{charger.go => switch-charger.go} (51%) diff --git a/provider/charger.go b/provider/switch-charger.go similarity index 51% rename from provider/charger.go rename to provider/switch-charger.go index 2496bc777b..dd4cf8b2a8 100644 --- a/provider/charger.go +++ b/provider/switch-charger.go @@ -2,7 +2,6 @@ package provider import ( "context" - "fmt" "github.com/evcc-io/evcc/api" charger "github.com/evcc-io/evcc/charger/config" @@ -11,16 +10,16 @@ import ( "github.com/spf13/cast" ) -type chargerProvider struct { +type switchChargerProvider struct { charger api.Charger } func init() { - registry.AddCtx("charger", NewChargerEnableFromConfig) + registry.AddCtx("switch", NewSwitchEnableFromConfig) } -// NewChargerEnableFromConfig creates type conversion provider -func NewChargerEnableFromConfig(ctx context.Context, other map[string]interface{}) (Provider, error) { +// NewSwitchEnableFromConfig creates type conversion provider +func NewSwitchEnableFromConfig(ctx context.Context, other map[string]interface{}) (Provider, error) { var cc struct { Config config.Typed } @@ -29,43 +28,39 @@ func NewChargerEnableFromConfig(ctx context.Context, other map[string]interface{ return nil, err } - if cc.Config.Type == "" { - return nil, fmt.Errorf("missing charger") - } - charger, err := charger.NewFromConfig(ctx, cc.Config.Type, cc.Config.Other) if err != nil { return nil, err } - o := &chargerProvider{ + o := &switchChargerProvider{ charger: charger, } return o, nil } -var _ FloatProvider = (*chargerProvider)(nil) +var _ FloatProvider = (*switchChargerProvider)(nil) -func (o *chargerProvider) FloatGetter() (func() (float64, error), error) { +func (o *switchChargerProvider) FloatGetter() (func() (float64, error), error) { return func() (float64, error) { v, err := o.charger.Enabled() return cast.ToFloat64(v), err }, nil } -var _ IntProvider = (*chargerProvider)(nil) +var _ IntProvider = (*switchChargerProvider)(nil) -func (o *chargerProvider) IntGetter() (func() (int64, error), error) { +func (o *switchChargerProvider) IntGetter() (func() (int64, error), error) { return func() (int64, error) { v, err := o.charger.Enabled() return cast.ToInt64(v), err }, nil } -var _ SetIntProvider = (*chargerProvider)(nil) +var _ SetIntProvider = (*switchChargerProvider)(nil) -func (o *chargerProvider) IntSetter(param string) (func(int64) error, error) { +func (o *switchChargerProvider) IntSetter(param string) (func(int64) error, error) { return func(val int64) error { b, err := cast.ToBoolE(val) if err != nil { @@ -75,17 +70,17 @@ func (o *chargerProvider) IntSetter(param string) (func(int64) error, error) { }, nil } -var _ BoolProvider = (*chargerProvider)(nil) +var _ BoolProvider = (*switchChargerProvider)(nil) -func (o *chargerProvider) BoolGetter() (func() (bool, error), error) { +func (o *switchChargerProvider) BoolGetter() (func() (bool, error), error) { return func() (bool, error) { return o.charger.Enabled() }, nil } -var _ SetBoolProvider = (*chargerProvider)(nil) +var _ SetBoolProvider = (*switchChargerProvider)(nil) -func (o *chargerProvider) BoolSetter(param string) (func(bool) error, error) { +func (o *switchChargerProvider) BoolSetter(param string) (func(bool) error, error) { return func(val bool) error { return o.charger.Enable(val) }, nil From 38fc148787be77eb9fa85d90285981bba4658456 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 5 Jan 2025 11:06:23 +0100 Subject: [PATCH 8/8] wip --- provider/{switch-charger.go => charger.go} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename provider/{switch-charger.go => charger.go} (88%) diff --git a/provider/switch-charger.go b/provider/charger.go similarity index 88% rename from provider/switch-charger.go rename to provider/charger.go index dd4cf8b2a8..6696f33118 100644 --- a/provider/switch-charger.go +++ b/provider/charger.go @@ -15,11 +15,11 @@ type switchChargerProvider struct { } func init() { - registry.AddCtx("switch", NewSwitchEnableFromConfig) + registry.AddCtx("charger", NewChargerEnableFromConfig) } -// NewSwitchEnableFromConfig creates type conversion provider -func NewSwitchEnableFromConfig(ctx context.Context, other map[string]interface{}) (Provider, error) { +// NewChargerEnableFromConfig creates type conversion provider +func NewChargerEnableFromConfig(ctx context.Context, other map[string]interface{}) (Provider, error) { var cc struct { Config config.Typed }