Add the ability to remove services/characteristics

This commit is contained in:
Khaos Tian
2016-02-17 13:18:25 -08:00
parent d3c77a4cda
commit 40266af8b2
4 changed files with 36 additions and 1 deletions

View File

@@ -143,6 +143,10 @@ API.prototype.registerPlatformAccessories = function(pluginName, platformName, a
this.emit('registerPlatformAccessories', accessories);
}
API.prototype.updatePlatformAccessories = function(accessories) {
this.emit('updatePlatformAccessories', accessories);
}
API.prototype.unregisterPlatformAccessories = function(pluginName, platformName, accessories) {
for (var index in accessories) {
var accessory = accessories[index];

View File

@@ -60,6 +60,28 @@ PlatformAccessory.prototype.addService = function(service) {
return service;
}
PlatformAccessory.prototype.removeService = function(service) {
var targetServiceIndex;
for (var index in this.services) {
var existingService = this.services[index];
if (existingService === service) {
targetServiceIndex = index;
break;
}
}
if (targetServiceIndex) {
this.services.splice(targetServiceIndex, 1);
service.removeAllListeners();
if (this._associatedHAPAccessory) {
this._associatedHAPAccessory.removeService(service);
}
}
}
/**
* searchs for a Service in the services collection and returns the first Service object that matches.
* If multiple services of the same type are present in one accessory, use getServiceByUUIDAndSubType instead.

View File

@@ -32,6 +32,10 @@ function Server(insecureAccess) {
this._handleRegisterPlatformAccessories(accessories);
}.bind(this));
this._api.on('updatePlatformAccessories', function(accessories) {
this._handleUpdatePlatformAccessories(accessories);
}.bind(this));
this._api.on('unregisterPlatformAccessories', function(accessories) {
this._handleUnregisterPlatformAccessories(accessories);
}.bind(this));
@@ -408,6 +412,11 @@ Server.prototype._handleRegisterPlatformAccessories = function(accessories) {
this._updateCachedAccessories();
}
Server.prototype._handleUpdatePlatformAccessories = function(accessories) {
// Update persisted accessories
this._updateCachedAccessories();
}
Server.prototype._handleUnregisterPlatformAccessories = function(accessories) {
var hapAccessories = [];
for (var index in accessories) {