Skip to content

Commit

Permalink
Fix decorators hiding optional interfaces (evcc-io#507)
Browse files Browse the repository at this point in the history
Without this PR, decorating an api.Charger which has additional interfaces removed those interfaces from the decorated charger.
  • Loading branch information
andig authored Dec 7, 2020
1 parent bb688ec commit ac9f8ba
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 81 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type ChargerEx interface {

// Diagnosis is a helper interface that allows to dump diagnostic data to console
type Diagnosis interface {
Diagnosis()
Diagnose()
}

// ChargeTimer provides current charge cycle duration
Expand Down
2 changes: 1 addition & 1 deletion charger/evsewifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func init() {
registry.Add("evsewifi", NewEVSEWifiFromConfig)
}

//go:generate go run ../cmd/tools/decorate.go -p charger -f decorateEVSE -b api.Charger -o evsewifi_decorators -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.MeterCurrent,Currents,func() (float64, float64, float64, error)"
//go:generate go run ../cmd/tools/decorate.go -p charger -f decorateEVSE -o evsewifi_decorators -b *EVSEWifi -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.MeterCurrent,Currents,func() (float64, float64, float64, error)"

// NewEVSEWifiFromConfig creates a EVSEWifi charger from generic config
func NewEVSEWifiFromConfig(other map[string]interface{}) (api.Charger, error) {
Expand Down
30 changes: 15 additions & 15 deletions charger/evsewifi_decorators.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions charger/evsewifi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestEvseWifi(t *testing.T) {
evse, err := NewEVSEWifiFromConfig(map[string]interface{}{
wb, err := NewEVSEWifiFromConfig(map[string]interface{}{
"meter": map[string]interface{}{
"power": true,
"energy": true,
Expand All @@ -19,11 +19,15 @@ func TestEvseWifi(t *testing.T) {
t.Error(err)
}

if _, ok := evse.(api.MeterEnergy); !ok {
if _, ok := wb.(api.MeterEnergy); !ok {
t.Error("missing api.MeterEnergy")
}

if _, ok := evse.(api.MeterCurrent); !ok {
if _, ok := wb.(api.MeterCurrent); !ok {
t.Error("missing api.MeterCurrent")
}

if _, ok := wb.(api.ChargeTimer); !ok {
t.Error("missing ChargeTimer api")
}
}
4 changes: 2 additions & 2 deletions charger/keba.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ func (c *Keba) Currents() (float64, float64, float64, error) {
return float64(kr.I1) / 1e3, float64(kr.I2) / 1e3, float64(kr.I3) / 1e3, err
}

// Diagnosis implements the Diagnosis interface
func (c *Keba) Diagnosis() {
// Diagnose implements the Diagnosis interface
func (c *Keba) Diagnose() {
var kr keba.Report100
if err := c.roundtrip("report 100", 100, &kr); err == nil {
fmt.Printf("%+v\n", kr)
Expand Down
2 changes: 1 addition & 1 deletion charger/phoenix-em-cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
registry.Add("phoenix-emcp", NewPhoenixEMCPFromConfig)
}

//go:generate go run ../cmd/tools/decorate.go -p charger -f decoratePhoenixEMCP -b api.Charger -o phoenix-em-cp_decorators -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.MeterCurrent,Currents,func() (float64, float64, float64, error)"
//go:generate go run ../cmd/tools/decorate.go -p charger -f decoratePhoenixEMCP -o phoenix-em-cp_decorators -b *PhoenixEMCP -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.MeterCurrent,Currents,func() (float64, float64, float64, error)"

// NewPhoenixEMCPFromConfig creates a Phoenix charger from generic config
func NewPhoenixEMCPFromConfig(other map[string]interface{}) (api.Charger, error) {
Expand Down
30 changes: 15 additions & 15 deletions charger/phoenix-em-cp_decorators.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions charger/phoenix-em-cp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package charger

import (
"testing"

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

func TestPhoenixEMCPDecorators(t *testing.T) {
wb, err := NewPhoenixEMCPFromConfig(map[string]interface{}{
"meter": map[string]interface{}{
"power": true,
"energy": true,
"currents": true,
},
})
if err != nil {
t.Error(err)
}

if _, ok := wb.(api.Meter); !ok {
t.Error("missing Meter api")
}

if _, ok := wb.(api.MeterEnergy); !ok {
t.Error("missing MeterEnergy api")
}

if _, ok := wb.(api.MeterCurrent); !ok {
t.Error("missing MeterCurrent api")
}

if _, ok := wb.(api.ChargeTimer); !ok {
t.Error("missing ChargeTimer api")
}
}
11 changes: 10 additions & 1 deletion charger/wallbe.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/andig/evcc/api"
"github.com/andig/evcc/util"
"github.com/andig/evcc/util/modbus"
"github.com/volkszaehler/mbmd/encoding"
)

const (
Expand All @@ -20,6 +21,7 @@ const (
wbRegActualCurrent = 300 // Holding
wbRegEnable = 400 // Coil
wbRegMaxCurrent = 528 // Holding
wbRegFirmware = 149 // Firmware

wbRegPower = 120 // power reading
wbRegEnergy = 128 // energy reading
Expand All @@ -43,7 +45,7 @@ func init() {
registry.Add("wallbe", NewWallbeFromConfig)
}

// go:generate go run ../cmd/tools/decorate.go -p charger -f decorateWallbe -b api.Charger -o wallbe_decorators -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.MeterCurrent,Currents,func() (float64, float64, float64, error)" -t "api.ChargerEx,MaxCurrentMillis,func(current float64) error"
// go:generate go run ../cmd/tools/decorate.go -p charger -f decorateWallbe -o wallbe_decorators -b *Wallbe -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.MeterCurrent,Currents,func() (float64, float64, float64, error)" -t "api.ChargerEx,MaxCurrentMillis,func(current float64) error"

// NewWallbeFromConfig creates a Wallbe charger from generic config
func NewWallbeFromConfig(other map[string]interface{}) (api.Charger, error) {
Expand Down Expand Up @@ -231,3 +233,10 @@ func (wb *Wallbe) currents() (float64, float64, float64, error) {

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

// Diagnose implements the Diagnosis interface
func (wb *Wallbe) Diagnose() {
if b, err := wb.conn.ReadInputRegisters(wbRegFirmware, 6); err == nil {
fmt.Printf("Firmware:\t%s\n", encoding.StringSwapped(b))
}
}
Loading

0 comments on commit ac9f8ba

Please sign in to comment.