-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathifttt-trigger.js
executable file
·77 lines (59 loc) · 1.85 KB
/
ifttt-trigger.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
module.exports = function (RED) {
'use strict';
var ifttt = require('ifttt');
var util = require('util');
var utility = require('./utility');
const iftttNodeTrigger = require('./IFTTTNodeTrigger');
function IFTTTTriggerNode(n) {
RED.nodes.createNode(this, n);
var node = this;
utility.setNodeUnknownStatus(node, undefined);
this.broker = n.broker;
this.brokerNode = RED.nodes.getNode(this.broker);
this.endpoint = n.endpoint;
this.fields = n.fields;
this.ingredients = n.ingredients;
this.triggerEventQueue = [];
this.unsentTriggerEventCount = function() {
return this.triggerEventQueue.filter(function(test) {
return test.sent === false;
}).length;
};
this.updateNodeStatus = function() {
if (this.brokerNode.ifttt) {
const count = this.unsentTriggerEventCount();
this.status({
fill: count > 0 ? "yellow" : "green",
shape: "dot",
text: count + " unsent"
});
} else {
utility.setNodeRegisterStatus(this, false);
}
};
this.addTriggerEvent = function(msg) {
const event = {
payload: msg.payload,
createdAt: new Date(),
sent: false
};
this.triggerEventQueue.unshift(event);
this.updateNodeStatus();
};
this.on("input", function(msg) {
msg.topic = this.endpoint;
this.addTriggerEvent(msg);
});
if (this.brokerNode.ifttt) {
const triggerInstance = iftttNodeTrigger.createDefault(node);
node.triggerInstance = triggerInstance;
this.brokerNode.ifttt.registerTrigger(triggerInstance);
this.on('close', function(done) {
this.brokerNode.ifttt.unregisterTrigger(triggerInstance);
done();
});
}
this.updateNodeStatus();
}
RED.nodes.registerType('ifttt-trigger', IFTTTTriggerNode);
};