"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const logger = require("./log"); const MqttDispatcher_1 = require("./MqttDispatcher"); const AItem_1 = require("./AItem"); const Export_1 = require("./Export"); 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.feedbackTopicPre = `${this.topicFirstPart}/feedback`; this.conflictTopicPre = `${this.topicFirstPart}/conflict`; this.subscribeTopics = [ `${this.deviceStatusTopic}`, `${this.stateTopicPre}/#` ]; this.oldSwitchStates = ''; } exportItem() { return Export_1.RelayBoxExport(this.itemId, this.stateTopicPre, this.feedbackTopicPre, this.conflictTopicPre, this.itemNames); } processMessage(topic, payload) { // logger.info(`RT: ${topic}, ${payload}`) if (topic == this.deviceStatusTopic) { // logger.info(`RT: status received`) this.status = JSON.parse(payload); let statusParsed = JSON.stringify(this.status); this.switchStates = JSON.stringify(this.status.data.switchStates); if (this.switchStates != this.oldSwitchStates) { this.oldSwitchStates = this.switchStates; logger.info(`RT: status parsed: ${statusParsed}`); logger.info(`RT: device: ${this.status.metadata.device}`); logger.info(`RT: uptime: ${this.status.data.uptime}`); for (let i = 0; i < this.itemNames.length; i++) { if (this.status.data.switchStates[i].feedbackState == '0') { MqttDispatcher_1.mqttHandler.send(`${this.feedbackTopicPre}/${i}`, 'OFF'); } else { MqttDispatcher_1.mqttHandler.send(`${this.feedbackTopicPre}/${i}`, 'ON'); } if (this.status.data.switchStates[i].stateConflict == '0') { MqttDispatcher_1.mqttHandler.send(`${this.conflictTopicPre}/${i}`, 'CLEAR'); } else { MqttDispatcher_1.mqttHandler.send(`${this.conflictTopicPre}/${i}`, 'CONFLICT'); } } } } else { logger.info(`RT: ${topic}, ${payload}`); 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