typescriptifying completed

This commit is contained in:
Wolfgang Hottgenroth
2018-01-09 17:40:11 +01:00
parent 8d85314fc9
commit 3a8dd1b577
13 changed files with 315 additions and 127 deletions

56
src/TimerAdaptor.ts Normal file
View File

@ -0,0 +1,56 @@
import { AItem } from './AItem'
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
export class TimerAdaptor extends AItem {
private timer: NodeJS.Timer|null
private delay: number
private state: string
private inTopic: string
private actionStateTopic: string
constructor(floor: string, room: string, item: string, delay: number) {
super(floor, room, item)
this.actionStateTopic = `${this.topicFirstPart}/state`
this.inTopic = `${this.topicFirstPart}/timerIn`
this.subscribeTopics = [ this.inTopic ]
this.state = 'OFF'
this.delay = delay
this.timer = null
}
processMessage(topic: string, payload: string) : void {
switch (topic) {
case this.inTopic:
switch (payload) {
case 'SHORT':
if ((this.state == 'ON') && (this.timer != null)) {
clearTimeout(this.timer)
}
this.state = 'ON'
mqttHandler.send(this.actionStateTopic, this.state, true)
this.timer = setTimeout(() => {
logger.info(`timer ${this.itemId} elapsed`)
this.state = 'OFF'
mqttHandler.send(this.actionStateTopic, this.state, true)
}, (this.delay * 1000))
break
case 'LONG_END':
if (this.timer != null) {
clearTimeout(this.timer)
}
if (this.state == 'OFF') {
this.state = 'ON'
} else {
this.state = 'OFF'
}
mqttHandler.send(this.actionStateTopic, this.state, true)
break
}
break
}
}
}