tariff/config.go
Rama master · réplica de lectura del repositorio original.
package tariff
import (
"context"
"fmt"
"slices"
"strings"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
reg "github.com/evcc-io/evcc/util/registry"
)
var registry = reg.New[api.Tariff]("tariff")
// Types returns the list of types
func Types() []string {
return registry.Types()
}
// NewFromConfig creates tariff from configuration
func NewFromConfig(ctx context.Context, typ string, other map[string]any) (api.Tariff, error) {
factory, err := registry.Get(strings.ToLower(typ))
if err != nil {
return nil, err
}
v, err := factory(ctx, other)
if err != nil {
return nil, fmt.Errorf("cannot create tariff type '%s': %w", util.TypeWithTemplateName(typ, other), err)
}
// check slot length
if rr, err := v.Rates(); err == nil && len(rr) > 0 {
if r, ok := shortSlot(rr); ok {
return nil, fmt.Errorf("tariff type '%s': slot duration %v shorter than %v", util.TypeWithTemplateName(typ, other), r.End.Sub(r.Start), SlotDuration)
}
if !slices.ContainsFunc(rr, func(r api.Rate) bool {
return r.End.Sub(r.Start) != SlotDuration
}) {
return v, nil
}
}
return &SlotWrapper{Tariff: v}, nil
}