-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDL-LP8P.js
119 lines (109 loc) · 4.15 KB
/
DL-LP8P.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
/* https://www.decentlab.com/products/co2-temperature-humidity-and-barometric-pressure-sensor-for-lorawan */
var decentlab_decoder = {
PROTOCOL_VERSION: 2,
SENSORS: [
{length: 2,
values: [{name: 'air_temperature',
displayName: 'Air temperature',
convert: function (x) { return 175.72 * x[0] / 65536 - 46.85; },
unit: '°C'},
{name: 'air_humidity',
displayName: 'Air humidity',
convert: function (x) { return 125 * x[1] / 65536 - 6; },
unit: '%'}]},
{length: 2,
values: [{name: 'barometer_temperature',
displayName: 'Barometer temperature',
convert: function (x) { return (x[0] - 5000) / 100; },
unit: '°C'},
{name: 'barometric_pressure',
displayName: 'Barometric pressure',
convert: function (x) { return x[1] * 2; },
unit: 'Pa'}]},
{length: 8,
values: [{name: 'co2_concentration',
displayName: 'CO2 concentration',
convert: function (x) { return x[0] - 32768; },
unit: 'ppm'},
{name: 'co2_concentration_lpf',
displayName: 'CO2 concentration LPF',
convert: function (x) { return x[1] - 32768; },
unit: 'ppm'},
{name: 'co2_sensor_temperature',
displayName: 'CO2 sensor temperature',
convert: function (x) { return (x[2] - 32768) / 100; },
unit: '°C'},
{name: 'capacitor_voltage_1',
displayName: 'Capacitor voltage 1',
convert: function (x) { return x[3] / 1000; },
unit: 'V'},
{name: 'capacitor_voltage_2',
displayName: 'Capacitor voltage 2',
convert: function (x) { return x[4] / 1000; },
unit: 'V'},
{name: 'co2_sensor_status',
displayName: 'CO2 sensor status',
convert: function (x) { return x[5]; }},
{name: 'raw_ir_reading',
displayName: 'Raw IR reading',
convert: function (x) { return x[6]; }},
{name: 'raw_ir_reading_lpf',
displayName: 'Raw IR reading LPF',
convert: function (x) { return x[7]; }}]},
{length: 1,
values: [{name: 'battery_voltage',
displayName: 'Battery voltage',
convert: function (x) { return x[0] / 1000; },
unit: 'V'}]}
],
read_int: function (bytes, pos) {
return (bytes[pos] << 8) + bytes[pos + 1];
},
decode: function (msg) {
var bytes = msg;
var i, j;
if (typeof msg === 'string') {
bytes = [];
for (i = 0; i < msg.length; i += 2) {
bytes.push(parseInt(msg.substring(i, i + 2), 16));
}
}
var version = bytes[0];
if (version != this.PROTOCOL_VERSION) {
return {error: "protocol version " + version + " doesn't match v2"};
}
var deviceId = this.read_int(bytes, 1);
var flags = this.read_int(bytes, 3);
var result = {'protocol_version': version, 'device_id': deviceId};
// decode payload
var pos = 5;
for (i = 0; i < this.SENSORS.length; i++, flags >>= 1) {
if ((flags & 1) !== 1)
continue;
var sensor = this.SENSORS[i];
var x = [];
// convert data to 16-bit integer array
for (j = 0; j < sensor.length; j++) {
x.push(this.read_int(bytes, pos));
pos += 2;
}
// decode sensor values
for (j = 0; j < sensor.values.length; j++) {
var value = sensor.values[j];
if ('convert' in value) {
result[value.name] = {displayName: value.displayName,
value: value.convert.bind(this)(x)};
if ('unit' in value)
result[value.name]['unit'] = value.unit;
}
}
}
return result;
}
};
function main() {
console.log(decentlab_decoder.decode("020578000f67bd618d1cedbd1081d981f4895b0bd80bb50000959895390c25"));
console.log(decentlab_decoder.decode("020578000b67bd618d1cedbd100c25"));
console.log(decentlab_decoder.decode("02057800080c25"));
}
main();