UNPKG

homebridge-rinnai-touch-platform

Version:

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

232 lines 9.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PushoverService = void 0; const Pushover = require("pushover-notifications"); const Status_1 = require("../models/Status"); class PushoverService { constructor(platform) { this.platform = platform; this.users = []; this.days = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT']; this.zones = ['U']; this.connectionError = false; this.faultDetected = false; this.dayIncorrect = false; this.timeIncorrect = false; this.minTemperatureThresholdSent = new Map(); this.maxTemperatureThresholdSent = new Map(); this.connectionErrorSent = false; this.faultMessageSent = ''; this.dayMessageSent = false; this.timeMessageSent = false; if (this.platform.settings.pushover === undefined) { return; } if (this.platform.settings.pushover.token === undefined) { return; } if (this.platform.settings.pushover.users === undefined) { return; } if (!Array.isArray(this.platform.settings.pushover.users)) { return; } this.token = this.platform.settings.pushover.token; this.users = this.platform.settings.pushover.users; } init() { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; this.platform.log.debug(this.constructor.name, 'init'); if (this.token === undefined || this.users.length === 0) { return; } this.pushover = new Pushover({ token: this.token, }); this.minTemperatureThreshold = (_a = this.platform.settings.pushover) === null || _a === void 0 ? void 0 : _a.minTemperatureThreshold; this.maxTemperatureThreshold = (_b = this.platform.settings.pushover) === null || _b === void 0 ? void 0 : _b.maxTemperatureThreshold; this.connectionError = (_d = (_c = this.platform.settings.pushover) === null || _c === void 0 ? void 0 : _c.connectionError) !== null && _d !== void 0 ? _d : false; this.faultDetected = (_f = (_e = this.platform.settings.pushover) === null || _e === void 0 ? void 0 : _e.faultDetected) !== null && _f !== void 0 ? _f : false; this.dayIncorrect = (_h = (_g = this.platform.settings.pushover) === null || _g === void 0 ? void 0 : _g.dayIncorrect) !== null && _h !== void 0 ? _h : false; this.timeIncorrect = (_k = (_j = this.platform.settings.pushover) === null || _j === void 0 ? void 0 : _j.timeIncorrect) !== null && _k !== void 0 ? _k : false; this.platform.temperatureService.on('temperature_change', this.handleTemperatureChange.bind(this)); this.platform.service.session.on('connection', this.handleConnection.bind(this)); this.platform.service.on('fault', this.handleFault.bind(this)); this.platform.service.session.on('status', this.handleStatus.bind(this)); } handleTemperatureChange(temperatures) { var _a, _b; this.platform.log.debug(this.constructor.name, 'handleTemperatureChange', temperatures); if (this.minTemperatureThreshold === undefined && this.maxTemperatureThreshold === undefined) { return; } const zones = this.platform.service.getHasMultiSetPoint() ? this.platform.service.getZonesInstalled() : ['U']; for (const zone of zones) { if (temperatures[zone] === undefined) { continue; } const zoneName = zone === 'U' ? '' : `[${this.platform.service.getZoneName(zone)}]`; // Min Temp Threshold if (this.minTemperatureThreshold !== undefined) { if (temperatures[zone] < this.minTemperatureThreshold) { if (!((_a = this.minTemperatureThresholdSent.get(zone)) !== null && _a !== void 0 ? _a : false)) { this.sendMessage(`Temperature is below ${this.minTemperatureThreshold} degrees ${zoneName}`); this.minTemperatureThresholdSent.set(zone, true); } } else { if (temperatures[zone] > this.minTemperatureThreshold + 1) { this.minTemperatureThresholdSent.set(zone, false); } } } // Max Temp Threshold if (this.maxTemperatureThreshold !== undefined) { if (temperatures[zone] > this.maxTemperatureThreshold) { if (!((_b = this.maxTemperatureThresholdSent.get(zone)) !== null && _b !== void 0 ? _b : false)) { this.sendMessage(`Temperature is above ${this.maxTemperatureThreshold} degrees ${zoneName}`); this.maxTemperatureThresholdSent.set(zone, true); } } else { if (temperatures[zone] < this.maxTemperatureThreshold - 1) { this.maxTemperatureThresholdSent.set(zone, false); } } } } } handleConnection() { this.platform.log.debug(this.constructor.name, 'handleConnection'); if (this.isBooting()) { return; } if (!this.connectionError) { return; } if (this.platform.service.session.hasConnectionError) { if (!this.connectionErrorSent) { this.sendMessage('TCP/IP Connection Error occured'); this.connectionErrorSent = true; } } else { if (this.connectionErrorSent) { this.sendMessage('TCP/IP Connection restored'); this.connectionErrorSent = false; } } } handleFault(fault) { this.platform.log.debug(this.constructor.name, 'handleFault', fault.toString()); if (this.isBooting()) { return; } if (!this.faultDetected) { return; } if (fault.detected) { if (this.faultMessageSent !== fault.toString()) { this.sendMessage(fault.toString()); this.faultMessageSent = fault.toString(); } } else { this.faultMessageSent = ''; } } handleStatus(status) { this.platform.log.debug(this.constructor.name, 'handleStatus', status.toString()); if (this.isBooting()) { return; } if (!this.dayIncorrect && !this.timeIncorrect) { return; } const now = new Date(); const module_day = status.getState(Status_1.States.Day); const module_time = status.getState(Status_1.States.Time); if (module_day === undefined || module_time === undefined) { return; } const module_hours = Number(module_time.substring(0, 2)); const module_minutes = Number(module_time.substring(3, 5)); let day_offset = 0; if (now.getHours() === 0 && module_hours === 23) { day_offset = -1; } else if (now.getHours() === 23 && module_hours === 0) { day_offset = 1; } const module_date = new Date(now.getFullYear(), now.getMonth(), now.getDate() + day_offset, module_hours, module_minutes, now.getSeconds()); if (this.dayIncorrect && day_offset === 0) { const system_day = this.days[now.getDay()]; if (system_day !== module_day) { if (!this.dayMessageSent) { this.sendMessage(`Controller's day (${module_day}) is different to system day (${system_day})`); this.dayMessageSent = true; } } else { this.dayMessageSent = false; } } if (this.timeIncorrect) { const timeDifference = Math.abs(now.getTime() - module_date.getTime()); if (timeDifference > 180000) { // 3+ minutes out of sync if (!this.timeMessageSent) { this.sendMessage(`Controller's time (${module_time}) is more than 3 minutes out of sync with system time`); this.timeMessageSent = true; } } else { if (timeDifference < 120000) { this.timeMessageSent = false; } } } } isBooting() { const bootTime = this.platform.settings.bootTime; if (bootTime === undefined) { return false; } const [bootHour, bootMinute] = bootTime.split(':').map(Number); const now = new Date(); if (now.getHours() === bootHour && now.getMinutes() === bootMinute) { return true; } return false; } sendMessage(message) { this.platform.log.debug(this.constructor.name, 'sendMessage', message); try { const pushoverMessage = { title: this.platform.settings.name, message: message, sound: 'siren', priority: 0, user: '', }; for (const user of this.users) { pushoverMessage.user = user; this.pushover.send(pushoverMessage, (error) => { if (error) { this.platform.log.warn('Pushover notification(s) failed:', error.message); } }); } } catch (error) { if (error instanceof Error) { this.platform.log.warn('Pushover notification(s) failed:', error.message); } } } } exports.PushoverService = PushoverService; //# sourceMappingURL=PushoverService.js.map