Skip to content

Commit

Permalink
Add the ability to remove services/characteristics
Browse files Browse the repository at this point in the history
  • Loading branch information
KhaosT committed Feb 17, 2016
1 parent 223c0ac commit 7152c11
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
41 changes: 41 additions & 0 deletions lib/Accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ Accessory.prototype.addService = function(service) {
this.emit('service-configurationChange', clone({accessory:this, service:service}));
}

service.on('service-configurationChange', function(change) {
if (!this.bridged) {
this._updateConfiguration();
} else {
this.emit('service-configurationChange', clone({accessory:this, service:service}));
}
}.bind(this));

// listen for changes in characteristics and bubble them up
service.on('characteristic-change', function(change) {
this.emit('service-characteristic-change', clone(change, {service:service}));
Expand All @@ -150,6 +158,31 @@ Accessory.prototype.addService = function(service) {
return service;
}

Accessory.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);

if (!this.bridged) {
this._updateConfiguration();
} else {
this.emit('service-configurationChange', clone({accessory:this, service:service}));
}

service.removeAllListeners();
}
}

Accessory.prototype.getService = function(name) {
for (var index in this.services) {
var service = this.services[index];
Expand Down Expand Up @@ -713,6 +746,14 @@ Accessory.prototype._handleCharacteristicChange = function(change) {
}

Accessory.prototype._setupService = function(service) {
service.on('service-configurationChange', function(change) {
if (!this.bridged) {
this._updateConfiguration();
} else {
this.emit('service-configurationChange', clone({accessory:this, service:service}));
}
}.bind(this));

// listen for changes in characteristics and bubble them up
service.on('characteristic-change', function(change) {
this.emit('service-characteristic-change', clone(change, {service:service}));
Expand Down
25 changes: 24 additions & 1 deletion lib/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function Service(displayName, UUID, subtype) {
// every service has an optional Characteristic.Name property - we'll set it to our displayName
// if one was given
if (displayName) {
// create the charactersitic if necessary
// create the characteristic if necessary
var nameCharacteristic =
this.getCharacteristic(Characteristic.Name) ||
this.addCharacteristic(Characteristic.Name);
Expand Down Expand Up @@ -78,9 +78,32 @@ Service.prototype.addCharacteristic = function(characteristic) {
}.bind(this));

this.characteristics.push(characteristic);

this.emit('service-configurationChange', clone({service:this}));

return characteristic;
}

Service.prototype.removeCharacteristic = function(characteristic) {
var targetCharacteristicIndex;

for (var index in this.characteristics) {
var existingCharacteristic = this.characteristics[index];

if (existingCharacteristic === characteristic) {
targetCharacteristicIndex = index;
break;
}
}

if (targetCharacteristicIndex) {
this.characteristics.splice(targetCharacteristicIndex, 1);
characteristic.removeAllListeners();

this.emit('service-configurationChange', clone({service:this}));
}
}

Service.prototype.getCharacteristic = function(name) {
// returns a characteristic object from the service
// If Service.prototype.getCharacteristic(Characteristic.Type) does not find the characteristic,
Expand Down

0 comments on commit 7152c11

Please sign in to comment.