Merge pull request #558 from andig/chore/split-openwb

Fix openWB energy and currents not exposed
This commit is contained in:
andig 2020-12-31 12:38:44 +01:00 • committed by GitHub
commit 133c8c382e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 92 additions and 31 deletions

View file

@ -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,
}
@ -41,14 +42,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,51 +79,73 @@ 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))
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", cc.Topic, cc.ID, openwb.CurrentTopic, i))
currents = append(currents, current)
current := floatG(fmt.Sprintf("%s/lp/%d/%s%d", topic, id, openwb.CurrentTopic, i))
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
}

View file

@ -1,7 +1,14 @@
package openwb
import "time"
// predefined openWB topic names
const (
Timeout = 15 * time.Second
// root topic
RootTopic = "openWB"
// alive
TimestampTopic = "Timestamp"

26
charger/openwb_test.go Normal file
View file

@ -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")
}
}

2
go.mod
View file

@ -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

4
go.sum
View file

@ -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=