UNPKG

homebridge-omnik

Version:
100 lines (99 loc) 5.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OmnikPlugin = void 0; const CurrentPowerProduction_1 = __importDefault(require("./Accessories/CurrentPowerProduction")); const TotalYield_1 = __importDefault(require("./Accessories/TotalYield")); const TodayYield_1 = __importDefault(require("./Accessories/TodayYield")); const OmnikApi_1 = __importDefault(require("./Utils/OmnikApi")); class OmnikPlugin { 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.devices = []; this.heartBeatInterval = (config.pollInterval || 5) * 60 * 1000; // minutes to miliseconds this.api.on('didFinishLaunching', () => { this.initialise(); }); } configureAccessory(accessory) { this.accessories.push(accessory); } validateConfig() { return !!this.config.ip && !!this.config.username && !!this.config.password; } async initialise() { if (!this.validateConfig()) { this.log.error('Configuration error. Please provide your Omnik-inverter\'s IP address, username and password'); return; } this.omnikApi = new OmnikApi_1.default(this.config.ip, this.config.username, this.config.password); if (!await this.omnikApi.ValidateIp()) { this.log.error('Your Omnik inverter\'s IP address or credentials seem to be incorrect. No connection possible'); return; } this.device = await this.omnikApi.GetDeviceInfo(); this.setupAccessoires(); await this.heartBeat(); setInterval(() => { this.heartBeat(); }, this.heartBeatInterval); } setupAccessoires() { const currentPowerProductionName = 'Current Power Production'; const currentPowerProductionUuid = this.api.hap.uuid.generate('omnik-inverter-current-power-production'); const currentPowerProductionExistingAccessory = this.accessories.find(accessory => accessory.UUID === currentPowerProductionUuid); if (currentPowerProductionExistingAccessory) { this.devices.push(new CurrentPowerProduction_1.default(this.config, this.log, this.api, currentPowerProductionExistingAccessory, this.device)); } else { this.log.info(`${currentPowerProductionName} added as accessory`); const accessory = new this.api.platformAccessory(currentPowerProductionName, currentPowerProductionUuid); this.devices.push(new CurrentPowerProduction_1.default(this.config, this.log, this.api, accessory, this.device)); this.api.registerPlatformAccessories('homebridge-omnik', 'Omnik', [accessory]); } const totalProductionName = 'Total Yield in kWh'; const totalProductionUuid = this.api.hap.uuid.generate('omnik-inverter-total-production'); const totalProductionExistingAccessory = this.accessories.find(accessory => accessory.UUID === totalProductionUuid); if (totalProductionExistingAccessory) { this.devices.push(new TotalYield_1.default(this.config, this.log, this.api, totalProductionExistingAccessory, this.device)); } else { this.log.info(`${totalProductionName} added as accessory`); const accessory = new this.api.platformAccessory(totalProductionName, totalProductionUuid); this.devices.push(new TotalYield_1.default(this.config, this.log, this.api, accessory, this.device)); this.api.registerPlatformAccessories('homebridge-omnik', 'Omnik', [accessory]); } const todayProductName = 'Today Yield in kWh'; const todayProductionUuid = this.api.hap.uuid.generate('omnik-inverter-today-production'); const todayProductionExistingAccessory = this.accessories.find(accessory => accessory.UUID === todayProductionUuid); if (todayProductionExistingAccessory) { this.devices.push(new TodayYield_1.default(this.config, this.log, this.api, todayProductionExistingAccessory, this.device)); } else { this.log.info(`${todayProductName} added as accessory`); const accessory = new this.api.platformAccessory(todayProductName, todayProductionUuid); this.devices.push(new TodayYield_1.default(this.config, this.log, this.api, accessory, this.device)); this.api.registerPlatformAccessories('homebridge-omnik', 'Omnik', [accessory]); } } async heartBeat() { try { const powerProductionInfo = await this.omnikApi.GetPowerProduction(); this.log.debug('heart beat'); this.devices.forEach((device) => { device.beat(powerProductionInfo); }); } catch (error) { this.log.error('Failed to update data'); } } } exports.OmnikPlugin = OmnikPlugin;