Files
dispatcher_ng/src/HomematicSwitchItem.ts
Wolfgang Hottgenroth 11c3def220 first Hue light
2018-01-25 16:23:06 +01:00

69 lines
2.4 KiB
TypeScript

import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
import { AHomegearItem } from './AHomegearItem'
import { SwitchExport, ExportType } from './Export'
import { HasStateAndFeedbackTopic } from './AItem';
export class HomematicSwitchItem extends AHomegearItem implements HasStateAndFeedbackTopic {
private oldState: string|undefined
private state: string
private actionTopic: string
private deviceFeedbackTopic: string
private stateFeedbackTopic: string
private stateTopic: string
private type: string
getStateTopic() : string {
return this.stateTopic
}
getStateFeedbackTopic() : string {
return this.stateFeedbackTopic
}
constructor(floor: string, room: string, item: string, label: string, hmId: number, type: string = 'bulb') {
super(floor, room, item, label, hmId)
this.stateTopic = `${this.topicFirstPart}/state`
this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback`
this.deviceFeedbackTopic = `${this.deviceTopicPre}/1/STATE`
this.actionTopic = `${this.actionTopicPre}/1/STATE`
this.subscribeTopics = [
this.stateTopic,
this.deviceFeedbackTopic
]
this.state = 'OFF'
this.oldState = undefined
this.type = type
}
exportItem() : ExportType|null {
return SwitchExport(this.itemId, this.label, this.stateTopic, this.stateFeedbackTopic, this.type)
}
processMessage(topic: string, payload: string) : void {
switch (topic) {
case this.stateTopic:
this.state = payload
mqttHandler.send(this.stateFeedbackTopic, this.state)
if (this.state != this.oldState) {
if (this.state == 'ON') {
mqttHandler.send(this.actionTopic, 'true')
} else {
mqttHandler.send(this.actionTopic, 'false')
}
this.oldState = this.state
}
break
case this.deviceFeedbackTopic:
if (payload == 'true') {
this.state = 'ON'
} else {
this.state = 'OFF'
}
this.oldState = this.state
mqttHandler.send(this.stateFeedbackTopic, this.state)
break
}
}
}