From 475cd56a9523709ad8927fb835f297e3257662db Mon Sep 17 00:00:00 2001 From: Magnus Marthinsen Date: Sun, 26 Jan 2025 15:04:20 +0100 Subject: [PATCH 1/4] Stop Yr weather provider from freezing on bad API resoonse updateAvailable must be called to schedule the next update. If we just return or throw an error the fetch-methods will not be called from weather.js. --- CHANGELOG.md | 1 + modules/default/weather/providers/yr.js | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d1a004c0b..e4706f5f83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ planned for 2025-04-01 - [weather] Fix wrong weatherCondition name in openmeteo provider which lead to n/a icon (#3691) - [core] Fix wrong port in log message when starting server only (#3696) - [calendar] NewYork event processed on system in Central timezone shows wrong time #3701 +- [weather/yr] The Yr weather provider is now able to recover from bad API resposes instead of freezing (#3296) ## [2.30.0] - 2025-01-01 diff --git a/modules/default/weather/providers/yr.js b/modules/default/weather/providers/yr.js index f305a1c09d..374c323227 100644 --- a/modules/default/weather/providers/yr.js +++ b/modules/default/weather/providers/yr.js @@ -34,7 +34,7 @@ WeatherProvider.register("yr", { }) .catch((error) => { Log.error(error); - throw new Error(error); + this.updateAvailable(); }); }, @@ -497,7 +497,7 @@ WeatherProvider.register("yr", { }) .catch((error) => { Log.error(error); - throw new Error(error); + this.updateAvailable(); }); }, @@ -600,7 +600,7 @@ WeatherProvider.register("yr", { }) .catch((error) => { Log.error(error); - throw new Error(error); + this.updateAvailable(); }); } }); From 0930ed086934a29894f84165d699550bf102e2a6 Mon Sep 17 00:00:00 2001 From: Magnus Marthinsen Date: Sun, 26 Jan 2025 15:31:21 +0100 Subject: [PATCH 2/4] Filter away the hours in the past If the forecast is fetched from the cache we should remove the entries that no longer provide any value to the user. --- modules/default/weather/providers/yr.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/modules/default/weather/providers/yr.js b/modules/default/weather/providers/yr.js index 374c323227..5fb11fea7b 100644 --- a/modules/default/weather/providers/yr.js +++ b/modules/default/weather/providers/yr.js @@ -530,7 +530,15 @@ WeatherProvider.register("yr", { getHourlyForecastFrom (weatherData) { const series = []; + const now = moment({ + year: moment().year(), + month: moment().month(), + day: moment().date(), + hour: moment().hour() + }); for (const forecast of weatherData.properties.timeseries) { + if (now.isAfter(moment(forecast.time))) continue; + forecast.symbol = forecast.data.next_1_hours?.summary?.symbol_code; forecast.precipitationAmount = forecast.data.next_1_hours?.details?.precipitation_amount; forecast.precipitationProbability = forecast.data.next_1_hours?.details?.probability_of_precipitation; From b57c68cdac224e83677ddfe2c86969b0bd5d7508 Mon Sep 17 00:00:00 2001 From: Magnus Marthinsen Date: Sun, 26 Jan 2025 15:41:05 +0100 Subject: [PATCH 3/4] Return cached data if API call fails This is probably due to a HTTP 429 (Too many requests). If the MagicMirror is throttled we should fall back to the cached data. --- modules/default/weather/providers/yr.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/default/weather/providers/yr.js b/modules/default/weather/providers/yr.js index 5fb11fea7b..5f7be8601c 100644 --- a/modules/default/weather/providers/yr.js +++ b/modules/default/weather/providers/yr.js @@ -119,7 +119,12 @@ WeatherProvider.register("yr", { }) .catch((err) => { Log.error(err); - reject("Unable to get weather data from Yr."); + if (weatherData) { + Log.warn("Using outdated cached weather data."); + resolve(weatherData); + } else { + reject("Unable to get weather data from Yr."); + } }) .finally(() => { localStorage.removeItem("yrIsFetchingWeatherData"); From dc090c6c2bacf8c40e894b9309ed930698d9e6d9 Mon Sep 17 00:00:00 2001 From: Magnus Marthinsen Date: Sun, 26 Jan 2025 17:26:46 +0100 Subject: [PATCH 4/4] Added check to make sure the update interval is at least 10 minutes Some users get HTTP 429 from the API. This indicates too many request. In the terms of service, yr says "don't make more than one poll every 10 mins". We now increase the updateInterval-value to 600000 if a smaller value is set by the user. --- CHANGELOG.md | 1 + modules/default/weather/providers/yr.js | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4706f5f83..1e02f2dcd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ planned for 2025-04-01 - [core] Optimize systeminformation calls and output (#3689) - [core] Add issue templates for feature requests and bug reports (#3695) - [core] Adapt `start:x11:dev` script +- [weather/yr] The Yr weather provider now enforces a minimum `updateInterval` of 600 000 ms (10 minutes) to comply with the terms of service. If a lower value is set, it will be automatically increased to this minimum. ### Removed diff --git a/modules/default/weather/providers/yr.js b/modules/default/weather/providers/yr.js index 5f7be8601c..a89dacd824 100644 --- a/modules/default/weather/providers/yr.js +++ b/modules/default/weather/providers/yr.js @@ -23,6 +23,10 @@ WeatherProvider.register("yr", { Log.error("The Yr weather provider requires local storage."); throw new Error("Local storage not available"); } + if (this.config.updateInterval < 600000) { + Log.warn("The Yr weather provider requires a minimum update interval of 10 minutes (600 000 ms). The configuration has been adjusted to meet this requirement."); + this.delegate.config.updateInterval = 600000; + } Log.info(`Weather provider: ${this.providerName} started.`); },