Skip to content

Commit

Permalink
Add ability to stop Accessory. Addresses homebridge#351
Browse files Browse the repository at this point in the history
  • Loading branch information
KhaosT committed Nov 28, 2016
1 parent 43cb64d commit 8ef9463
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/Accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,19 @@ Accessory.prototype.publish = function(info, allowInsecureRequest) {
this._server.listen(targetPort);
}

/**
* Removes this Accessory from the local network
* Accessory object will no longer vaild after invoking this method
* Trying to invoke publish() on the object will result undefined behavior
*/
Accessory.prototype.destory = function() {
this._server.stop();
this._advertiser.stopAdvertising();

this._server = undefined;
this._advertiser = undefined;
}

Accessory.prototype._updateConfiguration = function() {
if (this._advertiser && this._advertiser.isAdvertising()) {
// get our accessory information in HAP format and determine if our configuration (that is, our
Expand Down
5 changes: 5 additions & 0 deletions lib/HAPServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ HAPServer.prototype.listen = function(port) {
this._httpServer.listen(port);
}

HAPServer.prototype.stop = function() {
this._httpServer.stop();
clearInterval(this._keepAliveTimerID);
}

HAPServer.prototype._onKeepAliveTimerTick = function() {
// send out a "keepalive" event which all connections automatically sign up for once pairVerify is
// completed. The event contains no actual data, so iOS devices will simply ignore it.
Expand Down
5 changes: 5 additions & 0 deletions lib/util/eventedhttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ EventedHTTPServer.prototype.listen = function(targetPort) {
this._tcpServer.on('connection', this._onConnection.bind(this));
}

EventedHTTPServer.prototype.stop = function() {
this._tcpServer.close();
this._connections = [];
}

EventedHTTPServer.prototype.sendEvent = function(event, data, contentType, exclude) {
for (var index in this._connections) {
var connection = this._connections[index];
Expand Down

0 comments on commit 8ef9463

Please sign in to comment.