-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3661 from devsnd/owmweather-refactor
owmweather 0.07: refactored owmweather, reload weather on NRF connect if due
- Loading branch information
Showing
3 changed files
with
36 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters