From ecc0132206fc9e4079a0852d9871ffd27bd87559 Mon Sep 17 00:00:00 2001 From: David Baetge Date: Thu, 26 Oct 2023 17:36:30 +0200 Subject: [PATCH 1/5] Added daylie reset for min/max values via mqtt. --- changelog.md | 1 + skins/weewx-wdc/includes/ui-shell.inc | 16 ++++---- skins/weewx-wdc/skin.conf | 6 +-- skins/weewx-wdc/src/js/live-updates.ts | 57 +++++++++++++++++++++----- 4 files changed, 58 insertions(+), 22 deletions(-) diff --git a/changelog.md b/changelog.md index aae7432..f9082f5 100644 --- a/changelog.md +++ b/changelog.md @@ -728,6 +728,7 @@ See https://github.com/Daveiano/weewx-wdc/compare/v3.2.0...11eed6b3#diff-ba225fb - Bugfix: Manifest.json does not use base_path GH-197 - Bugfix: Tool Tip out of Scope GH-194 - Bugfix: Observations with $current N/A values are not shown anymore in stat-tiles/conditions table on day page GH-188 +- Bugfix: Day Mix/Max Values in Tiles Do Not reset at Midnight GH-198 - Added `show_min_max` configuration to gauges config, added ordinal display for windDir gauge min/max - Only show 3 decimals for the geocode provided by weewx-forecast GH-191 - Make sidebar scrollable (if there are too many items) GH-193 diff --git a/skins/weewx-wdc/includes/ui-shell.inc b/skins/weewx-wdc/includes/ui-shell.inc index caab089..44d7c5c 100644 --- a/skins/weewx-wdc/includes/ui-shell.inc +++ b/skins/weewx-wdc/includes/ui-shell.inc @@ -392,8 +392,8 @@ // Change the icons inside the button based on previous settings if ( - localStorage.getItem("color-theme") === "dark" || - (!("color-theme" in localStorage) && + localStorage.getItem("weewx.weewx_wdc.color-theme") === "dark" || + (!("weewx.weewx_wdc.color-theme" in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches) ) { document.documentElement.classList.add("dark"); @@ -419,23 +419,23 @@ }); // if set via local storage previously - if (localStorage.getItem("color-theme")) { - if (localStorage.getItem("color-theme") === "light") { + if (localStorage.getItem("weewx.weewx_wdc.color-theme")) { + if (localStorage.getItem("weewx.weewx_wdc.color-theme") === "light") { document.documentElement.classList.add("dark"); - localStorage.setItem("color-theme", "dark"); + localStorage.setItem("weewx.weewx_wdc.color-theme", "dark"); } else { document.documentElement.classList.remove("dark"); - localStorage.setItem("color-theme", "light"); + localStorage.setItem("weewx.weewx_wdc.color-theme", "light"); } // if NOT set via local storage previously } else { if (document.documentElement.classList.contains("dark")) { document.documentElement.classList.remove("dark"); - localStorage.setItem("color-theme", "light"); + localStorage.setItem("weewx.weewx_wdc.color-theme", "light"); } else { document.documentElement.classList.add("dark"); - localStorage.setItem("color-theme", "dark"); + localStorage.setItem("weewx.weewx_wdc.color-theme", "dark"); } } }); diff --git a/skins/weewx-wdc/skin.conf b/skins/weewx-wdc/skin.conf index 5041a93..ec61b65 100644 --- a/skins/weewx-wdc/skin.conf +++ b/skins/weewx-wdc/skin.conf @@ -39,10 +39,10 @@ SKIN_VERSION = 3.3.0 # For instructions, see https://github.com/Daveiano/weewx-wdc/wiki/Support-for-weewx-mqtt [[mqtt]] - mqtt_websockets_enabled = 0 - mqtt_websockets_host = "localhost" + mqtt_websockets_enabled = 1 + mqtt_websockets_host = "mqtt.weewx-hbt.de" mqtt_websockets_port = 9001 - mqtt_websockets_ssl = 0 + mqtt_websockets_ssl = 1 mqtt_websockets_topic = "weather/loop" # For instructions, see https://github.com/Daveiano/weewx-wdc/wiki/Webcams-and-Externals-Page diff --git a/skins/weewx-wdc/src/js/live-updates.ts b/skins/weewx-wdc/src/js/live-updates.ts index 094f620..a4bc9f1 100644 --- a/skins/weewx-wdc/src/js/live-updates.ts +++ b/skins/weewx-wdc/src/js/live-updates.ts @@ -62,7 +62,8 @@ const _updateStatTile = ( value: number, rounding: number, payLoad: any, - unitMQTT: string + unitMQTT: string, + dayChange: boolean ) => { // Update the main value. const sumObs = statTile @@ -133,7 +134,7 @@ const _updateStatTile = ( ? min!.querySelector(".stat-value span.value")!.innerHTML : ""; - if (min && payLoad[key] < parseFloat(minValue)) { + if (min && (payLoad[key] < parseFloat(minValue) || dayChange)) { min.querySelector(".stat-value span.value")!.textContent = `${parseFloat( payLoad[key] ).toFixed(rounding)}${unit}`; @@ -185,7 +186,7 @@ const _updateStatTile = ( ? max!.querySelector(".stat-value span.value")!.innerHTML : ""; - if (max && payLoad[key] > parseFloat(maxValue)) { + if (max && (payLoad[key] > parseFloat(maxValue) || dayChange)) { max.querySelector(".stat-value span.value")!.textContent = `${parseFloat( payLoad[key] ).toFixed(rounding)}${unit}`; @@ -271,7 +272,7 @@ const _updateStatTile = ( )!.textContent = `${rainRate.toFixed(rounding)}${rainRateUnit}`; } - if (rainRateMax && rainRate > parseFloat(rainRateMaxValue)) { + if (rainRateMax && (rainRate > parseFloat(rainRateMaxValue) || dayChange)) { rainRateMax.querySelector( ".stat-value span.value" )!.textContent = `${rainRate.toFixed(rounding)}${rainRateUnit}`; @@ -286,7 +287,8 @@ const _updateTableRow = ( unit: string, value: number, rounding: number, - payLoad: any + payLoad: any, + dayChange: boolean ) => { // Update the main value. tableRow.querySelector( @@ -333,7 +335,7 @@ const _updateTableRow = ( )!, minValue = minValueSpan.innerHTML; - if (min && payLoad[key] < parseFloat(minValue)) { + if (min && (payLoad[key] < parseFloat(minValue) || dayChange)) { minValueSpan.textContent = `${parseFloat(payLoad[key]).toFixed( rounding )}${unit}`; @@ -380,7 +382,8 @@ const _updateTableRow = ( "bx-structured-list-cell.cell-max > span" )!, maxValue = maxValueSpan.innerHTML; - if (max && payLoad[key] > parseFloat(maxValue)) { + + if (max && (payLoad[key] > parseFloat(maxValue) || dayChange)) { maxValueSpan.textContent = `${parseFloat(payLoad[key]).toFixed( rounding )}${unit}`; @@ -466,6 +469,30 @@ const onConnectionLost = (responseObject: any) => { const onMessageArrived = (message: Message) => { const payLoad = JSON.parse(message.payloadString); + const lastUpdate_ts = localStorage.getItem( + "weewx.weewx_wdc.mqtt-last-udpate" + ), + lastUpdate_formatted = lastUpdate_ts + ? dayjs(lastUpdate_ts).format("YYYY-MM-DD") + : null; + let dayChange = false; + + console.log("lastUpdate_formatted", lastUpdate_formatted); + console.log(dayjs.unix(payLoad.dateTime).format("YYYY-MM-DD")); + + // Day changed, reset min/max/sum. + if ( + lastUpdate_formatted !== dayjs.unix(payLoad.dateTime).format("YYYY-MM-DD") + ) { + dayChange = true; + console.log("MQTT WS: Day changed, resetting min/max/sum."); + } + + localStorage.setItem( + "weewx.weewx_wdc.mqtt-last-udpate", + dayjs.unix(payLoad.dateTime).toString() + ); + notfication!.setAttribute( "subtitle", `Last update was ${dayjs.unix(payLoad.dateTime).format("HH:mm:ss")}` @@ -496,11 +523,17 @@ const onMessageArrived = (message: Message) => { const gaugeTileSeriesName = gaugeTile.getAttribute("data-test")!; (window as any)[gaugeTileSeriesName].current = payLoad[key]; - if (payLoad[key] < (window as any)[gaugeTileSeriesName].min) { + if ( + payLoad[key] < (window as any)[gaugeTileSeriesName].min || + dayChange + ) { (window as any)[gaugeTileSeriesName].min = payLoad[key]; } - if (payLoad[key] > (window as any)[gaugeTileSeriesName].max) { + if ( + payLoad[key] > (window as any)[gaugeTileSeriesName].max || + dayChange + ) { (window as any)[gaugeTileSeriesName].max = payLoad[key]; } } @@ -526,7 +559,8 @@ const onMessageArrived = (message: Message) => { newValue, statTileRounding, payLoad, - unitMQTT + unitMQTT, + dayChange ); } @@ -563,7 +597,8 @@ const onMessageArrived = (message: Message) => { tableRowUnit, newValue, tableRowRounding, - payLoad + payLoad, + dayChange ); } } From 98966d34bde1dc45c588aef6e1c5fe83634dc717 Mon Sep 17 00:00:00 2001 From: David Baetge Date: Sun, 29 Oct 2023 01:57:31 +0200 Subject: [PATCH 2/5] Fixed bug when no weewx.weewx_wdc.mqtt-last-udpate was set. --- skins/weewx-wdc/src/js/live-updates.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/skins/weewx-wdc/src/js/live-updates.ts b/skins/weewx-wdc/src/js/live-updates.ts index a4bc9f1..e39cb7c 100644 --- a/skins/weewx-wdc/src/js/live-updates.ts +++ b/skins/weewx-wdc/src/js/live-updates.ts @@ -482,6 +482,7 @@ const onMessageArrived = (message: Message) => { // Day changed, reset min/max/sum. if ( + lastUpdate_ts && lastUpdate_formatted !== dayjs.unix(payLoad.dateTime).format("YYYY-MM-DD") ) { dayChange = true; From 6d89e52e6ad96b2b8ca183d66778c694b8514616 Mon Sep 17 00:00:00 2001 From: David Baetge Date: Wed, 1 Nov 2023 19:04:53 +0100 Subject: [PATCH 3/5] Reset logic set per obs basis, not per loop packet. --- skins/weewx-wdc/includes/html-head.inc | 2 + skins/weewx-wdc/src/js/live-updates.ts | 53 ++++++++++++++------------ 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/skins/weewx-wdc/includes/html-head.inc b/skins/weewx-wdc/includes/html-head.inc index e0da36e..35db26f 100644 --- a/skins/weewx-wdc/includes/html-head.inc +++ b/skins/weewx-wdc/includes/html-head.inc @@ -2,6 +2,7 @@ #errorCatcher Echo #encoding UTF-8 #import html +#import datetime #set $diagrams_config = $DisplayOptions.get("diagrams", {}) @@ -83,6 +84,7 @@ "Format_is": "$gettext('Format is')", }, locale: "$get_locale()", + time: $datetime.datetime.now().timestamp() }; diff --git a/skins/weewx-wdc/src/js/live-updates.ts b/skins/weewx-wdc/src/js/live-updates.ts index e39cb7c..4d74f7f 100644 --- a/skins/weewx-wdc/src/js/live-updates.ts +++ b/skins/weewx-wdc/src/js/live-updates.ts @@ -469,31 +469,6 @@ const onConnectionLost = (responseObject: any) => { const onMessageArrived = (message: Message) => { const payLoad = JSON.parse(message.payloadString); - const lastUpdate_ts = localStorage.getItem( - "weewx.weewx_wdc.mqtt-last-udpate" - ), - lastUpdate_formatted = lastUpdate_ts - ? dayjs(lastUpdate_ts).format("YYYY-MM-DD") - : null; - let dayChange = false; - - console.log("lastUpdate_formatted", lastUpdate_formatted); - console.log(dayjs.unix(payLoad.dateTime).format("YYYY-MM-DD")); - - // Day changed, reset min/max/sum. - if ( - lastUpdate_ts && - lastUpdate_formatted !== dayjs.unix(payLoad.dateTime).format("YYYY-MM-DD") - ) { - dayChange = true; - console.log("MQTT WS: Day changed, resetting min/max/sum."); - } - - localStorage.setItem( - "weewx.weewx_wdc.mqtt-last-udpate", - dayjs.unix(payLoad.dateTime).toString() - ); - notfication!.setAttribute( "subtitle", `Last update was ${dayjs.unix(payLoad.dateTime).format("HH:mm:ss")}` @@ -506,6 +481,34 @@ const onMessageArrived = (message: Message) => { const observation = keySplitted[0]; const unitMQTT = _getUnitFromMQTTProp(keySplitted); + const lastUpdate_ts = localStorage.getItem( + `weewx.weewx_wdc.mqtt-last-udpate-${key}` + ), + lastGenerated_ts = (window as any).weewxWdcConfig.time, + lastUpdate_formatted = lastUpdate_ts + ? dayjs(lastUpdate_ts).format("YYYY-MM-DD") + : null; + let dayChange = false; + + console.log("lastUpdate_formatted", lastUpdate_formatted); + console.log(dayjs.unix(payLoad.dateTime).format("YYYY-MM-DD")); + + // Day changed, reset min/max/sum. + if ( + lastUpdate_ts && + lastUpdate_formatted !== + dayjs.unix(payLoad.dateTime).format("YYYY-MM-DD") && + lastUpdate_ts > lastGenerated_ts + ) { + dayChange = true; + console.log("MQTT WS: Day changed, resetting min/max/sum for ." + key); + } + + localStorage.setItem( + `weewx.weewx_wdc.mqtt-last-udpate-${key}`, + dayjs.unix(payLoad.dateTime).toString() + ); + // Alternative layout. const statTile = document.querySelector( `.stat-tile[data-observation="${observation}"]` From 426e5bd12e8c96bdddb2fbdad20c5fcda39f4bf4 Mon Sep 17 00:00:00 2001 From: David Baetge Date: Thu, 2 Nov 2023 01:16:01 +0100 Subject: [PATCH 4/5] dayChange now uses timestamps. --- skins/weewx-wdc/src/js/live-updates.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/skins/weewx-wdc/src/js/live-updates.ts b/skins/weewx-wdc/src/js/live-updates.ts index 4d74f7f..32103f9 100644 --- a/skins/weewx-wdc/src/js/live-updates.ts +++ b/skins/weewx-wdc/src/js/live-updates.ts @@ -469,6 +469,8 @@ const onConnectionLost = (responseObject: any) => { const onMessageArrived = (message: Message) => { const payLoad = JSON.parse(message.payloadString); + console.log(payLoad); + notfication!.setAttribute( "subtitle", `Last update was ${dayjs.unix(payLoad.dateTime).format("HH:mm:ss")}` @@ -484,21 +486,18 @@ const onMessageArrived = (message: Message) => { const lastUpdate_ts = localStorage.getItem( `weewx.weewx_wdc.mqtt-last-udpate-${key}` ), - lastGenerated_ts = (window as any).weewxWdcConfig.time, lastUpdate_formatted = lastUpdate_ts - ? dayjs(lastUpdate_ts).format("YYYY-MM-DD") + ? dayjs.unix(parseInt(lastUpdate_ts)).format("YYYY-MM-DD") : null; let dayChange = false; console.log("lastUpdate_formatted", lastUpdate_formatted); - console.log(dayjs.unix(payLoad.dateTime).format("YYYY-MM-DD")); + console.log(dayjs.unix(parseInt(payLoad.dateTime)).format("YYYY-MM-DD")); // Day changed, reset min/max/sum. if ( lastUpdate_ts && - lastUpdate_formatted !== - dayjs.unix(payLoad.dateTime).format("YYYY-MM-DD") && - lastUpdate_ts > lastGenerated_ts + lastUpdate_formatted !== dayjs.unix(payLoad.dateTime).format("YYYY-MM-DD") ) { dayChange = true; console.log("MQTT WS: Day changed, resetting min/max/sum for ." + key); @@ -506,7 +505,7 @@ const onMessageArrived = (message: Message) => { localStorage.setItem( `weewx.weewx_wdc.mqtt-last-udpate-${key}`, - dayjs.unix(payLoad.dateTime).toString() + payLoad.dateTime ); // Alternative layout. @@ -626,3 +625,5 @@ client.connect({ useSSL: mqtt_ssl === "1", reconnect: true, }); + +alert("test 3"); From 3344afb652581fbb3a082fbf2511267454b5f719 Mon Sep 17 00:00:00 2001 From: David Baetge Date: Thu, 2 Nov 2023 17:00:45 +0100 Subject: [PATCH 5/5] Removed debug output. --- skins/weewx-wdc/skin.conf | 6 +++--- skins/weewx-wdc/src/js/live-updates.ts | 6 ------ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/skins/weewx-wdc/skin.conf b/skins/weewx-wdc/skin.conf index f460c54..c405845 100644 --- a/skins/weewx-wdc/skin.conf +++ b/skins/weewx-wdc/skin.conf @@ -39,10 +39,10 @@ SKIN_VERSION = 3.3.0 # For instructions, see https://github.com/Daveiano/weewx-wdc/wiki/Support-for-weewx-mqtt [[mqtt]] - mqtt_websockets_enabled = 1 - mqtt_websockets_host = "mqtt.weewx-hbt.de" + mqtt_websockets_enabled = 0 + mqtt_websockets_host = "localhost" mqtt_websockets_port = 9001 - mqtt_websockets_ssl = 1 + mqtt_websockets_ssl = 0 mqtt_websockets_topic = "weather/loop" # For instructions, see https://github.com/Daveiano/weewx-wdc/wiki/Webcams-and-Externals-Page diff --git a/skins/weewx-wdc/src/js/live-updates.ts b/skins/weewx-wdc/src/js/live-updates.ts index 32103f9..93526bc 100644 --- a/skins/weewx-wdc/src/js/live-updates.ts +++ b/skins/weewx-wdc/src/js/live-updates.ts @@ -469,8 +469,6 @@ const onConnectionLost = (responseObject: any) => { const onMessageArrived = (message: Message) => { const payLoad = JSON.parse(message.payloadString); - console.log(payLoad); - notfication!.setAttribute( "subtitle", `Last update was ${dayjs.unix(payLoad.dateTime).format("HH:mm:ss")}` @@ -491,16 +489,12 @@ const onMessageArrived = (message: Message) => { : null; let dayChange = false; - console.log("lastUpdate_formatted", lastUpdate_formatted); - console.log(dayjs.unix(parseInt(payLoad.dateTime)).format("YYYY-MM-DD")); - // Day changed, reset min/max/sum. if ( lastUpdate_ts && lastUpdate_formatted !== dayjs.unix(payLoad.dateTime).format("YYYY-MM-DD") ) { dayChange = true; - console.log("MQTT WS: Day changed, resetting min/max/sum for ." + key); } localStorage.setItem(