-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgombus_test.go
186 lines (164 loc) · 5.64 KB
/
gombus_test.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
package gombus
import (
"encoding/hex"
"fmt"
"os"
"strings"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
logrus.SetLevel(logrus.DebugLevel)
os.Exit(m.Run())
}
func TestToBCD(t *testing.T) {
// facit: 78 56 34 12 identification number = 12345678
s := fmt.Sprintf("% x", uintToBCD(12345678, 4))
assert.Equal(t, "78 56 34 12", s)
}
func TestFromBCD(t *testing.T) {
// facit: 78 56 34 12 identification number = 12345678
h, err := hex.DecodeString("78563412")
assert.NoError(t, err)
i := bcdToInt(h)
assert.Equal(t, 12345678, i)
}
func TestCheckKthBitSet(t *testing.T) {
assert.True(t, checkKthBitSet(0x80, 7))
assert.False(t, checkKthBitSet(0xf, 7))
assert.False(t, checkKthBitSet(0x2a, 7))
assert.True(t, checkKthBitSet(0x40, 6))
}
func TestPrimaryUsingSecondary(t *testing.T) {
data := SetPrimaryUsingSecondary(19004636, 2)
s := fmt.Sprintf("% x", data)
assert.Equal(t, "68 0e 0e 68 73 fd 51 36 46 00 19 ff ff ff ff 01 7a 02 cf 16", s)
}
func TestPrimaryUsingPrimary(t *testing.T) {
data := SetPrimaryUsingPrimary(0, 3)
s := fmt.Sprintf("% x", data)
assert.Equal(t, "68 06 06 68 73 00 51 01 7a 03 42 16", s)
}
func TestDecodeLongFrameGAROSecondFrame(t *testing.T) {
// Response from garo electric meter
s := `
68 78 78 68
08 01 72
14 21 07 90
36 1c
c7
02
25
00
00 00
84 40 2a a0 09 00 00
84 80 40 2a ba 00 00 00
84 c0 40 2a 00 00 00 00
84 40 fb 97 72 fb fe ff ff
84 80 40 fb 97 72 4b 00 00 00
84 c0 40 fb 97 72 00 00 00 00
84 40 fb b7 72 ae 09 00 00
84 80 40 fb b7 72 c8 00 00 00
84 c0 40 fb b7 72 00 00 00 00
82 40 fd ba 73 e2 03
82 80 40 fd ba 73 9f 03
82 c0 40 fd ba 73 00 00 1f
ef 16`
// 0x84 == 10000100 , 1000 = extension bit 1, 0100 == 32 bit integer // https://m-bus.com/documentation-wired/06-application-layer
// DIF:
// 7 extension bit
// 6 LSB always 0
// 5 function field
// 4 function field (2 bytes) 00 == Instantaneous Value
// 3 data field for example 0100 == 32 bit integer
// 2 data field
// 1 data field
// 0 data field
// DIFE
// 0x40 == 01000000
// 0x80 == 10000000
// 0xc0 == 11000000
// 7 extension bit
// 6 (device) unit 0 = reactive, 1=apparent
// 5 tariff
// 4 tariff
// 3 storagenumber
// 2 storagenumber
// 1 storagenumber
// 0 storagenumber
// VIF
// 0x2a == 00101010 == E010 1010 Reserved
// 7 extension bit
// 6 unit and multiplier
// 5 unit and multiplier
// 4 unit and multiplier
// 3 unit and multiplier
// 2 unit and multiplier
// 1 unit and multiplier
// 0 unit and multiplier 7 bits total
// VIFE
// inget för extension bit == 0
// DATA
// a0 09 00 00
s = strings.ReplaceAll(s, " ", "")
s = strings.ReplaceAll(s, "\n", "")
s = strings.ReplaceAll(s, "\t", "")
data, err := hex.DecodeString(s)
assert.NoError(t, err)
frame := LongFrame(data)
dFrame, err := frame.Decode()
assert.NoError(t, err)
assert.Equal(t, 90072114, dFrame.SerialNumber)
// fmt.Printf("%#v\n", dFrame)
// spew.Dump(dFrame)
}
func TestDecodeLongFrameGAROFirstFrame(t *testing.T) {
s := `68 65 65 68 08 01 72 14 21 07 90 36 1c c7 02 4d 00 00 00 04 05 9c 31 01 00 04 fb 82 75 63 91 00 00 04 2a 36 08 00 00 04 fb 97 72 ca fe ff ff 04 fb b7 72 6d 08 00 00 02 fd ba 73 dc 03 84 80 80 40 fd 48 c4 0f 00 00 04 fd 48 1a 09 00 00 84 40 fd 59 d2 04 00 00 84 80 40 fd 59 78 00 00 00 84 c0 40 fd 59 00 00 00 00 1f 95 16`
s = strings.ReplaceAll(s, " ", "")
data, err := hex.DecodeString(s)
assert.NoError(t, err)
frame := LongFrame(data)
dFrame, err := frame.Decode()
assert.NoError(t, err)
assert.Equal(t, 90072114, dFrame.SerialNumber)
// fmt.Printf("%#v\n", dFrame)
// spew.Dump(dFrame)
assert.True(t, dFrame.HasMoreRecords())
}
func TestDecodeLongFrameItronFirstFrame(t *testing.T) {
s := `68 56 56 68 08 02 72 36 46 00 19 77 04 14 07 9d 10 00 00 0c 78 36 46 00 19 0d 7c 08 44 49 20 2e 74 73 75 63 0a 20 20 20 20 20 20 20 20 20 20 04 6d 35 14 d3 26 02 7c 09 65 6d 69 74 20 2e 74 61 62 97 10 04 13 01 6e 03 00 04 93 7f 00 00 00 00 44 13 27 51 03 00 0f 00 00 1f 96 16`
s = strings.ReplaceAll(s, " ", "")
data, err := hex.DecodeString(s)
assert.NoError(t, err)
frame := LongFrame(data)
dFrame, err := frame.Decode()
assert.NoError(t, err)
assert.Equal(t, 19004636, dFrame.SerialNumber)
// fmt.Printf("%#v\n", dFrame)
// spew.Dump(dFrame)
fmt.Println("number of records: ", len(dFrame.DataRecords))
assert.True(t, dFrame.HasMoreRecords())
assert.Len(t, dFrame.DataRecords, 9)
assert.Equal(t, 217383.0, dFrame.DataRecords[6].RawValue)
assert.Equal(t, "bat. time", dFrame.DataRecords[3].Unit.Unit)
assert.Equal(t, "cust. ID", dFrame.DataRecords[1].Unit.Unit)
}
func Testnnt24ToInt(t *testing.T) {
// 03 13 15 31 00 Data block 1: unit 0, storage No 0, no tariff, instantaneous volume, 12565 l (24 bit integer)
d := []byte{0x15, 0x31, 0x0}
res := int24ToInt(d)
assert.Equal(t, 12565, res)
}
// Example for a RSP_UD with variable data structure answer (mode 1):
// (all values are hex.)
// 68 1F 1F 68 header of RSP_UD telegram (length 1Fh=31d bytes)
// 08 02 72 C field = 08 (RSP), address 2, CI field 72H (var.,LSByte first)
// 78 56 34 12 identification number = 12345678
// 24 40 01 07 manufacturer ID = 4024h (PAD in EN 61107), generation 1, water
// 55 00 00 00 TC = 55h = 85d, Status = 00h, Signature = 0000h
// 03 13 15 31 00 Data block 1: unit 0, storage No 0, no tariff, instantaneous volume, 12565 l (24 bit integer)
// DA 02 3B 13 01 Data block 2: unit 0, storage No 5, no tariff, maximum volume flow, 113 l/h (4 digit BCD)
// 8B 60 04 37 18 02 Data block 3: unit 1, storage No 0, tariff 2, instantaneous energy, 218,37 kWh (6 digit BCD)
// 18 16 checksum and stopsign