forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeba-udp.go
319 lines (261 loc) Β· 6.74 KB
/
keba-udp.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
314
315
316
317
318
319
package charger
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/keba"
"github.com/evcc-io/evcc/util"
)
// https://www.keba.com/file/downloads/e-mobility/KeContact_P20_P30_UDP_ProgrGuide_en.pdf
const (
udpTimeout = time.Second
)
// KebaUdp is an api.Charger implementation
type KebaUdp struct {
log *util.Logger
conn string
rfid keba.RFID
timeout time.Duration
recv chan keba.UDPMsg
sender *keba.Sender
}
func init() {
registry.Add("keba-udp", NewKebaUdpFromConfig)
}
//go:generate go run ../cmd/tools/decorate.go -f decorateKebaUdp -b *KebaUdp -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)"
// NewKebaUdpFromConfig creates a new Keba UDP charger
func NewKebaUdpFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
URI string
Serial string
Timeout time.Duration
RFID keba.RFID
}{
Timeout: udpTimeout,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
k, err := NewKebaUdp(cc.URI, cc.Serial, cc.RFID, cc.Timeout)
if err != nil {
return nil, err
}
energy, err := k.totalEnergy()
if err != nil {
return nil, err
}
if energy > 0 {
return decorateKebaUdp(k, k.currentPower, k.totalEnergy, k.currents), nil
}
return k, err
}
// NewKebaUdp creates a new charger
func NewKebaUdp(uri, serial string, rfid keba.RFID, timeout time.Duration) (*KebaUdp, error) {
log := util.NewLogger("keba")
instance, err := keba.Instance(log)
if err != nil {
return nil, err
}
// add default port
conn := util.DefaultPort(uri, keba.Port)
sender, err := keba.NewSender(log, conn)
c := &KebaUdp{
log: log,
conn: conn,
rfid: rfid,
timeout: timeout,
recv: make(chan keba.UDPMsg),
sender: sender,
}
// use serial to subscribe if defined for docker scenarios
if serial == "" {
serial = conn
}
instance.Subscribe(serial, c.recv)
return c, err
}
func (c *KebaUdp) receive(report int, resC chan<- keba.UDPMsg, errC chan<- error, closeC <-chan struct{}) {
t := time.NewTimer(c.timeout)
defer close(resC)
defer close(errC)
for {
select {
case msg := <-c.recv:
// matching result message
if msg.Report == nil && report == 0 {
resC <- msg
return
}
// matching report id
if msg.Report != nil && report == msg.Report.ID {
resC <- msg
return
}
case <-t.C:
errC <- errors.New("recv timeout")
return
case <-closeC:
return
}
}
}
func (c *KebaUdp) roundtrip(msg string, report int, res interface{}) error {
resC := make(chan keba.UDPMsg)
errC := make(chan error)
closeC := make(chan struct{})
defer close(closeC)
go c.receive(report, resC, errC, closeC)
// add report number to message and send
if report > 0 {
msg = fmt.Sprintf("%s %d", msg, report)
}
if err := c.sender.Send(msg); err != nil {
return err
}
for {
select {
case resp := <-resC:
if report == 0 {
// use reflection to write to simple string
rv := reflect.ValueOf(res)
if rv.Kind() != reflect.Ptr || rv.IsNil() || rv.Elem().Kind() != reflect.String {
return fmt.Errorf("invalid type: %s", reflect.TypeOf(res))
}
res := string(resp.Message)
if res != keba.OK {
continue
}
rv.Elem().SetString(res)
return nil
}
return json.Unmarshal(resp.Message, &res)
case err := <-errC:
return err
}
}
}
// Status implements the api.Charger interface
func (c *KebaUdp) Status() (api.ChargeStatus, error) {
var kr keba.Report2
err := c.roundtrip("report", 2, &kr)
if err != nil {
return api.StatusA, err
}
if kr.Plug < 5 {
return api.StatusA, nil
}
if kr.State == 3 {
return api.StatusC, nil
}
if kr.State != 4 {
return api.StatusB, nil
}
return api.StatusNone, fmt.Errorf("invalid status: %+d", kr.State)
}
// Enabled implements the api.Charger interface
func (c *KebaUdp) Enabled() (bool, error) {
var kr keba.Report2
err := c.roundtrip("report", 2, &kr)
if err != nil {
return false, err
}
return kr.EnableSys == 1 || kr.EnableUser == 1, nil
}
// enableRFID sends RFID credentials to enable charge
func (c *KebaUdp) enableRFID() error {
// check if authorization required
var kr keba.Report2
if err := c.roundtrip("report", 2, &kr); err != nil {
return err
}
// no auth required
if kr.AuthReq == 0 {
return nil
}
// auth required but missing tag
if c.rfid.Tag == "" {
return errors.New("missing credentials for RFID authorization")
}
// authorize
var resp string
return c.roundtrip(fmt.Sprintf("start %s", c.rfid.Tag), 0, &resp)
}
// Enable implements the api.Charger interface
func (c *KebaUdp) Enable(enable bool) error {
if enable {
if err := c.enableRFID(); err != nil {
return err
}
}
var d int
if enable {
d = 1
}
// ignore result...
var resp string
if err := c.roundtrip(fmt.Sprintf("ena %d", d), 0, &resp); err != nil {
return err
}
return nil
}
// MaxCurrent implements the api.Charger interface
func (c *KebaUdp) MaxCurrent(current int64) error {
d := 1000 * current
var resp string
if err := c.roundtrip(fmt.Sprintf("curr %d", d), 0, &resp); err != nil {
return err
}
return nil
}
var _ api.ChargerEx = (*KebaUdp)(nil)
// MaxCurrentMillis implements the api.ChargerEx interface
func (c *KebaUdp) MaxCurrentMillis(current float64) error {
d := int(1000 * current)
var resp string
if err := c.roundtrip(fmt.Sprintf("curr %d", d), 0, &resp); err != nil {
return err
}
if resp != keba.OK {
return fmt.Errorf("curr %d unexpected response: %s", d, resp)
}
return nil
}
// currentPower implements the api.Meter interface
func (c *KebaUdp) currentPower() (float64, error) {
var kr keba.Report3
err := c.roundtrip("report", 3, &kr)
// mW to W
return float64(kr.P) / 1e3, err
}
// totalEnergy implements the api.MeterEnergy interface
func (c *KebaUdp) totalEnergy() (float64, error) {
var kr keba.Report3
err := c.roundtrip("report", 3, &kr)
// mW to W
return float64(kr.ETotal) / 1e4, err
}
// currents implements the api.PhaseCurrents interface
func (c *KebaUdp) currents() (float64, float64, float64, error) {
var kr keba.Report3
err := c.roundtrip("report", 3, &kr)
// 1mA to A
return float64(kr.I1) / 1e3, float64(kr.I2) / 1e3, float64(kr.I3) / 1e3, err
}
var _ api.Identifier = (*KebaUdp)(nil)
// Identify implements the api.Identifier interface
func (c *KebaUdp) Identify() (string, error) {
var kr keba.Report100
err := c.roundtrip("report", 100, &kr)
return kr.RFIDTag, err
}
var _ api.Diagnosis = (*KebaUdp)(nil)
// Diagnose implements the api.Diagnosis interface
func (c *KebaUdp) Diagnose() {
var kr keba.Report100
if err := c.roundtrip("report", 100, &kr); err == nil {
fmt.Printf("%+v\n", kr)
}
}