typescriptifying

This commit is contained in:
Wolfgang Hottgenroth
2018-01-09 16:36:14 +01:00
parent 9e39d74084
commit 8d85314fc9
21 changed files with 372 additions and 182 deletions

56
src/DimmerAdaptor.ts Normal file
View File

@ -0,0 +1,56 @@
import { AItem } from './AItem'
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
export class DimmerAdaptor extends AItem {
private brightDirection: number
private bright: number
private state: string
private inTopic: string
private actionBrightTopic: string
private actionStateTopic: string
constructor(floor: string, room: string, item: string) {
super(floor, room, item)
this.actionStateTopic = `${this.topicFirstPart}/state`
this.actionBrightTopic = `${this.topicFirstPart}/bright`
this.inTopic = `${this.topicFirstPart}/dimmerIn`
this.subscribeTopics = [ this.inTopic ]
this.state = 'OFF'
this.bright = 100
this.brightDirection = -1
}
processMessage(topic: string, payload: string) {
switch (topic) {
case this.inTopic:
switch (payload) {
case 'SHORT':
if (this.state == 'OFF') {
this.state = 'ON'
} else {
this.state = 'OFF'
}
mqttHandler.send(this.actionStateTopic, this.state, true)
break
case 'LONG_HOLD':
this.bright += (5 * this.brightDirection)
if (this.bright > 100) {
this.bright = 100
}
if (this.bright < 0) {
this.bright = 0
}
mqttHandler.send(this.actionBrightTopic, this.bright.toString(), true)
break
case 'LONG_END':
this.brightDirection = this.brightDirection * -1
break
}
break
}
}
}