preset fix

This commit is contained in:
2018-04-06 23:38:08 +02:00
parent c503e31ee5
commit a5c629e331
8 changed files with 36 additions and 8 deletions

4
dist/AItem.js vendored
View File

@ -15,6 +15,8 @@ class AItem {
} }
this.topicFirstPart = `dispatcher_ng/items/${this.floor}/${this.room}/${this.item}`; this.topicFirstPart = `dispatcher_ng/items/${this.floor}/${this.room}/${this.item}`;
} }
startFunc() {
}
exportItem() { exportItem() {
return null; return null;
} }
@ -22,7 +24,7 @@ class AItem {
MqttDispatcher_1.mqttHandler.register(this.subscribeTopics, (topic, payload) => { MqttDispatcher_1.mqttHandler.register(this.subscribeTopics, (topic, payload) => {
// logger.info(`item ${this.itemId}: ${topic}, ${payload}`) // logger.info(`item ${this.itemId}: ${topic}, ${payload}`)
this.processMessage(topic, payload); this.processMessage(topic, payload);
}); }, () => { this.startFunc(); });
} }
} }
exports.AItem = AItem; exports.AItem = AItem;

View File

@ -36,7 +36,10 @@ class MaxThermostat extends AHomegearItem_1.AHomegearItem {
}); });
} }
setPresetTemperature(presetTemperature) { setPresetTemperature(presetTemperature) {
this.processMessage(this.presetTemperatureTopic, `${presetTemperature}`); this.presetTemperature = presetTemperature;
}
startFunc() {
MqttDispatcher_1.mqttHandler.send(this.presetTemperatureTopic, `${this.presetTemperature}`);
} }
processMessage(topic, payload) { processMessage(topic, payload) {
let setTemperature = false; let setTemperature = false;

View File

@ -17,13 +17,15 @@ class MqttHandler {
this.mqttOptions.rejectUnauthorized = true; this.mqttOptions.rejectUnauthorized = true;
} }
this.topicHandlers = []; this.topicHandlers = [];
this.startCallbacks = [];
logger.info("MqttHandler constructed"); logger.info("MqttHandler constructed");
} }
register(topics, cb) { register(topics, cb, startCb) {
topics.forEach((topic) => { topics.forEach((topic) => {
this.topicHandlers.push({ topic: topic, callback: cb }); this.topicHandlers.push({ topic: topic, callback: cb });
logger.info(`Callback registered for ${topic}`); logger.info(`Callback registered for ${topic}`);
}); });
this.startCallbacks.push(startCb);
} }
exec() { exec() {
logger.info(`Connecting to ${this.mqttBrokerUrl}`); logger.info(`Connecting to ${this.mqttBrokerUrl}`);
@ -39,6 +41,10 @@ class MqttHandler {
logger.info(`${topicHandler.topic} subscribed`); logger.info(`${topicHandler.topic} subscribed`);
}); });
logger.info('MQTT connection established'); logger.info('MQTT connection established');
this.startCallbacks.forEach((cb) => {
cb();
logger.info("started");
});
}); });
this.mqttClient.on('message', (topic, payload, packet) => { this.mqttClient.on('message', (topic, payload, packet) => {
if (!packet.retain) { if (!packet.retain) {

2
dist/main.js vendored
View File

@ -221,7 +221,7 @@ windowContactBathroom1st.start();
allLabeledItems.push(windowContactBathroom1st); allLabeledItems.push(windowContactBathroom1st);
let thermostatBathroom1st = new MaxThermostat_1.MaxThermostat('1st', 'Bathroom', 'Thermostat', 'Thermostat Bad oben', 3, [windowContactBathroom1st]); let thermostatBathroom1st = new MaxThermostat_1.MaxThermostat('1st', 'Bathroom', 'Thermostat', 'Thermostat Bad oben', 3, [windowContactBathroom1st]);
thermostatBathroom1st.start(); thermostatBathroom1st.start();
thermostatBedroom1st.setPresetTemperature(20.0); thermostatBathroom1st.setPresetTemperature(20.0);
allLabeledItems.push(thermostatBathroom1st); allLabeledItems.push(thermostatBathroom1st);
let thermostatBathroom1stCron = new Cron_1.Cron('thermostatBathroom1stCron', thermostatBathroom1st, [ let thermostatBathroom1stCron = new Cron_1.Cron('thermostatBathroom1stCron', thermostatBathroom1st, [
{ cronTime: '00 00 06 * * 1-5', output: 'ON' }, { cronTime: '00 00 06 * * 1-5', output: 'ON' },

View File

@ -41,6 +41,10 @@ export abstract class AItem {
abstract processMessage(topic: string, payload: string) : void abstract processMessage(topic: string, payload: string) : void
startFunc() : void {
}
exportItem() : ExportType|null { exportItem() : ExportType|null {
return null return null
} }
@ -49,6 +53,6 @@ export abstract class AItem {
mqttHandler.register(this.subscribeTopics, (topic: string, payload: string) : void => { mqttHandler.register(this.subscribeTopics, (topic: string, payload: string) : void => {
// logger.info(`item ${this.itemId}: ${topic}, ${payload}`) // logger.info(`item ${this.itemId}: ${topic}, ${payload}`)
this.processMessage(topic, payload) this.processMessage(topic, payload)
}) }, () => { this.startFunc() })
} }
} }

View File

@ -61,7 +61,11 @@ export class MaxThermostat extends AHomegearItem implements HasInTopic {
} }
setPresetTemperature(presetTemperature: number) { setPresetTemperature(presetTemperature: number) {
this.processMessage(this.presetTemperatureTopic, `${presetTemperature}`) this.presetTemperature = presetTemperature
}
startFunc() : void {
mqttHandler.send(this.presetTemperatureTopic, `${this.presetTemperature}`)
} }
processMessage(topic: string, payload: string) : void { processMessage(topic: string, payload: string) : void {

View File

@ -7,6 +7,7 @@ import { IClientPublishOptions } from 'mqtt';
export type TopicCallbackFunc = (topic: string, payload: string) => void export type TopicCallbackFunc = (topic: string, payload: string) => void
export type StartCallbackFunc = () => void
export interface TopicHandler { export interface TopicHandler {
topic: string, topic: string,
@ -14,11 +15,13 @@ export interface TopicHandler {
} }
class MqttHandler { class MqttHandler {
private mqttClient: Mqtt.Client private mqttClient: Mqtt.Client
private mqttOptions: Mqtt.IClientOptions = {} private mqttOptions: Mqtt.IClientOptions = {}
private mqttBrokerUrl: string private mqttBrokerUrl: string
private topicHandlers: TopicHandler[] private topicHandlers: TopicHandler[]
private startCallbacks: StartCallbackFunc[]
constructor() { constructor() {
this.mqttBrokerUrl = config.dict.brokerUrl this.mqttBrokerUrl = config.dict.brokerUrl
@ -33,15 +36,17 @@ class MqttHandler {
} }
this.topicHandlers = [] this.topicHandlers = []
this.startCallbacks = []
logger.info("MqttHandler constructed") logger.info("MqttHandler constructed")
} }
register(topics: string[], cb: TopicCallbackFunc) : void { register(topics: string[], cb: TopicCallbackFunc, startCb: StartCallbackFunc) : void {
topics.forEach((topic) => { topics.forEach((topic) => {
this.topicHandlers.push({topic: topic, callback: cb}) this.topicHandlers.push({topic: topic, callback: cb})
logger.info(`Callback registered for ${topic}`) logger.info(`Callback registered for ${topic}`)
}) })
this.startCallbacks.push(startCb)
} }
exec() : void { exec() : void {
@ -59,6 +64,10 @@ class MqttHandler {
logger.info(`${topicHandler.topic} subscribed`) logger.info(`${topicHandler.topic} subscribed`)
}) })
logger.info('MQTT connection established') logger.info('MQTT connection established')
this.startCallbacks.forEach((cb) => {
cb()
logger.info("started")
})
}) })
this.mqttClient.on('message', (topic: string, payload: Buffer, packet : Mqtt.IPublishPacket): void => { this.mqttClient.on('message', (topic: string, payload: Buffer, packet : Mqtt.IPublishPacket): void => {
if (! packet.retain) { if (! packet.retain) {

View File

@ -288,7 +288,7 @@ allLabeledItems.push(windowContactBathroom1st)
let thermostatBathroom1st = new MaxThermostat('1st', 'Bathroom', 'Thermostat', 'Thermostat Bad oben', 3, [windowContactBathroom1st]) let thermostatBathroom1st = new MaxThermostat('1st', 'Bathroom', 'Thermostat', 'Thermostat Bad oben', 3, [windowContactBathroom1st])
thermostatBathroom1st.start() thermostatBathroom1st.start()
thermostatBedroom1st.setPresetTemperature(20.0) thermostatBathroom1st.setPresetTemperature(20.0)
allLabeledItems.push(thermostatBathroom1st) allLabeledItems.push(thermostatBathroom1st)
let thermostatBathroom1stCron = new Cron('thermostatBathroom1stCron', thermostatBathroom1st, [ let thermostatBathroom1stCron = new Cron('thermostatBathroom1stCron', thermostatBathroom1st, [