owmweather - Correctly handle the http promise

master
Martin Boonk 2022-07-02 14:29:29 +02:00
parent 7a7499d20e
commit 37df546df0
1 changed files with 35 additions and 17 deletions

View File

@ -1,20 +1,38 @@
(function() { (function() {
let waitingForResponse = false; let responsePromise;
let settings = require("Storage").readJSON("owmweather.json",1)||{enabled:false}; let settings = require("Storage").readJSON("owmweather.json", 1) || {
enabled: false
};
console.log("Settings", settings); console.log("Settings", settings);
if (settings.enabled && settings.apikey){ if (settings.enabled && settings.apikey) {
let location = require("Storage").readJSON("mylocation.json",1)||{"lat":51.50,"lon":0.12,"location":"London"}; let location = require("Storage").readJSON("mylocation.json", 1) || {
"lat": 51.50,
"lon": 0.12,
"location": "London"
};
function pullWeather() { var pullWeather = function() {
if (waitingForResponse) return; if (responsePromise){
waitingForResponse = true; print("Waiting for response");
Bangle.http("https://api.openweathermap.org/data/2.5/weather?lat=" + location.lat.toFixed(2) + "&lon=" + location.lon.toFixed(2) + "&exclude=hourly,daily&appid=" + settings.apikey).then(event=>{ return;
parseWeather(event.resp); }
waitingForResponse = false; let uri = "https://api.openweathermap.org/data/2.5/weather?lat=" + location.lat.toFixed(2) + "&lon=" + location.lon.toFixed(2) + "&exclude=hourly,daily&appid=" + settings.apikey;
}); print("Calling uri " + uri);
} if (Bangle.http){
responsePromise = Bangle.http(uri, {id:"debug"}).then(event => {
print("Got event ", event);
parseWeather(event.resp);
responsePromise = false;
}).catch((e)=>{
print("Rejected call", e);
responsePromise = false;
});
} else {
print("No http method found");
}
};
function parseWeather(response) { var parseWeather = function(response) {
let owmData = JSON.parse(response); let owmData = JSON.parse(response);
console.log("OWM Data", owmData); console.log("OWM Data", owmData);
@ -42,15 +60,15 @@
} }
json.weather = weather; json.weather = weather;
print("Parsed data", json);
require("Storage").writeJSON('weather.json', json); require("Storage").writeJSON('weather.json', json);
require("weather").emit("update", json.weather); require("weather").emit("update", json.weather);
} }
} };
console.log("Setting interval"); console.log("Setting interval");
setInterval(()=>{ setInterval(() => {
pullWeather(); pullWeather();
}, settings.refresh * 1000 * 60); }, settings.refresh * 1000 * 60);
pullWeather();
} }
})(); })();