UNPKG

homebridge-config-ui-x

Version:

A web based management, configuration and control platform for Homebridge.

240 lines • 10.7 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; import { join } from 'node:path'; import { HapClient } from '@homebridge/hap-client'; import { BadRequestException, Inject, Injectable } from '@nestjs/common'; import { mkdirp, pathExists, readJson, writeJsonSync } from 'fs-extra/esm'; import NodeCache from 'node-cache'; import { ConfigService } from '../../core/config/config.service.js'; import { Logger } from '../../core/logger/logger.service.js'; let AccessoriesService = class AccessoriesService { configService; logger; hapClient; accessoriesCache = new NodeCache({ stdTTL: 0 }); constructor(configService, logger) { this.configService = configService; this.logger = logger; if (this.configService.homebridgeInsecureMode) { this.hapClient = new HapClient({ pin: this.configService.homebridgeConfig.bridge.pin, logger: this.logger, config: this.configService.ui.accessoryControl || {}, }); } } async connect(client) { if (!this.configService.homebridgeInsecureMode) { this.logger.error('Homebridge must be running in insecure mode to control accessories.'); return; } let services; const loadAllAccessories = async (refresh) => { if (!refresh) { const cached = this.accessoriesCache.get('services'); if (cached && cached.length) { client.emit('accessories-data', cached); } } services = await this.loadAccessories(); this.refreshCharacteristics(services); client.emit('accessories-ready-for-control'); client.emit('accessories-data', services); this.accessoriesCache.set('services', services); }; await loadAllAccessories(false); const requestHandler = async (msg) => { if (msg.set) { const service = services.find(x => x.uniqueId === msg.set.uniqueId); if (service) { try { await service.setCharacteristic(msg.set.iid, msg.set.value); services = await this.loadAccessories(); setTimeout(() => { this.refreshCharacteristics(services); }, 1500); } catch (e) { client.emit('accessory-control-failure', e.message); } } } }; client.on('accessory-control', requestHandler); const monitor = await this.hapClient.monitorCharacteristics(); const updateHandler = (data) => { client.emit('accessories-data', data); }; monitor.on('service-update', updateHandler); const instanceUpdateHandler = async (data) => { client.emit('accessories-reload-required', services); }; this.hapClient.on('instance-discovered', instanceUpdateHandler); const secondaryLoadTimeout = setTimeout(async () => { await loadAllAccessories(true); }, 3000); const onEnd = () => { clearTimeout(secondaryLoadTimeout); client.removeAllListeners('end'); client.removeAllListeners('disconnect'); client.removeAllListeners('accessory-control'); monitor.removeAllListeners('service-update'); monitor.finish(); this.hapClient.removeListener('instance-discovered', instanceUpdateHandler); }; client.on('disconnect', onEnd.bind(this)); client.on('end', onEnd.bind(this)); this.hapClient.refreshInstances(); } refreshCharacteristics(services) { Promise.all(services.map(service => service.refreshCharacteristics().catch((error) => { this.logger.error(`Failed to refresh characteristics for service ${service.uniqueId}: ${error.message}`); }))).catch((error) => { this.logger.warn(`Failed to refresh characteristics: ${error.message}`); }); } async loadAccessories() { if (!this.configService.homebridgeInsecureMode) { throw new BadRequestException('Homebridge must be running in insecure mode to access accessories.'); } try { return await this.hapClient.getAllServices(); } catch (e) { if (e.response?.status === 401) { this.logger.warn('Homebridge must be running in insecure mode to view and control accessories from this plugin.'); } else { this.logger.error(`Failed to load accessories from Homebridge as ${e.message}.`); } return []; } } async getAccessory(uniqueId) { const services = await this.loadAccessories(); const service = services.find(x => x.uniqueId === uniqueId); if (!service) { throw new BadRequestException(`Service with uniqueId of '${uniqueId}' not found.`); } try { await service.refreshCharacteristics(); return service; } catch (e) { throw new BadRequestException(e.message); } } async setAccessoryCharacteristic(uniqueId, characteristicType, value) { const services = await this.loadAccessories(); const service = services.find(x => x.uniqueId === uniqueId); if (!service) { throw new BadRequestException(`Service with uniqueId of '${uniqueId}' not found.`); } const characteristic = service.getCharacteristic(characteristicType); if (!characteristic || !characteristic.canWrite) { const types = service.serviceCharacteristics.filter(x => x.canWrite).map(x => `'${x.type}'`).join(', '); throw new BadRequestException(`Invalid characteristicType. Valid types are: ${types}.`); } if (['uint8', 'uint16', 'uint32', 'uint64'].includes(characteristic.format)) { value = Number.parseInt(value, 10); if (characteristic.minValue !== undefined && value < characteristic.minValue) { throw new BadRequestException(`Invalid value. The value must be between ${characteristic.minValue} and ${characteristic.maxValue}.`); } if (characteristic.maxValue !== undefined && value > characteristic.maxValue) { throw new BadRequestException(`Invalid value. The value must be between ${characteristic.minValue} and ${characteristic.maxValue}.`); } } if (characteristic.format === 'float') { value = Number.parseFloat(value); if (characteristic.minValue !== undefined && value < characteristic.minValue) { throw new BadRequestException(`Invalid value. The value must be between ${characteristic.minValue} and ${characteristic.maxValue}.`); } if (characteristic.maxValue !== undefined && value > characteristic.maxValue) { throw new BadRequestException(`Invalid value. The value must be between ${characteristic.minValue} and ${characteristic.maxValue}.`); } } if (characteristic.format === 'bool') { if (typeof value === 'string') { if (['true', '1'].includes(value.toLowerCase())) { value = true; } else if (['false', '0'].includes(value.toLowerCase())) { value = false; } } else if (typeof value === 'number') { value = value === 1; } if (typeof value !== 'boolean') { throw new BadRequestException('Invalid value. The value must be a boolean (true or false).'); } } try { await characteristic.setValue(value); await service.refreshCharacteristics(); return service; } catch (e) { throw new BadRequestException(e.message); } } async getAccessoryLayout(username) { try { const accessoryLayout = await readJson(this.configService.accessoryLayoutPath); if (username in accessoryLayout) { return accessoryLayout[username]; } else { throw new Error('User not in Accessory Layout'); } } catch (e) { return [ { name: 'Default Room', services: [], }, ]; } } async saveAccessoryLayout(user, layout) { let accessoryLayout; try { accessoryLayout = await readJson(this.configService.accessoryLayoutPath); } catch (e) { accessoryLayout = {}; } if (!await pathExists(join(this.configService.storagePath, 'accessories'))) { await mkdirp(join(this.configService.storagePath, 'accessories')); } accessoryLayout[user] = layout; writeJsonSync(this.configService.accessoryLayoutPath, accessoryLayout); this.logger.log(`Accessory layout changes saved for ${user}.`); return layout; } resetInstancePool() { if (this.configService.homebridgeInsecureMode) { this.hapClient.resetInstancePool(); } } }; AccessoriesService = __decorate([ Injectable(), __param(0, Inject(ConfigService)), __param(1, Inject(Logger)), __metadata("design:paramtypes", [ConfigService, Logger]) ], AccessoriesService); export { AccessoriesService }; //# sourceMappingURL=accessories.service.js.map