forked from node-red/node-red-nodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
57-prowl.js
58 lines (54 loc) · 1.99 KB
/
57-prowl.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
module.exports = function(RED) {
"use strict";
var Prowl = require('node-prowl');
try {
var pushkeys = RED.settings.prowl || require(process.env.NODE_RED_HOME+"/../pushkey.js");
}
catch(err) { }
function ProwlNode(n) {
RED.nodes.createNode(this,n);
this.title = n.title;
this.priority = parseInt(n.priority);
if (this.priority > 2) { this.priority = 2; }
if (this.priority < -2) { this.priority = -2; }
var credentials = this.credentials;
if ((credentials) && (credentials.hasOwnProperty("pushkey"))) { this.pushkey = credentials.pushkey; }
else {
if (pushkeys) { this.pushkey = pushkeys.prowlkey; }
else { this.error("No Prowl credentials set."); }
}
this.prowl = false;
if (this.pushkey) { this.prowl = new Prowl(this.pushkey); }
var node = this;
this.on("input",function(msg) {
var titl = this.title||msg.topic||"Node-RED";
var pri = msg.priority||this.priority;
var url = this.url||msg.url;
var options = { priority:pri };
if (url) { options.url = url; }
if (typeof(msg.payload) === 'object') {
msg.payload = JSON.stringify(msg.payload);
}
else { msg.payload = msg.payload.toString(); }
if (node.pushkey) {
try {
node.prowl.push(msg.payload, titl, options, function(err, remaining) {
if (err) { node.error(err); }
node.log( remaining + ' calls to Prowl api during current hour.' );
});
}
catch (err) {
node.error(err);
}
}
else {
node.warn("Prowl credentials not set.");
}
});
}
RED.nodes.registerType("prowl",ProwlNode,{
credentials: {
pushkey: {type: "password"}
}
});
}