forked from cakebake/node-red-contrib-alexa-remote-cakebaked
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathalexa-remote-init.js
64 lines (58 loc) · 2.04 KB
/
alexa-remote-init.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
const tools = require('../lib/common.js');
const util = require('util');
module.exports = function (RED) {
function AlexaRemoteInitNode(input) {
RED.nodes.createNode(this, input);
tools.assignNode(RED, this, ['account'], input);
tools.assign(this, ['option'], input);
if(!tools.nodeSetup(this, input, true)) return;
this.on('input', function (msg) {
const send = tools.nodeGetSendCb(this, msg);
const error = tools.nodeGetErrorCb(this);
switch(this.option) {
case 'initialise': return this.account.initAlexa(msg.payload).then(send).catch(error);
case 'fresh': return this.account.initAlexa(undefined, true).then(send).catch(error);
case 'refresh': return this.account.refreshAlexa().then(send).catch(error);
case 'update': return this.account.updateAlexa().then(send).catch(error);
case 'reset': this.account.resetAlexa(); return send();
case 'debug': return send(tools.stringifyOmitCircular(this.account));
case 'interval':
const start = this.account.refreshTimeoutStartTime;
const delta = this.account.refreshInterval;
const end = start + delta;
const now = Date.now();
const left = end - now;
const [d,h,m,s,u] = tools.durationToPieces(left);
return send({
message: `next refresh in ${d}d ${h}h ${m}m ${s}s`,
start: start,
interval: delta,
end: end,
});
default: return error('what the heck');
}
});
}
RED.nodes.registerType("alexa-remote-init", AlexaRemoteInitNode);
/*
RED.httpAdmin.post("/alexa-remote-init/:id", RED.auth.needsPermission("config.write"), function(req, res){
const node = RED.nodes.getNode(req.params.id);
if (node != null) {
try {
const msg = {}
const proxyRunning = node.account.status.code === 'wait-proxy';
if(proxyRunning) {
msg.payload = 'stop';
}
node.onInput(msg);
res.sendStatus(200);
} catch(err) {
res.sendStatus(500);
node.error(err);
}
} else {
res.sendStatus(404);
}
})
*/
};