refactoring

This commit is contained in:
Wolfgang Hottgenroth
2018-01-02 21:51:05 +01:00
parent 1c06b0f3a3
commit 0bcc75f53f
7 changed files with 84 additions and 50 deletions

14
src/AHomematicItem.js Normal file
View File

@ -0,0 +1,14 @@
let AItem = require('./AItem')
class AHomematicItem extends AItem {
constructor(floor, room, item, hmId) {
super(floor, room, item);
this.hmId = hmId;
this.actionTopicPre = `homegear/instance1/set/${this.hmId}`;
this.deviceTopicPre = `homegear/instance1/plain/${this.hmId}`;
}
}
module.exports = AHomematicItem;

View File

@ -1,3 +1,7 @@
let mqtt = require('./mqttHandler');
let logger = require('./log');
class AItem { class AItem {
constructor(floor, room, item) { constructor(floor, room, item) {
this.floor = floor; this.floor = floor;
@ -6,6 +10,13 @@ class AItem {
this.itemId = `${this.floor}.${this.room}.${this.item}`; this.itemId = `${this.floor}.${this.room}.${this.item}`;
this.topicFirstPart = `dispatcher_ng/items/${this.floor}/${this.room}/${this.item}`; this.topicFirstPart = `dispatcher_ng/items/${this.floor}/${this.room}/${this.item}`;
} }
start() {
mqtt.register(this.subscribeTopics, (topic, payload) => {
logger.info(`item ${this.itemId}: ${topic}, ${payload}`)
this.processMessage(topic, payload);
});
}
} }
module.exports = AItem; module.exports = AItem;

View File

@ -1,43 +1,64 @@
let logger = require('./log'); let logger = require('./log');
let mqtt = require('./mqttHandler'); let mqtt = require('./mqttHandler');
let AItem = require('./AItem') let AHomematicItem = require('./AHomematicItem')
class HomematicDimmerItem extends AItem { class HomematicDimmerItem extends AHomematicItem {
constructor(floor, room, item, actionTopic) { constructor(floor, room, item, hmId) {
super(floor, room, item); super(floor, room, item, hmId);
this.stateTopic = `${this.topicFirstPart}/state`; this.stateTopic = `${this.topicFirstPart}/state`;
this.brightTopic = `${this.topicFirstPart}/bright`; this.brightTopic = `${this.topicFirstPart}/bright`;
this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback`; this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback`;
this.brightFeedbackTopic = `${this.topicFirstPart}/bright/feedback`; this.brightFeedbackTopic = `${this.topicFirstPart}/bright/feedback`;
this.actionTopic = actionTopic; this.actionTopic = `${this.actionTopicPre}/1/LEVEL`;
this.errorOverheatTopic = `${this.deviceTopicPre}/1/ERROR_OVERHEAT`;
this.errorOverloadTopic = `${this.deviceTopicPre}/1/ERROR_OVERLOAD`;
this.errorReducedTopic = `${this.deviceTopicPre}/1/ERROR_REDUCED`;
this.subscribeTopics = [
this.stateTopic,
this.brightTopic,
this.errorOverheatTopic,
this.errorOverloadTopic,
this.errorReducedTopic
];
this.state = 'OFF'; this.state = 'OFF';
this.oldState = undefined; this.oldState = undefined;
this.bright = 0; this.bright = 0;
this.oldBright = undefined; this.oldBright = undefined;
} }
start() { dimmerAction() {
mqtt.register([this.stateTopic, this.brightTopic], (topic, payload) => { if ((this.state != this.oldState) || (this.bright != this.oldBright)) {
payload = payload.toString('UTF-8'); if (this.state == 'ON') {
logger.info(`item ${this.itemId}: ${topic}, ${payload}`) mqtt.send(this.actionTopic, `${this.bright}`);
if (topic == this.stateTopic) { } else {
mqtt.send(this.actionTopic, '0');
}
this.oldState = this.state;
this.oldBright = this.bright;
}
}
processMessage(topic, payload) {
switch (topic) {
case this.stateTopic:
this.state = payload; this.state = payload;
mqtt.send(this.stateFeedbackTopic, this.state); mqtt.send(this.stateFeedbackTopic, this.state);
} else if (topic == this.brightTopic) { this.dimmerAction();
break;
case this.brightTopic:
this.bright = payload; this.bright = payload;
mqtt.send(this.brightFeedbackTopic, this.bright); mqtt.send(this.brightFeedbackTopic, this.bright);
} this.dimmerAction();
if ((this.state != this.oldState) || (this.bright != this.oldBright)) { break;
if (this.state == 'ON') { case this.errorOverheatTopic:
mqtt.send(this.actionTopic, `${this.bright}`); case this.errorOverloadTopic:
} else { case this.errorReducedTopic:
mqtt.send(this.actionTopic, '0'); if (payload != 'false') {
logger.warn(`Homematic dimmer ${this.hmId}, ${this.itemId} reports: ${topic}: ${payload}`);
} }
this.oldState = this.state; break;
this.oldBright = this.bright; }
}
});
} }
} }

View File

@ -1,9 +0,0 @@
let logger = require('./log');
let mqtt = require('./mqttHandler');
mqtt.register(['dispatcher_ng/items/1/set', 'dispatcher_ng/items/1/bright'], (topic, payload) => {
logger.info(`item 1: ${topic}, ${payload}`)
mqtt.send('dispatcher_ng/items/1/feedback', 'bla');
});

View File

@ -7,6 +7,7 @@ class M433SwitchItem extends AItem {
constructor(floor, room, item, onCode, offCode) { constructor(floor, room, item, onCode, offCode) {
super(floor, room, item); super(floor, room, item);
this.stateTopic = `${this.topicFirstPart}/state`; this.stateTopic = `${this.topicFirstPart}/state`;
this.subscribeTopics = [this.stateTopic];
this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback`; this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback`;
this.actionTopic = 'IoT/Mqtt433Gateway/Message'; this.actionTopic = 'IoT/Mqtt433Gateway/Message';
this.state = 'OFF'; this.state = 'OFF';
@ -15,21 +16,17 @@ class M433SwitchItem extends AItem {
this.offCode = offCode; this.offCode = offCode;
} }
start() { processMessage(topic, payload) {
mqtt.register([this.stateTopic], (topic, payload) => { this.state = payload;
payload = payload.toString('UTF-8'); mqtt.send(this.stateFeedbackTopic, this.state);
logger.info(`item ${this.itemId}: ${topic}, ${payload}`) if (this.state != this.oldState) {
this.state = payload; if (this.state == 'ON') {
mqtt.send(this.stateFeedbackTopic, this.state); mqtt.send(this.actionTopic, this.onCode);
if (this.state != this.oldState) { } else {
if (this.state == 'ON') { mqtt.send(this.actionTopic, this.offCode);
mqtt.send(this.actionTopic, this.onCode);
} else {
mqtt.send(this.actionTopic, this.offCode);
}
this.oldState = this.state;
} }
}); this.oldState = this.state;
}
} }
} }

View File

@ -4,18 +4,17 @@ let mqtt = require('./mqttHandler');
logger.info("Hello world!"); logger.info("Hello world!");
require('./item1');
let homematicDimmerItemClass = require('./homematicDimmerItem'); let HomematicDimmerItemClass = require('./HomematicDimmerItem');
let m433SwitchItem = require('./m433SwitchItem'); let M433SwitchItem = require('./M433SwitchItem');
let item2 = new homematicDimmerItemClass('Gnd', 'Hallway', 'Testlight', 'homegear/instance1/items/8/state'); let item2 = new HomematicDimmerItemClass('Gnd', 'Hallway', 'Testlight', 8);
item2.start(); item2.start();
let aquariumLight = new m433SwitchItem('1st', 'Anna', 'AquariumLight', '14665044 24 1', '14665041 24 1'); let aquariumLight = new M433SwitchItem('1st', 'Anna', 'AquariumLight', '14665044 24 1', '14665041 24 1');
aquariumLight.start(); aquariumLight.start();
let deskLight = new m433SwitchItem('Gnd', 'Hallway', 'DeskLight', '83221 24 1', '83220 24 1'); let deskLight = new M433SwitchItem('Gnd', 'Hallway', 'DeskLight', '83221 24 1', '83220 24 1');
deskLight.start(); deskLight.start();

View File

@ -25,6 +25,7 @@ function start() {
logger.info('mqtt connection established'); logger.info('mqtt connection established');
}); });
client.on('message', (topic, payload) => { client.on('message', (topic, payload) => {
payload = payload.toString('UTF-8');
logger.info(`message received on topic ${topic}: ${payload}`); logger.info(`message received on topic ${topic}: ${payload}`);
if (topic in topicCallbacks) { if (topic in topicCallbacks) {
topicCallbacks[topic].forEach((cb) => { cb(topic, payload) }); topicCallbacks[topic].forEach((cb) => { cb(topic, payload) });