-
Notifications
You must be signed in to change notification settings - Fork 0
/
payloadToTeleinfo.go
75 lines (68 loc) · 1.6 KB
/
payloadToTeleinfo.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
package go_edf_teleinfo
import (
"errors"
"strconv"
"strings"
)
// PayloadToTeleinfo Convert text from EDF teleinfo to a proper structure
func PayloadToTeleinfo(edfPayload []byte) (Teleinfo, error) {
teleinfo := Teleinfo{
RAW: edfPayload,
}
for _, line := range strings.Split(string(edfPayload), "\n") {
line = strings.Replace(line, "\r", "", -1)
name, data, err := LineDecoder(line)
if err != nil {
return teleinfo, err
}
number, _ := strconv.Atoi(data)
switch name {
case "OPTARIF":
teleinfo.OPTARIF = data
case "HHPHC":
teleinfo.HHPHC = data
case "PTEC":
teleinfo.PTEC = data
case "ISOUSC":
teleinfo.ISOUSC = int64(number)
case "HCHC":
teleinfo.HCHC = int64(number)
case "HCHP":
teleinfo.HCHP = int64(number)
case "IINST":
teleinfo.IINST = int64(number)
case "IMAX":
teleinfo.IMAX = int64(number)
case "PAPP":
teleinfo.PAPP = int64(number)
}
}
return teleinfo, nil
}
// LineDecoder parse one line of EDF teleinfo and check data validity
func LineDecoder(rawLine string) (name string, data string, err error) {
parts := strings.Split(rawLine, " ")
if len(parts) < 3 {
err = errors.New(
"line is not the right format. Lines should look like 'PAPP 00290 ,' (Etiquette / Donnée / Checksum)",
)
return
}
sum := 0
rawLength := len(rawLine)
for index, char := range []byte(rawLine) {
if index >= rawLength-2 {
continue
}
sum = sum + int(char)
}
sum = (sum & 63) + 32
if sum == int([]byte(rawLine)[rawLength-1]) {
name = parts[0]
data = parts[1]
} else {
err = errors.New("checksum verification failed")
return
}
return
}