From b25b20e9c60fd9f0208368a9250b5568bf217045 Mon Sep 17 00:00:00 2001 From: andig Date: Thu, 31 Dec 2020 11:31:47 +0100 Subject: [PATCH 1/3] Refactor openWB implementation --- charger/openwb.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/charger/openwb.go b/charger/openwb.go index 86ce47dc4..e63bc7ffe 100644 --- a/charger/openwb.go +++ b/charger/openwb.go @@ -41,14 +41,19 @@ func NewOpenWBFromConfig(other map[string]interface{}) (api.Charger, error) { log := util.NewLogger("openwb") - client, err := mqtt.RegisteredClientOrDefault(log, cc.Config) + return NewOpenWB(log, cc.Config, cc.ID, cc.Topic, cc.Timeout) +} + +// NewOpenWB creates a new configurable charger +func NewOpenWB(log *util.Logger, mqttconf mqtt.Config, id int, topic string, timeout time.Duration) (*OpenWB, error) { + client, err := mqtt.RegisteredClientOrDefault(log, mqttconf) if err != nil { return nil, err } // timeout handler timer := provider.NewMqtt(log, client, - fmt.Sprintf("%s/system/%s", cc.Topic, openwb.TimestampTopic), "", 1, cc.Timeout, + fmt.Sprintf("%s/system/%s", topic, openwb.TimestampTopic), "", 1, timeout, ).IntGetter() // getters @@ -73,34 +78,34 @@ func NewOpenWBFromConfig(other map[string]interface{}) (api.Charger, error) { } // check if loadpoint configured - configured := boolG(fmt.Sprintf("%s/lp/%d/%s", cc.Topic, cc.ID, openwb.ConfiguredTopic)) + configured := boolG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.ConfiguredTopic)) if isConfigured, err := configured(); err != nil || !isConfigured { - return nil, fmt.Errorf("openWB loadpoint %d is not configured", cc.ID) + return nil, fmt.Errorf("openWB loadpoint %d is not configured", id) } // adapt plugged/charging to status - plugged := boolG(fmt.Sprintf("%s/lp/%d/%s", cc.Topic, cc.ID, openwb.PluggedTopic)) - charging := boolG(fmt.Sprintf("%s/lp/%d/%s", cc.Topic, cc.ID, openwb.ChargingTopic)) + plugged := boolG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.PluggedTopic)) + charging := boolG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.ChargingTopic)) status := provider.NewOpenWBStatusProvider(plugged, charging).StringGetter // remaining getters - enabled := boolG(fmt.Sprintf("%s/lp/%d/%s", cc.Topic, cc.ID, openwb.EnabledTopic)) + enabled := boolG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.EnabledTopic)) // setters enable := provider.NewMqtt(log, client, - fmt.Sprintf("%s/set/lp%d/%s", cc.Topic, cc.ID, openwb.EnabledTopic), - "", 1, cc.Timeout).BoolSetter("enable") + fmt.Sprintf("%s/set/lp%d/%s", topic, id, openwb.EnabledTopic), + "", 1, timeout).BoolSetter("enable") maxcurrent := provider.NewMqtt(log, client, - fmt.Sprintf("%s/set/lp%d/%s", cc.Topic, cc.ID, openwb.MaxCurrentTopic), - "", 1, cc.Timeout).IntSetter("maxcurrent") + fmt.Sprintf("%s/set/lp%d/%s", topic, id, openwb.MaxCurrentTopic), + "", 1, timeout).IntSetter("maxcurrent") // meter getters - power := floatG(fmt.Sprintf("%s/lp/%d/%s", cc.Topic, cc.ID, openwb.ChargePowerTopic)) - totalEnergy := floatG(fmt.Sprintf("%s/lp/%d/%s", cc.Topic, cc.ID, openwb.ChargeTotalEnergyTopic)) + power := floatG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.ChargePowerTopic)) + totalEnergy := floatG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.ChargeTotalEnergyTopic)) var currents []func() (float64, error) for i := 1; i <= 3; i++ { - current := floatG(fmt.Sprintf("%s/lp/%d/%s%d", cc.Topic, cc.ID, openwb.CurrentTopic, i)) + current := floatG(fmt.Sprintf("%s/lp/%d/%s%d", topic, id, openwb.CurrentTopic, i)) currents = append(currents, current) } From dd9ad41748c18ecd2a18ffbfc0b19a7fcd86d43d Mon Sep 17 00:00:00 2001 From: andig Date: Thu, 31 Dec 2020 12:35:33 +0100 Subject: [PATCH 2/3] 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") + } +} From 4dfbbbdc2fd09859bc4519a424dbbc756f0c0f35 Mon Sep 17 00:00:00 2001 From: andig Date: Thu, 31 Dec 2020 12:35:55 +0100 Subject: [PATCH 3/3] Speed up tests --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 4dfe3fc3d..2538e84ba 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.13 require ( github.com/PuerkitoBio/goquery v1.6.0 - github.com/andig/evcc-config v0.0.0-20201219105822-c89d29a41875 + github.com/andig/evcc-config v0.0.0-20201219164310-ee176b2c66ef github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef github.com/avast/retry-go v3.0.0+incompatible github.com/benbjohnson/clock v1.0.3 diff --git a/go.sum b/go.sum index de866137f..f7de45803 100644 --- a/go.sum +++ b/go.sum @@ -17,8 +17,8 @@ github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk5 github.com/alvaroloes/enumer v1.1.2 h1:5khqHB33TZy1GWCO/lZwcroBFh7u+0j40T83VUbfAMY= github.com/alvaroloes/enumer v1.1.2/go.mod h1:FxrjvuXoDAx9isTJrv4c+T410zFi0DtXIT0m65DJ+Wo= github.com/andig/evcc v0.0.0-20200727161511-d58eb15f2dc9/go.mod h1:8HONEC6cC2s4k0u3QL7GIjrYOZYTOKiiXybw0FIJL0A= -github.com/andig/evcc-config v0.0.0-20201219105822-c89d29a41875 h1:hCl4v1ZjhvfHFakIRLZiJfuraXSM0BN/4rou/yZmTCc= -github.com/andig/evcc-config v0.0.0-20201219105822-c89d29a41875/go.mod h1:N0hIjIy+5E2AR1fF7Tg2IzBlblBrnFvCCaDGAaHzbWk= +github.com/andig/evcc-config v0.0.0-20201219164310-ee176b2c66ef h1:f3TGa0Gz0YApZGnsXm/NdIimxQMFN2uNt6TtwZKpypQ= +github.com/andig/evcc-config v0.0.0-20201219164310-ee176b2c66ef/go.mod h1:N0hIjIy+5E2AR1fF7Tg2IzBlblBrnFvCCaDGAaHzbWk= github.com/andig/gosunspec v0.0.0-20200429133549-3cf6a82fed9c h1:AMtX56iHlNYVxMID7fe9efuVtaxgtdjyMeolg7q87IE= github.com/andig/gosunspec v0.0.0-20200429133549-3cf6a82fed9c/go.mod h1:YkshK8WMzYn1iXAZzHUO75gIqhMSan2ctgBVtBkRIyA= github.com/andig/viper v1.6.3-0.20201123175942-a5af09afab5b h1:n3O7DTcqZzpl8/6zVCCkHYbc/zyTfXQCk4xaTAkQ5aE=