diff --git a/dist/Scene.js b/dist/Scene.js new file mode 100644 index 0000000..11dc591 --- /dev/null +++ b/dist/Scene.js @@ -0,0 +1,85 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const AItem_1 = require("./AItem"); +const MqttDispatcher_1 = require("./MqttDispatcher"); +const Export_1 = require("./Export"); +class LightScene extends AItem_1.AItem { + constructor(floor, room, item, label = '', onItems, offItems) { + 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) => { + 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) => { + 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() { + return Export_1.SwitchExport(this.itemId, this.label, this.stateTopic, this.stateFeedbackTopic, 'Switch'); + } + processMessage(topic, payload) { + if (topic == this.stateTopic) { + this.state = payload; + if (this.state == 'ON') { + this.onTopics.forEach((topic2) => { + MqttDispatcher_1.mqttHandler.send(topic2, 'ON'); + }); + this.offTopics.forEach((topic2) => { + MqttDispatcher_1.mqttHandler.send(topic2, 'OFF'); + }); + } + else { + this.onTopics.forEach((topic2) => { + MqttDispatcher_1.mqttHandler.send(topic2, 'OFF'); + }); + this.offTopics.forEach((topic2) => { + MqttDispatcher_1.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) => { + if (v != 'ON') { + myFeedbackState = 'OFF'; + } + }); + Object.values(this.offFeedbackMap).forEach((v) => { + if (v != 'OFF') { + myFeedbackState = 'OFF'; + } + }); + if (myFeedbackState != this.myLastFeedbackState) { + MqttDispatcher_1.mqttHandler.send(this.stateFeedbackTopic, myFeedbackState); + this.myLastFeedbackState = myFeedbackState; + } + } +} +exports.LightScene = LightScene; +//# sourceMappingURL=Scene.js.map \ No newline at end of file diff --git a/dist/main.js b/dist/main.js index aadb9d5..a6260fa 100644 --- a/dist/main.js +++ b/dist/main.js @@ -11,6 +11,7 @@ const TimerAdaptor_1 = require("./TimerAdaptor"); const HomematicDimmerItem_1 = require("./HomematicDimmerItem"); const HomematicSwitchItem_1 = require("./HomematicSwitchItem"); const Forwarder_1 = require("./Forwarder"); +const Scene_1 = require("./Scene"); logger.info("Dispatcher starting"); let allLabeledItems = new Array(); // Anna ----------------------------------------------------------------------------------------------------- @@ -156,6 +157,8 @@ let testForwarder = new Forwarder_1.Forwarder('Gnd', 'Hallway', 'TestForwarder', 'dispatcher_ng/items/Gnd/Hallway/DeskLight/state' ]); testForwarder.start(); +let testScene = new Scene_1.LightScene('Gnd', 'Hallway', 'TestScene', 'TestScene', [aquariumLight, annaBedLight], [matthiasStandLights, matthiasBedLight]); +testScene.start(); // ---------------------------------------------------------------------------------------------------------- // Homekit export let homekitObject = {}; diff --git a/openhab.items b/openhab.items index b8d64b0..082e897 100644 --- a/openhab.items +++ b/openhab.items @@ -12,7 +12,7 @@ Switch Gnd.LivingRoom.Stars {mqtt=">[localbroker:dispatcher_ng/items/Gnd/LivingR Switch Gnd.LivingRoom.StandLight {mqtt=">[localbroker:dispatcher_ng/items/Gnd/LivingRoom/StandLight/state:command:*:default]", mqtt="<[localbroker:dispatcher_ng/items/Gnd/LivingRoom/StandLight/state/feedback:state:default]"} Switch Gnd.Hallway.DeskLight {mqtt=">[localbroker:dispatcher_ng/items/Gnd/Hallway/DeskLight/state:command:*:default]", mqtt="<[localbroker:dispatcher_ng/items/Gnd/Hallway/DeskLight/state/feedback:state:default]"} Switch Gnd.Hallway.StandLight {mqtt=">[localbroker:dispatcher_ng/items/Gnd/Hallway/StandLight/state:command:*:default]", mqtt="<[localbroker:dispatcher_ng/items/Gnd/Hallway/StandLight/state/feedback:state:default]"} -Switch Gnd.Hallway.StandLight {mqtt=">[localbroker:dispatcher_ng/items/Gnd/Hallway/StandLight/state:command:*:default]", mqtt="<[localbroker:dispatcher_ng/items/Gnd/Hallway/StandLight/state/feedback:state:default]"} +Switch Gnd.Hallway.WardrobeLight {mqtt=">[localbroker:dispatcher_ng/items/Gnd/Hallway/WardrobeLight/state:command:*:default]", mqtt="<[localbroker:dispatcher_ng/items/Gnd/Hallway/WardrobeLight/state/feedback:state:default]"} Switch Gnd.Kitchen.WindowLight {mqtt=">[localbroker:dispatcher_ng/items/Gnd/Kitchen/WindowLight/state:command:*:default]", mqtt="<[localbroker:dispatcher_ng/items/Gnd/Kitchen/WindowLight/state/feedback:state:default]"} Switch Gnd.Kitchen.CeilingLight {mqtt=">[localbroker:dispatcher_ng/items/Gnd/Kitchen/CeilingLight/state:command:*:default]", mqtt="<[localbroker:dispatcher_ng/items/Gnd/Kitchen/CeilingLight/state/feedback:state:default]"} Switch 1st.BedRoom.WolfgangsSide {mqtt=">[localbroker:dispatcher_ng/items/1st/BedRoom/WolfgangsSide/state:command:*:default]", mqtt="<[localbroker:dispatcher_ng/items/1st/BedRoom/WolfgangsSide/state/feedback:state:default]"} diff --git a/src/Scene.ts b/src/Scene.ts new file mode 100644 index 0000000..557c5ba --- /dev/null +++ b/src/Scene.ts @@ -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 + } + } + +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index c8e19c0..528c4df 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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 diff --git a/tsconfig.json b/tsconfig.json index 11656a5..238a3ba 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,10 +1,10 @@ { "compilerOptions": { - "target": "es6", + "target": "es2017", "module": "commonjs", "moduleResolution": "node", "sourceMap": true, - "lib": ["es6"], + "lib": ["es2017"], "strictNullChecks": true, "noImplicitAny": true, "noEmitOnError": true,