From 971bf4b50eaea7e3bc0739fd35f6c65a6949fd80 Mon Sep 17 00:00:00 2001 From: tobekas <43621645+tobekas@users.noreply.github.com> Date: Wed, 27 Nov 2019 11:10:21 +0100 Subject: [PATCH] realize schedules with "node-schedule" package --- fakegato-schedule.js | 45 ++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/fakegato-schedule.js b/fakegato-schedule.js index e37f975..6454071 100644 --- a/fakegato-schedule.js +++ b/fakegato-schedule.js @@ -2,6 +2,7 @@ 'use strict'; const Format = require('util').format; +var scheduler = require('node-schedule'); var homebridge; var Characteristic, Service; @@ -467,21 +468,65 @@ module.exports = function (pHomebridge) { } disableAllSchedules() { + this.jobs.forEach((j) => { + j.cancel(); + }); + this.jobs = []; + this.scheduleMode = false; this.log.debug('Schedule disabled'); } enableAllSchedules(tempNow) { + this.jobs.forEach((j) => { + j.cancel(); + }); + this.jobs = []; + if (tempNow) { // set HomeKit characteristic "TargetTemperature" to new temperature this.charac_targetTemp.setValue(tempNow, null, 'schedule enabled'); // set HomeKit characteristic "TargetHeatingCoolingState" to HEAT this.charac_targetState.setValue(Characteristic.TargetHeatingCoolingState.HEAT, null, 'schedule enabled'); } + + this.enableSchedule(this.program1, 1); // MO + this.enableSchedule(this.program2, 2); // TU + this.enableSchedule(this.program3, 3); // WE + this.enableSchedule(this.program4, 4); // TH + this.enableSchedule(this.program5, 5); // FR + this.enableSchedule(this.program6, 6); // SA + this.enableSchedule(this.program7, 7); // SU + this.scheduleMode = true; this.log.debug('Schedule enabled'); } + enableSchedule(program, day) { + const setTempHiFunc = () => { + // set HomeKit characteristic "TargetTemperature" to high temperature + this.charac_targetTemp.setValue(this.tempHi, null, 'schedule'); + // set HomeKit characteristic "TargetHeatingCoolingState" to HEAT + this.charac_targetState.setValue(Characteristic.TargetHeatingCoolingState.HEAT, null, 'schedule'); + this.log.debug('Schedule event: Start of heating period (set to %d°C)', this.tempHi); + }; + const setTempLoFunc = () => { + // set HomeKit characteristic "TargetTemperature" to low temperature + this.charac_targetTemp.setValue(this.tempLo, null, 'schedule'); + // set HomeKit characteristic "TargetHeatingCoolingState" to HEAT + this.charac_targetState.setValue(Characteristic.TargetHeatingCoolingState.HEAT, null, 'schedule'); + this.log.debug('Schedule event: End of heating period (set to %d°C)', this.tempLo); + }; + + if (!Array.isArray(program.periods)) { + return; + } + program.periods.forEach((period) => { + this.jobs.push( scheduler.scheduleJob(Format('%d %d * * %d', period.strMinute, period.strHour, day), setTempHiFunc) ); + this.jobs.push( scheduler.scheduleJob(Format('%d %d * * %d', period.endMinute, period.endHour, day), setTempLoFunc) ); + }); + } + setVacationMode(enable, temperature) { if (enable) { if (!this.vacationMode) { diff --git a/package.json b/package.json index 8093c3a..b4fbaca 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "dependencies": { "moment": "*", "googleapis": ">39.1.0", - "debug": "^2.2.0" + "debug": "^2.2.0", + "node-schedule": "^1.3.2" }, "author": "simont77", "license": "MIT",