Scene implemented
This commit is contained in:
98
src/Scene.ts
Normal file
98
src/Scene.ts
Normal file
@ -0,0 +1,98 @@
|
||||
import { AItem, HasStateAndFeedbackTopic } from './AItem'
|
||||
import * as logger from './log'
|
||||
import { mqttHandler } from './MqttDispatcher'
|
||||
import { ExportType, SwitchExport } from './Export'
|
||||
|
||||
export class LightScene extends AItem {
|
||||
private onFeedbackTopics: string[]
|
||||
private offFeedbackTopics: string[]
|
||||
private onTopics: string[]
|
||||
private offTopics: string[]
|
||||
private onFeedbackMap: { [key:string]:string }
|
||||
private offFeedbackMap: { [key:string]:string }
|
||||
private state: string
|
||||
private stateFeedbackTopic: string
|
||||
private stateTopic: string
|
||||
private offItems: HasStateAndFeedbackTopic[]
|
||||
private onItems: HasStateAndFeedbackTopic[]
|
||||
private myLastFeedbackState: string
|
||||
|
||||
constructor(floor: string, room: string, item: string, label: string = '',
|
||||
onItems: HasStateAndFeedbackTopic[], offItems: HasStateAndFeedbackTopic[]) {
|
||||
super(floor, room, item, label)
|
||||
this.onItems = onItems
|
||||
this.offItems = offItems
|
||||
this.stateTopic = `${this.topicFirstPart}/state`
|
||||
this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback`
|
||||
this.subscribeTopics = []
|
||||
this.subscribeTopics.push(this.stateTopic)
|
||||
this.onFeedbackTopics = []
|
||||
this.onTopics = []
|
||||
this.onFeedbackMap = {}
|
||||
this.onItems.forEach((item: HasStateAndFeedbackTopic) => {
|
||||
this.subscribeTopics.push(item.getStateFeedbackTopic())
|
||||
this.onFeedbackTopics.push(item.getStateFeedbackTopic())
|
||||
this.onTopics.push(item.getStateTopic())
|
||||
this.onFeedbackMap[item.getStateFeedbackTopic()] = '-'
|
||||
})
|
||||
this.offFeedbackTopics = []
|
||||
this.offTopics = []
|
||||
this.offFeedbackMap = {}
|
||||
this.offItems.forEach((item: HasStateAndFeedbackTopic) => {
|
||||
this.subscribeTopics.push(item.getStateFeedbackTopic())
|
||||
this.offFeedbackTopics.push(item.getStateFeedbackTopic())
|
||||
this.offTopics.push(item.getStateTopic())
|
||||
this.offFeedbackMap[item.getStateFeedbackTopic()] = '-'
|
||||
})
|
||||
this.state = 'OFF'
|
||||
this.myLastFeedbackState = '-'
|
||||
}
|
||||
|
||||
exportItem() : ExportType|null {
|
||||
return SwitchExport(this.itemId, this.label, this.stateTopic, this.stateFeedbackTopic, 'Switch')
|
||||
}
|
||||
|
||||
processMessage(topic: string, payload: string) : void {
|
||||
if (topic == this.stateTopic) {
|
||||
this.state = payload
|
||||
if (this.state == 'ON') {
|
||||
this.onTopics.forEach((topic2: string) => {
|
||||
mqttHandler.send(topic2, 'ON')
|
||||
})
|
||||
this.offTopics.forEach((topic2: string) => {
|
||||
mqttHandler.send(topic2, 'OFF')
|
||||
})
|
||||
} else {
|
||||
this.onTopics.forEach((topic2: string) => {
|
||||
mqttHandler.send(topic2, 'OFF')
|
||||
})
|
||||
this.offTopics.forEach((topic2: string) => {
|
||||
mqttHandler.send(topic2, 'OFF')
|
||||
})
|
||||
}
|
||||
} else if (this.onFeedbackTopics.some((x) => { return x == topic })) {
|
||||
let feedbackState = payload
|
||||
this.onFeedbackMap[topic] = feedbackState
|
||||
} else if (this.offFeedbackTopics.some((x) => { return x == topic })) {
|
||||
let feedbackState = payload
|
||||
this.offFeedbackMap[topic] = feedbackState
|
||||
}
|
||||
|
||||
let myFeedbackState = 'ON'
|
||||
Object.values(this.onFeedbackMap).forEach((v: string) => {
|
||||
if (v != 'ON') {
|
||||
myFeedbackState = 'OFF'
|
||||
}
|
||||
})
|
||||
Object.values(this.offFeedbackMap).forEach((v: string) => {
|
||||
if (v != 'OFF') {
|
||||
myFeedbackState = 'OFF'
|
||||
}
|
||||
})
|
||||
if (myFeedbackState != this.myLastFeedbackState) {
|
||||
mqttHandler.send(this.stateFeedbackTopic, myFeedbackState)
|
||||
this.myLastFeedbackState = myFeedbackState
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -13,7 +13,7 @@ import { TimerAdaptor } from './TimerAdaptor'
|
||||
import { HomematicDimmerItem } from './HomematicDimmerItem'
|
||||
import { HomematicSwitchItem } from './HomematicSwitchItem'
|
||||
import { Forwarder } from './Forwarder'
|
||||
|
||||
import { LightScene } from './Scene'
|
||||
|
||||
|
||||
logger.info("Dispatcher starting")
|
||||
@ -197,6 +197,12 @@ let testForwarder = new Forwarder('Gnd', 'Hallway', 'TestForwarder', 'state', 'T
|
||||
testForwarder.start()
|
||||
|
||||
|
||||
let testScene = new LightScene('Gnd', 'Hallway', 'TestScene', 'TestScene',
|
||||
[aquariumLight, annaBedLight],
|
||||
[matthiasStandLights, matthiasBedLight]
|
||||
)
|
||||
testScene.start()
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------------
|
||||
// Homekit export
|
||||
|
Reference in New Issue
Block a user