UNPKG

homebridge-rinnai-touch-platform

Version:

Homebridge Plugin to control heating/cooling via a Rinnai Touch WiFi Module

116 lines 4.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MqttService = void 0; const mqtt = require("async-mqtt"); const HomeAssistantFormat_1 = require("./HomeAssistantFormat"); const ConnectionFormat_1 = require("./ConnectionFormat"); const FaultFormat_1 = require("./FaultFormat"); const TemperatureFormat_1 = require("./TemperatureFormat"); class MqttService { constructor(platform) { this.platform = platform; this.formats = []; this.topicMap = new Map(); this.settings = platform.settings.mqtt; } async init() { this.platform.log.debug(this.constructor.name, 'init'); try { if (this.settings === undefined) { return; } if (this.settings.host === undefined) { this.platform.log.info('MQTT: No broker host defined'); return; } await this.connect(); if (this.settings.formatHomeAssistant) { this.formats.push(new HomeAssistantFormat_1.HomeAssistantFormat(this.platform, this.client)); } if (this.settings.formatConnection) { this.formats.push(new ConnectionFormat_1.ConnectionFormat(this.platform, this.client)); } if (this.settings.formatFault) { this.formats.push(new FaultFormat_1.FaultFormat(this.platform, this.client)); } if (this.settings.subscribeTemperature) { this.formats.push(new TemperatureFormat_1.TemperatureFormat(this.platform, this.client)); } // Subscriptions for (const format of this.formats) { await this.subscribe(format); } this.client.on('message', (topic, payload) => { if (this.platform.settings.mqtt.showMqttEvents) { this.platform.log.info(`MQTT: Received: ${topic}, Payload: ${payload}`); } const format = this.topicMap.get(topic); if (format) { format.process(topic, payload.toString()); } }); // Initial Publications for (const format of this.formats) { if (this.settings.publishIntervals || this.settings.publishStatusChanged) { await format.publishTopics(); } } // Publish at intervals if (this.settings.publishIntervals) { setInterval(async () => { if (this.platform.settings.mqtt.showMqttEvents) { this.platform.log.info('MQTT: Publish Event: Scheduled Interval'); } for (const format of this.formats) { await format.publishTopics(); } }, this.settings.publishFrequency * 1000); } } catch (error) { if (error instanceof Error) { this.platform.log.error(error.message); } } } async connect() { this.platform.log.debug(this.constructor.name, 'connect'); const url = `${this.settings.host}:${this.settings.port}`; const options = { username: this.settings.username, password: this.settings.password, }; let connected = false; while (!connected) { try { this.client = await mqtt.connectAsync(url, options); connected = true; } catch (error) { if (error instanceof Error) { this.platform.log.warn(`Failed to connect to MQTT broker. Error: ${error.message}`); } this.platform.log.warn('Will try again in 60 seconds'); await this.delay(60000); } } this.platform.log.info(`MQTT: Broker connected [${url}]`); } async subscribe(format) { this.platform.log.debug(this.constructor.name, 'subscribe', 'format'); for (const topic of format.subscriptionTopics) { if (this.platform.settings.mqtt.showMqttEvents) { this.platform.log.info(`MQTT: Subscribe: ${topic}`); } await this.client.subscribe(topic); this.topicMap.set(topic, format); } } async delay(ms) { await new Promise((resolve) => { setTimeout(resolve, ms); }); } } exports.MqttService = MqttService; //# sourceMappingURL=MqttService.js.map