-
Notifications
You must be signed in to change notification settings - Fork 0
/
downlinkCode.test.js
342 lines (300 loc) · 9.16 KB
/
downlinkCode.test.js
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* This code is free software:
* you can redistribute it and/or modify it under the terms of a Creative
* Commons Attribution-NonCommercial 4.0 International License
* (http://creativecommons.org/licenses/by-nc/4.0/)
*
* Copyright (c) 2024 March by Klaasjan Wagenaar, Tristan Bosveld and Richard Kroesen
*/
const { decodeUplink, SensorTypes } = require('./decoder_cayenneLPP_extreme');
describe('decodeDownlink', () => {
/* TEST #1 for DIG_IN */
it('correctly decodes DIG_IN sensor data', () => {
const input = {
fPort: 1,
bytes: [
SensorTypes.DIG_IN.type,
5, // Channel
0 // Data (digital low)
]
};
const expected = {
decoder_version: 1,
data: {
digital_5: 0
},
warnings: [],
errors: []
};
const result = decodeUplink(input);
expect(result).toEqual(expected);
});
/* TEST #2 for DIG_OUT */
it('correctly decodes DIG_OUT sensor data', () => {
const input = {
fPort: 1,
bytes: [
SensorTypes.DIG_OUT.type, // Assuming SensorTypes is structured as shown previously
2, // Channel
1 // Data (digital high)
]
};
const expected = {
decoder_version: 1,
data: {
digital_2: 1
},
warnings: [],
errors: []
};
const result = decodeUplink(input);
expect(result).toEqual(expected);
});
/* TEST #3 */
it('correctly decodes ANL_IN sensor data', () => {
const input = {
fPort: 1,
bytes: [
SensorTypes.ANL_IN.type,
1, // Channel
/* Decimal value of 2,68 represented: */
12, // LSB
1 // MSB
]
};
const expected = {
decoder_version: 1,
data: {
analog_1: 2.68
},
warnings: [],
errors: []
};
const result = decodeUplink(input);
expect(result).toEqual(expected);
});
/* TEST #4 Negative Values */
it('correctly decodes ANL_IN sensor negative data', () => {
const input = {
fPort: 1,
bytes: [
SensorTypes.ANL_IN.type,
1, // Channel
/* Decimal value of -12 represented as two's complement: */
244,
255
]
};
const expected = {
decoder_version: 1,
data: {
analog_1: -0.12
},
warnings: [],
errors: []
};
const result = decodeUplink(input);
expect(result).toEqual(expected);
});
/* Test #5 for ILLUM_SENS sensor data */
it('correctly decodes ILLUM_SENS sensor data', () => {
const input = {
fPort: 1,
bytes: [
SensorTypes.ILLUM_SENS.type,
1, // Channel
100, // LSB
0, // MSB
]
};
const expected = {
decoder_version: 1,
data: {
illumination_1: 100
},
warnings: [],
errors: []
};
const result = decodeUplink(input);
expect(result).toEqual(expected);
});
/* Test #6 for PRSNC_SENS sensor data */
it('correctly decodes PRSNC_SENS sensor data', () => {
const input = {
fPort: 1,
bytes: [
SensorTypes.PRSNC_SENS.type,
2, // Channel
1 // Presence detected
]
};
const expected = {
decoder_version: 1,
data: {
presence_2: 1
},
warnings: [],
errors: []
};
const result = decodeUplink(input);
expect(result).toEqual(expected);
});
/* Test #7 for TEMP_SENS sensor data (negative value) */
it('correctly decodes TEMP_SENS sensor negative data', () => {
const input = {
fPort: 1,
bytes: [
SensorTypes.TEMP_SENS.type,
3, // Channel
244, // LSB (244), represents -12 in 2's complement with 0.1°C precision
255, // MSB (-1 if interpreted as signed)
]
};
const expectedValue = -1.2;
const expected = {
decoder_version: 1,
data: {
temperature_3: expectedValue
},
warnings: [],
errors: []
};
const result = decodeUplink(input);
expect(result).toEqual(expected);
});
/* Test #8 for HUM_SENS sensor data */
it('correctly decodes HUM_SENS sensor data', () => {
const input = {
fPort: 1,
bytes: [
SensorTypes.HUM_SENS.type,
1, // Channel
50
]
};
const expected = {
decoder_version: 1,
data: {
humidity_1: 5.0
},
warnings: [],
errors: []
};
const result = decodeUplink(input);
expect(result).toEqual(expected);
});
/* Test #9 for ACCRM_SENS sensor data */
it('correctly decodes ACCRM_SENS sensor data', () => {
const input = {
fPort: 1,
bytes: [
SensorTypes.ACCRM_SENS.type,
1, // Channel
// Assuming 2 bytes per axis, with a total of 6 bytes for x, y, z
1, 0, // X-axis
2, 0, // Y-axis
3, 0 // Z-axis
]
};
const expected = {
decoder_version: 1,
data: {
accelerometer_1: {
x: 1 / SensorTypes.ACCRM_SENS.precision,
y: 2 / SensorTypes.ACCRM_SENS.precision,
z: 3 / SensorTypes.ACCRM_SENS.precision
}
},
warnings: [],
errors: []
};
const result = decodeUplink(input);
expect(result).toEqual(expected);
});
/* Test #10 for BARO_SENS sensor data */
it('correctly decodes BARO_SENS sensor data', () => {
const input = {
fPort: 1,
bytes: [
SensorTypes.BARO_SENS.type,
1, // Channel
144, // LSB, example value
1, // MSB
]
};
const expected = {
decoder_version: 1,
data: {
barometer_1: ((1 << 8) + 144) / SensorTypes.BARO_SENS.precision // Assuming 0.1 hPa precision
},
warnings: [],
errors: []
};
const result = decodeUplink(input);
expect(result).toEqual(expected);
});
/* Test #11 for GYRO_SENS sensor data */
it('correctly decodes GYRO_SENS sensor data', () => {
const input = {
fPort: 1,
bytes: [
SensorTypes.GYRO_SENS.type,
1, // Channel
// Example gyro data for x, y, z axes
100, 0, // X-axis
150, 0, // Y-axis
200, 0 // Z-axis
]
};
const expected = {
decoder_version: 1,
data: {
gyroscope_1: {
x: 100 / SensorTypes.GYRO_SENS.precision,
y: 150 / SensorTypes.GYRO_SENS.precision,
z: 200 / SensorTypes.GYRO_SENS.precision
}
},
warnings: [],
errors: []
};
const result = decodeUplink(input);
expect(result).toEqual(expected);
});
/* Test #12 for GPS_LOC sensor data */
it('correctly decodes GPS_LOC sensor data', () => {
function int32ToBytes(value) {
return [
value & 0xFF,
(value >> 8) & 0xFF,
(value >> 16) & 0xFF,
(value >> 24) & 0xFF
];
}
const LAT_LON_PRECISION = 10000;
const ALTITUDE_PRECISION = 100;
const input = {
fPort: 1,
bytes: [
SensorTypes.GPS_LOC.type,
6,
...int32ToBytes(515074), // Latitude (51.5074 scaled by 0.0001)
...int32ToBytes(-1278), // Longitude (-0.1278 scaled by 0.0001)
...int32ToBytes(3000) // Altitude (30.0 scaled by 0.01)
]
};
const expected = {
decoder_version: 1,
data: {
gps_6: { // Assuming channel 6 for GPS
x: 515074/LAT_LON_PRECISION, // Latitude
y: -1279/LAT_LON_PRECISION, // Longitude
z: 3000/ALTITUDE_PRECISION // Altitude
}
},
warnings: [],
errors: []
};
const result = decodeUplink(input);
expect(result).toEqual(expected);
});
});