Fix energy and currents not exposed

This commit is contained in:
andig 2020-12-31 12:35:33 +01:00
parent b25b20e9c6
commit dd9ad41748
3 changed files with 72 additions and 16 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,
}
@ -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
}

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