Cron implemented

This commit is contained in:
Wolfgang Hottgenroth
2018-01-15 17:49:25 +01:00
parent 09a63fe736
commit 9c0ac6dc3b
9 changed files with 107 additions and 2 deletions

View File

@ -12,6 +12,10 @@ export interface HasStateAndFeedbackTopic extends HasStateTopic {
getStateFeedbackTopic() : string
}
export interface HasInTopic {
getInTopic() : string
}
export abstract class AItem {
protected topicFirstPart: string
protected itemId: string

33
src/Cron.ts Normal file
View 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')
})
}
}

View File

@ -1,5 +1,6 @@
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
import { HasInTopic } from './AItem'
import { AHomematicItem } from './AHomematicItem'
import { MaxWindowContact } from './MaxWindowContact';
// import { SwitchExport, ExportType } from './Export'
@ -12,7 +13,7 @@ type WindowContactHolder = {
state : string
}
export class MaxThermostat extends AHomematicItem {
export class MaxThermostat extends AHomematicItem implements HasInTopic {
private actionTopic: string
private deviceFeedbackTopic: string
private temperatureFeedbackTopic: string
@ -22,6 +23,10 @@ export class MaxThermostat extends AHomematicItem {
private windowOpen: boolean
// Thermostat: homegear/instance1/set/3/1/SET_TEMPERATURE
getInTopic() : string {
return this.temperatureTopic
}
constructor(floor: string, room: string, item: string, label: string, hmId: number, windowContacts: MaxWindowContact[]) {
super(floor, room, item, label, hmId)
this.temperatureTopic = `${this.topicFirstPart}/temperature`

View File

@ -17,7 +17,7 @@ import { LightScene } from './Scene'
import { MaxEcoSwitch } from './MaxEcoSwitch'
import { MaxThermostat } from './MaxThermostat'
import { MaxWindowContact } from './MaxWindowContact'
import { Cron } from './Cron'
logger.info("Dispatcher starting")
@ -221,6 +221,12 @@ windowContact2.start()
let thermostat1 = new MaxThermostat('Gnd', 'Bathroom', 'Thermostat', 'Thermostat Bad unten', 3, [windowContact1, windowContact2])
thermostat1.start()
let thermostat1Cron = new Cron('Thermostat1Cron', thermostat1, [
{cronTime: '00 47 17 * * *', output: '5.0'},
{cronTime: '00 48 17 * * *', output: '20.0'},
{cronTime: '00 49 17 * * *', output: '25.0'}
])
thermostat1Cron.start()
// ----------------------------------------------------------------------------------------------------------
// Homekit export