homebridge-gira-client
Version:
Homebridge Plugin für Gira Homeserver 4 mit automatischer Geräteerkennung über IoT REST API
124 lines • 4.96 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GiraHomeserverPlatform = void 0;
const settings_1 = require("./settings");
const gira_rest_client_1 = require("./gira-rest-client");
const device_manager_1 = require("./device-manager");
class GiraHomeserverPlatform {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
this.accessories = [];
this.discoveryRunning = false;
const giraConfig = config;
this.log.debug('Finished initializing platform:', giraConfig.name);
if (!this.validateConfig(giraConfig)) {
this.log.error('Plugin configuration is invalid. Please check your config.json');
return;
}
this.platformContext = {
api: this.api,
log: this.log,
config: giraConfig,
accessories: new Map(),
};
this.giraClient = new gira_rest_client_1.GiraRestClient(giraConfig, this.log);
this.deviceManager = new device_manager_1.DeviceManager(this.platformContext, this.giraClient);
this.api.on('didFinishLaunching', () => {
this.log.debug('Executed didFinishLaunching callback');
this.discoverDevices();
});
this.api.on('shutdown', () => {
this.log.debug('Shutting down platform');
this.giraClient.disconnect();
});
}
validateConfig(config) {
if (!config.host) {
this.log.error('Host is required in configuration');
return false;
}
if (!config.username || !config.password) {
this.log.error('Username and password are required in configuration');
return false;
}
return true;
}
configureAccessory(accessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
this.accessories.push(accessory);
this.platformContext.accessories.set(accessory.UUID, accessory);
}
async discoverDevices() {
if (this.discoveryRunning) {
this.log.debug('Device discovery already running');
return;
}
this.discoveryRunning = true;
this.log.info('Starting device discovery...');
try {
await this.giraClient.connect();
const devices = await this.giraClient.getDevices();
this.log.info(`Discovered ${devices.length} devices`);
for (const device of devices) {
await this.processDevice(device);
}
this.setupPolling();
this.log.info('Device discovery completed');
}
catch (error) {
this.log.error('Error during device discovery:', error);
setTimeout(() => {
this.discoveryRunning = false;
this.discoverDevices();
}, 30000);
}
this.discoveryRunning = false;
}
async processDevice(device) {
this.log.debug(`Processing device: ${device.name} (${device.id})`);
try {
const accessory = await this.deviceManager.createOrUpdateAccessory(device);
if (accessory) {
this.platformContext.accessories.set(accessory.UUID, accessory);
this.log.info(`Successfully configured accessory: ${accessory.displayName}`);
}
}
catch (error) {
this.log.error(`Error processing device ${device.name}:`, error);
}
}
setupPolling() {
const interval = this.platformContext.config.pollingInterval || 30000;
setInterval(async () => {
try {
await this.giraClient.refreshDeviceStates();
}
catch (error) {
this.log.error('Error during polling:', error);
}
}, interval);
this.log.debug(`Polling setup with interval: ${interval}ms`);
}
removeAccessory(accessory) {
this.log.info('Removing existing accessory from cache:', accessory.displayName);
this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
this.platformContext.accessories.delete(accessory.UUID);
}
registerAccessory(accessory) {
this.log.info('Registering new accessory:', accessory.displayName);
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
this.platformContext.accessories.set(accessory.UUID, accessory);
}
getAccessory(uuid) {
return this.platformContext.accessories.get(uuid);
}
getAllAccessories() {
return Array.from(this.platformContext.accessories.values());
}
}
exports.GiraHomeserverPlatform = GiraHomeserverPlatform;
//# sourceMappingURL=platform.js.map