159 lines
6.8 KiB
TypeScript
159 lines
6.8 KiB
TypeScript
import * as logger from './log'
|
|
import { mqttHandler } from './MqttDispatcher'
|
|
import { AHomegearItem } from './AHomegearItem'
|
|
import { HueColorLightExport, ExportType } from './Export'
|
|
import { HasStateAndFeedbackTopic, HasInTopic } from './AItem'
|
|
|
|
export class HueColorBulbItem extends AHomegearItem implements HasStateAndFeedbackTopic{
|
|
private readonly BRIGHT_FACTOR: number = 2.54
|
|
private readonly HUE_FACTOR: number = (65535 / 360)
|
|
private readonly SATURATION_FACTOR: number = 2.54
|
|
private bright: number
|
|
private colorTemperature: number
|
|
private hue: number
|
|
private saturation: number
|
|
private state: string
|
|
private stateActionTopic: string
|
|
private brightActionTopic: string
|
|
private colorTemperatureActionTopic: string
|
|
private hueActionTopic: string
|
|
private saturationActionTopic: string
|
|
private brightFeedbackTopic: string
|
|
private stateFeedbackTopic: string
|
|
private colorTemperatureFeedbackTopic: string
|
|
private hueFeedbackTopic: string
|
|
private saturationFeedbackTopic: string
|
|
private brightTopic: string
|
|
private stateTopic: string
|
|
private hueTopic: string
|
|
private saturationTopic: string
|
|
private colorTemperatureTopic: string
|
|
private stateDeviceTopic: string
|
|
private brightDeviceTopic: string
|
|
private colorTemperatureDeviceTopic: string
|
|
private hueDeviceTopic: string
|
|
private saturationDeviceTopic: string
|
|
|
|
getStateTopic() : string {
|
|
return this.stateTopic
|
|
}
|
|
|
|
getInTopic() : string {
|
|
return this.stateTopic
|
|
}
|
|
|
|
getStateFeedbackTopic() : string {
|
|
return this.stateFeedbackTopic
|
|
}
|
|
|
|
constructor(floor: string, room: string, item: string, label: string, hmId: number) {
|
|
super(floor, room, item, label, hmId)
|
|
this.stateTopic = `${this.topicFirstPart}/state`
|
|
this.brightTopic = `${this.topicFirstPart}/bright`
|
|
this.colorTemperatureTopic = `${this.topicFirstPart}/colorTemperature`
|
|
this.hueTopic = `${this.topicFirstPart}/hue`
|
|
this.saturationTopic = `${this.topicFirstPart}/saturation`
|
|
this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback`
|
|
this.brightFeedbackTopic = `${this.topicFirstPart}/bright/feedback`
|
|
this.hueFeedbackTopic = `${this.topicFirstPart}/bright/hue`
|
|
this.saturationFeedbackTopic = `${this.topicFirstPart}/bright/saturation`
|
|
this.colorTemperatureFeedbackTopic = `${this.topicFirstPart}/colorTemperature/feedback`
|
|
this.stateActionTopic = `${this.actionTopicPre}/1/STATE`
|
|
this.brightActionTopic = `${this.actionTopicPre}/1/FAST_BRIGHTNESS`
|
|
this.hueActionTopic = `${this.actionTopicPre}/1/HUE`
|
|
this.saturationActionTopic = `${this.actionTopicPre}/1/SATURATION`
|
|
this.colorTemperatureActionTopic = `${this.actionTopicPre}/1/COLOR_TEMPERATURE`
|
|
this.stateDeviceTopic = `${this.deviceTopicPre}/1/STATE`
|
|
this.brightDeviceTopic = `${this.deviceTopicPre}/1/FAST_BRIGHTNESS`
|
|
this.hueDeviceTopic = `${this.deviceTopicPre}/1/HUE`
|
|
this.saturationDeviceTopic = `${this.deviceTopicPre}/1/SATURATION`
|
|
this.colorTemperatureDeviceTopic = `${this.deviceTopicPre}/1/COLOR_TEMPERATURE`
|
|
this.subscribeTopics = [
|
|
this.stateTopic,
|
|
this.brightTopic,
|
|
this.colorTemperatureTopic,
|
|
this.hueTopic,
|
|
this.saturationTopic,
|
|
this.stateDeviceTopic,
|
|
this.brightDeviceTopic,
|
|
this.colorTemperatureDeviceTopic,
|
|
this.hueDeviceTopic,
|
|
this.saturationDeviceTopic
|
|
]
|
|
this.state = 'OFF'
|
|
this.bright = 0
|
|
this.colorTemperature = 0
|
|
this.hue = 0
|
|
this.saturation = 0
|
|
}
|
|
|
|
exportItem() : ExportType|null {
|
|
return HueColorLightExport(this.itemId, this.label,
|
|
this.stateTopic, this.stateFeedbackTopic,
|
|
this.brightTopic, this.brightFeedbackTopic,
|
|
this.hueTopic, this.hueFeedbackTopic,
|
|
this.saturationTopic, this.saturationFeedbackTopic,
|
|
this.colorTemperatureTopic, this.colorTemperatureFeedbackTopic)
|
|
}
|
|
|
|
|
|
processMessage(topic: string, payload: string) : void {
|
|
switch (topic) {
|
|
case this.stateTopic:
|
|
this.state = payload
|
|
mqttHandler.send(this.stateFeedbackTopic, this.state)
|
|
if (this.state == "ON") {
|
|
mqttHandler.send(this.stateActionTopic, "true")
|
|
} else {
|
|
mqttHandler.send(this.stateActionTopic, "false")
|
|
}
|
|
break
|
|
case this.stateDeviceTopic:
|
|
this.state = payload
|
|
if (payload == "true") {
|
|
this.state = "ON"
|
|
} else {
|
|
this.state = "OFF"
|
|
}
|
|
mqttHandler.send(this.stateFeedbackTopic, this.state)
|
|
break
|
|
case this.brightTopic:
|
|
this.bright = parseFloat(payload)
|
|
mqttHandler.send(this.brightFeedbackTopic, `${this.bright}`)
|
|
mqttHandler.send(this.brightActionTopic, `${this.bright * this.BRIGHT_FACTOR}`)
|
|
break
|
|
case this.brightDeviceTopic:
|
|
this.bright = parseFloat(payload) / this.BRIGHT_FACTOR
|
|
mqttHandler.send(this.brightFeedbackTopic, `${this.bright}`)
|
|
break
|
|
case this.hueTopic:
|
|
this.hue = parseFloat(payload)
|
|
mqttHandler.send(this.hueFeedbackTopic, `${this.hue}`)
|
|
mqttHandler.send(this.hueActionTopic, `${this.hue * this.HUE_FACTOR}`)
|
|
break
|
|
case this.hueDeviceTopic:
|
|
this.hue = parseFloat(payload) / this.HUE_FACTOR
|
|
mqttHandler.send(this.hueFeedbackTopic, `${this.hue}`)
|
|
break
|
|
case this.saturationTopic:
|
|
this.saturation = parseFloat(payload)
|
|
mqttHandler.send(this.saturationFeedbackTopic, `${this.saturation}`)
|
|
mqttHandler.send(this.saturationActionTopic, `${this.saturation * this.SATURATION_FACTOR}`)
|
|
break
|
|
case this.saturationDeviceTopic:
|
|
this.saturation = parseFloat(payload) / this.SATURATION_FACTOR
|
|
mqttHandler.send(this.saturationFeedbackTopic, `${this.saturation}`)
|
|
break
|
|
case this.colorTemperatureTopic:
|
|
this.colorTemperature = parseInt(payload)
|
|
mqttHandler.send(this.colorTemperatureFeedbackTopic, `${this.colorTemperature}`)
|
|
mqttHandler.send(this.colorTemperatureActionTopic, `${this.colorTemperature}`)
|
|
break
|
|
case this.colorTemperatureDeviceTopic:
|
|
this.colorTemperature = parseInt(payload)
|
|
mqttHandler.send(this.colorTemperatureFeedbackTopic, `${this.colorTemperature}`)
|
|
break
|
|
}
|
|
}
|
|
}
|