diff --git a/lib/CronScheduler.js b/lib/CronScheduler.js index acc3fb4a..753470ed 100644 --- a/lib/CronScheduler.js +++ b/lib/CronScheduler.js @@ -14,7 +14,7 @@ const CronScheduler = function (parameters) { } // todo: add some more validation maybe let task = cron.schedule(job[2], () => { - this.vacuum.getCurrentStatus(function (err, data) { + self.vacuum.getCurrentStatus(function (err, data) { // running ONLY when state = 8 (charging) if (!err && data && data.state === 8) { self.vacuum.startCleaningZone(job[3], function (err, data) { @@ -28,40 +28,40 @@ const CronScheduler = function (parameters) { console.log('scheduled zoned cleaning start failed', err, data); } }); - },{timezone: this.timezone}); - this.schedules.push(task); + },{timezone: self.timezone}); + self.schedules.push(task); return true; }; this.removeSchedule = function(index) { - if (this.schedules[index]) { - let task = this.schedules[index]; + if (self.schedules[index]) { + let task = self.schedules[index]; task.destroy(); - this.schedules.splice(index, 1); + self.schedules.splice(index, 1); return true; } return false; }; this.resetSchedules = function() { - for (let i = this.schedules.length - 1; i >= 0 ; i--) { - this.removeSchedule(i); + for (let i = self.schedules.length - 1; i >= 0 ; i--) { + self.removeSchedule(i); } - this.configuration.get("ztimers").forEach(task => { - this.createSchedule(task); + self.configuration.get("ztimers").forEach(task => { + self.createSchedule(task); }); }; - this.vacuum.getTimezone(function (err, data) { - if (!err) { - self.timezone = data; - } - if (self.configuration.get("ztimers") && self.configuration.get("ztimers").length) { - self.configuration.get("ztimers").forEach(job => { - self.createSchedule(job); - }); - }; - }); + this.initSchedules = function() { + self.vacuum.getTimezone(function (err, data) { + if (!err) { + self.timezone = data; + } + self.resetSchedules(); + }); + }; + + setTimeout(() => { self.initSchedules() }, 30e3); }; module.exports = CronScheduler;