diff --git a/apps/owmweather/boot.js b/apps/owmweather/boot.js index 123d83842..612ccb940 100644 --- a/apps/owmweather/boot.js +++ b/apps/owmweather/boot.js @@ -1,74 +1,26 @@ (function() { - let responsePromise; + let waiting = false; let settings = require("Storage").readJSON("owmweather.json", 1) || { enabled: false }; - if (settings.enabled) { - Bangle.pullOwmWeather = function(force, completionCallback) { - if (!force && responsePromise){ - return; - } - let location = require("Storage").readJSON("mylocation.json", 1) || { - "lat": 51.50, - "lon": 0.12, - "location": "London" - }; - let settings = require("Storage").readJSON("owmweather.json", 1); - 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; - if (Bangle.http){ - responsePromise = Bangle.http(uri).then(event => { - let result = parseWeather(event.resp); - responsePromise = false; - if (completionCallback) completionCallback(result); - }).catch((e)=>{ - responsePromise = false; - if (completionCallback) completionCallback(e); - }); - } else { - if (completionCallback) completionCallback(/*LANG*/"No http method found"); - } - }; - - var parseWeather = function(response) { - let owmData = JSON.parse(response); - - let isOwmData = owmData.coord && owmData.weather && owmData.main; - - if (isOwmData) { - let json = require("Storage").readJSON('weather.json') || {}; - let weather = {}; - weather.time = Date.now(); - weather.hum = owmData.main.humidity; - weather.temp = owmData.main.temp; - weather.code = owmData.weather[0].id; - weather.wdir = owmData.wind.deg; - weather.wind = owmData.wind.speed; - weather.loc = owmData.name; - weather.txt = owmData.weather[0].main; - - if (weather.wdir != null) { - let deg = weather.wdir; - while (deg < 0 || deg > 360) { - deg = (deg + 360) % 360; - } - weather.wrose = ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', 'n'][Math.floor((deg + 22.5) / 45)]; - } - - json.weather = weather; - require("Storage").writeJSON('weather.json', json); - require("weather").emit("update", json.weather); - return undefined; - } else { - return /*LANG*/"Not OWM data"; - } - }; - + + function completion(){ + waiting = false; + } + + if (settings.enabled) { let weather = require("Storage").readJSON('weather.json') || {}; if (weather.time + settings.refresh * 1000 * 60 < Date.now()){ - Bangle.pullOwmWeather(); + if (!waiting){ + waiting = true; + require("owmweather").pull(completion); + } } setInterval(() => { - Bangle.pullOwmWeather(); + if (!waiting){ + waiting = true; + require("owmweather").pull(completion); + } }, settings.refresh * 1000 * 60); delete settings; } diff --git a/apps/owmweather/lib.js b/apps/owmweather/lib.js new file mode 100644 index 000000000..6ba52b498 --- /dev/null +++ b/apps/owmweather/lib.js @@ -0,0 +1,53 @@ +function parseWeather(response) { + let owmData = JSON.parse(response); + + let isOwmData = owmData.coord && owmData.weather && owmData.main; + + if (isOwmData) { + let json = require("Storage").readJSON('weather.json') || {}; + let weather = {}; + weather.time = Date.now(); + weather.hum = owmData.main.humidity; + weather.temp = owmData.main.temp; + weather.code = owmData.weather[0].id; + weather.wdir = owmData.wind.deg; + weather.wind = owmData.wind.speed; + weather.loc = owmData.name; + weather.txt = owmData.weather[0].main; + + if (weather.wdir != null) { + let deg = weather.wdir; + while (deg < 0 || deg > 360) { + deg = (deg + 360) % 360; + } + weather.wrose = ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', 'n'][Math.floor((deg + 22.5) / 45)]; + } + + json.weather = weather; + require("Storage").writeJSON('weather.json', json); + require("weather").emit("update", json.weather); + return undefined; + } else { + return /*LANG*/"Not OWM data"; + } +} + +exports.pull = function(completionCallback) { + let location = require("Storage").readJSON("mylocation.json", 1) || { + "lat": 51.50, + "lon": 0.12, + "location": "London" + }; + let settings = require("Storage").readJSON("owmweather.json", 1); + 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; + if (Bangle.http){ + Bangle.http(uri, {timeout:10000}).then(event => { + let result = parseWeather(event.resp); + if (completionCallback) completionCallback(result); + }).catch((e)=>{ + if (completionCallback) completionCallback(e); + }); + } else { + if (completionCallback) completionCallback(/*LANG*/"No http method found"); + } +}; diff --git a/apps/owmweather/metadata.json b/apps/owmweather/metadata.json index e2b702712..512f9de50 100644 --- a/apps/owmweather/metadata.json +++ b/apps/owmweather/metadata.json @@ -13,6 +13,7 @@ "storage": [ {"name":"owmweather.default.json","url":"default.json"}, {"name":"owmweather.boot.js","url":"boot.js"}, + {"name":"owmweather","url":"lib.js"}, {"name":"owmweather.settings.js","url":"settings.js"} ] } diff --git a/apps/owmweather/settings.js b/apps/owmweather/settings.js index cf9bbda46..a4d21dd7c 100644 --- a/apps/owmweather/settings.js +++ b/apps/owmweather/settings.js @@ -38,13 +38,13 @@ } }, "Force refresh": ()=>{ - if (!Bangle.pullOwmWeather){ - E.showAlert("Reload watch after enabling","Hint").then(()=>{ + if (!settings.apikey){ + E.showAlert("API key is needed","Hint").then(()=>{ E.showMenu(buildMainMenu()); }); } else { E.showMessage("Reloading weather"); - Bangle.pullOwmWeather(true, (e)=>{ + require("owmweather").pull((e)=>{ if (e) { E.showAlert(e,"Error").then(()=>{ E.showMenu(buildMainMenu());