93 lines
1.9 KiB
Go
93 lines
1.9 KiB
Go
package measurement
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/evcc-io/evcc/api/implement"
|
|
"github.com/evcc-io/evcc/plugin"
|
|
)
|
|
|
|
type Dimmer struct {
|
|
Dim *plugin.Config // optional
|
|
Dimmed *plugin.Config // optional
|
|
}
|
|
|
|
func (cc *Dimmer) Configure(ctx context.Context) (
|
|
func(bool) error,
|
|
func() (bool, error),
|
|
error,
|
|
) {
|
|
dimS, err := cc.Dim.BoolSetter(ctx, "dim")
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("dim: %w", err)
|
|
}
|
|
|
|
dimmedG, err := cc.Dimmed.BoolGetter(ctx)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("dimmed: %w", err)
|
|
}
|
|
|
|
if (dimS == nil) != (dimmedG == nil) {
|
|
return nil, nil, errors.New("must have dim and dimmed or none of both")
|
|
}
|
|
|
|
return dimS, dimmedG, nil
|
|
}
|
|
|
|
func (cc *Dimmer) Implement(ctx context.Context, i implement.Caps) error {
|
|
dimS, dimmedG, err := cc.Configure(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
implement.May(i, implement.Dimmer(dimS, dimmedG))
|
|
|
|
return nil
|
|
}
|
|
|
|
type Curtailer struct {
|
|
Curtail *plugin.Config // optional
|
|
Curtailed *plugin.Config // optional
|
|
}
|
|
|
|
func (cc *Curtailer) Configure(ctx context.Context) (
|
|
func(int64) error,
|
|
func() (int64, error),
|
|
error,
|
|
) {
|
|
curtailS, err := cc.Curtail.IntSetter(ctx, "curtail")
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("curtail: %w", err)
|
|
}
|
|
|
|
curtailedG, err := cc.Curtailed.IntGetter(ctx)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("curtailed: %w", err)
|
|
}
|
|
|
|
if (curtailS == nil) != (curtailedG == nil) {
|
|
return nil, nil, errors.New("must have curtail and curtailed or none of both")
|
|
}
|
|
|
|
return curtailS, curtailedG, nil
|
|
}
|
|
|
|
func (cc *Curtailer) Implement(ctx context.Context, i implement.Caps) error {
|
|
curtailS, curtailedG, err := cc.Configure(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if curtailS != nil {
|
|
getPercent := func() (int, error) {
|
|
percent, err := curtailedG()
|
|
return int(percent), err
|
|
}
|
|
setPercent := func(percent int) error { return curtailS(int64(percent)) }
|
|
implement.May(i, implement.Curtailer(getPercent, setPercent))
|
|
}
|
|
|
|
return nil
|
|
}
|