58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
import * as logger from './log'
|
|
import { mqttHandler } from './MqttDispatcher'
|
|
import { AItem } from './AItem'
|
|
|
|
|
|
export class TouchSwitchButtonSingleItem {
|
|
private actionTopic: string
|
|
|
|
constructor(actionTopic: string) {
|
|
this.actionTopic = actionTopic
|
|
}
|
|
|
|
processMessage(topic: string, payload: string) {
|
|
switch(topic) {
|
|
case 'SHORT':
|
|
mqttHandler.send(this.actionTopic, 'SHORT', true)
|
|
break
|
|
case 'LONG_BEGIN':
|
|
case 'LONG_CONT':
|
|
mqttHandler.send(this.actionTopic, 'LONG_HOLD', true)
|
|
break
|
|
case 'LONG_END':
|
|
mqttHandler.send(this.actionTopic, 'LONG_END', true)
|
|
break
|
|
default:
|
|
logger.warn(`TWBSI: no handling available for ${topic}`)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
export class TouchSwitchMultiButtonThing extends AItem {
|
|
private itemObjs: TouchSwitchButtonSingleItem[]
|
|
private readonly deviceTopicPre: string
|
|
|
|
constructor(floor: string, room: string, item: string, itemObjs: TouchSwitchButtonSingleItem[]) {
|
|
super(floor, room, item, '')
|
|
this.itemObjs = itemObjs
|
|
this.deviceTopicPre = `IoT/Touchswitch/${this.floor}/${this.room}/${this.item}`
|
|
this.subscribeTopics = [
|
|
`${this.deviceTopicPre}/#`
|
|
]
|
|
}
|
|
|
|
processMessage(topic: string, payload: string) {
|
|
logger.info(`TSMBT: ${topic}, ${payload}`)
|
|
let buttonRelatedPart = topic.substring(this.deviceTopicPre.length+1)
|
|
let buttonIdx = parseInt(buttonRelatedPart.substring(0, buttonRelatedPart.indexOf('/')))
|
|
if (buttonIdx >= 1 && buttonIdx <= this.itemObjs.length) {
|
|
this.itemObjs[buttonIdx-1].processMessage(buttonRelatedPart.substring(buttonRelatedPart.indexOf('/')+1), payload)
|
|
} else {
|
|
logger.warn(`TSMBT: no handling available for ${topic}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
|