-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslide.js
366 lines (323 loc) · 10.4 KB
/
slide.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
module.exports = function(RED) {
"use strict";
const LocalApi = require('./lib/LocalApi');
/**
* Temporarily updates the node status
*
* @param {Node} node The node to update the status for.
* @param {boolean} ok The result of the call.
* @param {string} msg The message to display.
*/
function blinkStatus(node, ok, msg) {
clearTimeout(node.statusTimer);
node.status({ 'fill': (ok ? 'green' : 'red'), 'shape': 'ring', 'text': msg });
node.statusTimer = setTimeout(() => {
if (!node.slide.localApi.appearsValid()) {
node.status({ 'fill': 'red', 'shape': 'ring', 'text': 'Invalid config' });
} else {
node.status({ 'fill': 'blue', 'shape': 'ring', 'text': node.slide.hostname });
}
}, 3000);
}
/**
* Helper function to parse a passed float value
*
* @param {any} value Float to parse
* @param {float} fallback Float to return in case parsing failed.
* @returns {Promise} Promise object representing the result of the API call.
*/
function checkFloat(value, fallback) {
return (/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/.test(value)) ? Number(value) : fallback;
}
/**
* Helper function to get the right topic for the slide
*
* @param {object} node Node object
* @param {object} msg Input message object
* @returns {string} The topic which we should use
*/
function getTopic(node, msg) {
node.topic = (node.topic ? node.topic : '').trim();
msg.topic = (msg.topic ? msg.topic : '').trim();
let topic = 'slide';
if (node.topic !== '') {
topic = node.topic
} else {
if (msg.topic !== '') {
topic = msg.topic
}
}
return topic;
}
/**
* Configuration node which holds only the device code.
*
* @param {Node} node The node to create.
*/
function localConf(n) {
RED.nodes.createNode(this, n);
// Properties
this.name = n.name;
this.hostname = n.hostname;
this.devicecode = n.devicecode;
this.openPosition = n.openPosition;
this.closePosition = n.closePosition;
// Create object
this.localApi = new LocalApi(this.hostname, this.devicecode, this.openPosition, this.closePosition);
}
RED.nodes.registerType("slide-conf", localConf);
/**
* Get info node.
*
* @param {Node} node The node to create.
*/
function slideGetInfo(n) {
RED.nodes.createNode(this, n);
this.slide = RED.nodes.getNode(n.slide);
if (!this.slide) {
return null;
} else if (!this.slide.localApi.appearsValid()) {
this.status({ 'fill': 'red', 'shape': 'ring', 'text': 'Invalid config' });
} else {
this.status({ 'fill': 'blue', 'shape': 'ring', 'text': this.slide.hostname });
}
this.topic = n.topic;
let node = this;
node.on('input', (msg, send, done) => {
blinkStatus(node, true, 'Sending command ...');
node.slide.localApi.getInfo().then(result => {
blinkStatus(node, true, 'Command OK');
send({ 'topic': getTopic(node, msg), 'succeeded': true, 'payload': result });
done();
}).catch(e => {
blinkStatus(node, false, 'Command failed!');
send({ 'topic': getTopic(node, msg), 'succeeded': false, 'payload': e });
done();
});
});
}
RED.nodes.registerType("slide-get-info", slideGetInfo);
/**
* Set position node
*
* @param {Node} node The node to create.
*/
function slideSetPosition(n) {
RED.nodes.createNode(this, n);
this.slide = RED.nodes.getNode(n.slide);
if (!this.slide) {
return null;
} else if (!this.slide.localApi.appearsValid()) {
this.status({ 'fill': 'red', 'shape': 'ring', 'text': 'Invalid config' });
} else {
this.status({ 'fill': 'blue', 'shape': 'ring', 'text': this.slide.hostname });
}
this.topic = n.topic;
let node = this;
node.on('input', (msg, send, done) => {
let position = -1;
if (msg && msg.payload && msg.payload.hasOwnProperty('percent')) {
let percent = Number(msg.payload.percent);
if (!isNaN(percent) && ((percent >= 0) && (percent <= 100))) {
position = (percent / 100);
}
}
if (position === -1) {
blinkStatus(node, false, 'Invalid input');
node.warn('Please pass a message with a payload which looks like this: { "payload": { "percent": 50 } }');
send(null);
done();
} else {
blinkStatus(node, true, 'Sending command ...');
node.slide.localApi.setPos(position).then(result => {
blinkStatus(node, true, 'Command OK');
send({ 'topic': getTopic(node, msg), 'succeeded': true, 'payload': result });
done();
}).catch(e => {
blinkStatus(node, false, 'Command failed!');
send({ 'topic': getTopic(node, msg), 'succeeded': false, 'payload': e });
done();
});
}
});
}
RED.nodes.registerType("slide-set-position", slideSetPosition);
/**
* Open node.
*
* @param {Node} node The node to create.
*/
function slideOpen(n) {
RED.nodes.createNode(this, n);
this.slide = RED.nodes.getNode(n.slide);
if (!this.slide) {
return null;
} else if (!this.slide.localApi.appearsValid()) {
this.status({ 'fill': 'red', 'shape': 'ring', 'text': 'Invalid config' });
} else {
this.status({ 'fill': 'blue', 'shape': 'ring', 'text': this.slide.hostname });
}
this.topic = n.topic;
let node = this;
node.on('input', (msg, send, done) => {
blinkStatus(node, true, 'Sending command ...');
node.slide.localApi.open().then(result => {
blinkStatus(node, true, 'Command OK');
send({ 'topic': getTopic(node, msg), 'succeeded': true, 'payload': result });
done();
}).catch(e => {
blinkStatus(node, false, 'Command failed!');
send({ 'topic': getTopic(node, msg), 'succeeded': false, 'payload': e });
done();
});
});
}
RED.nodes.registerType("slide-open", slideOpen);
/**
* Close node.
*
* @param {Node} node The node to create.
*/
function slideClose(n) {
RED.nodes.createNode(this, n);
this.slide = RED.nodes.getNode(n.slide);
if (!this.slide) {
return null;
} else if (!this.slide.localApi.appearsValid()) {
this.status({ 'fill': 'red', 'shape': 'ring', 'text': 'Invalid config' });
} else {
this.status({ 'fill': 'blue', 'shape': 'ring', 'text': this.slide.hostname });
}
this.topic = n.topic;
let node = this;
node.on('input', (msg, send, done) => {
blinkStatus(node, true, 'Sending command ...');
node.slide.localApi.close().then(result => {
blinkStatus(node, true, 'Command OK');
send({ 'topic': getTopic(node, msg), 'succeeded': true, 'payload': result });
done();
}).catch(e => {
blinkStatus(node, false, 'Command failed!');
send({ 'topic': getTopic(node, msg), 'succeeded': false, 'payload': e });
done();
});
});
}
RED.nodes.registerType("slide-close", slideClose);
/**
* Stop node.
*
* @param {Node} node The node to create.
*/
function slideStop(n) {
RED.nodes.createNode(this, n);
this.slide = RED.nodes.getNode(n.slide);
if (!this.slide) {
return null;
} else if (!this.slide.localApi.appearsValid()) {
this.status({ 'fill': 'red', 'shape': 'ring', 'text': 'Invalid config' });
} else {
this.status({ 'fill': 'blue', 'shape': 'ring', 'text': this.slide.hostname });
}
this.topic = n.topic;
let node = this;
node.on('input', (msg, send, done) => {
blinkStatus(node, true, 'Sending command ...');
node.slide.localApi.stop().then(result => {
blinkStatus(node, true, 'Command OK');
send({ 'topic': getTopic(node, msg), 'succeeded': true, 'payload': result });
done();
}).catch(e => {
blinkStatus(node, false, 'Command failed!');
send({ 'topic': getTopic(node, msg), 'succeeded': false, 'payload': e });
done();
});
});
}
RED.nodes.registerType("slide-stop", slideStop);
/**
* Updates the slide WiFi
*
* @param {Node} node The node to create.
*/
function slideUpdateWiFi(n) {
RED.nodes.createNode(this, n);
this.slide = RED.nodes.getNode(n.slide);
if (!this.slide) {
return null;
} else if (!this.slide.localApi.appearsValid()) {
this.status({ 'fill': 'red', 'shape': 'ring', 'text': 'Invalid config' });
} else {
this.status({ 'fill': 'blue', 'shape': 'ring', 'text': this.slide.hostname });
}
this.topic = n.topic;
let node = this;
node.on('input', (msg, send, done) => {
let ssid = '';
let pass = '';
if (msg && msg.payload && msg.payload.hasOwnProperty('ssid') && msg.payload.hasOwnProperty('pass')) {
ssid = msg.payload.ssid.trim();
pass = msg.payload.pass.trim();
}
if ((ssid === '') || (pass === '')) {
blinkStatus(node, false, 'Invalid input');
node.warn('Please pass a message with a payload which looks like this: { "payload": { "ssid": "your_new_ssid", "pass": "your_new_ssid_password" } }');
send(null);
done();
} else {
blinkStatus(node, true, 'Sending command ...');
node.slide.localApi.updateWifi(ssid, pass).then(result => {
blinkStatus(node, true, 'Command OK');
send({ 'topic': getTopic(node, msg), 'succeeded': true, 'payload': result });
done();
}).catch(e => {
blinkStatus(node, false, 'Command failed!');
send({ 'topic': getTopic(node, msg), 'succeeded': false, 'payload': e });
done();
});
}
});
}
RED.nodes.registerType("slide-update-wifi", slideUpdateWiFi);
// HTTP callbacks for config node
RED.httpAdmin.post('/slide/info', function(req, res) {
let localApi = new LocalApi(req.body.hostname, req.body.devicecode);
localApi.getInfo().then(result => {
res.status(200).send(result);
}).catch(e => {
res.status(e.code).send(e);
});
});
RED.httpAdmin.post('/slide/calibrate', function(req, res) {
let localApi = new LocalApi(req.body.hostname, req.body.devicecode);
localApi.calibrate().then(result => {
res.status(200).send(result);
}).catch(e => {
res.status(e.code).send(e);
});
});
RED.httpAdmin.post('/slide/open', function(req, res) {
let localApi = new LocalApi(req.body.hostname, req.body.devicecode, req.body.openPosition, req.body.closePosition);
localApi.open().then(result => {
res.status(200).send(result);
}).catch(e => {
res.status(e.code).send(e);
});
});
RED.httpAdmin.post('/slide/close', function(req, res) {
let localApi = new LocalApi(req.body.hostname, req.body.devicecode, req.body.openPosition, req.body.closePosition);
localApi.close().then(result => {
res.status(200).send(result);
}).catch(e => {
res.status(e.code).send(e);
});
});
RED.httpAdmin.post('/slide/stop', function(req, res) {
let localApi = new LocalApi(req.body.hostname, req.body.devicecode);
localApi.stop().then(() => {
res.status(200).send();
}).catch(e => {
res.status(e.code).send(e);
});
});
};