Files
dispatcher_ng/src/DimmerAdaptor.ts
Wolfgang Hottgenroth f6db2369c3 dimmer
2018-01-31 21:24:42 +01:00

61 lines
2.0 KiB
TypeScript

import { AItem, HasInTopic } from './AItem'
import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
export class DimmerAdaptor extends AItem implements HasInTopic {
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
}
getInTopic() : string {
return this.inTopic
}
processMessage(topic: string, payload: string) : void {
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
}
}
}