typescriptifying
This commit is contained in:
58
src/HomematicFourButtonThing.ts
Normal file
58
src/HomematicFourButtonThing.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import * as logger from './log'
|
||||
import { mqttHandler } from './MqttDispatcher'
|
||||
import { AHomematicItem } from './AHomematicItem'
|
||||
|
||||
|
||||
export class HomematicFourButtonSingleItem {
|
||||
private actionTopic: string
|
||||
|
||||
constructor(actionTopic: string) {
|
||||
this.actionTopic = actionTopic
|
||||
}
|
||||
|
||||
processMessage(topic: string, payload: string) {
|
||||
switch(topic) {
|
||||
case 'PRESS_SHORT':
|
||||
mqttHandler.send(this.actionTopic, 'SHORT', true)
|
||||
break
|
||||
case 'PRESS_LONG':
|
||||
case 'PRESS_CONT':
|
||||
mqttHandler.send(this.actionTopic, 'LONG_HOLD', true)
|
||||
break
|
||||
case 'PRESS_LONG_RELEASE':
|
||||
mqttHandler.send(this.actionTopic, 'LONG_END', true)
|
||||
break
|
||||
default:
|
||||
logger.warn(`HM4BSI: no handling available for ${topic}`)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class HomematicFourButtonThing extends AHomematicItem {
|
||||
private itemObjs: HomematicFourButtonSingleItem[]
|
||||
|
||||
constructor(floor: string, room: string, item: string, hmId: number, itemObjs: HomematicFourButtonSingleItem[]) {
|
||||
super(floor, room, item, hmId)
|
||||
this.itemObjs = itemObjs
|
||||
if (this.itemObjs.length != 4) {
|
||||
throw new Error('itemObjs for HomematicFourButtonThing must have four elements')
|
||||
}
|
||||
this.subscribeTopics = [
|
||||
`${this.deviceTopicPre}/#`
|
||||
]
|
||||
}
|
||||
|
||||
processMessage(topic: string, payload: string) {
|
||||
logger.info(`HM4B: ${topic}, ${payload}`)
|
||||
let buttonRelatedPart = topic.substring(this.deviceTopicPre.length+1)
|
||||
let buttonIdx = parseInt(buttonRelatedPart.substring(0, buttonRelatedPart.indexOf('/')))
|
||||
if (buttonIdx >= 1 && buttonIdx <= 4) {
|
||||
this.itemObjs[buttonIdx-1].processMessage(buttonRelatedPart.substring(buttonRelatedPart.indexOf('/')+1), payload)
|
||||
} else {
|
||||
logger.warn(`HM4B: no handling available for ${topic}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user