relaybox stuff

This commit is contained in:
2018-03-27 20:44:49 +02:00
parent 7992ccd7a1
commit 71f00fdf31
5 changed files with 103 additions and 28 deletions

47
src/RelayBox.ts Normal file
View File

@ -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}`)
}
}
}
}