From 0d149338632c6f4bdcf31a1f05c484d837574ae9 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 27 Mar 2018 21:23:24 +0200 Subject: [PATCH] status handling --- dist/RelayBox.js | 25 ++++++++++++++++++------- src/RelayBox.ts | 26 +++++++++++++++++++------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/dist/RelayBox.js b/dist/RelayBox.js index 9154dce..bb4e4e0 100644 --- a/dist/RelayBox.js +++ b/dist/RelayBox.js @@ -10,6 +10,8 @@ class RelayBoxThing extends AItem_1.AItem { 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}/#` @@ -19,15 +21,24 @@ class RelayBoxThing extends AItem_1.AItem { // logger.info(`RT: ${topic}, ${payload}`) if (topic == this.deviceStatusTopic) { logger.info(`RT: status received`); - let status = JSON.parse(payload); - let statusParsed = JSON.stringify(status); + this.status = JSON.parse(payload); + let statusParsed = JSON.stringify(this.status); logger.info(`RT: status parsed: ${statusParsed}`); - logger.info(`RT: device: ${status.metadata.device}`); - logger.info(`RT: uptime: ${status.data.uptime}`); + 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++) { - logger.info(`RT: state: ${status.data.switchstatus[i].state}`); - logger.info(`RT: feedb: ${status.data.switchstatus[i].feedbackState}`); - logger.info(`RT: confl: ${status.data.switchstatus[i].stateConflict}`); + if (this.status.data.switchStates[i].feedbackState == '0') { + MqttDispatcher_1.mqttHandler.send(`${this.feedbackTopicPre}/${i}`, 'ON'); + } + else { + MqttDispatcher_1.mqttHandler.send(`${this.feedbackTopicPre}/${i}`, 'OFF'); + } + 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 { diff --git a/src/RelayBox.ts b/src/RelayBox.ts index 8fe1b4d..57e4551 100644 --- a/src/RelayBox.ts +++ b/src/RelayBox.ts @@ -9,6 +9,9 @@ export class RelayBoxThing extends AItem { private readonly deviceCommandTopic: string private readonly deviceStatusTopic: string private readonly stateTopicPre: string + private readonly feedbackTopicPre: string + private readonly conflictTopicPre: string + private status : any constructor(floor: string, room: string, item: string, deviceCommandTopic: string, deviceStatusTopic: string, itemNames: string[]) { @@ -17,6 +20,8 @@ export class RelayBoxThing extends AItem { 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}/#` @@ -27,15 +32,22 @@ export class RelayBoxThing extends AItem { // logger.info(`RT: ${topic}, ${payload}`) if (topic == this.deviceStatusTopic) { logger.info(`RT: status received`) - let status : any = JSON.parse(payload) - let statusParsed: string = JSON.stringify(status) + this.status = JSON.parse(payload) + let statusParsed = JSON.stringify(this.status) logger.info(`RT: status parsed: ${statusParsed}`) - logger.info(`RT: device: ${status.metadata.device}`) - logger.info(`RT: uptime: ${status.data.uptime}`) + logger.info(`RT: device: ${this.status.metadata.device}`) + logger.info(`RT: uptime: ${this.status.data.uptime}`) for (let i : number = 0; i < this.itemNames.length; i ++) { - logger.info(`RT: state: ${status.data.switchStates[i].state}`) - logger.info(`RT: feedb: ${status.data.switchStates[i].feedbackState}`) - logger.info(`RT: confl: ${status.data.switchStates[i].stateConflict}`) + if (this.status.data.switchStates[i].feedbackState == '0') { + mqttHandler.send(`${this.feedbackTopicPre}/${i}`, 'ON') + } else { + mqttHandler.send(`${this.feedbackTopicPre}/${i}`, 'OFF') + } + if (this.status.data.switchStates[i].stateConflict == '0') { + mqttHandler.send(`${this.conflictTopicPre}/${i}`, 'CLEAR') + } else { + mqttHandler.send(`${this.conflictTopicPre}/${i}`, 'CONFLICT') + } } } else { logger.info(`RT: ${topic}, ${payload}`)