33 lines
1005 B
TypeScript
33 lines
1005 B
TypeScript
import * as logger from './log'
|
|
import { mqttHandler } from './MqttDispatcher'
|
|
import { HasInTopic } from './AItem'
|
|
import * as cron from 'cron'
|
|
|
|
|
|
type CronEntry = {
|
|
output: string
|
|
cronTime: string
|
|
cronJob?: cron.CronJob
|
|
}
|
|
|
|
export class Cron {
|
|
private crontab: CronEntry[]
|
|
private targetTopic: string
|
|
private name: string
|
|
|
|
constructor(name: string, target: HasInTopic, crontab: CronEntry[]) {
|
|
this.name = name
|
|
this.targetTopic = target.getInTopic()
|
|
this.crontab = crontab
|
|
}
|
|
|
|
start() : void {
|
|
this.crontab.forEach((cronEntry) => {
|
|
logger.info(`Starting cronjob for: ${this.targetTopic}, ${cronEntry.output} at ${cronEntry.cronTime}`)
|
|
cronEntry.cronJob = new cron.CronJob(cronEntry.cronTime, () => {
|
|
logger.info(`Firing ${this.targetTopic}, ${cronEntry.output}`)
|
|
mqttHandler.send(this.targetTopic, cronEntry.output)
|
|
}, undefined, true, 'Europe/Berlin')
|
|
})
|
|
}
|
|
} |