Files
dispatcher_ng/src/UrlSwitchItem.ts

57 lines
1.8 KiB
TypeScript

import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
import { AItem, HasStateAndFeedbackTopic } from './AItem'
import { SwitchExport, ExportType } from './Export'
import * as http from 'http'
export class UrlSwitchItem extends AItem implements HasStateAndFeedbackTopic {
private offUrl: string
private onUrl: 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, onUrl: string, offUrl: 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.onUrl = onUrl
this.offUrl = offUrl
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') {
http.get(this.onUrl,)
} else {
http.get(this.offUrl)
}
this.oldState = this.state;
}
}
}