Skip to content

Commit

Permalink
chore: make type conversion provider more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Nov 27, 2023
1 parent 36945ba commit 65e470b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 35 deletions.
55 changes: 55 additions & 0 deletions provider/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package provider

import (
"fmt"

"github.com/evcc-io/evcc/util"
)

type convertProvider struct {
Convert string
Set Config
}

func init() {
registry.Add("convert", NewConvertFromConfig)
}

// NewConvertFromConfig creates type conversion provider
func NewConvertFromConfig(other map[string]interface{}) (Provider, error) {
var cc convertProvider

if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}

return &cc, nil
}

var _ SetFloatProvider = (*convertProvider)(nil)

func (o *convertProvider) FloatSetter(param string) (func(float64) error, error) {
if o.Convert == "float2int" {
return nil, fmt.Errorf("convert: invalid conversion: %s", o.Convert)
}

set, err := NewIntSetterFromConfig(param, o.Set)

return func(val float64) error {
return set(int64(val))
}, err
}

var _ SetIntProvider = (*convertProvider)(nil)

func (o *convertProvider) IntSetter(param string) (func(int64) error, error) {
if o.Convert == "int2float" {
return nil, fmt.Errorf("convert: invalid conversion: %s", o.Convert)
}

set, err := NewFloatSetterFromConfig(param, o.Set)

return func(val int64) error {
return set(float64(val))
}, err
}
34 changes: 0 additions & 34 deletions provider/float_to_int.go

This file was deleted.

3 changes: 2 additions & 1 deletion templates/definition/meter/victron-energy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ render: |
type: input
decode: uint16
limitsoc:
source: float2int
source: convert
convert: float2int
set:
source: modbus
uri: {{ .host }}:{{ .port }}
Expand Down

0 comments on commit 65e470b

Please sign in to comment.