homebridge-framework
Version:
Framework for easy creation of homebridge plugins.
141 lines (140 loc) • 5.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var characteristic_1 = require("./characteristic");
/**
* Represents a wrapper around HAP services with with support for auto-removal of unused characteristics.
*/
var Service = /** @class */ (function () {
/**
* Initializes a new Service instance.
* @param accessory The parent accessory.
* @param type The type of the service.
* @param name The name that should be displayed in HomeKit.
* @param subType The sub type of the service. May be omitted if the type is already unique.
*/
function Service(accessory, type, name, subType) {
/**
* Contains the characteristics.
*/
this._characteristics = new Array();
this._accessory = accessory;
this._type = type;
this._name = name;
this._subType = subType || null;
// Checks if the service has been cached
var hapService = null;
if (this.subType != null) {
hapService = this.accessory.platformAccessory.getServiceById(this.type, this.subType) || null;
}
else {
hapService = this.accessory.platformAccessory.getService(this.type) || null;
}
if (hapService) {
this._hapService = hapService;
return;
}
// Creates the new service
this._hapService = this.accessory.platformAccessory.addService(this.type, this.name, this.subType);
}
Object.defineProperty(Service.prototype, "accessory", {
/**
* Gets the parent accessory.
* @internal
*/
get: function () {
return this._accessory;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Service.prototype, "hapService", {
/**
* Gets the HAP service.
* @internal
*/
get: function () {
return this._hapService;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Service.prototype, "type", {
/**
* Gets the type of the service.
*/
get: function () {
return this._type;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Service.prototype, "name", {
/**
* Gets the name that should be displayed in HomeKit.
*/
get: function () {
return this._name;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Service.prototype, "subType", {
/**
* Gets the sub type of the service. May be omitted if the type is already unique.
*/
get: function () {
return this._subType;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Service.prototype, "characteristics", {
/**
* Gets the characteristics.
*/
get: function () {
return this._characteristics;
},
enumerable: true,
configurable: true
});
/**
* Defines a characteristic for usage with the service. When defining a characteristic, it is marked as used and thus not removed from HomeKit after the initialization.
* @param type The type of the characteristic.
* @param value The initial value. If omitted, the cached value is used.
*/
Service.prototype.useCharacteristic = function (type, value) {
// Checks if the characteristic has already been defined for usage
var characteristic = this.characteristics.find(function (s) { return s.type === type; });
if (characteristic) {
return characteristic;
}
// Creates a new characteristic and returns it
var newCharacteristic = new characteristic_1.Characteristic(this, type, value);
this.characteristics.push(newCharacteristic);
return newCharacteristic;
};
/**
* Removes all cached characteristics that have not been defined for usage.
*/
Service.prototype.removeUnusedCharacteristics = function () {
var characteristics = this.hapService.characteristics.slice();
var _loop_1 = function (characteristic) {
// The name characteristic is always used by homebridge
if (characteristic.UUID === this_1.accessory.platform.api.hap.Characteristic.Name.UUID) {
return "continue";
}
// Removes the unused services
if (!this_1.characteristics.some(function (d) { return characteristic.UUID === d.hapCharacteristic.UUID; })) {
this_1.hapService.removeCharacteristic(characteristic);
}
};
var this_1 = this;
for (var _i = 0, characteristics_1 = characteristics; _i < characteristics_1.length; _i++) {
var characteristic = characteristics_1[_i];
_loop_1(characteristic);
}
};
return Service;
}());
exports.Service = Service;