From dd9ad41748c18ecd2a18ffbfc0b19a7fcd86d43d Mon Sep 17 00:00:00 2001 From: andig Date: Thu, 31 Dec 2020 12:35:33 +0100 Subject: [PATCH] Fix energy and currents not exposed --- charger/openwb.go | 55 ++++++++++++++++++++++++++++------------ charger/openwb/topics.go | 7 +++++ charger/openwb_test.go | 26 +++++++++++++++++++ 3 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 charger/openwb_test.go diff --git a/charger/openwb.go b/charger/openwb.go index e63bc7ffe..a2116fca4 100644 --- a/charger/openwb.go +++ b/charger/openwb.go @@ -6,7 +6,6 @@ import ( "github.com/andig/evcc/api" "github.com/andig/evcc/charger/openwb" - "github.com/andig/evcc/meter" "github.com/andig/evcc/provider" "github.com/andig/evcc/provider/mqtt" "github.com/andig/evcc/util" @@ -19,7 +18,9 @@ func init() { // OpenWB configures generic charger and charge meter for an openWB loadpoint type OpenWB struct { api.Charger - api.Meter + currentPowerG func() (float64, error) + totalEnergyG func() (float64, error) + currentsG []func() (float64, error) } // NewOpenWBFromConfig creates a new configurable charger @@ -30,8 +31,8 @@ func NewOpenWBFromConfig(other map[string]interface{}) (api.Charger, error) { Timeout time.Duration ID int }{ - Topic: "openWB", - Timeout: 15 * time.Second, + Topic: openwb.RootTopic, + Timeout: openwb.Timeout, ID: 1, } @@ -100,29 +101,51 @@ func NewOpenWB(log *util.Logger, mqttconf mqtt.Config, id int, topic string, tim "", 1, timeout).IntSetter("maxcurrent") // meter getters - power := floatG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.ChargePowerTopic)) - totalEnergy := floatG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.ChargeTotalEnergyTopic)) + currentPowerG := floatG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.ChargePowerTopic)) + totalEnergyG := floatG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.ChargeTotalEnergyTopic)) - var currents []func() (float64, error) + var currentsG []func() (float64, error) for i := 1; i <= 3; i++ { current := floatG(fmt.Sprintf("%s/lp/%d/%s%d", topic, id, openwb.CurrentTopic, i)) - currents = append(currents, current) + currentsG = append(currentsG, current) } - c, err := NewConfigurable(status, enabled, enable, maxcurrent) - if err != nil { - return nil, err - } - - m, err := meter.NewConfigurable(power) + charger, err := NewConfigurable(status, enabled, enable, maxcurrent) if err != nil { return nil, err } res := &OpenWB{ - Charger: c, - Meter: m.Decorate(totalEnergy, currents, nil), + Charger: charger, + currentPowerG: currentPowerG, + totalEnergyG: totalEnergyG, + currentsG: currentsG, } return res, nil } + +// CurrentPower implements the Meter.CurrentPower interface +func (m *OpenWB) CurrentPower() (float64, error) { + return m.currentPowerG() +} + +// TotalEnergy implements the Meter.TotalEnergy interface +func (m *OpenWB) TotalEnergy() (float64, error) { + return m.totalEnergyG() +} + +// Currents implements the Meter.Currents interface +func (m *OpenWB) Currents() (float64, float64, float64, error) { + var currents []float64 + for _, currentG := range m.currentsG { + c, err := currentG() + if err != nil { + return 0, 0, 0, err + } + + currents = append(currents, c) + } + + return currents[0], currents[1], currents[2], nil +} diff --git a/charger/openwb/topics.go b/charger/openwb/topics.go index 8ed860df0..5fdc493ae 100644 --- a/charger/openwb/topics.go +++ b/charger/openwb/topics.go @@ -1,7 +1,14 @@ package openwb +import "time" + // predefined openWB topic names const ( + Timeout = 15 * time.Second + + // root topic + RootTopic = "openWB" + // alive TimestampTopic = "Timestamp" diff --git a/charger/openwb_test.go b/charger/openwb_test.go new file mode 100644 index 000000000..90e69db3f --- /dev/null +++ b/charger/openwb_test.go @@ -0,0 +1,26 @@ +package charger + +import ( + "testing" + + "github.com/andig/evcc/api" +) + +func TestOpenWBDecorators(t *testing.T) { + // host not reachable + wb, _ := NewOpenWBFromConfig(map[string]interface{}{ + "broker": "192.0.2.2", + }) + + 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") + } +}