forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenwb.go
277 lines (232 loc) · 6.86 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package charger
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/openwb"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/provider/mqtt"
"github.com/evcc-io/evcc/util"
)
func init() {
registry.Add("openwb", NewOpenWBFromConfig)
}
// OpenWB configures generic charger and charge meter for an openWB loadpoint
type OpenWB struct {
current int64
enabled bool
statusG func() (string, error)
currentS func(int64) error
currentPowerG func() (float64, error)
totalEnergyG func() (float64, error)
currentsG []func() (float64, error)
wakeupS func(int64) error
authS func(string) error
}
//go:generate go run ../cmd/tools/decorate.go -f decorateOpenWB -b *OpenWB -r api.Charger -t "api.PhaseSwitcher,Phases1p3p,func(int) error" -t "api.Battery,Soc,func() (float64, error)"
// NewOpenWBFromConfig creates a new configurable charger
func NewOpenWBFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
mqtt.Config `mapstructure:",squash"`
Topic string
Timeout time.Duration
ID int
Phases1p3p, DC bool
}{
Topic: openwb.RootTopic,
Timeout: openwb.Timeout,
ID: 1,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
log := util.NewLogger("openwb")
return NewOpenWB(log, cc.Config, cc.ID, cc.Topic, cc.Phases1p3p, cc.DC, cc.Timeout)
}
// NewOpenWB creates a new configurable charger
func NewOpenWB(log *util.Logger, mqttconf mqtt.Config, id int, topic string, p1p3, dc bool, timeout time.Duration) (api.Charger, error) {
client, err := mqtt.RegisteredClientOrDefault(log, mqttconf)
if err != nil {
return nil, err
}
// timeout handler
h, err := provider.NewMqtt(log, client, fmt.Sprintf("%s/system/%s", topic, openwb.TimestampTopic), timeout).StringGetter()
if err != nil {
return nil, err
}
to := provider.NewTimeoutHandler(h)
mq := func(subtopic string) *provider.Mqtt {
return provider.NewMqtt(log, client, fmt.Sprintf("%s/lp/%d/%s", topic, id, subtopic), 0)
}
// check if loadpoint configured
configured, err := provider.NewMqtt(log, client, fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.ConfiguredTopic), timeout).BoolGetter()
if err != nil {
return nil, err
}
if isConfigured, err := configured(); err != nil || !isConfigured {
return nil, fmt.Errorf("loadpoint %d is not configured", id)
}
// adapt plugged/charging to status
pluggedG, err := to.BoolGetter(mq(openwb.PluggedTopic))
if err != nil {
return nil, err
}
chargingG, err := to.BoolGetter(mq(openwb.ChargingTopic))
if err != nil {
return nil, err
}
statusG := provider.NewOpenWBStatusProvider(pluggedG, chargingG).StringGetter
// setters
currentTopic := openwb.SlaveChargeCurrentTopic
if id == 2 {
// TODO remove after https://github.com/snaptec/openWB/issues/1757
currentTopic = "Lp2" + openwb.SlaveChargeCurrentTopic
}
currentS, err := provider.NewMqtt(log, client, fmt.Sprintf("%s/set/isss/%s", topic, currentTopic),
timeout).WithRetained().IntSetter("current")
if err != nil {
return nil, err
}
cpTopic := openwb.SlaveCPInterruptTopic
if id == 2 {
// TODO remove after https://github.com/snaptec/openWB/issues/1757
cpTopic = strings.TrimSuffix(cpTopic, "1") + "2"
}
wakeupS, err := provider.NewMqtt(log, client, fmt.Sprintf("%s/set/isss/%s", topic, cpTopic),
timeout).WithRetained().IntSetter("cp")
if err != nil {
return nil, err
}
authS, err := provider.NewMqtt(log, client, fmt.Sprintf("%s/set/chargepoint/%d/set/%s", topic, id, openwb.RfidTopic),
timeout).WithRetained().StringSetter("rfid")
if err != nil {
return nil, err
}
// meter getters
currentPowerG, err := to.FloatGetter(mq(openwb.ChargePowerTopic))
if err != nil {
return nil, err
}
totalEnergyG, err := to.FloatGetter(mq(openwb.ChargeTotalEnergyTopic))
if err != nil {
return nil, err
}
var currentsG []func() (float64, error)
for i := 1; i <= 3; i++ {
current, err := to.FloatGetter(mq(openwb.CurrentTopic + strconv.Itoa(i)))
if err != nil {
return nil, err
}
currentsG = append(currentsG, current)
}
c := &OpenWB{
currentS: currentS,
statusG: statusG,
currentPowerG: currentPowerG,
totalEnergyG: totalEnergyG,
currentsG: currentsG,
wakeupS: wakeupS,
authS: authS,
}
// heartbeat
heartbeatS, err := provider.NewMqtt(log, client, fmt.Sprintf("%s/set/isss/%s", topic, openwb.SlaveHeartbeatTopic),
timeout).WithRetained().IntSetter("heartbeat")
if err != nil {
return nil, err
}
go func() {
for range time.Tick(openwb.HeartbeatInterval) {
if err := heartbeatS(1); err != nil {
log.ERROR.Printf("heartbeat: %v", err)
}
}
}()
// optional capabilities
var phases func(int) error
if p1p3 {
phasesTopic := openwb.SlavePhasesTopic
if id == 2 {
// TODO remove after https://github.com/snaptec/openWB/issues/1757
phasesTopic += "Lp2"
}
phasesS, err := provider.NewMqtt(log, client, fmt.Sprintf("%s/set/isss/%s", topic, phasesTopic),
timeout).WithRetained().IntSetter("phases")
if err != nil {
return nil, err
}
phases = func(phases int) error {
return phasesS(int64(phases))
}
}
var soc func() (float64, error)
if dc {
soc, err = to.FloatGetter(mq(openwb.VehicleSocTopic))
if err != nil {
return nil, err
}
}
return decorateOpenWB(c, phases, soc), nil
}
func (m *OpenWB) Enable(enable bool) error {
var current int64
if enable {
current = m.current
}
err := m.currentS(current)
if err == nil {
m.enabled = enable
}
return err
}
func (m *OpenWB) Enabled() (bool, error) {
return verifyEnabled(m, m.enabled)
}
func (m *OpenWB) Status() (api.ChargeStatus, error) {
status, err := m.statusG()
if err != nil {
return api.StatusNone, err
}
return api.ChargeStatusString(status)
}
func (m *OpenWB) MaxCurrent(current int64) error {
err := m.currentS(current)
if err == nil {
m.current = current
}
return err
}
var _ api.Meter = (*OpenWB)(nil)
// CurrentPower implements the api.Meter interface
func (m *OpenWB) CurrentPower() (float64, error) {
return m.currentPowerG()
}
var _ api.MeterEnergy = (*OpenWB)(nil)
// TotalEnergy implements the api.MeterEnergy interface
func (m *OpenWB) TotalEnergy() (float64, error) {
return m.totalEnergyG()
}
var _ api.PhaseCurrents = (*OpenWB)(nil)
// Currents implements the api.PhaseCurrents interface
func (m *OpenWB) Currents() (float64, float64, float64, error) {
var res []float64
for _, currentG := range m.currentsG {
c, err := currentG()
if err != nil {
return 0, 0, 0, err
}
res = append(res, c)
}
return res[0], res[1], res[2], nil
}
var _ api.Authorizer = (*OpenWB)(nil)
// Authorize implements the api.Authorizer interface
func (m *OpenWB) Authorize(key string) error {
return m.authS(key)
}
var _ api.Resurrector = (*OpenWB)(nil)
// WakeUp implements the api.Resurrector interface
func (m *OpenWB) WakeUp() error {
return m.wakeupS(1)
}