From ca4aac346e922266ce2764b425603639f2d925ab Mon Sep 17 00:00:00 2001 From: Chris Blyth Date: Tue, 15 Oct 2024 17:51:32 +0100 Subject: [PATCH] Don't fail if can't get last weather --- src/366x366/companion/index.js | 38 ++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/366x366/companion/index.js b/src/366x366/companion/index.js index d93b770..548ab90 100644 --- a/src/366x366/companion/index.js +++ b/src/366x366/companion/index.js @@ -158,19 +158,23 @@ function sendWeather(unit) { unitKey = "fahrenheit"; } - localStorage.setItem("lastWeatherUnit", unit); + localStorage.setItem("lastWeatherUnit", unitKey); - let lastWeatherJson = localStorage.getItem("lastWeather"); - if(lastWeatherJson != null) { - let lastWeather = JSON.parse(lastWeatherJson) - console.log(`lastWeather: ${lastWeatherJson}`) - let lastWeatherAge = new Date() - new Date(lastWeather.date); - - if (lastWeather.condition >= 0 && lastWeather.unit == unitKey && lastWeatherAge < 600000) { - console.warn(`Weather requested again within 10 minutes (${lastWeatherAge}ms), returning old weather`); - msgq.send("weather", lastWeather, true); - return; + try { + let lastWeatherJson = localStorage.getItem("lastWeather"); + if(lastWeatherJson != null) { + let lastWeather = JSON.parse(lastWeatherJson) + console.log(`lastWeather: ${lastWeatherJson}`) + let lastWeatherAge = new Date() - new Date(lastWeather.date); + + if (lastWeather.condition >= 0 && lastWeather.unit == unitKey && lastWeatherAge < 600000) { + console.warn(`Weather requested again within 10 minutes (${lastWeatherAge}ms), returning old weather`); + msgq.send("weather", lastWeather, true); + return; + } } + } catch (e) { + console.error(e); } console.log("Trying to get weather as last weather no good"); @@ -223,9 +227,13 @@ function sendWeather(unit) { } function locationChange() { - let lastWeatherUnit = localStorage.getItem("lastWeatherUnit"); - if (lastWeatherUnit != null) { - localStorage.setItem("lastWeather", null); - sendWeather(lastWeatherUnit); + try { + let lastWeatherUnit = localStorage.getItem("lastWeatherUnit"); + if (lastWeatherUnit != null) { + localStorage.setItem("lastWeather", null); + sendWeather(lastWeatherUnit); + } + } catch (e) { + console.error(e); } }