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 {
constructor(floor, room, item) {
this.floor = floor;
@ -6,6 +10,13 @@ class AItem {
this.itemId = `${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;

View File

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

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

View File

@ -4,18 +4,17 @@ let mqtt = require('./mqttHandler');
logger.info("Hello world!");
require('./item1');
let homematicDimmerItemClass = require('./homematicDimmerItem');
let m433SwitchItem = require('./m433SwitchItem');
let HomematicDimmerItemClass = require('./HomematicDimmerItem');
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();
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();
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();

View File

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