relaybox stuff
This commit is contained in:
47
src/RelayBox.ts
Normal file
47
src/RelayBox.ts
Normal 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}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user