From 40266af8b2e290240cf5afb61ea5e71564281579 Mon Sep 17 00:00:00 2001 From: Khaos Tian Date: Wed, 17 Feb 2016 13:18:25 -0800 Subject: [PATCH] Add the ability to remove services/characteristics --- lib/api.js | 4 ++++ lib/platformAccessory.js | 22 ++++++++++++++++++++++ lib/server.js | 9 +++++++++ package.json | 2 +- 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/api.js b/lib/api.js index 5a10168..d2aa1b3 100644 --- a/lib/api.js +++ b/lib/api.js @@ -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]; diff --git a/lib/platformAccessory.js b/lib/platformAccessory.js index 1079738..a923257 100644 --- a/lib/platformAccessory.js +++ b/lib/platformAccessory.js @@ -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. diff --git a/lib/server.js b/lib/server.js index 814aa09..8d02d5a 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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) { diff --git a/package.json b/package.json index 18c6747..9456967 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "dependencies": { "chalk": "^1.1.1", "commander": "2.8.1", - "hap-nodejs": "0.2.3", + "hap-nodejs": "0.2.4", "semver": "5.0.3", "node-persist": "^0.0.8" }