This commit is contained in:
Wolfgang Hottgenroth
2018-01-03 15:26:00 +01:00
parent 23fab8d33c
commit 1aadc7562e
4 changed files with 91 additions and 3 deletions

View File

@ -0,0 +1,31 @@
let logger = require('./log');
let mqtt = require('./mqttHandler');
class HomematicFourButtonSingleItem {
constructor(name, actionTopic) {
this.name = name;
this.actionTopic = actionTopic;
}
processMessage(topic, payload) {
switch(topic) {
case 'PRESS_SHORT':
mqtt.send(this.actionTopic, 'SHORT');
break;
case 'PRESS_LONG':
case 'PRESS_CONT':
mqtt.send(this.actionTopic, 'LONG_HOLD');
break;
case 'PRESS_LONG_RELEASE':
mqtt.send(this.actionTopic, 'LONG_END');
break;
default:
logger.warn(`HM4BSI: no handling available for ${topic}`);
break;
}
}
}
module.exports = HomematicFourButtonSingleItem;

View File

@ -0,0 +1,31 @@
let logger = require('./log');
let mqtt = require('./mqttHandler');
let AHomematicItem = require('./AHomematicItem')
class HomematicFourButtonThing extends AHomematicItem {
constructor(floor, room, item, hmId, itemObjs) {
super(floor, room, item, hmId);
this.itemObjs = itemObjs;
if (this.itemObjs.length != 4) {
throw new Error('itemObjs for HomematicFourButtonThing must have four elements');
}
this.subscribeTopics = [
`${this.deviceTopicPre}/#`
];
}
processMessage(topic, payload) {
logger.info(`HM4B: ${topic}, ${payload}`);
let buttonRelatedPart = topic.substring(this.deviceTopicPre.length+1);
let buttonIdx = parseInt(buttonRelatedPart.substring(0, buttonRelatedPart.indexOf('/')));
if (buttonIdx >= 1 && buttonIdx <= 4) {
this.itemObjs[buttonIdx-1].processMessage(buttonRelatedPart.substring(buttonRelatedPart.indexOf('/')+1), payload);
} else {
logger.warn(`HM4B: no handling available for ${topic}`);
}
}
}
module.exports = HomematicFourButtonThing;

View File

@ -7,6 +7,9 @@ logger.info("Hello world!");
let HomematicDimmerItemClass = require('./HomematicDimmerItem');
let M433SwitchItem = require('./M433SwitchItem');
let HomematicFourButtonThing = require('./HomematicFourButtonThing');
let HomematicFourButtonSingleItem = require('./HomematicFourButtonSingleItem');
let item2 = new HomematicDimmerItemClass('Gnd', 'Hallway', 'Testlight', 8);
item2.start();
@ -17,5 +20,14 @@ aquariumLight.start();
let deskLight = new M433SwitchItem('Gnd', 'Hallway', 'DeskLight', '83221 24 1', '83220 24 1');
deskLight.start();
let testFourButton = new HomematicFourButtonThing('Gnd', 'Hallway', 'TestButton', 9, [
new HomematicFourButtonSingleItem('button1', 'test/button/1'),
new HomematicFourButtonSingleItem('button2', 'test/button/2'),
new HomematicFourButtonSingleItem('button3', 'test/button/3'),
new HomematicFourButtonSingleItem('button4', 'test/button/4')
])
testFourButton.start();
mqtt.start();

View File

@ -27,9 +27,23 @@ function start() {
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) });
}
//if (topic in topicCallbacks) {
// topicCallbacks[topic].forEach((cb) => { cb(topic, payload) });
//}
Object.keys(topicCallbacks).forEach((subscribedTopic) => {
// logger.warn(`Test: ${subscribedTopic}, ${topic}`);
// console.log(`Test: ${subscribedTopic}, ${topic}`);
if (subscribedTopic == topic) {
// logger.warn('1');
topicCallbacks[topic].forEach((cb) => { cb(topic, payload) });
} else if (subscribedTopic.endsWith('#') &&
(subscribedTopic.substring(0, subscribedTopic.length-1) ==
topic.substring(0, subscribedTopic.length-1))) {
// logger.warn('2');
// console.log('2');
topicCallbacks[subscribedTopic].forEach((cb) => { cb(topic, payload) });
}
});
});
}