2016-11-07 15:17:02 +01:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var Prowl = require('node-prowl');
|
2013-10-30 21:38:23 +01:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
try {
|
|
|
|
var pushkeys = RED.settings.prowl || require(process.env.NODE_RED_HOME+"/../pushkey.js");
|
2014-04-20 19:00:43 +02:00
|
|
|
}
|
2014-06-29 00:34:59 +02:00
|
|
|
catch(err) { }
|
2014-04-20 19:00:43 +02:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
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; }
|
2015-02-06 22:10:14 +01:00
|
|
|
var credentials = this.credentials;
|
2014-06-29 00:34:59 +02:00
|
|
|
if ((credentials) && (credentials.hasOwnProperty("pushkey"))) { this.pushkey = credentials.pushkey; }
|
|
|
|
else {
|
|
|
|
if (pushkeys) { this.pushkey = pushkeys.prowlkey; }
|
|
|
|
else { this.error("No Prowl credentials set."); }
|
2014-02-08 14:31:00 +01:00
|
|
|
}
|
2014-06-29 00:34:59 +02:00
|
|
|
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;
|
2016-01-24 19:30:45 +01:00
|
|
|
var url = this.url||msg.url;
|
|
|
|
var options = { priority:pri };
|
2016-03-01 22:10:58 +01:00
|
|
|
if (url) { options.url = url; }
|
2014-06-29 00:34:59 +02:00
|
|
|
if (typeof(msg.payload) === 'object') {
|
|
|
|
msg.payload = JSON.stringify(msg.payload);
|
|
|
|
}
|
|
|
|
else { msg.payload = msg.payload.toString(); }
|
|
|
|
if (node.pushkey) {
|
|
|
|
try {
|
2016-01-24 19:30:45 +01:00
|
|
|
node.prowl.push(msg.payload, titl, options, function(err, remaining) {
|
2014-06-29 00:34:59 +02:00
|
|
|
if (err) { node.error(err); }
|
|
|
|
node.log( remaining + ' calls to Prowl api during current hour.' );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
node.error(err);
|
|
|
|
}
|
2014-02-08 14:31:00 +01:00
|
|
|
}
|
2014-06-29 00:34:59 +02:00
|
|
|
else {
|
|
|
|
node.warn("Prowl credentials not set.");
|
2014-02-08 14:31:00 +01:00
|
|
|
}
|
2014-06-29 00:34:59 +02:00
|
|
|
});
|
|
|
|
}
|
2015-02-06 22:10:14 +01:00
|
|
|
RED.nodes.registerType("prowl",ProwlNode,{
|
|
|
|
credentials: {
|
|
|
|
pushkey: {type: "password"}
|
2014-02-08 14:31:00 +01:00
|
|
|
}
|
2014-06-29 00:34:59 +02:00
|
|
|
});
|
|
|
|
}
|