@ronniepettersson/homebridge-dummy
Version:
Create Homebridge accessories to help with automation and control — scheduling, delays, sensors, commands, webhooks, and more
80 lines • 3.08 kB
JavaScript
import { CronJob, validateCronExpression } from 'cron';
import { DelayLogStrings, Timeout } from './timeout.js';
import { strings } from '../i18n/i18n.js';
import { isValidTimeUnits, printableValues, ScheduleType, TimeUnits } from '../model/enums.js';
import { assert } from '../tools/validation.js';
const CRON_CUSTOM = 'CRON_CUSTOM';
export class Schedule extends Timeout {
schedule;
callback;
static new(schedule, caller, log, disableLogging, callback) {
if (!assert(log, caller, schedule, 'type')) {
return;
}
switch (schedule.type) {
case ScheduleType.INTERVAL:
if (!assert(log, caller, schedule, 'interval', 'units')) {
return;
}
if (!isValidTimeUnits(schedule.units)) {
log.error(strings.schedule.badUnits, caller, `'${schedule.units}'`, printableValues(TimeUnits));
return;
}
break;
case ScheduleType.CRON:
if (!assert(log, caller, schedule, 'cron')) {
return;
}
if (schedule.cron === CRON_CUSTOM && !assert(log, caller, schedule, 'cronCustom')) {
return;
}
break;
default:
log.error(strings.schedule.badType, caller, `'${schedule.type}'`, printableValues(ScheduleType));
return;
}
return new Schedule(schedule, caller, log, disableLogging, callback);
}
cronjob;
constructor(schedule, caller, log, disableLogging, callback) {
super(caller, log, disableLogging);
this.schedule = schedule;
this.callback = callback;
switch (this.schedule.type) {
case ScheduleType.INTERVAL:
this.startTimeout();
break;
case ScheduleType.CRON:
this.startCron();
break;
}
}
startTimeout() {
this.reset();
const logStrings = DelayLogStrings(strings.schedule.intervalMilliseconds, strings.schedule.intervalSeconds, strings.schedule.intervalMinutes, strings.schedule.intervalHours);
const delay = this.getDelay(this.schedule.interval, this.schedule.units, this.schedule.random, logStrings);
this.timeout = setTimeout(async () => {
this.reset();
await this.callback();
this.startTimeout();
}, delay);
}
startCron() {
let cron = this.schedule.cron;
if (cron === CRON_CUSTOM) {
cron = this.schedule.cronCustom;
}
if (!validateCronExpression(cron).valid) {
this.log.error(strings.accessory.invalidCron, this.caller, `'${cron}'`);
return;
}
this.logIfDesired(strings.schedule.cron);
this.cronjob = new CronJob(cron, this.callback);
this.cronjob.start();
}
teardown() {
super.teardown();
this.cronjob?.stop();
}
}
//# sourceMappingURL=schedule.js.map