diff --git a/index.js b/index.js index 3bda926..8af2349 100755 --- a/index.js +++ b/index.js @@ -52,7 +52,10 @@ AppleTVAccessory.prototype.atvConnect = function () { that.device = devices[0]; that.device.on('error', function (error) { that.log("ERROR: " + error.message); - that.log("ERROR: " + error.stack); + that.log("ERROR Code: " + error.code); + if(error.code == "ECONNRESET" || error.code == "EPIPE") { + that.atvConnect(); + }; }); return that.device.openConnection(credentials); }) @@ -61,7 +64,11 @@ AppleTVAccessory.prototype.atvConnect = function () { that.updateStatus(); }) .catch(function (error) { - that.log("ERROR: " + error); + that.log("ERROR: " + error.message); + that.log("ERROR Code: " + error.code); + if(error.code == "ECONNRESET" || error.code == "EPIPE") { + that.atvConnect(); + }; }); } @@ -86,7 +93,11 @@ AppleTVAccessory.prototype.checkATVStatus = function () { } }) .catch(function (error) { - that.log("ERROR: " + error); + that.log("ERROR: " + error.message); + that.log("ERROR Code: " + error.code); + if(error.code == "ECONNRESET" || error.code == "EPIPE") { + that.atvConnect(); + }; }); } @@ -110,17 +121,29 @@ AppleTVAccessory.prototype.setPowerState = function (state, callback) { that.log("AppleTV: " + that.name + " is turned off"); that.getPowerState(callback); }).catch(function (error) { - that.log("ERROR: " + error); + that.log("ERROR: " + error.message); + that.log("ERROR Code: " + error.code); + if(error.code == "ECONNRESET" || error.code == "EPIPE") { + that.atvConnect(); + }; }); }).catch(function (error) { - that.log("ERROR: " + error); + that.log("ERROR: " + error.message); + that.log("ERROR Code: " + error.code); + if(error.code == "ECONNRESET" || error.code == "EPIPE") { + that.atvConnect(); + }; }); } else { that.device.sendKeyCommand(appletv.AppleTV.Key.Tv).then(function () { that.log("AppleTV: " + that.name + " is turned on"); that.getPowerState(callback); }).catch(function (error) { - that.log("ERROR: " + error); + that.log("ERROR: " + error.message); + that.log("ERROR Code: " + error.code); + if(error.code == "ECONNRESET" || error.code == "EPIPE") { + that.atvConnect(); + }; }); } } \ No newline at end of file diff --git a/package.json b/package.json index 67a8af0..92afd0f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "homebridge-appletv-onoff-switch", - "version": "0.1.1", + "version": "0.1.2", "description": "Homebridge plugin that allows you to turn on and turn off AppleTV (Gen 4 and newer) with real On/Off status", "keywords": [ "homebridge-plugin",