-
Notifications
You must be signed in to change notification settings - Fork 0
/
vedirect.go
615 lines (565 loc) · 12.2 KB
/
vedirect.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// go get github.com/brianolson/vedirect
//
// with learnings from https://github.com/karioja/vedirect by Janne Kario
package vedirect
import (
"bufio"
"context"
ehex "encoding/hex"
"errors"
"fmt"
"io"
"io/fs"
"log"
"os"
"reflect"
"strconv"
"strings"
"sync"
"time"
"github.com/tarm/serial"
)
type vedState int
const (
waitHeader vedState = iota
hex
inKey
inValue
inChecksum
)
const (
hexmarker = ':'
delimiter = '\t'
)
type Command byte
const (
// Enter bootloader = 0x00
Ping Command = 1
AppVersion Command = 3
ProductId Command = 4
Restart Command = 6
Get Command = 7
Set Command = 8
Async Command = 0xA
)
type Option int
const (
AddTime Option = 1
)
type Vedirect struct {
// AddTime if true will add to each record {"_t": time.Now().UnixMilli()}
AddTime bool
fin io.Reader
fout io.Writer
// debug output
dout io.Writer
ctx context.Context
bytesSum uint
data map[string]string
out chan<- map[string]string
key []byte
value []byte
hexMessage []byte
state vedState
wg *sync.WaitGroup
}
// Open a VE.Direct serial device (starts a thread).
// path is the serial device.
// out chan receives data.
// wg may be nil.
// debugOut may be nil.
func Open(path string, out chan<- map[string]string, wg *sync.WaitGroup, ctx context.Context, debugOut io.Writer, options ...Option) (v *Vedirect, err error) {
v = new(Vedirect)
v.dout = debugOut
st, err := os.Stat(path)
if err != nil {
v.debug("%s: could not stat, %v", path, err)
return nil, err
}
if (st.Mode() & charDevice) == charDevice {
v.debug("%s: is char device", path)
sc := serial.Config{Name: path, Baud: 19200}
dev, derr := serial.OpenPort(&sc)
err = derr
v.fin = dev
v.fout = dev
} else {
v.debug("%s: is not char device, assuming debug file capture")
fin, ferr := os.OpenFile(path, os.O_RDONLY, 0777)
err = ferr
v.fin = fin
}
if err != nil {
v.debug("%s: could not open, %v", path, err)
return nil, err
}
v.ctx = ctx
if v.ctx == nil {
v.ctx = context.Background()
}
v.out = out
v.wg = wg
if v.wg == nil {
v.wg = new(sync.WaitGroup)
}
v.wg.Add(1)
for _, opt := range options {
if opt == AddTime {
v.AddTime = true
}
}
go v.readThread()
return v, nil
}
func (v *Vedirect) debug(msg string, args ...interface{}) {
if v.dout == nil {
return
}
fmt.Fprintf(v.dout, msg+"\n", args...)
}
type fdFile interface {
Fd() uintptr // os.File has this
}
type statFile interface {
Stat() (os.FileInfo, error) // os.File has this
}
const charDevice = fs.ModeDevice | fs.ModeCharDevice
// handle achieves parsing VE.Direct status messages
// lines are mostly text except Checksum byte:
// "\r\n{key}\t{value}"
// "\r\nChecksum\t{cs byte}"
// {cs byte} + {all bytes through "\r\n" after previous csbyte} == 0x00
//
// HEX protocol line:
// :{command nybble}{[xx] hex bytes...}{cs byte}\n
// 0x0{command nybble} + bytes + cs byte == 0x55
func (v *Vedirect) handle(b uint8) {
if b == hexmarker && v.state != inChecksum {
v.state = hex
v.hexMessage = make([]byte, 1, 20)
v.hexMessage[0] = '0' // prefix for command nybble
return
}
v.bytesSum += uint(b)
switch v.state {
case waitHeader:
if b == '\n' {
v.state = inKey
}
case inKey:
if b == delimiter {
if string(v.key) == "Checksum" {
v.state = inChecksum
} else {
v.state = inValue
}
} else {
v.key = append(v.key, b)
}
case inValue:
if b == '\r' {
v.state = waitHeader
if v.data == nil {
v.data = make(map[string]string)
}
v.data[string(v.key)] = string(v.value)
v.key = nil
v.value = nil
} else {
v.value = append(v.value, b)
}
case inChecksum:
v.key = nil
v.value = nil
v.state = waitHeader
if v.bytesSum%256 == 0 {
if v.AddTime {
v.data["_t"] = strconv.FormatInt(time.Now().UnixMilli(), 10)
}
v.out <- v.data
v.data = nil
}
v.bytesSum = 0
case hex:
v.bytesSum = 0
if b == '\n' {
v.finishHexMessage()
v.state = waitHeader
} else {
// accumulate nybbles, parse at end
v.hexMessage = append(v.hexMessage, b)
}
default:
panic("bad state")
}
}
func (v *Vedirect) finishHexMessage() {
blen := len(v.hexMessage) / 2
hbytes := make([]byte, blen)
count, err := ehex.Decode(hbytes, v.hexMessage)
if err != nil {
log.Printf("bad HEX message, %v", err)
log.Printf("hexbyte: %s", ehex.EncodeToString(v.hexMessage))
return
}
var hexSum uint
for _, c := range hbytes[:count] {
hexSum += uint(c)
}
if hexSum&0x0ff != 0x055 {
log.Printf("bad HEX checksum, 0x%02x != 0x55", hexSum&0x0ff)
return
}
data := make(map[string]string)
if v.AddTime {
data["_t"] = strconv.FormatInt(time.Now().UnixMilli(), 10)
}
data["_x"] = string(v.hexMessage)
v.out <- data
}
var ErrNoOutput = errors.New("no output configured")
// SendHexCommand sends a HEX protocol command to a VE.Direct device.
//
// If successful, response will come in normal message stream parsed into data["_x"] = "aabbccddeeff"
//
// Parsed HEX messages have their command nybble filled out to a whole byte so that hex.DecodeString(data["_x"]) should work.
//
// Actual message fields vary by length and content and are left to application code, but you might want "encoding/binary" LittleEndian.Uint16([]byte) and .PutUint16([]byte, uint16)
func (v *Vedirect) SendHexCommand(cmd Command, msg []byte) error {
if v.fout == nil {
v.debug("could not send hex command, null out")
return ErrNoOutput
}
command := formatHexCommand(cmd, msg)
_, err := v.fout.Write(command)
return err
}
func formatHexCommand(cmd Command, msg []byte) []byte {
hexSum := uint(cmd)
out := make([]byte, 4+(len(msg)*2))
wat := []byte{byte(cmd)}
for _, c := range msg {
hexSum += uint(c)
}
cs := (0x055 - (hexSum & 0x0ff)) & 0x0ff
if (cs+hexSum)&0x0ff != 0x055 {
panic("can't generate a cs")
}
ehex.Encode(out, wat)
ehex.Encode(out[2:], msg)
wat[0] = uint8(cs & 0x0ff)
ehex.Encode(out[2+(len(msg)*2):], wat)
upper := strings.ToUpper(string(out[1:]))
command := fmt.Sprintf(":%s\n", upper)
return []byte(command)
}
func (v *Vedirect) readThread() {
fc, ok := v.fin.(io.Closer)
if ok {
defer fc.Close()
}
if v.wg != nil {
defer v.wg.Done()
}
done := v.ctx.Done()
buf := make([]byte, 4096)
for {
select {
case <-done:
return
default:
}
n, err := v.fin.Read(buf)
if err != nil {
// TODO: capture error back to Vedirect.err or something?
log.Printf("ve read err: %v", err)
close(v.out)
return
}
for i := 0; i < n; i++ {
v.handle(buf[i])
}
}
}
// from "VE.Direct Protocol" text protocol spec
const intFieldsBlob = `V mV
V2 mV
V3 mV
VS mV
VM mV
DM ‰
VPV mV
PPV W
I mA
I2 mA
I3 mA
IL mA
T °C
P W
CE mAh
SOC ‰
TTG Minutes
H1 mAh
H2 mAh
H3 mAh
H4
H5
H6 mAh
H7 mV
H8 mV
H9 Seconds
H10
H11
H12
H13
H14
H15 mV
H16 mV
H17 0.01kWh
H18 0.01kWh
H19 0.01kWh
H20 0.01kWh
H21 W
H22 0.01kWh
H23 W
HSDS Day sequence number
AC_OUT_V 0.01V
AC_OUT_I 0.1A
AC_OUT_S VA
_t ms
`
const otherFieldsBlob = `LOAD
Alarm
Relay
AR
OR
ERR
CS
BMV
FW
FWE
PID
SER#
MODE
WARN
MPPT
MON
`
// IntFields map field name to unit description (if any).
//
// Some fields will have empty string for unit description.
var IntFields map[string]string
// OtherFields is a set of known VE.Direct fields that are not int.
//
// isKnownOtherField := vedirect.OtherFields["BLAH"]
var OtherFields map[string]bool
func init() {
IntFields = make(map[string]string, 30)
fin := strings.NewReader(intFieldsBlob)
sc := bufio.NewScanner(fin)
for sc.Scan() {
line := sc.Text()
if len(line) == 0 {
continue
}
a, b, didCut := strings.Cut(line, " ")
if didCut {
IntFields[a] = b
} else {
IntFields[line] = ""
}
}
OtherFields = make(map[string]bool, 20)
fin = strings.NewReader(otherFieldsBlob)
sc = bufio.NewScanner(fin)
for sc.Scan() {
line := sc.Text()
if len(line) == 0 {
continue
}
OtherFields[line] = true
}
}
var ParseRecordDebug io.Writer
// ParseRecord will parse some field values to int64
func ParseRecord(rec map[string]string) map[string]interface{} {
nrec := make(map[string]interface{}, len(rec))
for k, v := range rec {
_, isInt := IntFields[k]
if isInt {
iv, err := strconv.ParseInt(v, 10, 64)
if err == nil {
nrec[k] = iv
} else {
debug := ParseRecordDebug
if debug != nil {
fmt.Fprintf(debug, "bad int [%s]=%#v (%v)", k, v, err)
}
nrec[k] = v
}
} else {
knownOther := OtherFields[k]
if !knownOther {
debug := ParseRecordDebug
if debug != nil {
fmt.Fprintf(debug, "unknown field [%s]=%#v", k, v)
}
}
nrec[k] = v
}
}
return nrec
}
func ParseRecordField(k string, v any) any {
switch vt := v.(type) {
case string:
return ParseRecordFieldString(k, vt)
default:
return v
}
}
func ParseRecordFieldString(k, v string) any {
_, isInt := IntFields[k]
if isInt {
iv, err := strconv.ParseInt(v, 10, 64)
if err == nil {
return iv
} else {
debug := ParseRecordDebug
if debug != nil {
fmt.Fprintf(debug, "bad int [%s]=%#v (%v)", k, v, err)
}
}
} else {
knownOther := OtherFields[k]
if !knownOther {
debug := ParseRecordDebug
if debug != nil {
fmt.Fprintf(debug, "unknown field [%s]=%#v", k, v)
}
}
}
return v
}
// FloatWholeUnits uses the unit string from IntFields to convert some values into float64 of their whole unit.
// e.g. (10_000, "mV)" -> (10.0, "V")
//
// If there was no conversion, unit returned is same as passed in.
func FloatWholeUnits(v int64, unit string) (float64, string) {
// "mV" "mA" "mAh" "0.01kWh" "0.01V" "0.1A"
if unit == "" {
return float64(v), unit
}
if unit[0] == 'm' {
return float64(v) / 1000.0, unit[1:]
}
if strings.HasPrefix(unit, "0.1") {
return float64(v) / 10.0, unit[3:]
}
if strings.HasPrefix(unit, "0.01") {
return float64(v) / 100.0, unit[4:]
}
return float64(v), unit
}
func stringRecDiff(a, b map[string]string) map[string]string {
d := make(map[string]string, len(b))
for ak, av := range a {
bv, ok := b[ak]
if ok {
if av != bv {
// change
d[ak] = bv
} // else no change
} else {
// not present in b
//d[ak] = nil
}
}
for bk, bv := range b {
_, ok := a[bk]
if !ok {
// new value not in a
d[bk] = bv
}
}
return d
}
// StringRecordDeltas makes a list of deltas.
// The first record has all its fields, each record after only has fields that have changed.
// passed in deltas object is appeneded to, or may be nil.
// Output records have ParseRecord applied
func StringRecordDeltas(batch []map[string]string, deltas []map[string]interface{}, keyframePeriod int) []map[string]interface{} {
pos := len(deltas)
if deltas == nil {
deltas = make([]map[string]interface{}, 0, len(batch))
}
last := make(map[string]string, 20)
for ; pos < len(batch); pos++ {
var nrec map[string]string
if (pos % keyframePeriod) == 0 {
nrec = make(map[string]string, len(batch[pos]))
for k, v := range batch[pos] {
nrec[k] = v
}
} else {
nrec = stringRecDiff(last, batch[pos])
}
deltas = append(deltas, ParseRecord(nrec))
for k, v := range batch[pos] {
last[k] = v
}
}
return deltas
}
func parsedRecDiff(a, b map[string]interface{}) map[string]interface{} {
d := make(map[string]interface{}, len(b))
for ak, av := range a {
bv, ok := b[ak]
if ok {
if !reflect.DeepEqual(av, bv) {
// change
d[ak] = bv
} // else no change
} else {
// not present in b
//d[ak] = nil
}
}
for bk, bv := range b {
_, ok := a[bk]
if !ok {
// new value not in a
d[bk] = bv
}
}
return d
}
// ParsedRecordDeltas converts records from ParseRecord() into a list of record deltas.
// The first record has full data and each following record only has fields that changed.
func ParsedRecordDeltas(alldata []map[string]interface{}) []map[string]interface{} {
var alldeltas []map[string]interface{} = nil
last := make(map[string]interface{}, 20)
if len(alldata) > 0 {
alldeltas = make([]map[string]interface{}, 0, len(alldata))
for i := 0; i < len(alldata); i++ {
nrec := parsedRecDiff(last, alldata[i])
alldeltas = append(alldeltas, nrec)
for k, v := range alldata[i] {
last[k] = v
}
}
}
return alldeltas
}
// Convert *in-place* deltas into whole records
func ParsedRecordRebuild(deltas []map[string]interface{}) {
cv := make(map[string]interface{}, 20)
for i, rec := range deltas {
for k, v := range rec {
cv[k] = v
}
for k, v := range cv {
deltas[i][k] = v
}
}
}