forked from TheThingsNetwork/go-cayenne-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder_test.go
171 lines (140 loc) · 4.14 KB
/
decoder_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
// Copyright © 2021 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package cayennelpp
import (
"bytes"
"testing"
"io"
. "github.com/smartystreets/assertions"
)
type target struct {
values map[uint8]interface{}
}
func (t *target) Port(channel uint8, value float64) {
t.values[channel] = value
}
func (t *target) DigitalInput(channel, value uint8) {
t.values[channel] = value
}
func (t *target) DigitalOutput(channel, value uint8) {
t.values[channel] = value
}
func (t *target) AnalogInput(channel uint8, value float64) {
t.values[channel] = value
}
func (t *target) AnalogOutput(channel uint8, value float64) {
t.values[channel] = value
}
func (t *target) Luminosity(channel uint8, value uint16) {
t.values[channel] = value
}
func (t *target) Presence(channel, value uint8) {
t.values[channel] = value
}
func (t *target) Temperature(channel uint8, celcius float64) {
t.values[channel] = celcius
}
func (t *target) RelativeHumidity(channel uint8, rh float64) {
t.values[channel] = rh
}
func (t *target) Accelerometer(channel uint8, x, y, z float64) {
t.values[channel] = []float64{x, y, z}
}
func (t *target) BarometricPressure(channel uint8, hpa float64) {
t.values[channel] = hpa
}
func (t *target) Gyrometer(channel uint8, x, y, z float64) {
t.values[channel] = []float64{x, y, z}
}
func (t *target) GPS(channel uint8, latitude, longitude, altitude float64) {
t.values[channel] = []float64{latitude, longitude, altitude}
}
func TestDecode(t *testing.T) {
a := New(t)
// Happy flow: uplink
{
buf := []byte{
1, DigitalInput, 255,
2, DigitalOutput, 100,
3, AnalogInput, 21, 74,
4, AnalogOutput, 234, 182,
5, Luminosity, 1, 244,
6, Presence, 50,
7, Temperature, 255, 100,
8, RelativeHumidity, 160,
9, Accelerometer, 254, 88, 0, 15, 6, 130,
10, BarometricPressure, 41, 239,
11, Gyrometer, 1, 99, 2, 49, 254, 102,
12, GPS, 7, 253, 135, 0, 190, 245, 0, 8, 106,
}
decoder := NewDecoder(bytes.NewBuffer(buf))
target := &target{make(map[uint8]interface{})}
err := decoder.DecodeUplink(target)
a.So(err, ShouldBeNil)
a.So(target.values[1], ShouldEqual, 255)
a.So(target.values[2], ShouldEqual, 100)
a.So(target.values[3], ShouldEqual, 54.5)
a.So(target.values[4], ShouldEqual, -54.5)
a.So(target.values[5], ShouldEqual, 500)
a.So(target.values[6], ShouldEqual, 50)
a.So(target.values[7], ShouldEqual, -15.6)
a.So(target.values[8], ShouldEqual, 80)
a.So(target.values[9], ShouldResemble, []float64{-0.424, 0.015, 1.666})
a.So(target.values[10], ShouldEqual, 1073.5)
a.So(target.values[11], ShouldResemble, []float64{3.55, 5.61, -4.10})
a.So(target.values[12], ShouldResemble, []float64{52.3655, 4.8885, 21.54})
}
// Happy flow: downlink
{
buf := []byte{
1, 0, 100,
2, 234, 182,
255,
}
decoder := NewDecoder(bytes.NewBuffer(buf))
target := &target{make(map[uint8]interface{})}
err := decoder.DecodeDownlink(target)
a.So(err, ShouldBeNil)
a.So(target.values[1], ShouldEqual, 1)
a.So(target.values[2], ShouldEqual, -54.5)
}
// Invalid data type
{
buf := []byte{
1, 255, 255,
}
decoder := NewDecoder(bytes.NewBuffer(buf))
target := &target{make(map[uint8]interface{})}
err := decoder.DecodeUplink(target)
a.So(err, ShouldEqual, ErrInvalidChannelType)
}
// Not enough data: uplink
{
buf := []byte{
12, GPS, 7, 253, 135, 0, 190,
}
decoder := NewDecoder(bytes.NewBuffer(buf))
target := &target{make(map[uint8]interface{})}
err := decoder.DecodeUplink(target)
a.So(err, ShouldEqual, io.ErrUnexpectedEOF)
}
// Not enough data: downlink
{
buf := []byte{
12, 1,
}
decoder := NewDecoder(bytes.NewBuffer(buf))
target := &target{make(map[uint8]interface{})}
err := decoder.DecodeDownlink(target)
a.So(err, ShouldEqual, io.ErrUnexpectedEOF)
}
// Negative coordinates
{
buf := []byte{0x01, GPS, 0x06, 0x76, 0x5f, 0xf2, 0x96, 0x0a, 0x00, 0x03, 0xe8}
decoder := NewDecoder(bytes.NewBuffer(buf))
target := &target{make(map[uint8]interface{})}
err := decoder.DecodeUplink(target)
a.So(err, ShouldBeNil)
a.So(target.values[1], ShouldResemble, []float64{42.3519, -87.9094, 10})
}
}