84 lines
3.1 KiB
TypeScript
84 lines
3.1 KiB
TypeScript
import * as logger from './log'
|
|
import { mqttHandler } from './MqttDispatcher'
|
|
import { HasInTopic } from './AItem'
|
|
import { AHomematicItem } from './AHomematicItem'
|
|
import { MaxWindowContact } from './MaxWindowContact';
|
|
import { ThermostatExport, ExportType } from './Export'
|
|
|
|
const WINDOW_OPEN_TEMPERATURE = 4.5
|
|
|
|
|
|
type WindowContactHolder = {
|
|
windowContact : MaxWindowContact
|
|
state : string
|
|
}
|
|
|
|
export class MaxThermostat extends AHomematicItem implements HasInTopic {
|
|
private actionTopic: string
|
|
private deviceFeedbackTopic: string
|
|
private temperatureFeedbackTopic: string
|
|
private temperatureTopic: string
|
|
private temperature: number
|
|
private windowContactMap: { [key:string]: WindowContactHolder }
|
|
private windowOpen: boolean
|
|
// Thermostat: homegear/instance1/set/3/1/SET_TEMPERATURE
|
|
|
|
getInTopic() : string {
|
|
return this.temperatureTopic
|
|
}
|
|
|
|
exportItem() : ExportType|null {
|
|
return ThermostatExport(this.itemId, this.label, this.temperatureTopic, this.temperatureFeedbackTopic)
|
|
}
|
|
|
|
constructor(floor: string, room: string, item: string, label: string, hmId: number, windowContacts: MaxWindowContact[]) {
|
|
super(floor, room, item, label, hmId)
|
|
this.temperatureTopic = `${this.topicFirstPart}/temperature`
|
|
this.temperatureFeedbackTopic = `${this.topicFirstPart}/temperature/feedback`
|
|
this.deviceFeedbackTopic = `${this.deviceTopicPre}/1/SET_TEMPERATURE`
|
|
this.actionTopic = `${this.actionTopicPre}/1/SET_TEMPERATURE`
|
|
this.subscribeTopics = [
|
|
this.temperatureTopic,
|
|
this.deviceFeedbackTopic
|
|
]
|
|
this.windowOpen = false
|
|
this.windowContactMap = {}
|
|
windowContacts.forEach((windowContact) => {
|
|
this.subscribeTopics.push(windowContact.getStateFeedbackTopic())
|
|
this.windowContactMap[windowContact.getStateFeedbackTopic()] = { windowContact: windowContact, state: 'unknown' }
|
|
})
|
|
}
|
|
|
|
processMessage(topic: string, payload: string) : void {
|
|
let setTemperature : boolean = false
|
|
if (topic == this.temperatureTopic) {
|
|
this.temperature = parseFloat(payload)
|
|
setTemperature = true
|
|
} else if (topic == this.deviceFeedbackTopic) {
|
|
// this.temperature = parseFloat(payload)
|
|
setTemperature = false
|
|
} else if (topic in this.windowContactMap) {
|
|
this.windowContactMap[topic].state = payload
|
|
this.windowOpen = false
|
|
Object.values(this.windowContactMap).forEach((w) => {
|
|
if (w.state == 'OPEN') {
|
|
this.windowOpen = true
|
|
}
|
|
})
|
|
setTemperature = true
|
|
}
|
|
|
|
if (setTemperature) {
|
|
if (! this.windowOpen) {
|
|
mqttHandler.send(this.temperatureFeedbackTopic, `${this.temperature}`)
|
|
mqttHandler.send(this.actionTopic, `${this.temperature}`)
|
|
} else {
|
|
mqttHandler.send(this.temperatureFeedbackTopic, `${WINDOW_OPEN_TEMPERATURE}`)
|
|
mqttHandler.send(this.actionTopic, `${WINDOW_OPEN_TEMPERATURE}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|