-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.go
75 lines (67 loc) · 1.86 KB
/
packet.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 main
import (
"fmt"
"github.com/shopspring/decimal"
"strconv"
)
var packetTypes = map[int]func(r *sBProtocolResult){
SBC_PACKET_HOME_MODE: parseInt,
SBC_PACKET_HOME_HSRCSTATE: parseInt,
SBC_PACKET_ANODE_VOLTAGE: parseAnodeVoltage,
SBC_PACKET_HOME_SENSOR1: parseDecimal,
SBC_PACKET_HOME_SENSOR2: parseDecimal,
SBC_PACKET_HOME_TEMPERATURE: parseInt,
SBC_PACKET_HOME_TEMPNIGHT: parseInt,
SBC_PACKET_HOME_TEMPNIGHTCURR: parseInt,
SBC_PACKET_HOME_TEMPNIGHTLOW: parseInt,
SBC_PACKET_HOME_TIME: parseTime,
SBC_PACKET_HOME_CAPACITY: parseInt,
SBC_PACKET_HOME_ERROR: parseInt,
SBC_PACKET_HOME_BOILERMODEL: parseString,
SBC_PACKET_HOME_FWVERSION: parseString,
SBC_PACKET_HOME_BOILERNAME: parseString,
SBC_PACKET_ANODE_PARAMS: parseAnodeParams,
SBC_PACKET_HOME_ANTILEGIO: parseInt,
}
func parseDecimal(r *sBProtocolResult) {
if t, err := decimal.NewFromString(r.stringData); err == nil {
r.result = fmt.Sprint(t)
}
}
func parseAnodeParams(r *sBProtocolResult) {
if len(r.stringData) < 13 {
return
}
enabled, err := strconv.Atoi(r.stringData[0:3])
if err != nil {
return
}
errorThreshold, err := strconv.Atoi(r.stringData[3:8])
if err != nil {
return
}
okThreshold, err := strconv.Atoi(r.stringData[8:13])
if err != nil {
return
}
r.result = fmt.Sprintf("%d:%d:%d", enabled, errorThreshold, okThreshold)
}
func parseString(r *sBProtocolResult) {
r.result = r.stringData
}
func parseAnodeVoltage(r *sBProtocolResult) {
if i, err := decimal.NewFromString(r.stringData); err == nil {
r.result = fmt.Sprint(i.Div(decimal.NewFromInt(1000)))
}
}
func parseTime(r *sBProtocolResult) {
if len(r.stringData) < 10 {
return
}
r.result = r.stringData
}
func parseInt(r *sBProtocolResult) {
if i, err := strconv.Atoi(r.stringData); err == nil {
r.result = strconv.Itoa(i)
}
}