diff --git a/dist/RelayBox.js b/dist/RelayBox.js new file mode 100644 index 0000000..0b1b092 --- /dev/null +++ b/dist/RelayBox.js @@ -0,0 +1,42 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const logger = require("./log"); +const MqttDispatcher_1 = require("./MqttDispatcher"); +const AItem_1 = require("./AItem"); +class RelayBoxThing extends AItem_1.AItem { + constructor(floor, room, item, deviceCommandTopic, deviceStatusTopic, itemNames) { + super(floor, room, item, ''); + this.itemNames = itemNames; + this.deviceCommandTopic = deviceCommandTopic; + this.deviceStatusTopic = deviceStatusTopic; + this.stateTopicPre = `${this.topicFirstPart}/state`; + this.subscribeTopics = [ + `${this.deviceStatusTopic}`, + `${this.stateTopicPre}/#` + ]; + } + processMessage(topic, payload) { + logger.info(`RT: ${topic}, ${payload}`); + if (topic == this.deviceStatusTopic) { + logger.info(`RT: status received`); + } + else { + let thingRelatedPart = topic.substring(this.stateTopicPre.length + 1); + let itemIdx = parseInt(thingRelatedPart); + logger.info(`RT: pre: ${this.stateTopicPre}, thingRelatedPart: ${thingRelatedPart}, itemIdx: ${itemIdx}`); + if (itemIdx >= 0 && itemIdx < this.itemNames.length) { + if (payload == 'ON') { + MqttDispatcher_1.mqttHandler.send(this.deviceCommandTopic, `switch ${itemIdx} on`); + } + else { + MqttDispatcher_1.mqttHandler.send(this.deviceCommandTopic, `switch ${itemIdx} off`); + } + } + else { + logger.warn(`RT: no handling available for ${topic}`); + } + } + } +} +exports.RelayBoxThing = RelayBoxThing; +//# sourceMappingURL=RelayBox.js.map \ No newline at end of file diff --git a/dist/main.js b/dist/main.js index d1221fa..37dd4d5 100644 --- a/dist/main.js +++ b/dist/main.js @@ -19,6 +19,7 @@ const UrlSwitchItem_1 = require("./UrlSwitchItem"); const Cron_1 = require("./Cron"); const HueColorBulbItem_1 = require("./HueColorBulbItem"); const TouchSwitchMultiButtonThing_1 = require("./TouchSwitchMultiButtonThing"); +const RelayBox_1 = require("./RelayBox"); logger.info("Dispatcher starting"); let allLabeledItems = new Array(); // Anna ----------------------------------------------------------------------------------------------------- @@ -249,6 +250,11 @@ let thermostatKitchenCron = new Cron_1.Cron('thermostatKitchenCron', thermostatK ]); thermostatKitchenCron.start(); // ---------------------------------------------------------------------------------------------------------- +let relayBox = new RelayBox_1.RelayBoxThing('base', 'labor', 'relaybox', 'IoT/Command/RelayBox', 'IoT/Status/RelayBox', [ + "Küche", "Herd", "Waschküche", "Licht Keller", "Licht EG", "Licht OG" +]); +relayBox.start(); +// ---------------------------------------------------------------------------------------------------------- let testFourButton = new HomematicFourButtonThing_1.HomematicFourButtonThing('Gnd', 'Hallway', 'TestButton', 9, [ new HomematicFourButtonThing_1.HomematicFourButtonSingleItem('dispatcher_ng/items/Gnd/Hallway/Testlight/dimmerIn'), new HomematicFourButtonThing_1.HomematicFourButtonSingleItem('dispatcher_ng/items/Gnd/Hallway/DeskLight/timerIn'), diff --git a/src/RelayBox.js b/src/RelayBox.js deleted file mode 100644 index 49e5acb..0000000 --- a/src/RelayBox.js +++ /dev/null @@ -1,28 +0,0 @@ -let mqtt = require('./mqttHandler'); -let logger = require('./log'); - - - -class Switch { - -} - -class RelayBox { - constructor(switches) { - this.switches = switches; - - } - - start() { - mqtt.register(this.subscribeTopics, (topic, payload) => { - logger.info(`item ${this.itemId}: ${topic}, ${payload}`) - this.processMessage(topic, payload); - }); - } -} - -module.exports = { - RelayBox, - Switch -}; - diff --git a/src/RelayBox.ts b/src/RelayBox.ts new file mode 100644 index 0000000..fc8c1ba --- /dev/null +++ b/src/RelayBox.ts @@ -0,0 +1,47 @@ +import * as logger from './log' +import { mqttHandler } from './MqttDispatcher' +import { AItem } from './AItem' + + + +export class RelayBoxThing extends AItem { + private itemNames: string[] + private readonly deviceCommandTopic: string + private readonly deviceStatusTopic: string + private readonly stateTopicPre: string + + constructor(floor: string, room: string, item: string, deviceCommandTopic: string, + deviceStatusTopic: string, itemNames: string[]) { + super(floor, room, item, '') + this.itemNames = itemNames + this.deviceCommandTopic = deviceCommandTopic + this.deviceStatusTopic = deviceStatusTopic + this.stateTopicPre = `${this.topicFirstPart}/state` + this.subscribeTopics = [ + `${this.deviceStatusTopic}`, + `${this.stateTopicPre}/#` + ] + } + + processMessage(topic: string, payload: string) { + logger.info(`RT: ${topic}, ${payload}`) + if (topic == this.deviceStatusTopic) { + logger.info(`RT: status received`) + } else { + let thingRelatedPart = topic.substring(this.stateTopicPre.length+1) + let itemIdx = parseInt(thingRelatedPart) + logger.info(`RT: pre: ${this.stateTopicPre}, thingRelatedPart: ${thingRelatedPart}, itemIdx: ${itemIdx}`) + if (itemIdx >= 0 && itemIdx < this.itemNames.length) { + if (payload == 'ON') { + mqttHandler.send(this.deviceCommandTopic, `switch ${itemIdx} on`) + } else { + mqttHandler.send(this.deviceCommandTopic, `switch ${itemIdx} off`) + } + } else { + logger.warn(`RT: no handling available for ${topic}`) + } + } + } +} + + diff --git a/src/main.ts b/src/main.ts index 3f0df9d..2ebb991 100644 --- a/src/main.ts +++ b/src/main.ts @@ -21,6 +21,8 @@ import { UrlSwitchItem } from './UrlSwitchItem' import { Cron } from './Cron' import { HueColorBulbItem } from './HueColorBulbItem' import { TouchSwitchMultiButtonThing, TouchSwitchButtonSingleItem } from './TouchSwitchMultiButtonThing' +import { RelayBoxThing } from './RelayBox' + logger.info("Dispatcher starting") @@ -319,6 +321,12 @@ let thermostatKitchenCron = new Cron('thermostatKitchenCron', thermostatKitchen, ]) thermostatKitchenCron.start() +// ---------------------------------------------------------------------------------------------------------- +let relayBox = new RelayBoxThing('base', 'labor', 'relaybox', 'IoT/Command/RelayBox', 'IoT/Status/RelayBox', [ + "Küche", "Herd", "Waschküche", "Licht Keller", "Licht EG", "Licht OG" +]) +relayBox.start() + // ---------------------------------------------------------------------------------------------------------- let testFourButton = new HomematicFourButtonThing('Gnd', 'Hallway', 'TestButton', 9, [ new HomematicFourButtonSingleItem('dispatcher_ng/items/Gnd/Hallway/Testlight/dimmerIn'),