-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTN_MClimate-Wirelessthermostat_downlink-encoder.js
108 lines (102 loc) · 3.08 KB
/
TTN_MClimate-Wirelessthermostat_downlink-encoder.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
function encodeDownlink(input) {
var bytes = [];
for (let key of Object.keys(input.data)) {
switch(key) {
case 'getAllParams': {
bytes.push(0x12);
bytes.push(0x14);
bytes.push(0x15);
bytes.push(0x19);
bytes.push(0x1B);
break;}
case 'setKeepAlive': {
bytes.push(0x02);
bytes.push(input.data.setKeepAlive);
break;}
case 'getDeviceVersions': {
bytes.push(0x04);
break;}
case 'setChildLock': {
bytes.push(0x07);
bytes.push(Number(input.data.setChildLock));
break;}
case 'setTemperatureRange': {
bytes.push(0x08);
bytes.push(input.data.setTemperatureRange.min);
bytes.push(input.data.setTemperatureRange.max);
break;}
case 'setTargetTemperature': {
bytes.push(0x2E);
bytes.push(input.data.setTargetTemperature);
break;}
case 'setJoinRetryPeriod': {
// period should be passed in minutes
let periodToPass = (input.data.setJoinRetryPeriod * 60) / 5;
periodToPass = int(periodToPass);
bytes.push(0x10);
bytes.push(periodToPass);
break;}
case 'setUplinkType': {
bytes.push(0x11);
bytes.push(input.data.setUplinkType);
break;}
case 'getKeepAliveTime': {
bytes.push(0x12);
break;}
case 'getOpenWindowParams': {
bytes.push(0x13);
break;}
case 'getChildLock': {
bytes.push(0x14);
break;}
case 'getTemperatureRange': {
bytes.push(0x15);
break;}
case 'getOperationalMode': {
bytes.push(0x18);
break;}
case 'getJoinRetryPeriod': {
bytes.push(0x19);
break;}
case 'getUplinkType': {
bytes.push(0x1B);
break;}
case 'receivedKeepalive': {
bytes.push(0x55);
break;}
case 'sendCustomHexCommand': {
let sendCustomHexCommand = input.data.sendCustomHexCommand
for (let i = 0; i < sendCustomHexCommand.length; i += 2) {
const byte = parseInt(sendCustomHexCommand.substr(i, 2), 16);
bytes.push(byte);
}
break;}
default: {}
}
}
return {
bytes: bytes,
fPort: 1,
warnings: [],
errors: []
};
}
function decodeDownlink(input) {
return {
data: {
bytes: input.bytes
},
warnings: [],
errors: []
}
}
// example downlink commands
// {"getOperationalMode":""} --> 0x18
// {"setTargetTemperature":20} --> 0x0E14
// {"setTemperatureRange":{"min":15,"max":21}} --> 0x080F15
// {"setChildLock":true} --> 0701
// {"sendCustomHexCommand":"080F15"} --> 0x080F15
// {"setOpenWindow":{"enabled": true, "closeTime": 20 , "delta": 3, "motorPosition": 540}} --> 0x0601041C23
// example Node Red mqtt message
// msg.topic = 'v3/<Application ID>@ttn/devices/<End device ID>/down/push'
// msg.payload = {"downlinks":[{f_port:1,decoded_payload:{setTargetTemperature:20},priority:'NORMAL',confirmed:false}]}