diff --git a/dist/RelayBox.js b/dist/RelayBox.js index bb4e4e0..2b580f4 100644 --- a/dist/RelayBox.js +++ b/dist/RelayBox.js @@ -16,6 +16,7 @@ class RelayBoxThing extends AItem_1.AItem { `${this.deviceStatusTopic}`, `${this.stateTopicPre}/#` ]; + this.oldSwitchStates = ''; } processMessage(topic, payload) { // logger.info(`RT: ${topic}, ${payload}`) @@ -23,21 +24,25 @@ class RelayBoxThing extends AItem_1.AItem { logger.info(`RT: status received`); this.status = JSON.parse(payload); let statusParsed = JSON.stringify(this.status); - 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}`, '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'); + 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}`, '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'); + } } } } diff --git a/src/RelayBox.ts b/src/RelayBox.ts index 57e4551..15dc72b 100644 --- a/src/RelayBox.ts +++ b/src/RelayBox.ts @@ -12,6 +12,8 @@ export class RelayBoxThing extends AItem { private readonly feedbackTopicPre: string private readonly conflictTopicPre: string private status : any + private switchStates : string + private oldSwitchStates : string constructor(floor: string, room: string, item: string, deviceCommandTopic: string, deviceStatusTopic: string, itemNames: string[]) { @@ -26,6 +28,7 @@ export class RelayBoxThing extends AItem { `${this.deviceStatusTopic}`, `${this.stateTopicPre}/#` ] + this.oldSwitchStates = '' } processMessage(topic: string, payload: string) { @@ -34,19 +37,23 @@ export class RelayBoxThing extends AItem { logger.info(`RT: status received`) this.status = JSON.parse(payload) let statusParsed = JSON.stringify(this.status) - 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 : number = 0; i < this.itemNames.length; i ++) { - 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') + 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 : number = 0; i < this.itemNames.length; i ++) { + 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 {