Skip to content

Commit

Permalink
realize schedules with "node-schedule" package
Browse files Browse the repository at this point in the history
  • Loading branch information
tobekas committed Nov 27, 2019
1 parent e8e1464 commit 971bf4b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
45 changes: 45 additions & 0 deletions fakegato-schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

const Format = require('util').format;
var scheduler = require('node-schedule');

var homebridge;
var Characteristic, Service;
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 971bf4b

Please sign in to comment.