UNPKG

homebridge-hilo

Version:

Plugin Homebridge (non officiel) pour la passerelle et les appareils Hilo de Hydro-Québec | Unofficial Homebridge plugin for Hydro-Québec Hilo bridge and devices

126 lines 5.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HiloChallengeSensor = void 0; const config_1 = require("../config"); const logger_1 = require("../logger"); const subHours_1 = require("date-fns/subHours"); const phases = (challenge, config) => ({ inProgress: { start: new Date(challenge.phases.preheatStartDateUTC), end: new Date(challenge.phases.recoveryEndDateUTC), }, preheat: { start: new Date(challenge.phases.preheatStartDateUTC), end: new Date(challenge.phases.preheatEndDateUTC), }, reduction: { start: new Date(challenge.phases.reductionStartDateUTC), end: new Date(challenge.phases.reductionEndDateUTC), }, recovery: { start: new Date(challenge.phases.recoveryStartDateUTC), end: new Date(challenge.phases.recoveryEndDateUTC), }, plannedAM: challenge.period === "am" ? { start: (0, subHours_1.subHours)(new Date(challenge.phases.preheatStartDateUTC), config.plannedHours), end: new Date(challenge.phases.preheatStartDateUTC), } : undefined, plannedPM: challenge.period === "pm" ? { start: (0, subHours_1.subHours)(new Date(challenge.phases.preheatStartDateUTC), config.plannedHours), end: new Date(challenge.phases.preheatStartDateUTC), } : undefined, }); class HiloChallengeSensor { constructor(accessory, api, logger = (0, logger_1.getLogger)()) { this.accessory = accessory; this.api = api; this.logger = logger; this.challenges = {}; this.service = null; accessory .getService(this.api.hap.Service.AccessoryInformation) .setCharacteristic(this.api.hap.Characteristic.Manufacturer, "Hilo") .setCharacteristic(this.api.hap.Characteristic.Model, "Challenge") .setCharacteristic(this.api.hap.Characteristic.SerialNumber, this.accessory.context.v4Device.localId); this.service = accessory.getService(this.api.hap.Service.ContactSensor) || accessory.addService(this.api.hap.Service.ContactSensor); this.service.setCharacteristic(this.api.hap.Characteristic.Name, this.accessory.context.v4Device.phase + " Hilo Challenge"); this.service .getCharacteristic(this.api.hap.Characteristic.ContactSensorState) .onGet(this.getContactSensorState.bind(this)); } updateChallenge(value) { var _a, _b; this.accessory.context.v4Device.value = value; (_b = (_a = this.service) === null || _a === void 0 ? void 0 : _a.getCharacteristic(this.api.hap.Characteristic.ContactSensorState)) === null || _b === void 0 ? void 0 : _b.updateValue(value); } async getContactSensorState() { var _a; const activeChallenge = (_a = this.accessory.context.v4Device.value) !== null && _a !== void 0 ? _a : false; this.logger.debug(`Getting active Hilo Challenge: ${activeChallenge}`); return activeChallenge; } async updateChallengeStatus(challenges) { challenges.forEach((challenge) => { if (challenge.isParticipating) { this.participating(challenge); } else { this.notParticipating(challenge); } }); if (challenges.filter((challenge) => challenge.isParticipating).length === 0) { this.updateChallengeValue(false); } } updateChallengeValue(active) { this.updateChallenge(active); } participating(challenge) { if (this.challenges[challenge.id]) { return; } this.challenges[challenge.id] = []; const devicePhase = this.accessory.context.v4Device.phase; const phase = phases(challenge, (0, config_1.getConfig)())[devicePhase]; const startPhase = phase === null || phase === void 0 ? void 0 : phase.start; const endPhase = phase === null || phase === void 0 ? void 0 : phase.end; if (!startPhase || !endPhase) { this.logger.debug(`Could not find phase or period does not match device for device ${this.accessory.context.v4Device.localId} - ${this.accessory.context.v4Device.phase}`); return; } const startsIn = startPhase.getTime() - new Date().getTime(); const endsIn = endPhase.getTime() - new Date().getTime(); if (startsIn >= 0) { this.challenges[challenge.id].push(setTimeout(() => { this.updateChallengeValue(true); }, startsIn)); } else if (startsIn < 0 && endsIn > 0) { this.updateChallengeValue(true); } if (endsIn >= 0) { this.challenges[challenge.id].push(setTimeout(() => { this.updateChallengeValue(false); delete this.challenges[challenge.id]; }, endsIn)); } else { this.updateChallengeValue(false); delete this.challenges[challenge.id]; } } notParticipating(challenge) { if (this.challenges[challenge.id]) { this.challenges[challenge.id].forEach((timeout) => clearTimeout(timeout)); delete this.challenges[challenge.id]; } } } exports.HiloChallengeSensor = HiloChallengeSensor; //# sourceMappingURL=HiloChallengeSensor.js.map