Files
dispatcher_ng/src/M433SwitchItem.ts

64 lines
1.9 KiB
TypeScript

import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
import { AItem, HasStateAndFeedbackTopicAndLabelAndRoom, HasInTopic, HasStateAndFeedbackTopic } from './AItem'
import { SwitchExport, ExportType } from './Export'
export class M433SwitchItem extends AItem implements HasStateAndFeedbackTopicAndLabelAndRoom {
private offCode: string
private onCode: string
private oldState: string|undefined
private state: string
private actionTopic: string
private stateFeedbackTopic: string
private stateTopic: string
private type: string
getLabel() : string {
return this.label
}
getRoom() : string {
return this.room
}
getStateTopic() : string {
return this.stateTopic
}
getInTopic() : string {
return this.stateTopic
}
getStateFeedbackTopic() : string {
return this.stateFeedbackTopic
}
constructor(floor: string, room: string, item: string, label: string, onCode: string, offCode: string, type: string = 'bulb') {
super(floor, room, item, label)
this.stateTopic = `${this.topicFirstPart}/state`
this.subscribeTopics = [this.stateTopic]
this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback`
this.actionTopic = 'IoT/Mqtt433Gateway/Message'
this.state = 'OFF'
this.onCode = onCode
this.offCode = offCode
this.type = type
}
exportItem() : ExportType|null {
return SwitchExport(this.itemId, this.label, this.stateTopic, this.stateFeedbackTopic, this.type)
}
processMessage(topic: string, payload: string) {
this.state = payload;
mqttHandler.send(this.stateFeedbackTopic, this.state);
if (this.state == 'ON') {
mqttHandler.send(this.actionTopic, this.onCode);
} else {
mqttHandler.send(this.actionTopic, this.offCode);
}
}
}