Skip to content

Commit

Permalink
Don't fail if can't get last weather
Browse files Browse the repository at this point in the history
  • Loading branch information
BlythMeister authored Oct 15, 2024
1 parent a743e76 commit ca4aac3
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/366x366/companion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
}

0 comments on commit ca4aac3

Please sign in to comment.