-
Notifications
You must be signed in to change notification settings - Fork 389
/
windsensor.js
68 lines (64 loc) · 1.23 KB
/
windsensor.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
var directions = ['N', 'E', 'S', 'W'];
var colors = ['red', 'green'];
var degrees = {
N: 0,
E: 90,
S: 180,
W: 270,
};
function decodeUplink(input) {
switch (input.fPort) {
case 1:
return {
// Decoded data
data: {
direction: directions[input.bytes[0]],
speed: input.bytes[1],
},
};
default:
return {
errors: ['unknown FPort'],
};
}
}
function normalizeUplink(input) {
return {
// Normalized data
data: {
wind: {
direction: degrees[input.data.direction], // letter to degrees
speed: input.data.speed * 0.5144, // knots to m/s
},
},
};
}
function encodeDownlink(input) {
var i = colors.indexOf(input.data.led);
if (i === -1) {
return {
errors: ['invalid LED color'],
};
}
return {
// LoRaWAN FPort used for the downlink message
fPort: 2,
// Encoded bytes
bytes: [i],
};
}
function decodeDownlink(input) {
switch (input.fPort) {
case 2:
return {
// Decoded downlink (must be symmetric with encodeDownlink)
data: {
led: colors[input.bytes[0]],
},
};
default:
return {
errors: ['invalid FPort'],
};
}
}