From f3b315ea7ca156a03bc67bf0608b84533c5e11b9 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Sun, 31 Jan 2016 23:27:37 +0000 Subject: [PATCH] Let Pushbullet in reconnect on errors to fix #171 --- social/pushbullet/57-pushbullet.js | 26 +++++++++++++++++++------- social/pushbullet/package.json | 2 +- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/social/pushbullet/57-pushbullet.js b/social/pushbullet/57-pushbullet.js index cec3a3f7..69a053d9 100644 --- a/social/pushbullet/57-pushbullet.js +++ b/social/pushbullet/57-pushbullet.js @@ -1,5 +1,5 @@ /** - * Copyright 2013,2015 IBM Corp. + * Copyright 2013,2016 IBM Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,7 +43,7 @@ module.exports = function(RED) { this._inputNodes = []; this.initialised = false; } - + RED.nodes.registerType("pushbullet-config", PushbulletConfig, { credentials: { apikey: {type: "password"} @@ -123,6 +123,8 @@ module.exports = function(RED) { 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); @@ -136,21 +138,31 @@ module.exports = function(RED) { }); 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 } }); - - } }; @@ -531,13 +543,13 @@ module.exports = function(RED) { self.error(err); }); config.onConfig('stream_connected', function() { - self.status({fill: 'green', shape: 'ring', text: 'connected'}); + self.status({fill:'green', shape:'dot', text:'connected'}); }); config.onConfig('stream_disconnected', function(err) { - self.status({fill: 'red', shape: 'ring', text: 'disconnected'}); + self.status({fill:'grey', shape:'ring', text:'disconnected'}); }); config.onConfig('stream_error', function(err) { - self.status({fill: 'red', shape: 'ring', text: 'error, see log'}); + self.status({fill:'red', shape:'ring', text:'error, see log'}); self.error(err); }); } diff --git a/social/pushbullet/package.json b/social/pushbullet/package.json index 5db70c9d..fcbc504d 100644 --- a/social/pushbullet/package.json +++ b/social/pushbullet/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-pushbullet", - "version" : "0.0.7", + "version" : "0.0.8", "description" : "A Node-RED node to send alerts via Pushbullet", "dependencies" : { "pushbullet": "1.4.*",