import * as logger from './log' import { mqttHandler } from './MqttDispatcher' import { AItem, HasStateAndFeedbackTopic } from './AItem' import { SwitchExport, ExportType } from './Export' export class M433SwitchItem extends AItem implements HasStateAndFeedbackTopic { 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 getStateTopic() : 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.oldState = undefined 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 != this.oldState) { if (this.state == 'ON') { mqttHandler.send(this.actionTopic, this.onCode); } else { mqttHandler.send(this.actionTopic, this.offCode); } this.oldState = this.state; } } }