2015-01-27 08:08:54 +01:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var PushBullet = require('pushbullet');
|
|
|
|
var fs = require('fs');
|
|
|
|
var util = require('util');
|
|
|
|
var when = require('when');
|
|
|
|
var nodefn = require('when/node');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
|
|
|
|
function onError(err, node) {
|
2015-05-11 20:25:39 +02:00
|
|
|
if (err && node) {
|
|
|
|
if (node.emitter) {
|
|
|
|
if (!node.emitter.emit('error', err)) {
|
2015-01-27 08:08:54 +01:00
|
|
|
node.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function PushbulletConfig(n) {
|
|
|
|
RED.nodes.createNode(this, n);
|
2015-04-20 11:50:36 +02:00
|
|
|
this.n = n;
|
2015-01-27 08:08:54 +01:00
|
|
|
this.name = n.name;
|
|
|
|
this._inputNodes = [];
|
2015-04-20 11:50:36 +02:00
|
|
|
this.initialised = false;
|
|
|
|
}
|
2016-02-01 00:27:37 +01:00
|
|
|
|
2015-04-20 11:50:36 +02:00
|
|
|
RED.nodes.registerType("pushbullet-config", PushbulletConfig, {
|
|
|
|
credentials: {
|
|
|
|
apikey: {type: "password"}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
PushbulletConfig.prototype.initialise = function() {
|
|
|
|
if (this.initialised) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-22 14:19:51 +02:00
|
|
|
this.emitter = new EventEmitter();
|
|
|
|
|
2015-04-20 11:50:36 +02:00
|
|
|
this.initialised = true;
|
2015-01-27 08:08:54 +01:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// sort migration from old node
|
|
|
|
var apikey;
|
2015-05-11 20:25:39 +02:00
|
|
|
if (this.n._migrate) {
|
2015-04-20 11:50:36 +02:00
|
|
|
apikey = this.n._apikey;
|
2015-01-27 08:08:54 +01:00
|
|
|
this.credentials = {apikey:apikey};
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (this.credentials) {
|
2015-01-27 08:08:54 +01:00
|
|
|
apikey = this.credentials.apikey;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apikey) {
|
|
|
|
try {
|
|
|
|
var pusher = new PushBullet(apikey);
|
|
|
|
// get 'me' info
|
|
|
|
this.me = when.promise(function(resolve, reject) {
|
|
|
|
pusher.me(function(err, me) {
|
2015-05-11 20:25:39 +02:00
|
|
|
if (err) {
|
2015-01-27 08:08:54 +01:00
|
|
|
reject(err);
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-04-20 11:50:36 +02:00
|
|
|
resolve(me);
|
2015-01-27 08:08:54 +01:00
|
|
|
}
|
|
|
|
});
|
2015-04-20 11:50:36 +02:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
onError(err, self);
|
2015-01-27 08:08:54 +01:00
|
|
|
});
|
|
|
|
// get latest timestamp
|
|
|
|
this.last = when.promise(function(resolve) {
|
|
|
|
pusher.history({limit:1}, function(err, res) {
|
2015-05-11 20:25:39 +02:00
|
|
|
if (err) {
|
2015-01-27 08:08:54 +01:00
|
|
|
resolve(0);
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-04-20 11:50:36 +02:00
|
|
|
try {
|
|
|
|
resolve(res.pushes[0].modified);
|
|
|
|
}
|
2015-05-11 21:04:51 +02:00
|
|
|
catch(ex) {
|
2015-04-20 11:50:36 +02:00
|
|
|
self.warn('Unable to get history.');
|
|
|
|
resolve(0);
|
|
|
|
}
|
2015-01-27 08:08:54 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.pusher = pusher;
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
onError(err, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.error("No credentials set for pushbullet config.");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.on("close", function() {
|
|
|
|
self._inputNodes.length = 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PushbulletConfig.prototype.onConfig = function(type, cb) {
|
|
|
|
this.emitter.on(type, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
PushbulletConfig.prototype.setupStream = function() {
|
|
|
|
var self = this;
|
2015-05-11 20:25:39 +02:00
|
|
|
if (this.pusher) {
|
2015-01-27 08:08:54 +01:00
|
|
|
var stream = this.pusher.stream();
|
2016-02-01 00:27:37 +01:00
|
|
|
var closing = false;
|
|
|
|
var tout;
|
2015-01-27 08:08:54 +01:00
|
|
|
stream.on('message', function(res) {
|
2015-05-11 20:25:39 +02:00
|
|
|
if (res.type === 'tickle') {
|
2015-01-27 08:08:54 +01:00
|
|
|
self.handleTickle(res);
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (res.type === 'push') {
|
2015-01-27 08:08:54 +01:00
|
|
|
self.pushMsg(res.push);
|
2015-03-26 19:55:03 +01:00
|
|
|
}
|
2015-01-27 08:08:54 +01:00
|
|
|
});
|
|
|
|
stream.on('connect', function() {
|
|
|
|
self.emitter.emit('stream_connected');
|
|
|
|
});
|
|
|
|
stream.on('close', function() {
|
|
|
|
self.emitter.emit('stream_disconnected');
|
2016-02-01 00:27:37 +01:00
|
|
|
if (!closing) {
|
|
|
|
tout = setTimeout(function() {
|
|
|
|
stream.connect();
|
|
|
|
},15000);
|
|
|
|
}
|
2015-01-27 08:08:54 +01:00
|
|
|
});
|
|
|
|
stream.on('error', function(err) {
|
|
|
|
self.emitter.emit('stream_error', err);
|
2016-02-01 00:27:37 +01:00
|
|
|
if (!closing) {
|
|
|
|
tout = setTimeout(function() {
|
|
|
|
stream.connect();
|
|
|
|
},15000);
|
|
|
|
}
|
2015-01-27 08:08:54 +01:00
|
|
|
});
|
|
|
|
stream.connect();
|
|
|
|
this.stream = stream;
|
2015-04-20 11:50:36 +02:00
|
|
|
this.on("close",function() {
|
2016-02-01 00:27:37 +01:00
|
|
|
if (tout) { clearTimeout(tout); }
|
|
|
|
closing = true;
|
2015-04-20 11:50:36 +02:00
|
|
|
try {
|
2015-05-11 20:25:39 +02:00
|
|
|
this.stream.close();
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
catch(err) {
|
2015-04-20 11:50:36 +02:00
|
|
|
// Ignore error if not connected
|
|
|
|
}
|
|
|
|
});
|
2015-01-27 08:08:54 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PushbulletConfig.prototype.handleTickle = function(ticklemsg) {
|
|
|
|
var self = this;
|
2015-05-11 20:25:39 +02:00
|
|
|
if (this.pusher && ticklemsg.subtype === "push") {
|
2015-01-27 08:08:54 +01:00
|
|
|
var lastprom = this.last;
|
|
|
|
this.last = when.promise(function(resolve) {
|
|
|
|
when(lastprom).then(function(last) {
|
|
|
|
self.pusher.history({modified_after: last}, function(err, res) {
|
2015-05-11 20:25:39 +02:00
|
|
|
if (err) {
|
2015-01-27 08:08:54 +01:00
|
|
|
resolve(last);
|
|
|
|
return onError(err);
|
|
|
|
}
|
2017-04-12 14:32:08 +02:00
|
|
|
for (var i=0; i<res.pushes.length; i++) {
|
2015-01-27 08:08:54 +01:00
|
|
|
self.pushMsg(res.pushes[i]);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
resolve(res.pushes[0].modified);
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
catch(ex) {
|
2015-01-27 08:08:54 +01:00
|
|
|
resolve(last);
|
|
|
|
}
|
2015-03-26 19:55:03 +01:00
|
|
|
});
|
|
|
|
});
|
2015-01-27 08:08:54 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PushbulletConfig.prototype.pushMsg = function(incoming) {
|
2015-05-11 20:25:39 +02:00
|
|
|
if (this._inputNodes.length === 0) {
|
2015-01-27 08:08:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg = {
|
|
|
|
pushtype: incoming.type,
|
|
|
|
data: incoming
|
|
|
|
}
|
|
|
|
|
2015-05-11 20:25:39 +02:00
|
|
|
if (incoming.dismissed === true) {
|
2015-01-27 08:08:54 +01:00
|
|
|
msg.pushtype = 'dismissal';
|
|
|
|
msg.topic = 'Push dismissed';
|
|
|
|
msg.payload = incoming.iden;
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (incoming.active === false && incoming.type === undefined) {
|
2015-01-27 08:08:54 +01:00
|
|
|
msg.pushtype = 'delete';
|
|
|
|
msg.topic = 'Push deleted';
|
|
|
|
msg.payload = incoming.iden;
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (incoming.type === 'clip') {
|
2015-01-27 08:08:54 +01:00
|
|
|
msg.topic = 'Clipboard content';
|
|
|
|
msg.payload = incoming.body;
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (incoming.type === 'note') {
|
2015-01-27 08:08:54 +01:00
|
|
|
msg.topic = incoming.title;
|
|
|
|
msg.payload = incoming.body;
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (incoming.type === 'link') {
|
2015-01-27 08:08:54 +01:00
|
|
|
msg.topic = incoming.title;
|
|
|
|
msg.payload = incoming.url;
|
|
|
|
msg.message = incoming.body;
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (incoming.type === 'address') {
|
2015-01-27 08:08:54 +01:00
|
|
|
msg.topic = incoming.name;
|
|
|
|
msg.payload = incoming.address;
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (incoming.type === 'list') {
|
2015-01-27 08:08:54 +01:00
|
|
|
msg.topic = incoming.title;
|
|
|
|
msg.payload = incoming.items;
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (incoming.type === 'file') {
|
2015-01-27 08:08:54 +01:00
|
|
|
msg.topic = incoming.file_name;
|
|
|
|
msg.payload = incoming.file_url;
|
|
|
|
msg.message = incoming.body;
|
|
|
|
}
|
|
|
|
// Android specific, untested
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (incoming.type === 'mirror') {
|
2015-01-27 08:08:54 +01:00
|
|
|
msg.topic = incoming.title;
|
|
|
|
msg.payload = incoming.body;
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (incoming.type === 'dismissal') {
|
2015-01-27 08:08:54 +01:00
|
|
|
msg.topic = "dismissal";
|
|
|
|
msg.topic = "Push dismissed";
|
|
|
|
msg.payload = incoming.iden;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.error("unknown push type: " + incoming.type + " content: " + JSON.stringify(incoming));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < this._inputNodes.length; i++) {
|
|
|
|
this._inputNodes[i].emitPush(msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PushbulletConfig.prototype.registerInputNode = function(/*Node*/handler) {
|
2015-05-11 20:25:39 +02:00
|
|
|
if (!this.stream) {
|
2015-01-27 08:08:54 +01:00
|
|
|
this.setupStream();
|
|
|
|
}
|
|
|
|
this._inputNodes.push(handler);
|
|
|
|
};
|
|
|
|
|
|
|
|
function migrateOldSettings(n) {
|
2015-05-11 20:25:39 +02:00
|
|
|
if (n.config === undefined) {
|
2015-01-27 08:08:54 +01:00
|
|
|
var newid, config, apikey, deviceid, pushkeys;
|
|
|
|
|
|
|
|
try {
|
|
|
|
pushkeys = RED.settings.pushbullet || require(process.env.NODE_RED_HOME+"/../pushkey.js");
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
}
|
|
|
|
|
|
|
|
var cred = RED.nodes.getCredentials(n.id);
|
|
|
|
// get old apikey
|
2015-05-11 20:25:39 +02:00
|
|
|
if (cred && cred.hasOwnProperty("pushkey")) {
|
2015-03-26 19:55:03 +01:00
|
|
|
apikey = cred.pushkey;
|
2015-01-27 08:08:54 +01:00
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (pushkeys) {
|
2015-01-27 08:08:54 +01:00
|
|
|
apikey = pushkeys.pushbullet;
|
|
|
|
}
|
|
|
|
// get old device
|
2015-03-26 19:55:03 +01:00
|
|
|
if (cred && cred.hasOwnProperty("deviceid")) {
|
|
|
|
deviceid = cred.deviceid;
|
2015-01-27 08:08:54 +01:00
|
|
|
}
|
2015-03-26 19:55:03 +01:00
|
|
|
else if (pushkeys) {
|
|
|
|
deviceid = pushkeys.deviceid;
|
2015-01-27 08:08:54 +01:00
|
|
|
}
|
|
|
|
|
2015-05-11 20:25:39 +02:00
|
|
|
if (apikey) {
|
2015-01-27 08:08:54 +01:00
|
|
|
newid = (1+Math.random()*4294967295).toString(16);
|
|
|
|
config = new PushbulletConfig({
|
|
|
|
id: newid,
|
|
|
|
type: 'pushbullet-config',
|
|
|
|
name: n.name,
|
|
|
|
_migrate: true,
|
|
|
|
_apikey: apikey,
|
2015-03-26 19:55:03 +01:00
|
|
|
});
|
2015-01-27 08:08:54 +01:00
|
|
|
}
|
|
|
|
|
2015-05-11 20:25:39 +02:00
|
|
|
if (!(apikey || deviceid)) {
|
2015-01-27 08:08:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// override configuration properties to compatible migrated ones
|
|
|
|
n.pushtype = "note";
|
|
|
|
n.deviceid = deviceid;
|
|
|
|
return {
|
|
|
|
deviceid: deviceid,
|
|
|
|
apikey: apikey,
|
|
|
|
config: config,
|
|
|
|
id: newid
|
|
|
|
};
|
|
|
|
}
|
2015-03-26 19:55:03 +01:00
|
|
|
return false;
|
2015-01-27 08:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function PushbulletOut(n) {
|
|
|
|
RED.nodes.createNode(this, n);
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.migrated = migrateOldSettings(n);
|
|
|
|
this.title = n.title;
|
|
|
|
this.chan = n.chan;
|
|
|
|
this.pushtype = n.pushtype;
|
|
|
|
this.pusher = null;
|
|
|
|
|
|
|
|
var configNode;
|
2015-05-11 20:25:39 +02:00
|
|
|
if (this.migrated) {
|
2015-01-27 08:08:54 +01:00
|
|
|
this.warn('Settings migrated from previous version of Pushbullet Node, please edit node to update settings.');
|
|
|
|
this.status({fill: 'yellow', shape: 'ring', text: 'Node migrated'});
|
|
|
|
this.deviceid = this.migrated.deviceid;
|
|
|
|
configNode = this.migrated.config;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.status({});
|
|
|
|
configNode = RED.nodes.getNode(n.config);
|
2015-03-26 19:55:03 +01:00
|
|
|
try {
|
2015-01-27 08:08:54 +01:00
|
|
|
this.deviceid = this.credentials.deviceid;
|
|
|
|
}
|
2015-05-11 21:04:51 +02:00
|
|
|
catch(err) {}
|
2015-01-27 08:08:54 +01:00
|
|
|
}
|
|
|
|
|
2015-05-11 20:25:39 +02:00
|
|
|
if (configNode) {
|
2015-04-20 11:50:36 +02:00
|
|
|
configNode.initialise();
|
2015-01-27 08:08:54 +01:00
|
|
|
this.pusher = configNode.pusher;
|
|
|
|
configNode.onConfig('error', function(err) {
|
|
|
|
self.error(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.on("input", function(msg) {
|
2015-05-11 20:25:39 +02:00
|
|
|
var title = self.title || msg.topic || "Node-RED";
|
|
|
|
var deviceid = (self.deviceid === '_msg_')? (msg.deviceid || ""): (self.deviceid || "");
|
|
|
|
var pushtype = self.pushtype || msg.pushtype || "note";
|
2015-01-27 08:08:54 +01:00
|
|
|
var channel = self.chan || msg.channel;
|
|
|
|
|
|
|
|
if (typeof(msg.payload) === 'object') {
|
|
|
|
msg.payload = JSON.stringify(msg.payload);
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (msg.payload) {
|
2015-03-26 19:55:03 +01:00
|
|
|
msg.payload = msg.payload.toString();
|
|
|
|
}
|
2015-01-27 08:08:54 +01:00
|
|
|
|
2015-05-11 20:25:39 +02:00
|
|
|
if (['delete', 'dismissal', 'updatelist', '_rawupdate_'].indexOf(pushtype) === -1) {
|
2015-01-27 08:08:54 +01:00
|
|
|
if (channel) {
|
|
|
|
deviceid = { channel_tag : channel };
|
2015-03-26 19:55:03 +01:00
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (deviceid === "") {
|
2015-01-27 08:08:54 +01:00
|
|
|
try {
|
|
|
|
when(configNode.me).then(function(me) {
|
2015-04-20 11:50:36 +02:00
|
|
|
if (me) {
|
|
|
|
deviceid = me.email;
|
|
|
|
self.pushMsg(pushtype, deviceid, title, msg);
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-04-20 11:50:36 +02:00
|
|
|
self.error("Unable to push",msg);
|
|
|
|
}
|
2015-01-27 08:08:54 +01:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
self.error('Unable to push to "all".');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!isNaN(deviceid)) {
|
2015-03-26 19:55:03 +01:00
|
|
|
deviceid = Number(deviceid);
|
|
|
|
}
|
2015-01-27 08:08:54 +01:00
|
|
|
}
|
|
|
|
self.pushMsg(pushtype, deviceid, title, msg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("pushbullet", PushbulletOut, {
|
|
|
|
credentials: {
|
|
|
|
deviceid: {value: ""},
|
|
|
|
pushkey: {value: ""}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
PushbulletOut.prototype.pushMsg = function(pushtype, deviceid, title, msg) {
|
|
|
|
var self = this;
|
|
|
|
if (this.pusher) {
|
2015-05-11 21:04:51 +02:00
|
|
|
var handleErr = function(msg) {
|
2015-01-27 08:08:54 +01:00
|
|
|
return function(err) {
|
2015-05-11 20:25:39 +02:00
|
|
|
if (err) {
|
2015-01-27 08:08:54 +01:00
|
|
|
self.error(msg);
|
2015-03-26 19:55:03 +01:00
|
|
|
onError(err, self);
|
2015-01-27 08:08:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 20:25:39 +02:00
|
|
|
if (deviceid) {
|
|
|
|
if (pushtype === 'note') {
|
2015-03-26 19:55:03 +01:00
|
|
|
this.pusher.note(deviceid, title, msg.payload, handleErr('Unable to push note'));
|
2015-01-27 08:08:54 +01:00
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (pushtype === 'address') {
|
2015-01-27 08:08:54 +01:00
|
|
|
this.pusher.address(deviceid, title, msg.payload, handleErr('Unable to push address'));
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (pushtype === 'list') {
|
2015-01-27 08:08:54 +01:00
|
|
|
this.pusher.list(deviceid, title, JSON.parse(msg.payload), handleErr('Unable to push list'));
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (pushtype === 'link') {
|
2015-01-27 08:08:54 +01:00
|
|
|
this.pusher.push(deviceid, {
|
|
|
|
type: 'link',
|
|
|
|
title: title,
|
|
|
|
body: msg.message,
|
2015-03-26 19:55:03 +01:00
|
|
|
url: msg.payload
|
2015-01-27 08:08:54 +01:00
|
|
|
}, handleErr('Unable to push link'));
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (pushtype === 'file') {
|
2015-01-27 08:08:54 +01:00
|
|
|
// Workaround for Pushbullet dep not handling error on file open
|
2015-05-11 20:25:39 +02:00
|
|
|
if (fs.existsSync(msg.payload)) {
|
2015-01-27 08:08:54 +01:00
|
|
|
this.pusher.file(deviceid, msg.payload, title, handleErr('Unable to push file'));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.error('File does not exist!');
|
|
|
|
}
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (pushtype === '_raw_') {
|
2015-01-27 08:08:54 +01:00
|
|
|
this.pusher.push(deviceid, msg.raw, handleErr('Unable to push raw data'));
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 19:55:03 +01:00
|
|
|
|
2015-05-11 20:25:39 +02:00
|
|
|
if (msg.data && msg.data.iden) {
|
|
|
|
if (pushtype === 'delete') {
|
2015-01-27 08:08:54 +01:00
|
|
|
this.pusher.deletePush(msg.data.iden, handleErr('Unable to delete push'));
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (pushtype === 'dismissal') {
|
2015-01-27 08:08:54 +01:00
|
|
|
this.pusher.updatePush(msg.data.iden, {dismissed: true}, handleErr('Unable to dismiss push'));
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (pushtype === 'updatelist') {
|
2015-01-27 08:08:54 +01:00
|
|
|
try {
|
|
|
|
var data = JSON.parse(msg.payload);
|
2015-05-11 20:25:39 +02:00
|
|
|
if (msg.data.type && msg.data.type !== 'list') {
|
2015-01-27 08:08:54 +01:00
|
|
|
this.warn('Trying to update list items in non list push');
|
|
|
|
}
|
|
|
|
this.pusher.updatePush(msg.data.iden, {items: data}, handleErr('Unable to update list'));
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
this.warn("Invalid list");
|
|
|
|
}
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (pushtype === '_rawupdate_') {
|
2015-01-27 08:08:54 +01:00
|
|
|
this.pusher.updatePush(msg.data.iden, msg.raw, handleErr('Unable to update raw data'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.error("Pushbullet credentials not set/found.");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-06 21:27:21 +01:00
|
|
|
RED.httpAdmin.get('/pushbullet/:id/migrate', RED.auth.needsPermission('pushbullet.read'), function(req, res) {
|
2015-01-27 08:08:54 +01:00
|
|
|
var node = RED.nodes.getNode(req.params.id);
|
2015-05-11 20:25:39 +02:00
|
|
|
if (node && node.migrated) {
|
|
|
|
if (req.query.save) {
|
2015-01-27 08:08:54 +01:00
|
|
|
var promise;
|
2015-05-11 20:25:39 +02:00
|
|
|
if (node.migrated.apikey) {
|
2015-01-27 08:08:54 +01:00
|
|
|
promise = RED.nodes.addCredentials(node.migrated.id, {apikey: node.migrated.apikey});
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
if (node.migrated.deviceid) {
|
2015-01-27 08:08:54 +01:00
|
|
|
when(promise).then(function() {
|
|
|
|
RED.nodes.addCredentials(req.params.id, {deviceid: node.migrated.deviceid});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.send(JSON.stringify({migrated: true, config: node.migrated.id}));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.send("{}");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-11-06 21:27:21 +01:00
|
|
|
RED.httpAdmin.get('/pushbullet/:id/devices', RED.auth.needsPermission('pushbullet.read'), function(req, res) {
|
2015-01-27 08:08:54 +01:00
|
|
|
var config = RED.nodes.getNode(req.params.id);
|
|
|
|
var cred = RED.nodes.getCredentials(req.params.id);
|
2015-03-26 19:55:03 +01:00
|
|
|
var pb;
|
2015-01-27 08:08:54 +01:00
|
|
|
|
2015-05-11 20:25:39 +02:00
|
|
|
if (config && config.pusher) {
|
2015-01-27 08:08:54 +01:00
|
|
|
config.pusher.devices(function(err, chans) {
|
2015-05-11 20:25:39 +02:00
|
|
|
if (err) {
|
2015-01-27 08:08:54 +01:00
|
|
|
res.send("[]");
|
|
|
|
return onError(err, config);
|
|
|
|
}
|
|
|
|
res.send(JSON.stringify(chans.devices));
|
2015-03-26 19:55:03 +01:00
|
|
|
});
|
2015-01-27 08:08:54 +01:00
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (cred && cred.apikey) {
|
2015-03-26 19:55:03 +01:00
|
|
|
pb = new PushBullet(cred.apikey);
|
2015-01-27 08:08:54 +01:00
|
|
|
pb.devices(function(err, chans) {
|
2015-05-11 20:25:39 +02:00
|
|
|
if (err) {
|
2015-01-27 08:08:54 +01:00
|
|
|
res.send("[]");
|
|
|
|
return onError(err, config);
|
|
|
|
}
|
|
|
|
res.send(JSON.stringify(chans.devices));
|
2015-03-26 19:55:03 +01:00
|
|
|
});
|
2015-01-27 08:08:54 +01:00
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
else if (req.query.apikey) {
|
2015-03-26 19:55:03 +01:00
|
|
|
pb = new PushBullet(req.query.apikey);
|
2015-01-27 08:08:54 +01:00
|
|
|
pb.devices(function(err, chans) {
|
2015-05-11 20:25:39 +02:00
|
|
|
if (err) {
|
2015-01-27 08:08:54 +01:00
|
|
|
res.send("[]");
|
|
|
|
return onError(err, config);
|
|
|
|
}
|
|
|
|
res.send(JSON.stringify(chans.devices));
|
2015-03-26 19:55:03 +01:00
|
|
|
});
|
2015-01-27 08:08:54 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.send("[]");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function PushbulletIn(n) {
|
|
|
|
RED.nodes.createNode(this, n);
|
|
|
|
var self = this;
|
|
|
|
var config = RED.nodes.getNode(n.config);
|
2015-05-11 20:25:39 +02:00
|
|
|
if (config) {
|
2015-04-20 11:50:36 +02:00
|
|
|
config.initialise();
|
2015-01-27 08:08:54 +01:00
|
|
|
config.registerInputNode(this);
|
|
|
|
config.onConfig('error', function(err) {
|
|
|
|
self.error(err);
|
|
|
|
});
|
|
|
|
config.onConfig('stream_connected', function() {
|
2016-02-01 00:27:37 +01:00
|
|
|
self.status({fill:'green', shape:'dot', text:'connected'});
|
2015-01-27 08:08:54 +01:00
|
|
|
});
|
|
|
|
config.onConfig('stream_disconnected', function(err) {
|
2016-02-01 00:27:37 +01:00
|
|
|
self.status({fill:'grey', shape:'ring', text:'disconnected'});
|
2015-01-27 08:08:54 +01:00
|
|
|
});
|
|
|
|
config.onConfig('stream_error', function(err) {
|
2016-02-01 00:27:37 +01:00
|
|
|
self.status({fill:'red', shape:'ring', text:'error, see log'});
|
2015-01-27 08:08:54 +01:00
|
|
|
self.error(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("pushbullet in", PushbulletIn, {
|
|
|
|
credentials: {
|
|
|
|
filters: {value: []}
|
|
|
|
}
|
|
|
|
});
|
2015-03-26 19:55:03 +01:00
|
|
|
|
2015-01-27 08:08:54 +01:00
|
|
|
PushbulletIn.prototype.emitPush = function(msg) {
|
|
|
|
try {
|
2015-05-11 20:25:39 +02:00
|
|
|
if (this.credentials.filters.length > 0) {
|
|
|
|
if ( (this.credentials.filters.indexOf(msg.data.source_device_iden) > -1) ||
|
2015-01-27 08:08:54 +01:00
|
|
|
(this.credentials.filters.indexOf(msg.data.target_device_iden) > -1) ||
|
|
|
|
(!msg.data.target_device_iden && !msg.data.source_device_iden)) { /* All */
|
|
|
|
this.send(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.send(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
this.send(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|