Files
dispatcher_ng/src/MaxThermostat.ts

132 lines
5.4 KiB
TypeScript

import * as logger from './log'
import { mqttHandler } from './MqttDispatcher'
import { HasInTopic } from './AItem'
import { AHomegearItem } from './AHomegearItem'
// import { MaxWindowContact } from './MaxWindowContact';
import { ThermostatExport, HeatingMainSwitchExport, ExportType } from './Export'
import { Disabler } from './Disabler'
const DISABLED_TEMPERATURE: number = 5.0
type DisabledHolder = {
disabler : Disabler
state : string
}
export class MaxThermostat extends AHomegearItem implements HasInTopic {
private actionTopic: string
private deviceFeedbackTopic: string
private temperatureFeedbackTopic: string
private temperatureTopic: string
private temperature: number
private presetTemperatureFeedbackTopic: string
private presetTemperatureTopic: string
private presetTemperature: number
private hardDisablerMap: { [key:string]: DisabledHolder }
private hardDisabled: boolean
private commandTopic: string
static heatingMainSwitchTopic : string = 'dispatcher_ng/items/heatingMainSwitch'
private heatingMainFlag: boolean = false
// Thermostat: homegear/instance1/set/3/1/SET_TEMPERATURE
getInTopic() : string {
return this.commandTopic
}
exportItem() : ExportType|null {
return ThermostatExport(this.itemId, this.label, this.temperatureTopic, this.temperatureFeedbackTopic, this.presetTemperatureTopic, this.presetTemperatureFeedbackTopic)
}
static exportHeatingMainSwitchItem() : ExportType|null {
return HeatingMainSwitchExport(MaxThermostat.heatingMainSwitchTopic)
}
constructor(floor: string, room: string, item: string, label: string, hmId: number, hardDisablers: Disabler[]) {
super(floor, room, item, label, hmId)
this.temperatureTopic = `${this.topicFirstPart}/temperature`
this.temperatureFeedbackTopic = `${this.topicFirstPart}/temperature/feedback`
this.presetTemperatureTopic = `${this.topicFirstPart}/presetTemperature`
this.presetTemperatureFeedbackTopic = `${this.topicFirstPart}/presetTemperature/feedback`
this.deviceFeedbackTopic = `${this.deviceTopicPre}/1/SET_TEMPERATURE`
this.actionTopic = `${this.actionTopicPre}/1/SET_TEMPERATURE`
this.commandTopic = `${this.topicFirstPart}/command`
this.subscribeTopics = [
this.temperatureTopic,
this.presetTemperatureTopic,
this.deviceFeedbackTopic,
this.commandTopic,
MaxThermostat.heatingMainSwitchTopic
]
this.hardDisabled = false
this.hardDisablerMap = {}
hardDisablers.forEach((hardDisabler) => {
this.subscribeTopics.push(hardDisabler.getStateFeedbackTopic())
this.hardDisablerMap[hardDisabler.getStateFeedbackTopic()] = { disabler: hardDisabler, state: 'unknown' }
})
}
setPresetTemperature(presetTemperature: number) {
this.presetTemperature = presetTemperature
}
startFunc() : void {
mqttHandler.send(this.presetTemperatureTopic, `${this.presetTemperature}`)
}
processMessage(topic: string, payload: string) : void {
let setTemperature : boolean = false
if (topic == this.temperatureTopic) {
this.temperature = parseFloat(payload)
setTemperature = true
} else if (topic == this.commandTopic) {
if ((payload == 'ON') && this.heatingMainFlag) {
this.temperature = this.presetTemperature
setTemperature = true
} else if (payload == 'OFF') {
this.temperature = DISABLED_TEMPERATURE
setTemperature = true
} else if (payload == 'FORCE_ON') {
this.temperature = this.presetTemperature
setTemperature = true
}
} else if (topic == MaxThermostat.heatingMainSwitchTopic) {
this.heatingMainFlag = (payload == 'ON')
logger.info(`${this.itemId} heating main: ${this.heatingMainFlag}`)
if (! this.heatingMainFlag) {
this.temperature = DISABLED_TEMPERATURE
mqttHandler.send(this.temperatureFeedbackTopic, `${this.temperature}`)
mqttHandler.send(this.actionTopic, `${this.temperature}`)
}
} else if (topic == this.presetTemperatureTopic) {
this.presetTemperature = parseFloat(payload)
mqttHandler.send(this.presetTemperatureFeedbackTopic, `${this.presetTemperature}`)
} else if (topic == this.deviceFeedbackTopic) {
// this.temperature = parseFloat(payload)
setTemperature = false
} else if (topic in this.hardDisablerMap) {
this.hardDisablerMap[topic].state = this.hardDisablerMap[topic].disabler.transform(payload)
this.hardDisabled = false
Object.values(this.hardDisablerMap).forEach((w) => {
if (w.state == 'DISABLE') {
this.hardDisabled = true
}
})
setTemperature = true
}
if (setTemperature) {
if (! this.hardDisabled) {
mqttHandler.send(this.temperatureFeedbackTopic, `${this.temperature}`)
mqttHandler.send(this.actionTopic, `${this.temperature}`)
} else {
mqttHandler.send(this.temperatureFeedbackTopic, `${DISABLED_TEMPERATURE}`)
mqttHandler.send(this.actionTopic, `${DISABLED_TEMPERATURE}`)
}
}
}
}