-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdebug.js
More file actions
53 lines (45 loc) · 1.56 KB
/
debug.js
File metadata and controls
53 lines (45 loc) · 1.56 KB
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
'use strict';
/**
* This decoding profile is meant as a general starting point for profiles.
* It decodes the buffer according to a custom byteMask, and transforms the
* data to integer values.
* The byteMask defines the amount of bytes to consume for each measurement.
* It is applied to the sensors in the order they are defined.
* @module decoding/debug
* @license MIT
*/
const { bytesToInt } = require('./helpers');
const { DecodingError } = require('../errors');
/**
* returns a bufferTransfomer for transformation of a buffer to measurements.
* @see module:decoding~bufferToMeasurements
* @param {Box} box - The box to retrieve byteMask and sensorIds from
* @return {Array} A bufferTransformer for the box
* @example <caption>decodeOptions format</caption>
* ttn: {
* profile: 'debug',
* // use first 3 bytes for first sensor, 4th byte for second, next two bytes for third sensor
* decodeOptions: [3, 1, 2]
* }
*/
const createBufferTransformer = function createBufferTransformer (box) {
const byteMask = box.integrations.ttn.decodeOptions,
transformer = [];
if (!byteMask) {
throw new DecodingError('box requires a valid byteMask', 'debug');
}
if (box.sensors.length < byteMask.length) {
throw new DecodingError(`box requires at least ${byteMask.length} sensors`, 'debug');
}
for (let i = 0; i < byteMask.length; i++) {
transformer.push({
sensorId: box.sensors[i]._id.toString(),
bytes: byteMask[i],
transformer: bytesToInt
});
}
return transformer;
};
module.exports = {
createBufferTransformer
};