mqtt: always send globally, always retain, thermostat und windowContact completed

This commit is contained in:
Wolfgang Hottgenroth
2018-01-15 17:10:42 +01:00
parent f88cf5ee0d
commit 09a63fe736
8 changed files with 179 additions and 32 deletions

View File

@ -7,14 +7,18 @@ import { MaxWindowContact } from './MaxWindowContact';
const WINDOW_OPEN_TEMPERATURE = 4.5
type WindowContactHolder = {
windowContact : MaxWindowContact
state : string
}
export class MaxThermostat extends AHomematicItem {
private actionTopic: string
private deviceFeedbackTopic: string
private temperatureFeedbackTopic: string
private temperatureTopic: string
private temperature: number
private windowContacts: MaxWindowContact[]
private windowContactTopics: string[]
private windowContactMap: { [key:string]: WindowContactHolder }
private windowOpen: boolean
// Thermostat: homegear/instance1/set/3/1/SET_TEMPERATURE
@ -28,32 +32,34 @@ export class MaxThermostat extends AHomematicItem {
this.temperatureTopic,
this.deviceFeedbackTopic
]
this.windowContacts = windowContacts
this.windowOpen = false
this.windowContactTopics = []
this.windowContacts.forEach((windowContact) => {
this.windowContactMap = {}
windowContacts.forEach((windowContact) => {
this.subscribeTopics.push(windowContact.getStateFeedbackTopic())
this.windowContactTopics.push(windowContact.getStateFeedbackTopic())
this.windowContactMap[windowContact.getStateFeedbackTopic()] = { windowContact: windowContact, state: 'unknown' }
})
}
processMessage(topic: string, payload: string) : void {
if (topic == this.temperatureTopic) {
if ((topic == this.temperatureTopic) || (topic == this.deviceFeedbackTopic)) {
this.temperature = parseFloat(payload)
mqttHandler.send(this.temperatureFeedbackTopic, `${this.temperature}`)
mqttHandler.send(this.actionTopic, `${this.temperature}`)
} else if (topic == this.deviceFeedbackTopic) {
this.temperature = parseFloat(payload)
mqttHandler.send(this.temperatureFeedbackTopic, `${this.temperature}`)
} else if (this.windowContactTopics.indexOf(topic) >= 0) {
if (payload == 'CLOSED') {
} else {
}
} 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
}
})
}
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}`)
}
}

View File

@ -2,6 +2,7 @@ import * as logger from './log'
import * as Mqtt from 'mqtt'
import * as fs from 'fs'
import * as config from './config'
import { IClientPublishOptions } from 'mqtt';
@ -97,7 +98,8 @@ class MqttHandler {
//}
//if (! sent) {
logger.info(`External sending required: ${topic}`)
this.mqttClient.publish(topic, payload)
let options : IClientPublishOptions = { retain: true, qos: 0 }
this.mqttClient.publish(topic, payload, options)
//} else {
// logger.info(`Internally delivered: ${topic}`)
//}

View File

@ -15,6 +15,8 @@ import { HomematicSwitchItem } from './HomematicSwitchItem'
import { Forwarder } from './Forwarder'
import { LightScene } from './Scene'
import { MaxEcoSwitch } from './MaxEcoSwitch'
import { MaxThermostat } from './MaxThermostat'
import { MaxWindowContact } from './MaxWindowContact'
logger.info("Dispatcher starting")
@ -211,6 +213,15 @@ let testScene = new LightScene('Gnd', 'Hallway', 'TestScene', 'TestScene',
testScene.start()
let windowContact1 = new MaxWindowContact('Gnd', 'Bathroom', 'WindowContact1', 'Fenster Bad unten', 2)
windowContact1.start()
let windowContact2 = new MaxWindowContact('Gnd', 'Bathroom', 'WindowContact2', 'Fenster Bad unten', 20)
windowContact2.start()
let thermostat1 = new MaxThermostat('Gnd', 'Bathroom', 'Thermostat', 'Thermostat Bad unten', 3, [windowContact1, windowContact2])
thermostat1.start()
// ----------------------------------------------------------------------------------------------------------
// Homekit export
let homekitObject : { [key:string]:{} } = {}