typescriptifying
This commit is contained in:
56
src/DimmerAdaptor.ts
Normal file
56
src/DimmerAdaptor.ts
Normal file
@ -0,0 +1,56 @@
|
||||
import { AItem } from './AItem'
|
||||
import * as logger from './log'
|
||||
import { mqttHandler } from './MqttDispatcher'
|
||||
|
||||
|
||||
export class DimmerAdaptor extends AItem {
|
||||
private brightDirection: number
|
||||
private bright: number
|
||||
private state: string
|
||||
private inTopic: string
|
||||
private actionBrightTopic: string
|
||||
private actionStateTopic: string
|
||||
|
||||
constructor(floor: string, room: string, item: string) {
|
||||
super(floor, room, item)
|
||||
this.actionStateTopic = `${this.topicFirstPart}/state`
|
||||
this.actionBrightTopic = `${this.topicFirstPart}/bright`
|
||||
this.inTopic = `${this.topicFirstPart}/dimmerIn`
|
||||
this.subscribeTopics = [ this.inTopic ]
|
||||
this.state = 'OFF'
|
||||
this.bright = 100
|
||||
this.brightDirection = -1
|
||||
}
|
||||
|
||||
processMessage(topic: string, payload: string) {
|
||||
switch (topic) {
|
||||
case this.inTopic:
|
||||
switch (payload) {
|
||||
case 'SHORT':
|
||||
if (this.state == 'OFF') {
|
||||
this.state = 'ON'
|
||||
} else {
|
||||
this.state = 'OFF'
|
||||
}
|
||||
mqttHandler.send(this.actionStateTopic, this.state, true)
|
||||
break
|
||||
case 'LONG_HOLD':
|
||||
this.bright += (5 * this.brightDirection)
|
||||
if (this.bright > 100) {
|
||||
this.bright = 100
|
||||
}
|
||||
if (this.bright < 0) {
|
||||
this.bright = 0
|
||||
}
|
||||
mqttHandler.send(this.actionBrightTopic, this.bright.toString(), true)
|
||||
break
|
||||
case 'LONG_END':
|
||||
this.brightDirection = this.brightDirection * -1
|
||||
break
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user