refactoring
This commit is contained in:
14
src/AHomematicItem.js
Normal file
14
src/AHomematicItem.js
Normal 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;
|
||||||
|
|
11
src/AItem.js
11
src/AItem.js
@ -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;
|
||||||
|
@ -1,33 +1,33 @@
|
|||||||
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) => {
|
|
||||||
payload = payload.toString('UTF-8');
|
|
||||||
logger.info(`item ${this.itemId}: ${topic}, ${payload}`)
|
|
||||||
if (topic == this.stateTopic) {
|
|
||||||
this.state = payload;
|
|
||||||
mqtt.send(this.stateFeedbackTopic, this.state);
|
|
||||||
} else if (topic == this.brightTopic) {
|
|
||||||
this.bright = payload;
|
|
||||||
mqtt.send(this.brightFeedbackTopic, this.bright);
|
|
||||||
}
|
|
||||||
if ((this.state != this.oldState) || (this.bright != this.oldBright)) {
|
if ((this.state != this.oldState) || (this.bright != this.oldBright)) {
|
||||||
if (this.state == 'ON') {
|
if (this.state == 'ON') {
|
||||||
mqtt.send(this.actionTopic, `${this.bright}`);
|
mqtt.send(this.actionTopic, `${this.bright}`);
|
||||||
@ -37,7 +37,28 @@ class HomematicDimmerItem extends AItem {
|
|||||||
this.oldState = this.state;
|
this.oldState = this.state;
|
||||||
this.oldBright = this.bright;
|
this.oldBright = this.bright;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
|
processMessage(topic, payload) {
|
||||||
|
switch (topic) {
|
||||||
|
case this.stateTopic:
|
||||||
|
this.state = payload;
|
||||||
|
mqtt.send(this.stateFeedbackTopic, this.state);
|
||||||
|
this.dimmerAction();
|
||||||
|
break;
|
||||||
|
case this.brightTopic:
|
||||||
|
this.bright = payload;
|
||||||
|
mqtt.send(this.brightFeedbackTopic, this.bright);
|
||||||
|
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}`);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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');
|
|
||||||
});
|
|
||||||
|
|
@ -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,10 +16,7 @@ class M433SwitchItem extends AItem {
|
|||||||
this.offCode = offCode;
|
this.offCode = offCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
processMessage(topic, payload) {
|
||||||
mqtt.register([this.stateTopic], (topic, payload) => {
|
|
||||||
payload = payload.toString('UTF-8');
|
|
||||||
logger.info(`item ${this.itemId}: ${topic}, ${payload}`)
|
|
||||||
this.state = payload;
|
this.state = payload;
|
||||||
mqtt.send(this.stateFeedbackTopic, this.state);
|
mqtt.send(this.stateFeedbackTopic, this.state);
|
||||||
if (this.state != this.oldState) {
|
if (this.state != this.oldState) {
|
||||||
@ -29,7 +27,6 @@ class M433SwitchItem extends AItem {
|
|||||||
}
|
}
|
||||||
this.oldState = this.state;
|
this.oldState = this.state;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
src/main.js
11
src/main.js
@ -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();
|
||||||
|
|
||||||
|
|
||||||
|
@ -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) });
|
||||||
|
Reference in New Issue
Block a user