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) { if (err && node) { if (node.emitter) { if (!node.emitter.emit('error', err)) { node.error(err); } } else { node.error(err); } } } function PushbulletConfig(n) { RED.nodes.createNode(this, n); this.n = n; this.name = n.name; this._inputNodes = []; this.initialised = false; } RED.nodes.registerType("pushbullet-config", PushbulletConfig, { credentials: { apikey: {type: "password"} } }); PushbulletConfig.prototype.initialise = function() { if (this.initialised) { return; } this.emitter = new EventEmitter(); this.initialised = true; var self = this; // sort migration from old node var apikey; if (this.n._migrate) { apikey = this.n._apikey; this.credentials = {apikey:apikey}; } else if (this.credentials) { 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) { if (err) { reject(err); } else { resolve(me); } }); }).otherwise(function(err) { onError(err, self); }); // get latest timestamp this.last = when.promise(function(resolve) { pusher.history({limit:1}, function(err, res) { if (err) { resolve(0); } else { try { resolve(res.pushes[0].modified); } catch(ex) { self.warn('Unable to get history.'); resolve(0); } } }); }); 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; if (this.pusher) { var stream = this.pusher.stream(); var closing = false; var tout; stream.on('message', function(res) { if (res.type === 'tickle') { self.handleTickle(res); } else if (res.type === 'push') { self.pushMsg(res.push); } }); stream.on('connect', function() { self.emitter.emit('stream_connected'); }); stream.on('close', function() { self.emitter.emit('stream_disconnected'); if (!closing) { tout = setTimeout(function() { stream.connect(); },15000); } }); stream.on('error', function(err) { self.emitter.emit('stream_error', err); if (!closing) { tout = setTimeout(function() { stream.connect(); },15000); } }); stream.connect(); this.stream = stream; this.on("close",function() { if (tout) { clearTimeout(tout); } closing = true; try { this.stream.close(); } catch(err) { // Ignore error if not connected } }); } }; PushbulletConfig.prototype.handleTickle = function(ticklemsg) { var self = this; if (this.pusher && ticklemsg.subtype === "push") { var lastprom = this.last; this.last = when.promise(function(resolve) { when(lastprom).then(function(last) { self.pusher.history({modified_after: last}, function(err, res) { if (err) { resolve(last); return onError(err); } for (var i=0;i 0) { if ( (this.credentials.filters.indexOf(msg.data.source_device_iden) > -1) || (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); } } }