diff --git a/src/WatchFace.cpp b/src/WatchFace.cpp index 0545867..65df974 100644 --- a/src/WatchFace.cpp +++ b/src/WatchFace.cpp @@ -121,8 +121,10 @@ void WatchFace::DrawWatchFace(bool partialRefresh) m_pages[m_watchFacePage]->DrawPage(partialRefresh); - // Resync weather on matter what - static_cast(m_pages[1].get())->Resync(); + // Resync weather in background + if (m_watchFacePage != 1) { + static_cast(m_pages[1].get())->Resync(); + } } void WatchFace::SetupVolatileMenuStuff() diff --git a/src/WatchFacePages/Weather.cpp b/src/WatchFacePages/Weather.cpp index 2cc1940..c4641ab 100644 --- a/src/WatchFacePages/Weather.cpp +++ b/src/WatchFacePages/Weather.cpp @@ -11,6 +11,7 @@ #include RTC_DATA_ATTR uint64_t WatchFacePages::Weather::m_lastSyncTime = 0; +RTC_DATA_ATTR uint64_t WatchFacePages::Weather::m_lastSyncAttemptTime = 0; RTC_DATA_ATTR int WatchFacePages::Weather::m_lastCalculatedDay = 0XFFFFFFFF; RTC_DATA_ATTR double WatchFacePages::Weather::m_locationLat = DEFAULT_LATITUDE; RTC_DATA_ATTR double WatchFacePages::Weather::m_locationLon = DEFAULT_LONGITUDE; @@ -46,9 +47,8 @@ void WatchFacePages::Weather::InitWake() void WatchFacePages::Weather::DrawPage(bool partialRefresh) { - if (m_lastSyncTime == 0) { - Resync(); - } + Resync(); + Recalc(); m_display.setFullWindow(); m_display.fillScreen(GxEPD_WHITE); @@ -219,7 +219,12 @@ void WatchFacePages::Weather::Resync() return; } + if (m_lastSyncTime > 0 && currentTime - m_lastSyncAttemptTime < WEATHER_UPDATE_BACKOFF) { + return; + } + if(!m_features.wifi.Connect()) { + m_lastSyncAttemptTime = currentTime; return; } @@ -232,6 +237,7 @@ void WatchFacePages::Weather::Resync() int httpCode = client.GET(); if (httpCode != 200) { m_features.wifi.Disconnect(); + m_lastSyncAttemptTime = currentTime; return; } @@ -252,6 +258,7 @@ void WatchFacePages::Weather::Resync() // Check if we got enough lines if (lines.size() < 14) { m_features.wifi.Disconnect(); + m_lastSyncAttemptTime = currentTime; return; } @@ -261,9 +268,11 @@ void WatchFacePages::Weather::Resync() memcpy(m_locationCity, location.c_str(), location.length()); m_locationLat = std::stof(lines[7]); m_locationLon = std::stof(lines[8]); + + // Force recalculation next time + m_lastCalculatedDay = 0xFFFFFFFF; } - // https://wttr.in/60.170833,24.9375?0Q&format=+%x:%t:%h std::ostringstream url; url << "http://wttr.in/" << m_locationLat << "," << m_locationLon << "?0Q&format=%x:%t:%h:%w"; // Grr. wttr.in does a redirect to HTTPS if your agent isn't curl @@ -273,6 +282,7 @@ void WatchFacePages::Weather::Resync() int httpCode = client.GET(); m_features.wifi.Disconnect(); if (httpCode != 200) { + m_lastSyncAttemptTime = currentTime; return; } @@ -292,6 +302,7 @@ void WatchFacePages::Weather::Resync() parts.push_back(reponse.substr(prev)); if (parts.size() != 4) { + m_lastSyncAttemptTime = currentTime; return; } @@ -310,7 +321,6 @@ void WatchFacePages::Weather::Resync() m_currentWindSpeed = std::round(windSpeedKmh * 0.277778); m_features.wifi.Disconnect(); - Recalc(); m_lastSyncTime = currentTime; } diff --git a/src/WatchFacePages/Weather.h b/src/WatchFacePages/Weather.h index c0f77bc..905047b 100644 --- a/src/WatchFacePages/Weather.h +++ b/src/WatchFacePages/Weather.h @@ -27,6 +27,7 @@ private: WatchyDisplay & m_display; WatchFeatures::WatchFeatures & m_features; static RTC_DATA_ATTR uint64_t m_lastSyncTime; + static RTC_DATA_ATTR uint64_t m_lastSyncAttemptTime; static RTC_DATA_ATTR int m_lastCalculatedDay; static RTC_DATA_ATTR char m_locationCity[128]; static RTC_DATA_ATTR double m_locationLat, m_locationLon; diff --git a/src/config.h b/src/config.h index 8b9d793..0f29096 100644 --- a/src/config.h +++ b/src/config.h @@ -42,6 +42,7 @@ #define WAKE_ON_ACCEL_EVENTS false // useful if saving battery by not updating every minute #define WEATHER_UPDATE_INTERVAL 3600 // seconds +#define WEATHER_UPDATE_BACKOFF 900 // If weather update fails, how long to wait before trying again #define DO_GEOLOCATION true // if false then use defaults below #define DEFAULT_LATITUDE 60.170833