4button
This commit is contained in:
31
src/HomematicFourButtonSingleItem.js
Normal file
31
src/HomematicFourButtonSingleItem.js
Normal 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;
|
||||
|
||||
|
31
src/HomematicFourButtonThing.js
Normal file
31
src/HomematicFourButtonThing.js
Normal 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;
|
||||
|
12
src/main.js
12
src/main.js
@ -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();
|
||||
|
@ -27,10 +27,24 @@ function start() {
|
||||
client.on('message', (topic, payload) => {
|
||||
payload = payload.toString('UTF-8');
|
||||
logger.info(`message received on topic ${topic}: ${payload}`);
|
||||
if (topic in topicCallbacks) {
|
||||
//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) });
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function send(topic, payload) {
|
||||
|
Reference in New Issue
Block a user