diff --git a/src/AItem.js b/src/AItem.js new file mode 100644 index 0000000..8008335 --- /dev/null +++ b/src/AItem.js @@ -0,0 +1,12 @@ +class AItem { + constructor(floor, room, item) { + this.floor = floor; + this.room = room; + this.item = item; + this.itemId = `${this.floor}.${this.room}.${this.item}`; + this.topicFirstPart = `dispatcher_ng/items/${this.floor}/${this.room}/${this.item}`; + } +} + +module.exports = AItem; + diff --git a/src/homematicDimmerItem.js b/src/homematicDimmerItem.js index f6a7b4f..f534653 100644 --- a/src/homematicDimmerItem.js +++ b/src/homematicDimmerItem.js @@ -1,15 +1,15 @@ let logger = require('./log'); let mqtt = require('./mqttHandler'); +let AItem = require('./AItem') - -class HomematicDimmerItem { - constructor(itemId, actionTopic) { - this.itemId = itemId; - this.stateTopic = `dispatcher_ng/items/${this.itemId}/state`; - this.brightTopic = `dispatcher_ng/items/${this.itemId}/bright`; - this.stateFeedbackTopic = `dispatcher_ng/items/${this.itemId}/state/feedback`; - this.brightFeedbackTopic = `dispatcher_ng/items/${this.itemId}/bright/feedback`; +class HomematicDimmerItem extends AItem { + constructor(floor, room, item, actionTopic) { + super(floor, room, item); + 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.state = 'OFF'; this.oldState = undefined; diff --git a/src/m433SwitchItem.js b/src/m433SwitchItem.js new file mode 100644 index 0000000..14b25f7 --- /dev/null +++ b/src/m433SwitchItem.js @@ -0,0 +1,37 @@ +let logger = require('./log'); +let mqtt = require('./mqttHandler'); +let AItem = require('./AItem'); + + +class M433SwitchItem extends AItem { + constructor(floor, room, item, onCode, offCode) { + super(floor, room, item); + this.stateTopic = `${this.topicFirstPart}/state`; + this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback`; + this.actionTopic = 'IoT/Mqtt433Gateway/Message'; + this.state = 'OFF'; + this.oldState = undefined; + this.onCode = onCode; + 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; + } + }); + } +} + +module.exports = M433SwitchItem; + diff --git a/src/main.js b/src/main.js index 9c16c03..c9f6ddf 100644 --- a/src/main.js +++ b/src/main.js @@ -7,8 +7,16 @@ logger.info("Hello world!"); require('./item1'); let homematicDimmerItemClass = require('./homematicDimmerItem'); +let m433SwitchItem = require('./m433SwitchItem'); -let item2 = new homematicDimmerItemClass(2, 'homegear/instance1/items/8/state'); +let item2 = new homematicDimmerItemClass('Gnd', 'Hallway', 'Testlight', 'homegear/instance1/items/8/state'); item2.start(); +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'); +deskLight.start(); + + mqtt.start();