-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTN_MClimate-Vicki_downlink-encoder.js
156 lines (150 loc) · 4.94 KB
/
TTN_MClimate-Vicki_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function encodeDownlink(input) {
var bytes = [];
for (let key of Object.keys(input.data)) {
switch(key) {
case 'getAllParams': {
bytes.push(0x12);
bytes.push(0x13);
bytes.push(0x14);
bytes.push(0x15);
bytes.push(0x16);
bytes.push(0x17);
bytes.push(0x18);
bytes.push(0x19);
bytes.push(0x1B);
break;}
case 'setKeepAlive': {
bytes.push(0x02);
bytes.push(input.data.setKeepAlive);
break;}
case 'recalibrateMotor': {
bytes.push(0x03);
break;}
case 'getDeviceVersions': {
bytes.push(0x04);
break;}
case 'setOpenWindow': {
let enabled = Number(input.data.setOpenWindow.enabled);
let closeTime = parseInt(input.data.setOpenWindow.closeTime / 5);
let delta = parseInt(input.data.setOpenWindow.delta, 8);
let motorPosition = input.data.setOpenWindow.motorPosition;
let motorPositionFirstPart = motorPosition & 0xFF;
let motorPositionSecondPart = (motorPosition >> 8) & 0xFF;
bytes.push(0x06);
bytes.push(enabled);
bytes.push(closeTime);
bytes.push(motorPositionFirstPart);
bytes.push((motorPositionSecondPart << 4) | delta);
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 'forceClose': {
bytes.push(0x0B);
break;}
case 'setInternalAlgoParams': {
bytes.push(0x0C);
bytes.push(input.data.setInternalAlgoParams.pFirstLast);
bytes.push(input.data.setInternalAlgoParams.pNext);
break;}
case 'setOperationalMode': {
bytes.push(0x0D);
bytes.push(input.data.setOperationalMode);
break;}
case 'setTargetTemperature': {
bytes.push(0x0E);
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 'getInternalAlgoParams': {
bytes.push(0x16);
break;}
case 'getInternalAlgoTdiffParams': {
bytes.push(0x17);
break;}
case 'getOperationalMode': {
bytes.push(0x18);
break;}
case 'getJoinRetryPeriod': {
bytes.push(0x19);
break;}
case 'setInternalAlgoTdiffParams': {
bytes.push(0x1A);
bytes.push(input.data.setInternalAlgoTdiffParams.cold);
bytes.push(input.data.setInternalAlgoTdiffParams.warm);
break;}
case 'getUplinkType': {
bytes.push(0x1B);
break;}
case 'setTargetTemperatureAndMotorPosition': {
bytes.push(0x31);
bytes.push(input.data.setTargetTemperatureAndMotorPosition.motorPosition);
bytes.push(input.data.setTargetTemperatureAndMotorPosition.targetTemperature);
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}]}