typescriptifying completed
This commit is contained in:
54
src/HomematicSwitchItem.ts
Normal file
54
src/HomematicSwitchItem.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import * as logger from './log'
|
||||
import { mqttHandler } from './MqttDispatcher'
|
||||
import { AHomematicItem } from './AHomematicItem'
|
||||
|
||||
|
||||
export class HomematicSwitchItem extends AHomematicItem {
|
||||
private oldState: string|undefined
|
||||
private state: string
|
||||
private actionTopic: string
|
||||
private deviceFeedbackTopic: string
|
||||
private stateFeedbackTopic: string
|
||||
private stateTopic: string
|
||||
|
||||
constructor(floor: string, room: string, item: string, hmId: number) {
|
||||
super(floor, room, item, 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
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user