Skip to content

Commit

Permalink
v0.1.2
Browse files Browse the repository at this point in the history
Added connection recovery logic after Apple TV restart.
  • Loading branch information
stickpin committed Jan 19, 2020
1 parent ad9a8ac commit 59dc6ea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
35 changes: 29 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand All @@ -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();
};
});
}

Expand All @@ -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();
};
});
}

Expand All @@ -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();
};
});
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 59dc6ea

Please sign in to comment.