forked from evcc-io/evcc
/
openwb.go
121 lines (98 loc) · 2.78 KB
/
openwb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package meter
import (
"errors"
"fmt"
"strings"
"time"
"github.com/mark-sch/evcc/api"
"github.com/mark-sch/evcc/internal/charger/openwb"
"github.com/mark-sch/evcc/provider"
"github.com/mark-sch/evcc/provider/mqtt"
"github.com/mark-sch/evcc/util"
)
func init() {
registry.Add("openwb", NewOpenWBFromConfig)
}
// NewOpenWBFromConfig creates a new configurable meter
func NewOpenWBFromConfig(other map[string]interface{}) (api.Meter, error) {
cc := struct {
mqtt.Config `mapstructure:",squash"`
Topic string
Timeout time.Duration
Usage string
}{
Topic: "openWB",
Timeout: 15 * time.Second,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
log := util.NewLogger("openwb")
client, err := mqtt.RegisteredClientOrDefault(log, cc.Config)
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,
).IntGetter()
// getters
boolG := func(topic string) func() (bool, error) {
g := provider.NewMqtt(log, client, topic, "", 1, 0).BoolGetter()
return func() (val bool, err error) {
if val, err = g(); err == nil {
_, err = timer()
}
return val, err
}
}
floatG := func(topic string) func() (float64, error) {
g := provider.NewMqtt(log, client, topic, "", 1, 0).FloatGetter()
return func() (val float64, err error) {
if val, err = g(); err == nil {
_, err = timer()
}
return val, err
}
}
var power func() (float64, error)
var soc func() (float64, error)
var currents []func() (float64, error)
switch strings.ToLower(cc.Usage) {
case "grid":
power = floatG(fmt.Sprintf("%s/evu/%s", cc.Topic, openwb.PowerTopic))
for i := 1; i <= 3; i++ {
current := floatG(fmt.Sprintf("%s/evu/%s%d", cc.Topic, openwb.CurrentTopic, i))
currents = append(currents, current)
}
case "pv":
configuredG := boolG(fmt.Sprintf("%s/pv/%s", cc.Topic, openwb.PvConfigured))
configured, err := configuredG()
if err != nil {
return nil, err
}
if !configured {
return nil, errors.New("pv not available")
}
power = floatG(fmt.Sprintf("%s/pv/%s", cc.Topic, openwb.PowerTopic))
case "battery":
configuredG := boolG(fmt.Sprintf("%s/housebattery/%s", cc.Topic, openwb.BatteryConfigured))
configured, err := configuredG()
if err != nil {
return nil, err
}
if !configured {
return nil, errors.New("battery not available")
}
power = floatG(fmt.Sprintf("%s/housebattery/%s", cc.Topic, openwb.PowerTopic))
soc = floatG(fmt.Sprintf("%s/housebattery/%s", cc.Topic, openwb.SoCTopic))
default:
return nil, fmt.Errorf("invalid usage: %s", cc.Usage)
}
m, err := NewConfigurable(power)
if err != nil {
return nil, err
}
res := m.Decorate(nil, currents, soc)
return res, nil
}