Skip to content

Commit

Permalink
Don't send event when weather cache setting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
BlythMeister committed Oct 15, 2024
1 parent b2222c8 commit cae4328
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/366x366/companion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ setDefaultSettings();

// Settings have been changed
settingsStorage.addEventListener("change", (evt) => {
sendSettingValue(evt.key, evt.newValue);
if(evt.key != "weatherCache")
{
sendSettingValue(evt.key, evt.newValue);
}
});

function setDefaultSettings() {
Expand Down Expand Up @@ -153,15 +156,13 @@ function sendAllSettings() {

function sendWeather(unit) {
let unitKey = "celsius";
if (unit == "F") {
if (unit == "F" || unit == "fahrenheit") {
unitKey = "fahrenheit";
}

try {
settingsStorage.setItem("lastWeatherUnit", unitKey);

let lastWeatherJson = settingsStorage.getItem("lastWeather");
if (lastWeatherJson != null) {
let lastWeatherJson = settingsStorage.getItem("weatherCache");
if (lastWeatherJson) {
let lastWeather = JSON.parse(lastWeatherJson);
//console.log(`lastWeather: ${lastWeatherJson}`);
let lastWeatherAge = new Date() - new Date(lastWeather.date);
Expand Down Expand Up @@ -196,7 +197,7 @@ function sendWeather(unit) {
date: new Date(),
};
//console.log(`Weather:${JSON.stringify(sendData)}`);
settingsStorage.setItem("lastWeather", JSON.stringify(weatherData));
settingsStorage.setItem("weatherCache", JSON.stringify(weatherData));
msgq.send("weather", weatherData, true);
})
.catch((e) => {
Expand All @@ -208,7 +209,7 @@ function sendWeather(unit) {
location: e.message,
date: new Date(),
};
settingsStorage.setItem("lastWeather", null);
settingsStorage.setItem("weatherCache", null);
msgq.send("weather", errorWeather, true);
});
} catch (e) {
Expand All @@ -220,17 +221,17 @@ function sendWeather(unit) {
location: e.message,
date: new Date(),
};
settingsStorage.setItem("lastWeather", null);
settingsStorage.setItem("weatherCache", null);
msgq.send("weather", errorWeather, true);
}
}

function locationChange() {
try {
let lastWeatherUnit = settingsStorage.getItem("lastWeatherUnit");
if (lastWeatherUnit != null) {
settingsStorage.setItem("lastWeather", null);
sendWeather(lastWeatherUnit);
let lastWeather = settingsStorage.getItem("weatherCache");
if (lastWeather) {
settingsStorage.setItem("weatherCache", null);
sendWeather(lastWeather.unit);
}
} catch (e) {
console.error(e);
Expand Down

0 comments on commit cae4328

Please sign in to comment.