From 591d9d196b8d1528022da31edab402c95866cd00 Mon Sep 17 00:00:00 2001 From: britzelpuf Date: Wed, 16 Mar 2022 14:55:00 +0100 Subject: [PATCH] Cleanup DetailEntityWatcher for sending changes to display --- ioBroker/NsPanelTs.ts | 67 ++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/ioBroker/NsPanelTs.ts b/ioBroker/NsPanelTs.ts index d6993250..c681384c 100644 --- a/ioBroker/NsPanelTs.ts +++ b/ioBroker/NsPanelTs.ts @@ -93,12 +93,14 @@ on([config.pvEntity, config.batEntity], function () { }) + on({ id: config.panelRecvTopic }, function (obj) { if (obj.state.val.startsWith('\{"CustomRecv":')) { var json = JSON.parse(obj.state.val); var split = json.CustomRecv.split(","); if (split[1] == "pageOpenDetail") { + UnsubscribeWatcher(); SendToPanel(GenerateDetailPage(split[2], split[3])); } else { @@ -107,7 +109,7 @@ on({ id: config.panelRecvTopic }, function (obj) { } }); -function SendToPanel(val: Payload | Payload[]) { +function SendToPanel(val: Payload | Payload[]): void { if (Array.isArray(val)) { val.forEach(function (id, i) { @@ -146,14 +148,14 @@ function HandleMessage(typ: string, method: string, page: number, words: Array = []; out_msgs.push({ payload: "pageType,cardThermo" }); @@ -335,8 +346,7 @@ function GenerateThermoPage(pageNum: number, page: PageThermo) { return out_msgs } -function HandleButtonEvent(words) { - var out_msgs: Array = []; +function HandleButtonEvent(words): void { let id = words[4] if (words[6] == "OnOff" && existsObject(id)) { @@ -375,10 +385,8 @@ function HandleButtonEvent(words) { // if (words[6] == "colorTempSlider") // out_msgs.push({ payload: id, action: "turn_on", domain: "lightTemperature", temperature: parseInt(words[7]) }) if (words[1] == "tempUpd") { - log(words[1] + " " + words[3]) setState(words[3] + ".SET", parseInt(words[4]) / 10) } - return out_msgs } function GenerateDetailPage(type: string, entityId: string): Payload[] { @@ -392,9 +400,16 @@ function GenerateDetailPage(type: string, entityId: string): Payload[] { let switchVal = "0" if (o.common.role == "light") { if (existsState(id + ".GET")) + { val = getState(id + ".GET").val; + RegisterDetailEntityWatcher(id + ".GET", id, type); + } else if (existsState(id + ".SET")) + { val = getState(id + ".SET").val; + RegisterDetailEntityWatcher(id + ".SET", id, type); + } + if (val) switchVal = "1" @@ -403,13 +418,27 @@ function GenerateDetailPage(type: string, entityId: string): Payload[] { if (o.common.role == "dimmer") { if (existsState(id + ".ON_ACTUAL")) + { val = getState(id + ".ON_ACTUAL").val; + RegisterDetailEntityWatcher(id + ".ON_ACTUAL", id, type); + } + else if (existsState(id + ".ON_SET")) + { val = getState(id + ".ON_SET").val; + RegisterDetailEntityWatcher(id + ".ON_SET", id, type); + } + if (val === true || val === "true") switchVal = "1" - - let brightness = Math.trunc(scale(getState(id + ".ACTUAL").val, 0, 100, 0, 100)) + let brightness = 0; + if (existsState(id + ".ACTUAL")) + { + brightness = Math.trunc(scale(getState(id + ".ACTUAL").val, 0, 100, 0, 100)) + RegisterDetailEntityWatcher(id + ".ACTUAL", id, type); + } + + let colortemp = "disable" //let attr_support_color = attr.supported_color_modes @@ -429,29 +458,28 @@ function GenerateDetailPage(type: string, entityId: string): Payload[] { val = getState(id + ".SET").val; out_msgs.push({ payload: "entityUpdateDetail," + val }) - log("entityUpdateDetail," + val) } } return out_msgs } -function scale(number, inMin, inMax, outMin, outMax) { +function scale(number: number, inMin: number, inMax: number, outMin: number, outMax: number): number { return (number - inMin) * (outMax - outMin) / (inMax - inMin) + outMin; } -function UnsubscribeWatcher() { +function UnsubscribeWatcher(): void { for (const [key, value] of Object.entries(subscriptions)) { unsubscribe(value); delete subscriptions[key] } } -function HandleScreensaver() { +function HandleScreensaver(): void { UnsubscribeWatcher(); HandleScreensaverUpdate(); } -function HandleScreensaverUpdate() { +function HandleScreensaverUpdate(): void { if (config.weatherEntity != null && existsObject(config.weatherEntity)) { var icon = getState(config.weatherEntity + ".ICON").val; @@ -460,7 +488,7 @@ function HandleScreensaverUpdate() { let u1 = getState(config.batEntity).val; let u2 = getState(config.pvEntity).val; - SendToPanel({ payload: "weatherUpdate,?" + GetAccuWeatherIcon(parseInt(icon)) + "?" + temperature.toString() + " " + config.temperatureUnit + "?26?" + humidity + " %?Batterie?4?" + u1 + "%?PV?23?" + u2 + "W" }) + //SendToPanel({ payload: "weatherUpdate,?" + GetAccuWeatherIcon(parseInt(icon)) + "?" + temperature.toString() + " " + config.temperatureUnit + "?26?" + humidity + " %?Batterie?4?" + u1 + "%?PV?23?" + u2 + "W" }) } } @@ -491,6 +519,5 @@ function GetAccuWeatherIcon(icon: number): number { return 1; break; } - }