fixes and start of UrlSwitchItem
This commit is contained in:
55
src/UrlSwitchItem.ts
Normal file
55
src/UrlSwitchItem.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import * as logger from './log'
|
||||
import { mqttHandler } from './MqttDispatcher'
|
||||
import { AItem, HasStateAndFeedbackTopic } from './AItem'
|
||||
import { SwitchExport, ExportType } from './Export'
|
||||
|
||||
|
||||
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') {
|
||||
mqttHandler.send(this.actionTopic, this.onCode);
|
||||
} else {
|
||||
mqttHandler.send(this.actionTopic, this.offCode);
|
||||
}
|
||||
this.oldState = this.state;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user