Skip to content

Commit

Permalink
Merge pull request #3661 from devsnd/owmweather-refactor
Browse files Browse the repository at this point in the history
owmweather 0.07: refactored owmweather, reload weather on NRF connect if due
  • Loading branch information
bobrippling authored Nov 18, 2024
2 parents 9421179 + da55ade commit 5211884
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
1 change: 1 addition & 0 deletions apps/owmweather/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
0.04: Minor code improvements
0.05: Upgrade OWM to One Call API 3.0. Add pressure to weather.json
0.06: Fix One Call API 3.0 not returning city names, which are required by the weather app
0.07: Update weather after reconnecting bluetooth if update is due, refactor code
54 changes: 34 additions & 20 deletions apps/owmweather/boot.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
{
let waiting = false;
let loading = false;
let timeoutRef = null;
let settings = Object.assign(
require('Storage').readJSON("owmweather.default.json", true) || {},
require('Storage').readJSON("owmweather.json", true) || {}
);

let completion = function(){
waiting = false;

let refreshMillis = function () {
return settings.refresh * 1000 * 60 + 1 // +1 <- leave some slack
}

let onCompleted = function () {
loading = false;
settings.updated = Date.now();
require('Storage').writeJSON("owmweather.json", settings);
if (timeoutRef) clearTimeout(timeoutRef);
timeoutRef = setTimeout(loadIfDueAndReschedule, refreshMillis());
}

if (settings.enabled) {
let loadIfDueAndReschedule = function () {
// also check if the weather.json file has been updated (e.g. force refresh)
let weather = require("Storage").readJSON('weather.json') || {};
if (weather && weather.weather && weather.weather.time) lastUpdate = weather.weather.time;

if (!settings.updated || settings.updated + settings.refresh * 1000 * 60 < Date.now()){
setTimeout(() => {
if (!waiting){
waiting = true;
require("owmweather").pull(completion);
}
}, 5000);
let lastWeatherUpdate = weather && weather.weather && weather.weather.time && weather.weather.time || 0;
if (lastWeatherUpdate > settings.updated) {
settings.updated = lastWeatherUpdate;
}
setInterval(() => {
if (!waiting && NRF.getSecurityStatus().connected){
waiting = true;
require("owmweather").pull(completion);

let MillisUntilDue = settings.updated + refreshMillis() - Date.now();
if (MillisUntilDue <= 0) {
if (!loading) {
loading = true;
require("owmweather").pull(onCompleted);
}
}, settings.refresh * 1000 * 60);
} else {
// called to early, reschedule
// console.log('Weather data is not due yet, rescheduling in ' + MillisUntilDue|0 + 'ms');
if (timeoutRef) clearTimeout(timeoutRef);
timeoutRef = setTimeout(loadIfDueAndReschedule, MillisUntilDue + 1);
}
}

if (settings.enabled) {
setTimeout(loadIfDueAndReschedule, 5000); // run 5 seconds after boot
NRF.on('connect', loadIfDueAndReschedule); // after reconnect, fetch the weather data right away if it's due
}
}
}
2 changes: 1 addition & 1 deletion apps/owmweather/metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ "id": "owmweather",
"name": "OpenWeatherMap weather provider",
"shortName":"OWM Weather",
"version": "0.06",
"version": "0.07",
"description": "Pulls weather from OpenWeatherMap (OWM) API",
"icon": "app.png",
"type": "bootloader",
Expand Down

0 comments on commit 5211884

Please sign in to comment.