new getServiceByUUIDAndSubtype(UUID, subtype) function

Some platforms may have accessories that contain more than one service of a given type, such as multiple lightbulbs.
This commit is contained in:
Raoul
2016-02-08 00:01:44 +01:00
parent 9e6bf028ba
commit 9c8812da70

View File

@@ -60,6 +60,12 @@ PlatformAccessory.prototype.addService = function(service) {
return service; return 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.
* @param {ServiceConstructor|string} name
* @returns Service
*/
PlatformAccessory.prototype.getService = function(name) { PlatformAccessory.prototype.getService = function(name) {
for (var index in this.services) { for (var index in this.services) {
var service = this.services[index]; var service = this.services[index];
@@ -71,6 +77,25 @@ PlatformAccessory.prototype.getService = function(name) {
} }
} }
/**
* 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.
* @param {string} UUID Can be an UUID, a service.displayName, or a constructor of a Service
* @param {string} subtype A subtype string to match
* @returns Service
*/
PlatformAccessory.prototype.getServiceByUUIDAndSubType = function(UUID, subtype) {
for (var index in this.services) {
var service = this.services[index];
if (typeof UUID === 'string' && (service.displayName === UUID || service.name === UUID) && service.subtype === subtype )
return service;
else if (typeof name === 'function' && ((service instanceof name) || (name.UUID === service.UUID)) && service.subtype === subtype)
return service;
}
}
PlatformAccessory.prototype.updateReachability = function(reachable) { PlatformAccessory.prototype.updateReachability = function(reachable) {
this.reachable = reachable; this.reachable = reachable;
@@ -149,4 +174,4 @@ PlatformAccessory.prototype._configFromData = function(data) {
} }
this.services = services; this.services = services;
} }