Cron implemented
This commit is contained in:
33
src/Cron.ts
Normal file
33
src/Cron.ts
Normal file
@ -0,0 +1,33 @@
|
||||
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')
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user