@pietrolubini/homebridge-ecoflow
Version:
Homebridge plugin for EcoFlow devices
107 lines • 4.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceBase = void 0;
class ServiceBase {
serviceType;
ecoFlowAccessory;
serviceSubType;
log;
platform;
characteristics = [];
_service = null;
isReachable = true;
enabled = true;
constructor(serviceType, ecoFlowAccessory, serviceSubType) {
this.serviceType = serviceType;
this.ecoFlowAccessory = ecoFlowAccessory;
this.serviceSubType = serviceSubType;
this.log = ecoFlowAccessory.log;
this.platform = ecoFlowAccessory.platform;
}
initialize() {
this._service = this.createService();
this.characteristics = this.addCharacteristics();
this.characteristics.push(this.addCharacteristic(this.platform.Characteristic.Name));
}
cleanupCharacteristics() {
this.service.characteristics
.filter(characteristic => !this.characteristics.includes(characteristic))
.forEach(characteristic => {
this.log.warn(`[${this.service.displayName}] Removing obsolete characteristic:`, characteristic.displayName);
this.service.removeCharacteristic(characteristic);
});
}
// Getter for service
get service() {
if (!this._service) {
this.log.error('Service is not initialized:', this.constructor.name);
throw new Error(`Service is not initialized: ${this.constructor.name}`);
}
return this._service;
}
updateEnabled(enabled) {
this.enabled = enabled;
if (!enabled) {
this.onDisabled();
}
}
updateReachability(value) {
this.isReachable = value;
}
get serviceName() {
return this.ecoFlowAccessory.config.name + (this.serviceSubType ? ` ${this.serviceSubType}` : '');
}
createService() {
return this.serviceSubType
? this.getOrAddServiceById(this.serviceType, this.serviceName, this.serviceSubType)
: this.getOrAddService(this.serviceType, this.ecoFlowAccessory.config.name);
}
addCharacteristic(characteristic) {
return this.service.getCharacteristic(characteristic);
}
getOrAddService(service, displayName) {
const result = this.ecoFlowAccessory.accessory.getService(service) ||
this.ecoFlowAccessory.accessory.addService(service, displayName, service.UUID);
result.displayName = displayName ?? result.displayName;
return result;
}
getOrAddServiceById(service, serviceName, serviceSubType) {
const result = this.ecoFlowAccessory.accessory.getServiceById(service, serviceSubType) ||
this.ecoFlowAccessory.accessory.addService(service, serviceName, serviceSubType);
return result;
}
updateCharacteristic(characteristic, name, value) {
const newName = this.serviceSubType !== undefined ? `${this.serviceSubType} ${name}` : name;
this.log.debug(`${newName} ->`, value);
this.service.getCharacteristic(characteristic).updateValue(value);
}
covertPercentsToValue(percents, maxValue) {
return (percents * maxValue) / 100;
}
covertValueToPercents(value, maxValue) {
return (value * 100) / maxValue;
}
onDisabled() { }
processOnGet(value) {
this.checkIsReachable();
return value;
}
processOnSet(name, func) {
this.checkIsReachable();
this.checkIsEnabled(name);
func();
}
checkIsReachable() {
if (!this.isReachable) {
throw new this.platform.api.hap.HapStatusError(-70402 /* this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE */);
}
}
checkIsEnabled(name) {
if (!this.enabled) {
this.log.warn(`[${this.serviceName}] Service is disabled. Setting of "${name}" is disallowed`);
throw new this.platform.api.hap.HapStatusError(-70404 /* this.platform.api.hap.HAPStatus.READ_ONLY_CHARACTERISTIC */);
}
}
}
exports.ServiceBase = ServiceBase;
//# sourceMappingURL=serviceBase.js.map