status handling

This commit is contained in:
2018-03-27 21:27:59 +02:00
parent 0d14933863
commit 166f958cac
2 changed files with 40 additions and 28 deletions

View File

@ -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 {