-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
250 lines (245 loc) · 12.4 KB
/
examples_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
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
package senml
import (
"regexp"
"time"
)
type TestVector struct {
SkipEncode bool
JSON, XML string
CBOR []byte
Result []Measurement
}
var testVectors = map[string]TestVector{
"Empty": {
JSON: `[]`,
CBOR: []byte{0x80},
XML: `<sensml xmlns="urn:ietf:params:xml:ns:senml"></sensml>`,
},
"Numeric types": {
SkipEncode: false, // only test if the values below are decoded correctly
JSON: `[
{"v":5},{"v":44},{"v":11290},{"v":739910425},{"v":3177891079421588000},
{"v":-6},{"v":-45},{"v":-11291},{"v":-739910426},{"v":-3177891079421588000},
{"v":8.1171875},{"v":2.9390623569488525},{"v":1.7},{"v":271.15}
]`,
CBOR: []byte{
0x8E,
0xa1, 0x02, 0x05,
0xa1, 0x02, 0x18, 0x2C,
0xa1, 0x02, 0x19, 0x2C, 0x1A,
0xa1, 0x02, 0x1a, 0x2C, 0x1A, 0x23, 0x19,
0xa1, 0x02, 0x1b, 0x2C, 0x1A, 0x23, 0x19, 0x7b, 0xce, 0x72, 0xd7,
0xa1, 0x02, 0x25,
0xa1, 0x02, 0x38, 0x2C,
0xa1, 0x02, 0x39, 0x2C, 0x1A,
0xa1, 0x02, 0x3a, 0x2C, 0x1A, 0x23, 0x19,
0xa1, 0x02, 0x3b, 0x2C, 0x1A, 0x23, 0x19, 0x7b, 0xce, 0x72, 0xd7,
0xa1, 0x02, 0xf9, 0x48, 0x0f,
0xa1, 0x02, 0xfa, 0x40, 0x3c, 0x19, 0x99,
0xa1, 0x02, 0xfb, 0x3f, 0xfb, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0xa1, 0x02, 0xc4, 0x82, 0x21, 0x19, 0x69, 0xeb,
},
Result: []Measurement{
NewValue("", 5, None, time.Time{}, 0),
NewValue("", 44, None, time.Time{}, 0),
NewValue("", 11290, None, time.Time{}, 0),
NewValue("", 739910425, None, time.Time{}, 0),
NewValue("", 3177891079421588183, None, time.Time{}, 0),
NewValue("", -6, None, time.Time{}, 0),
NewValue("", -45, None, time.Time{}, 0),
NewValue("", -11291, None, time.Time{}, 0),
NewValue("", -739910426, None, time.Time{}, 0),
NewValue("", -3177891079421588184, None, time.Time{}, 0),
NewValue("", 8.1171875, None, time.Time{}, 0),
NewValue("", 2.9390623569488525, None, time.Time{}, 0),
NewValue("", 1.7, None, time.Time{}, 0),
NewValue("", 271.15, None, time.Time{}, 0),
},
},
// The rest of the test vectors are based on RFC8428
"Single Data Point": {
JSON: `[{"n":"urn:dev:ow:10e2073a01080063","u":"Cel","v":23.1}]`,
Result: []Measurement{
NewValue("urn:dev:ow:10e2073a01080063", 23.1, Celsius, time.Time{}, 0),
},
},
"Multiple Data Points 1": {
JSON: `[
{"bn":"urn:dev:ow:10e2073a01080063:","n":"voltage","u":"V","v":120.1},
{"n":"current","u":"A","v":1.2}
]`,
Result: []Measurement{
NewValue("urn:dev:ow:10e2073a01080063:voltage", 120.1, Volt, time.Time{}, 0),
NewValue("urn:dev:ow:10e2073a01080063:current", 1.2, Ampere, time.Time{}, 0),
},
},
"Multiple Data Points 2": {
SkipEncode: true, // Encode uses positive increments, base version not supported
JSON: `[
{"bn":"urn:dev:ow:10e2073a0108006:","bt":1.276020076001e+09,
"bu":"A","bver":5,
"n":"voltage","u":"V","v":120.1},
{"n":"current","t":-5,"v":1.2},
{"n":"current","t":-4,"v":1.3},
{"n":"current","t":-3,"v":1.4},
{"n":"current","t":-2,"v":1.5},
{"n":"current","t":-1,"v":1.6},
{"n":"current","v":1.7}
]`,
CBOR: []byte{
0x87, 0xa7, 0x21, 0x78, 0x1b, 0x75, 0x72, 0x6e, 0x3a, 0x64, 0x65, 0x76, 0x3a, 0x6f, 0x77, 0x3a,
0x31, 0x30, 0x65, 0x32, 0x30, 0x37, 0x33, 0x61, 0x30, 0x31, 0x30, 0x38, 0x30, 0x30, 0x36, 0x3a,
0x22, 0xfb, 0x41, 0xd3, 0x03, 0xa1, 0x5b, 0x00, 0x10, 0x62, 0x23, 0x61, 0x41, 0x20, 0x05, 0x00,
0x67, 0x76, 0x6f, 0x6c, 0x74, 0x61, 0x67, 0x65, 0x01, 0x61, 0x56, 0x02, 0xfb, 0x40, 0x5e, 0x06,
0x66, 0x66, 0x66, 0x66, 0x66, 0xa3, 0x00, 0x67, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x06,
0x24, 0x02, 0xfb, 0x3f, 0xf3, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0xa3, 0x00, 0x67, 0x63, 0x75,
0x72, 0x72, 0x65, 0x6e, 0x74, 0x06, 0x23, 0x02, 0xfb, 0x3f, 0xf4, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
0xcd, 0xa3, 0x00, 0x67, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x06, 0x22, 0x02, 0xfb, 0x3f,
0xf6, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0xa3, 0x00, 0x67, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e,
0x74, 0x06, 0x21, 0x02, 0xf9, 0x3e, 0x00, 0xa3, 0x00, 0x67, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e,
0x74, 0x06, 0x20, 0x02, 0xfb, 0x3f, 0xf9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xa3, 0x00, 0x67,
0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x06, 0x00, 0x02, 0xfb, 0x3f, 0xfb, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33,
},
XML: `<sensml xmlns="urn:ietf:params:xml:ns:senml">
<senml bn="urn:dev:ow:10e2073a0108006:" bt="1.276020076001e+09"
bu="A" bver="5" n="voltage" u="V" v="120.1"></senml>
<senml n="current" t="-5" v="1.2"></senml>
<senml n="current" t="-4" v="1.3"></senml>
<senml n="current" t="-3" v="1.4"></senml>
<senml n="current" t="-2" v="1.5"></senml>
<senml n="current" t="-1" v="1.6"></senml>
<senml n="current" v="1.7"></senml>
</sensml>`,
Result: []Measurement{
NewValue("urn:dev:ow:10e2073a0108006:voltage", 120.1, Volt, floatToTime(1.276020076001e+09), 0),
NewValue("urn:dev:ow:10e2073a0108006:current", 1.2, Ampere, floatToTime(1.276020076001e+09-5), 0),
NewValue("urn:dev:ow:10e2073a0108006:current", 1.3, Ampere, floatToTime(1.276020076001e+09-4), 0),
NewValue("urn:dev:ow:10e2073a0108006:current", 1.4, Ampere, floatToTime(1.276020076001e+09-3), 0),
NewValue("urn:dev:ow:10e2073a0108006:current", 1.5, Ampere, floatToTime(1.276020076001e+09-2), 0),
NewValue("urn:dev:ow:10e2073a0108006:current", 1.6, Ampere, floatToTime(1.276020076001e+09-1), 0),
NewValue("urn:dev:ow:10e2073a0108006:current", 1.7, Ampere, floatToTime(1.276020076001e+09-0), 0),
},
},
"Multiple Data Points 3": {
JSON: `[
{"bn":"urn:dev:ow:10e2073a01080063","bt":1.3200674641e+09,
"bu":"%RH","v":21.2},
{"t":10,"v":21.3},
{"t":20,"v":21.4},
{"t":30,"v":21.4},
{"t":40,"v":21.5},
{"t":50,"v":21.5},
{"t":60,"v":21.5},
{"t":70,"v":21.6},
{"t":80,"v":21.7}
]`,
Result: []Measurement{
NewValue("urn:dev:ow:10e2073a01080063", 21.2, RelativeHumidityPercent, floatToTime(1.3200674641e+09+00), 0),
NewValue("urn:dev:ow:10e2073a01080063", 21.3, RelativeHumidityPercent, floatToTime(1.3200674641e+09+10), 0),
NewValue("urn:dev:ow:10e2073a01080063", 21.4, RelativeHumidityPercent, floatToTime(1.3200674641e+09+20), 0),
NewValue("urn:dev:ow:10e2073a01080063", 21.4, RelativeHumidityPercent, floatToTime(1.3200674641e+09+30), 0),
NewValue("urn:dev:ow:10e2073a01080063", 21.5, RelativeHumidityPercent, floatToTime(1.3200674641e+09+40), 0),
NewValue("urn:dev:ow:10e2073a01080063", 21.5, RelativeHumidityPercent, floatToTime(1.3200674641e+09+50), 0),
NewValue("urn:dev:ow:10e2073a01080063", 21.5, RelativeHumidityPercent, floatToTime(1.3200674641e+09+60), 0),
NewValue("urn:dev:ow:10e2073a01080063", 21.6, RelativeHumidityPercent, floatToTime(1.3200674641e+09+70), 0),
NewValue("urn:dev:ow:10e2073a01080063", 21.7, RelativeHumidityPercent, floatToTime(1.3200674641e+09+80), 0),
},
},
"Multiple Measurements": {
SkipEncode: true, // Encode chooses 'lat' as base unit, as '%RH', 'lon' and 'lat' save the same amount of bytes
JSON: `[
{"bn":"urn:dev:ow:10e2073a01080063","bt":1.320067464e+09,
"bu":"%RH","v":20},
{"u":"lon","v":24.30621},
{"u":"lat","v":60.07965},
{"t":60,"v":20.3},
{"u":"lon","t":60,"v":24.30622},
{"u":"lat","t":60,"v":60.07965},
{"t":120,"v":20.7},
{"u":"lon","t":120,"v":24.30623},
{"u":"lat","t":120,"v":60.07966},
{"u":"%EL","t":150,"v":98},
{"t":180,"v":21.2},
{"u":"lon","t":180,"v":24.30628},
{"u":"lat","t":180,"v":60.07967}
]`,
Result: []Measurement{
NewValue("urn:dev:ow:10e2073a01080063", 20, RelativeHumidityPercent, floatToTime(1.320067464e+09), 0),
NewValue("urn:dev:ow:10e2073a01080063", 24.30621, Longitude, floatToTime(1.320067464e+09), 0),
NewValue("urn:dev:ow:10e2073a01080063", 60.07965, Latitude, floatToTime(1.320067464e+09), 0),
NewValue("urn:dev:ow:10e2073a01080063", 20.3, RelativeHumidityPercent, floatToTime(1.320067464e+09+60), 0),
NewValue("urn:dev:ow:10e2073a01080063", 24.30622, Longitude, floatToTime(1.320067464e+09+60), 0),
NewValue("urn:dev:ow:10e2073a01080063", 60.07965, Latitude, floatToTime(1.320067464e+09+60), 0),
NewValue("urn:dev:ow:10e2073a01080063", 20.7, RelativeHumidityPercent, floatToTime(1.320067464e+09+120), 0),
NewValue("urn:dev:ow:10e2073a01080063", 24.30623, Longitude, floatToTime(1.320067464e+09+120), 0),
NewValue("urn:dev:ow:10e2073a01080063", 60.07966, Latitude, floatToTime(1.320067464e+09+120), 0),
NewValue("urn:dev:ow:10e2073a01080063", 98, RemainingBatteryPercent, floatToTime(1.320067464e+09+150), 0),
NewValue("urn:dev:ow:10e2073a01080063", 21.2, RelativeHumidityPercent, floatToTime(1.320067464e+09+180), 0),
NewValue("urn:dev:ow:10e2073a01080063", 24.30628, Longitude, floatToTime(1.320067464e+09+180), 0),
NewValue("urn:dev:ow:10e2073a01080063", 60.07967, Latitude, floatToTime(1.320067464e+09+180), 0),
},
},
"Resolved Data": {
SkipEncode: true, // Encode chooses 'lat' as base unit, as '%RH', 'lon' and 'lat' save the same amount of bytes
JSON: `[
{"n":"urn:dev:ow:10e2073a01080063","u":"%RH","t":1.320067464e+09,
"v":20},
{"n":"urn:dev:ow:10e2073a01080063","u":"lon","t":1.320067464e+09,
"v":24.30621},
{"n":"urn:dev:ow:10e2073a01080063","u":"lat","t":1.320067464e+09,
"v":60.07965},
{"n":"urn:dev:ow:10e2073a01080063","u":"%RH","t":1.320067524e+09,
"v":20.3},
{"n":"urn:dev:ow:10e2073a01080063","u":"lon","t":1.320067524e+09,
"v":24.30622},
{"n":"urn:dev:ow:10e2073a01080063","u":"lat","t":1.320067524e+09,
"v":60.07965},
{"n":"urn:dev:ow:10e2073a01080063","u":"%RH","t":1.320067584e+09,
"v":20.7},
{"n":"urn:dev:ow:10e2073a01080063","u":"lon","t":1.320067584e+09,
"v":24.30623},
{"n":"urn:dev:ow:10e2073a01080063","u":"lat","t":1.320067584e+09,
"v":60.07966},
{"n":"urn:dev:ow:10e2073a01080063","u":"%EL","t":1.320067614e+09,
"v":98},
{"n":"urn:dev:ow:10e2073a01080063","u":"%RH","t":1.320067644e+09,
"v":21.2},
{"n":"urn:dev:ow:10e2073a01080063","u":"lon","t":1.320067644e+09,
"v":24.30628},
{"n":"urn:dev:ow:10e2073a01080063","u":"lat","t":1.320067644e+09,
"v":60.07967}
]`,
Result: []Measurement{
NewValue("urn:dev:ow:10e2073a01080063", 20, RelativeHumidityPercent, floatToTime(1.320067464e+09), 0),
NewValue("urn:dev:ow:10e2073a01080063", 24.30621, Longitude, floatToTime(1.320067464e+09), 0),
NewValue("urn:dev:ow:10e2073a01080063", 60.07965, Latitude, floatToTime(1.320067464e+09), 0),
NewValue("urn:dev:ow:10e2073a01080063", 20.3, RelativeHumidityPercent, floatToTime(1.320067464e+09+60), 0),
NewValue("urn:dev:ow:10e2073a01080063", 24.30622, Longitude, floatToTime(1.320067464e+09+60), 0),
NewValue("urn:dev:ow:10e2073a01080063", 60.07965, Latitude, floatToTime(1.320067464e+09+60), 0),
NewValue("urn:dev:ow:10e2073a01080063", 20.7, RelativeHumidityPercent, floatToTime(1.320067464e+09+120), 0),
NewValue("urn:dev:ow:10e2073a01080063", 24.30623, Longitude, floatToTime(1.320067464e+09+120), 0),
NewValue("urn:dev:ow:10e2073a01080063", 60.07966, Latitude, floatToTime(1.320067464e+09+120), 0),
NewValue("urn:dev:ow:10e2073a01080063", 98, RemainingBatteryPercent, floatToTime(1.320067464e+09+150), 0),
NewValue("urn:dev:ow:10e2073a01080063", 21.2, RelativeHumidityPercent, floatToTime(1.320067464e+09+180), 0),
NewValue("urn:dev:ow:10e2073a01080063", 24.30628, Longitude, floatToTime(1.320067464e+09+180), 0),
NewValue("urn:dev:ow:10e2073a01080063", 60.07967, Latitude, floatToTime(1.320067464e+09+180), 0),
},
},
"Multiple Data Types": {
SkipEncode: true, // Encode chooses 'lat' as base unit, as '%RH', 'lon' and 'lat' save the same amount of bytes
JSON: `[
{"bn":"urn:dev:ow:10e2073a01080063:","n":"temp","u":"Cel","v":23.1},
{"n":"label","vs":"Machine Room"},
{"n":"open","vb":false},
{"n":"nfc-reader","vd":"aGkgCg=="}
]`,
Result: []Measurement{
NewValue("urn:dev:ow:10e2073a01080063:temp", 23.1, Celsius, time.Time{}, 0),
NewString("urn:dev:ow:10e2073a01080063:label", "Machine Room", None, time.Time{}, 0),
NewBoolean("urn:dev:ow:10e2073a01080063:open", false, None, time.Time{}, 0),
NewData("urn:dev:ow:10e2073a01080063:nfc-reader", []byte("hi \n"), None, time.Time{}, 0),
},
},
}
var regexpWhitespace = regexp.MustCompile(`\s`)