typescriptifying completed
This commit is contained in:
56
src/TimerAdaptor.ts
Normal file
56
src/TimerAdaptor.ts
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user