forked from cakebake/node-red-contrib-alexa-remote-cakebaked
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathalexa-remote-event.js
42 lines (36 loc) · 1.53 KB
/
alexa-remote-event.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
const tools = require('../lib/common.js');
module.exports = function (RED) {
function AlexaRemoteEventNode(input) {
RED.nodes.createNode(this, input);
tools.assign(this, ['event'], input);
tools.assignNode(RED, this, ['account'], input);
if(!tools.nodeSetup(this, input, true)) return;
this.onAlexaEvent = (val) => {
this.status({ fill: "green", shape: "dot", text: "event fired!" });
setTimeout(() => this.status({ fill: "grey", shape: "dot", text: "listening" }), 2000);
this.send({ payload: val });
};
this.onStatus = (code) => {
if(code !== 'READY') return;
this.status({ fill: "yellow", shape: "dot", text: "starting listening" });
setTimeout(() => this.status({ fill: "grey", shape: "dot", text: "listening" }), 2000);
this.account.alexa.removeListener(this.event, this.onAlexaEvent);
this.account.alexa.addListener(this.event, this.onAlexaEvent);
};
if(!this.account.usePushConnection) {
return this.status({ fill: "red", shape: "dot", text: "events not supported by account" });
}
else {
this.account.emitter.removeListener('state', this.onStatus);
this.account.emitter.addListener('state', this.onStatus);
const {code, message} = this.account.state;
this.onStatus(code, message);
}
this.on('close', function() {
this.account.emitter.removeListener('status', this.onStatus);
this.account.alexa.removeListener(this.event, this.onAlexaEvent);
this.account.emitter.removeListener('state', this.onStatus);
});
}
RED.nodes.registerType("alexa-remote-event", AlexaRemoteEventNode);
};