Add credentials capability to PushBullet node

This commit is contained in:
Dave C-J 2014-04-23 21:06:36 +01:00
parent a32e87ff96
commit 4be0e05c2a
2 changed files with 110 additions and 20 deletions

View File

@ -19,6 +19,15 @@
<label for="node-input-title"><i class="icon-flag"></i> Title</label>
<input type="text" id="node-input-title" placeholder="Node-RED">
</div>
<div class="form-row">
<label for="node-config-input-deviceid"><i class="icon-user"></i> Device ID</label>
<input type="text" id="node-config-input-deviceid">
</div>
<div class="form-row">
<label for="node-config-input-pushkey"><i class="icon-lock"></i> API Key</label>
<input type="password" id="node-config-input-pushkey">
</div>
<br/>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
@ -28,11 +37,10 @@
<script type="text/x-red" data-help-name="pushbullet">
<p>Uses PushBullet to push the <b>msg.payload</b> to an Android device that has PushBullet app installed.</p>
<p>Optionally uses <b>msg.topic</b> to set the title, if not already set in the properties.</p>
<p>You MUST configure both your <i>API key</i> and the target <i>device ID</i>. Either into settings.js like this</p>
<p><pre>pushbullet: { pushbullet:'My-API-KEY', deviceid:'xyzzyWabc' },</pre></p>
<p>Or as a pushkey.js file in the directory <b>above</b> node-red.<p>
<p>You MUST configure both your <i>API key</i> and the target <i>device ID</i>. You can set these per node in the edit dialog
or as a pushkey.js file in the directory <b>above</b> node-red.<p>
<p><pre>module.exports = { pushbullet:'My-API-KEY', deviceid:'xyzzyWabc' }</pre></p>
<p>The deviceid can be found by hovering over you required device on the <a href="https://www.pushbullet.com/">PushBullet website</a>.</p>
<p>The deviceid can be found by hovering over your required device on the <a href="https://www.pushbullet.com/">PushBullet website</a>.</p>
</script>
<script type="text/javascript">
@ -52,6 +60,41 @@
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
$.getJSON('pushbullet/'+this.id,function(data) {
if (data.deviceid) {
$('#node-config-input-deviceid').val(data.deviceid);
}
if (data.hasPassword) {
$('#node-config-input-pushkey').val('__PWRD__');
} else {
$('#node-config-input-pushkey').val('');
}
});
},
oneditsave: function() {
var newUser = $('#node-config-input-deviceid').val();
var newPass = $('#node-config-input-pushkey').val();
var credentials = {};
credentials.deviceid = newUser;
if (newPass != '__PWRD__') {
credentials.pushkey = newPass;
}
$.ajax({
url: 'pushbullet/'+this.id,
type: 'POST',
data: credentials,
success:function(result){}
});
},
ondelete: function() {
$.ajax({
url: 'pushbullet/'+this.id,
type: 'DELETE',
success: function(result) {}
});
}
});
</script>

View File

@ -18,37 +18,46 @@ var RED = require(process.env.NODE_RED_HOME+"/red/red");
var PushBullet = require('pushbullet');
var util = require('util');
// Either add a line like this to settings.js
// pushbullet: {pushbullet:'My-API-KEY', deviceid:'12345'},
// or create pushkey.js in dir ABOVE node-red, it just needs to be like
// module.exports = {pushbullet:'My-API-KEY', deviceid:'12345'}
// Either create pushkey.js in dir ABOVE node-red, it just needs to be like
// module.exports = {pushbullet:'My-API-KEY', deviceid:'12345'}
// or set them per node in the edit dialog
try {
var pushkey = RED.settings.pushbullet || require(process.env.NODE_RED_HOME+"/../pushkey.js");
var pushkeys = RED.settings.pushbullet || require(process.env.NODE_RED_HOME+"/../pushkey.js");
}
catch(err) {
util.log("[57-pushbullet.js] Error: Failed to load PushBullet credentials");
}
if (pushkey) {
if (pushkey.pushbullet) { var pusher = new PushBullet(pushkey.pushbullet); }
if (pushkey.deviceid) { var deviceId = pushkey.deviceid; }
//util.log("[57-pushbullet.js] Warning: Failed to load global PushBullet credentials");
}
function PushbulletNode(n) {
RED.nodes.createNode(this,n);
this.title = n.title;
var credentials = RED.nodes.getCredentials(n.id);
if ((credentials) && (credentials.hasOwnProperty("pushkey"))) { this.pushkey = credentials.pushkey; }
else {
if (pushkeys) { this.pushkey = pushkeys.pushbullet; }
else { this.error("No credentials set"); }
}
if ((credentials) && (credentials.hasOwnProperty("deviceid"))) { this.deviceid = credentials.deviceid; }
else {
if (pushkeys) { this.deviceid = pushkeys.deviceid; }
else { this.warn("No deviceId set"); }
}
//console.log("Cred:",n.id,this.deviceid,this.pushkey);
this.pusher = new PushBullet(this.pushkey);
var node = this;
this.on("input",function(msg) {
var titl = this.title||msg.topic||"Node-RED";
var titl = node.title||msg.topic||"Node-RED";
var dev = msg.deviceID||node.deviceId;
if (typeof(msg.payload) === 'object') {
msg.payload = JSON.stringify(msg.payload);
}
else { msg.payload = msg.payload.toString(); }
if (pushkey.pushbullet && pushkey.deviceid) {
if (node.pushkey && node.deviceid) {
try {
if (!isNaN(deviceId)) { deviceId = Number(deviceId); }
pusher.note(deviceId, titl, msg.payload, function(err, response) {
if (!isNaN(node.deviceId)) { node.deviceId = Number(node.deviceId); }
node.pusher.note(dev, titl, msg.payload, function(err, response) {
if (err) node.error("Pushbullet error: "+err);
//console.log(response);
});
@ -62,5 +71,43 @@ function PushbulletNode(n) {
}
});
}
RED.nodes.registerType("pushbullet",PushbulletNode);
var querystring = require('querystring');
RED.httpAdmin.get('/pushbullet/:id',function(req,res) {
var credentials = RED.nodes.getCredentials(req.params.id);
if (credentials) {
res.send(JSON.stringify({deviceid:credentials.deviceid,hasPassword:(credentials.pushkey&&credentials.pushkey!="")}));
} else {
res.send(JSON.stringify({}));
}
});
RED.httpAdmin.delete('/pushbullet/:id',function(req,res) {
RED.nodes.deleteCredentials(req.params.id);
res.send(200);
});
RED.httpAdmin.post('/pushbullet/: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.deviceid == null || newCreds.deviceid == "") {
delete credentials.deviceid;
} else {
credentials.deviceid = newCreds.deviceid;
}
if (newCreds.pushkey == "") {
delete credentials.pushkey;
} else {
credentials.pushkey = newCreds.pushkey||credentials.pushkey;
}
RED.nodes.addCredentials(req.params.id,credentials);
res.send(200);
});
});