forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarp.go
313 lines (248 loc) Β· 7.16 KB
/
warp.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package charger
import (
"encoding/json"
"errors"
"fmt"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/warp"
v1 "github.com/evcc-io/evcc/charger/warp/v1"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/provider/mqtt"
"github.com/evcc-io/evcc/util"
)
// Warp is the Warp charger implementation
type Warp struct {
log *util.Logger
root string
client *mqtt.Client
enabledG func() (string, error)
statusG func() (string, error)
meterG func() (string, error)
meterDetailsG func() (string, error)
enableS func(bool) error
maxcurrentS func(int64) error
enabled bool // cache
}
func init() {
registry.Add("warp", NewWarpFromConfig)
}
//go:generate go run ../cmd/tools/decorate.go -f decorateWarp -b *Warp -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.PhaseCurrents,Currents,func() (float64, float64, float64, error)"
// NewWarpFromConfig creates a new configurable charger
func NewWarpFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
mqtt.Config `mapstructure:",squash"`
Topic string
Timeout time.Duration
}{
Topic: warp.RootTopic,
Timeout: warp.Timeout,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
wb, err := NewWarp(cc.Config, cc.Topic, cc.Timeout)
if err != nil {
return nil, err
}
hasMeter := wb.hasMeter()
var currentPower, totalEnergy func() (float64, error)
if hasMeter {
currentPower = wb.currentPower
totalEnergy = wb.totalEnergy
}
var currents func() (float64, float64, float64, error)
if hasMeter && wb.hasCurrents() {
currents = wb.currents
}
return decorateWarp(wb, currentPower, totalEnergy, currents), err
}
// NewWarp creates a new configurable charger
func NewWarp(mqttconf mqtt.Config, topic string, timeout time.Duration) (*Warp, error) {
log := util.NewLogger("warp")
client, err := mqtt.RegisteredClientOrDefault(log, mqttconf)
if err != nil {
return nil, err
}
wb := &Warp{
log: log,
root: topic,
client: client,
}
// timeout handler
to := provider.NewTimeoutHandler(provider.NewMqtt(log, client,
fmt.Sprintf("%s/evse/state", topic), timeout,
).StringGetter())
stringG := func(topic string) func() (string, error) {
g := provider.NewMqtt(log, client, topic, 0).StringGetter()
return to.StringGetter(g)
}
wb.enabledG = stringG(fmt.Sprintf("%s/evse/auto_start_charging", topic))
wb.statusG = stringG(fmt.Sprintf("%s/evse/state", topic))
wb.meterG = stringG(fmt.Sprintf("%s/meter/state", topic))
wb.meterDetailsG = stringG(fmt.Sprintf("%s/meter/detailed_values", topic))
wb.enableS = provider.NewMqtt(log, client,
fmt.Sprintf("%s/evse/auto_start_charging_update", topic), 0).
WithPayload(`{ "auto_start_charging": ${enable} }`).
BoolSetter("enable")
wb.maxcurrentS = provider.NewMqtt(log, client,
fmt.Sprintf("%s/evse/current_limit", topic), 0).
WithPayload(`{ "current": ${maxcurrent} }`).
IntSetter("maxcurrent")
return wb, nil
}
func (wb *Warp) hasMeter() bool {
topic := fmt.Sprintf("%s/meter/state", wb.root)
if state, err := provider.NewMqtt(wb.log, wb.client, topic, 0).StringGetter()(); err == nil {
var res v1.MeterState
if err := json.Unmarshal([]byte(state), &res); err == nil {
return res.State == 2 || len(res.PhasesConnected) > 0
}
}
return false
}
func (wb *Warp) hasCurrents() bool {
topic := fmt.Sprintf("%s/meter/detailed_values", wb.root)
if state, err := provider.NewMqtt(wb.log, wb.client, topic, 0).StringGetter()(); err == nil {
var res []float64
if err := json.Unmarshal([]byte(state), &res); err == nil {
return len(res) > 5
}
}
return false
}
// Enable implements the api.Charger interface
func (wb *Warp) Enable(enable bool) error {
// set auto_start_charging
if err := wb.enableS(enable); err != nil {
return err
}
// trigger start/stop
action := "stop_charging"
if enable {
action = "start_charging"
}
topic := fmt.Sprintf("%s/%s/%s", wb.root, "evse", action)
err := wb.client.Publish(topic, false, "null")
if err == nil {
wb.enabled = enable
}
return err
}
func (wb *Warp) status() (v1.EvseState, error) {
var res v1.EvseState
s, err := wb.statusG()
if err == nil {
err = json.Unmarshal([]byte(s), &res)
}
return res, err
}
// autostart reads the enabled state from charger
// use function instead of jq to honor evse/state updates
func (wb *Warp) autostart() (bool, error) {
var res struct {
AutoStartCharging bool `json:"auto_start_charging"`
}
s, err := wb.enabledG()
if err == nil {
err = json.Unmarshal([]byte(s), &res)
}
return res.AutoStartCharging, err
}
// isEnabled reads enabled status from mqtt
func (wb *Warp) isEnabled() (bool, error) {
enabled, err := wb.autostart()
var status v1.EvseState
if err == nil {
status, err = wb.status()
}
if enabled {
// check that charge_release is not blocked
enabled = status.ChargeRelease != 2
} else {
// check that vehicle is really not charging
enabled = status.VehicleState == 2
}
return enabled, err
}
// Enabled implements the api.Charger interface
func (wb *Warp) Enabled() (bool, error) {
enabled, err := wb.isEnabled()
if err == nil && enabled != wb.enabled {
start := time.Now()
// retry to avoid out of sync errors in case of slow warp updates
for time.Since(start) <= 2*time.Second {
if enabled, err = wb.isEnabled(); err != nil {
break
}
if enabled == wb.enabled {
break
}
time.Sleep(50 * time.Millisecond)
}
}
return enabled, err
}
// Status implements the api.Charger interface
func (wb *Warp) Status() (api.ChargeStatus, error) {
var status v1.EvseState
s, err := wb.statusG()
if err == nil {
err = json.Unmarshal([]byte(s), &status)
}
res := api.StatusNone
switch status.VehicleState {
case 0:
res = api.StatusA
case 1:
res = api.StatusB
case 2:
res = api.StatusC
default:
if err == nil {
err = fmt.Errorf("invalid status: %d", status.VehicleState)
}
}
return res, err
}
// MaxCurrent implements the api.Charger interface
func (wb *Warp) MaxCurrent(current int64) error {
return wb.maxcurrentS(1000 * current)
}
var _ api.ChargerEx = (*Warp)(nil)
// MaxCurrentMillis implements the api.ChargerEx interface
func (wb *Warp) MaxCurrentMillis(current float64) error {
return wb.maxcurrentS(int64(1000 * current))
}
// CurrentPower implements the api.Meter interface
func (wb *Warp) currentPower() (float64, error) {
var res v1.MeterState
s, err := wb.meterG()
if err == nil {
err = json.Unmarshal([]byte(s), &res)
}
return res.Power, err
}
// TotalEnergy implements the api.MeterEnergy interface
func (wb *Warp) totalEnergy() (float64, error) {
var res v1.MeterState
s, err := wb.meterG()
if err == nil {
err = json.Unmarshal([]byte(s), &res)
}
return res.EnergyAbs, err
}
// currents implements the api.MeterCurrrents interface
func (wb *Warp) currents() (float64, float64, float64, error) {
var res []float64
s, err := wb.meterDetailsG()
if err == nil {
if err = json.Unmarshal([]byte(s), &res); err == nil {
if len(res) > 5 {
return res[3], res[4], res[5], nil
}
err = errors.New("invalid length")
}
}
return 0, 0, 0, err
}