Add Pushover Node - with credentials support.

This commit is contained in:
Dave C-J
2014-04-20 18:00:43 +01:00
parent 9cb153635f
commit e23be73090
4 changed files with 307 additions and 19 deletions

View File

@@ -24,15 +24,9 @@ var util = require('util');
// module.exports = {prowlkey:'My-API-KEY'}
try {
var pushkey = RED.settings.prowl || require(process.env.NODE_RED_HOME+"/../pushkey.js");
}
catch(err) {
util.log("[57-prowl.js] Error: Failed to load Prowl credentials");
}
if (pushkey) {
if (pushkey.prowlkey) { var prowl = new Prowl(pushkey.prowlkey); }
var pushkeys = RED.settings.prowl || require(process.env.NODE_RED_HOME+"/../pushkey.js");
}
catch(err) { }
function ProwlNode(n) {
RED.nodes.createNode(this,n);
@@ -40,7 +34,16 @@ function ProwlNode(n) {
this.priority = parseInt(n.priority);
if (this.priority > 2) this.priority = 2;
if (this.priority < -2) this.priority = -2;
var credentials = RED.nodes.getCredentials(n.id);
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;
@@ -48,9 +51,9 @@ function ProwlNode(n) {
msg.payload = JSON.stringify(msg.payload);
}
else { msg.payload = msg.payload.toString(); }
if (pushkey.prowlkey) {
if (node.pushkey) {
try {
prowl.push(msg.payload, titl, { priority: pri }, function(err, remaining) {
node.prowl.push(msg.payload, titl, { priority: pri }, function(err, remaining) {
if (err) node.error(err);
node.log( remaining + ' calls to Prowl api during current hour.' );
});
@@ -60,8 +63,42 @@ function ProwlNode(n) {
}
}
else {
node.warn("Prowl credentials not set/found. See node info.");
node.warn("Prowl credentials not set.");
}
});
}
RED.nodes.registerType("prowl",ProwlNode);
var querystring = require('querystring');
RED.httpAdmin.get('/prowl/:id',function(req,res) {
var credentials = RED.nodes.getCredentials(req.params.id);
if (credentials) {
res.send(JSON.stringify({hasPassword:(credentials.pushkey&&credentials.pushkey!="")}));
} else {
res.send(JSON.stringify({}));
}
});
RED.httpAdmin.delete('/prowl/:id',function(req,res) {
RED.nodes.deleteCredentials(req.params.id);
res.send(200);
});
RED.httpAdmin.post('/prowl/:id',function(req,res) {
var body = "";
req.on('data', function(chunk) {
body+=chunk;
});
req.on('end', function(){
var newCreds = querystring.parse(body);
var credentials = RED.nodes.getCredentials(req.params.id)||{};
if (newCreds.pushkey == "") {
delete credentials.pushkey;
} else {
credentials.pushkey = newCreds.pushkey||credentials.pushkey;
}
RED.nodes.addCredentials(req.params.id,credentials);
res.send(200);
});
});