/
DL-ATM22.js
103 lines (93 loc) · 3.42 KB
/
DL-ATM22.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
/* https://www.decentlab.com/products/wind-speed-wind-direction-and-temperature-sensor-for-lorawan */
var decentlab_decoder = {
PROTOCOL_VERSION: 2,
SENSORS: [
{length: 8,
values: [{name: 'wind_speed',
displayName: 'Wind speed',
convert: function (x) { return (x[0] - 32768) / 100; },
unit: 'm⋅s⁻¹'},
{name: 'wind_direction',
displayName: 'Wind direction',
convert: function (x) { return (x[1] - 32768) / 10; },
unit: '°'},
{name: 'maximum_wind_speed',
displayName: 'Maximum wind speed',
convert: function (x) { return (x[2] - 32768) / 100; },
unit: 'm⋅s⁻¹'},
{name: 'air_temperature',
displayName: 'Air temperature',
convert: function (x) { return (x[3] - 32768) / 10; },
unit: '°C'},
{name: 'x_orientation_angle',
displayName: 'X orientation angle',
convert: function (x) { return (x[4] - 32768) / 10; },
unit: '°'},
{name: 'y_orientation_angle',
displayName: 'Y orientation angle',
convert: function (x) { return (x[5] - 32768) / 10; },
unit: '°'},
{name: 'north_wind_speed',
displayName: 'North wind speed',
convert: function (x) { return (x[6] - 32768) / 100; },
unit: 'm⋅s⁻¹'},
{name: 'east_wind_speed',
displayName: 'East wind speed',
convert: function (x) { return (x[7] - 32768) / 100; },
unit: 'm⋅s⁻¹'}]},
{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("0208c900038009812b8014810880027fe8800880040bf5"));
console.log(decentlab_decoder.decode("0208c900020bf5"));
}
main();