Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add currents to default meter #267

Merged
merged 3 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 additions & 2 deletions core/wrapper/chargerater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
"time"

"github.com/andig/evcc/meter"
"github.com/andig/evcc/api"
"github.com/andig/evcc/mock"
"github.com/andig/evcc/util"
"github.com/benbjohnson/clock"
Expand Down Expand Up @@ -45,7 +45,13 @@ func TestWrappedMeter(t *testing.T) {

mm := mock.NewMockMeter(ctrl)
me := mock.NewMockMeterEnergy(ctrl)
cm := &meter.EnergyDecorator{Meter: mm, MeterEnergy: me}

type EnergyDecorator struct {
api.Meter
api.MeterEnergy
}

cm := &EnergyDecorator{Meter: mm, MeterEnergy: me}

me.EXPECT().
TotalEnergy().
Expand Down
112 changes: 99 additions & 13 deletions meter/meter.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package meter

import (
"errors"
"fmt"

"github.com/andig/evcc/api"
"github.com/andig/evcc/provider"
"github.com/andig/evcc/util"
)

// EnergyDecorator decorates an api.Meter with api.MeterEnergy
type EnergyDecorator struct {
api.Meter
api.MeterEnergy
}

// NewConfigurableFromConfig creates api.Meter from config
func NewConfigurableFromConfig(other map[string]interface{}) (api.Meter, error) {
cc := struct {
Power provider.Config
Energy *provider.Config // optional
Power provider.Config
Energy *provider.Config // optional
Currents []provider.Config // optional
}{}

if err := util.DecodeOther(other, &cc); err != nil {
Expand All @@ -40,14 +36,54 @@ func NewConfigurableFromConfig(other map[string]interface{}) (api.Meter, error)

// decorate Meter with MeterEnergy
if cc.Energy != nil {
energy, err := provider.NewFloatGetterFromConfig(*cc.Energy)
energy, err := NewMeterEnergy(*cc.Energy)
if err != nil {
return nil, err
}

type EnergyDecorator struct {
api.Meter
api.MeterEnergy
}

m = &EnergyDecorator{
Meter: m,
MeterEnergy: NewMeterEnergy(energy),
MeterEnergy: energy,
}
}

// decorate Meter with MeterEnergy
if len(cc.Currents) > 0 {
currents, err := NewCurrents(cc.Currents)
if err != nil {
return nil, err
}

type PowerEnergy interface {
api.Meter
api.MeterEnergy
}

if pe, ok := m.(PowerEnergy); ok {
type CurrentDecorator struct {
PowerEnergy
api.MeterCurrent
}

m = &CurrentDecorator{
PowerEnergy: pe,
MeterCurrent: currents,
}
} else {
type CurrentDecorator struct {
api.Meter
api.MeterCurrent
}

m = &CurrentDecorator{
Meter: m,
MeterCurrent: currents,
}
}
}

Expand Down Expand Up @@ -77,14 +113,64 @@ type MeterEnergy struct {
totalEnergyG func() (float64, error)
}

// NewMeterEnergy creates a new charger
func NewMeterEnergy(totalEnergyG func() (float64, error)) api.MeterEnergy {
return &MeterEnergy{
// NewMeterEnergy creates a new api.MeterEnergy
func NewMeterEnergy(ccEnergy provider.Config) (api.MeterEnergy, error) {
totalEnergyG, err := provider.NewFloatGetterFromConfig(ccEnergy)
if err != nil {
return nil, err
}

e := &MeterEnergy{
totalEnergyG: totalEnergyG,
}

return e, nil
}

// TotalEnergy implements the Meter.TotalEnergy interface
func (m *MeterEnergy) TotalEnergy() (float64, error) {
return m.totalEnergyG()
}

// Currents is an api.MeterCurrent implementation
type Currents struct {
currentsG []func() (float64, error)
}

// NewCurrents creates a new api.MeterCurrent
func NewCurrents(ccCurrents []provider.Config) (api.MeterCurrent, error) {
if len(ccCurrents) != 3 {
return nil, errors.New("need 3 currents")
}

var currentsG []func() (float64, error)
for _, cc := range ccCurrents {
c, err := provider.NewFloatGetterFromConfig(cc)
if err != nil {
return nil, err
}

currentsG = append(currentsG, c)
}

c := &Currents{
currentsG: currentsG,
}

return c, nil
}

// Currents implements the api.Currents interface
func (c *Currents) Currents() (float64, float64, float64, error) {
var currents []float64
for _, currentG := range c.currentsG {
c, err := currentG()
if err != nil {
return 0, 0, 0, err
}

currents = append(currents, c)
}

return currents[0], currents[1], currents[2], nil
}