Added backoff time in case weather updates fail
All checks were successful
Compile / Compile (push) Successful in 1m25s

This commit is contained in:
Lewis Jackson 2023-06-02 22:24:00 +03:00
parent 74695597cf
commit 8d6edca411
4 changed files with 21 additions and 7 deletions

View file

@ -121,9 +121,11 @@ void WatchFace::DrawWatchFace(bool partialRefresh)
m_pages[m_watchFacePage]->DrawPage(partialRefresh); m_pages[m_watchFacePage]->DrawPage(partialRefresh);
// Resync weather on matter what // Resync weather in background
if (m_watchFacePage != 1) {
static_cast<WatchFacePages::Weather *>(m_pages[1].get())->Resync(); static_cast<WatchFacePages::Weather *>(m_pages[1].get())->Resync();
} }
}
void WatchFace::SetupVolatileMenuStuff() void WatchFace::SetupVolatileMenuStuff()
{ {

View file

@ -11,6 +11,7 @@
#include <iomanip> #include <iomanip>
RTC_DATA_ATTR uint64_t WatchFacePages::Weather::m_lastSyncTime = 0; 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 int WatchFacePages::Weather::m_lastCalculatedDay = 0XFFFFFFFF;
RTC_DATA_ATTR double WatchFacePages::Weather::m_locationLat = DEFAULT_LATITUDE; RTC_DATA_ATTR double WatchFacePages::Weather::m_locationLat = DEFAULT_LATITUDE;
RTC_DATA_ATTR double WatchFacePages::Weather::m_locationLon = DEFAULT_LONGITUDE; RTC_DATA_ATTR double WatchFacePages::Weather::m_locationLon = DEFAULT_LONGITUDE;
@ -46,9 +47,8 @@ void WatchFacePages::Weather::InitWake()
void WatchFacePages::Weather::DrawPage(bool partialRefresh) void WatchFacePages::Weather::DrawPage(bool partialRefresh)
{ {
if (m_lastSyncTime == 0) {
Resync(); Resync();
} Recalc();
m_display.setFullWindow(); m_display.setFullWindow();
m_display.fillScreen(GxEPD_WHITE); m_display.fillScreen(GxEPD_WHITE);
@ -219,7 +219,12 @@ void WatchFacePages::Weather::Resync()
return; return;
} }
if (m_lastSyncTime > 0 && currentTime - m_lastSyncAttemptTime < WEATHER_UPDATE_BACKOFF) {
return;
}
if(!m_features.wifi.Connect()) { if(!m_features.wifi.Connect()) {
m_lastSyncAttemptTime = currentTime;
return; return;
} }
@ -232,6 +237,7 @@ void WatchFacePages::Weather::Resync()
int httpCode = client.GET(); int httpCode = client.GET();
if (httpCode != 200) { if (httpCode != 200) {
m_features.wifi.Disconnect(); m_features.wifi.Disconnect();
m_lastSyncAttemptTime = currentTime;
return; return;
} }
@ -252,6 +258,7 @@ void WatchFacePages::Weather::Resync()
// Check if we got enough lines // Check if we got enough lines
if (lines.size() < 14) { if (lines.size() < 14) {
m_features.wifi.Disconnect(); m_features.wifi.Disconnect();
m_lastSyncAttemptTime = currentTime;
return; return;
} }
@ -261,9 +268,11 @@ void WatchFacePages::Weather::Resync()
memcpy(m_locationCity, location.c_str(), location.length()); memcpy(m_locationCity, location.c_str(), location.length());
m_locationLat = std::stof(lines[7]); m_locationLat = std::stof(lines[7]);
m_locationLon = std::stof(lines[8]); 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; std::ostringstream url;
url << "http://wttr.in/" << m_locationLat << "," << m_locationLon << "?0Q&format=%x:%t:%h:%w"; 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 // 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(); int httpCode = client.GET();
m_features.wifi.Disconnect(); m_features.wifi.Disconnect();
if (httpCode != 200) { if (httpCode != 200) {
m_lastSyncAttemptTime = currentTime;
return; return;
} }
@ -292,6 +302,7 @@ void WatchFacePages::Weather::Resync()
parts.push_back(reponse.substr(prev)); parts.push_back(reponse.substr(prev));
if (parts.size() != 4) { if (parts.size() != 4) {
m_lastSyncAttemptTime = currentTime;
return; return;
} }
@ -310,7 +321,6 @@ void WatchFacePages::Weather::Resync()
m_currentWindSpeed = std::round(windSpeedKmh * 0.277778); m_currentWindSpeed = std::round(windSpeedKmh * 0.277778);
m_features.wifi.Disconnect(); m_features.wifi.Disconnect();
Recalc();
m_lastSyncTime = currentTime; m_lastSyncTime = currentTime;
} }

View file

@ -27,6 +27,7 @@ private:
WatchyDisplay & m_display; WatchyDisplay & m_display;
WatchFeatures::WatchFeatures & m_features; WatchFeatures::WatchFeatures & m_features;
static RTC_DATA_ATTR uint64_t m_lastSyncTime; 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 int m_lastCalculatedDay;
static RTC_DATA_ATTR char m_locationCity[128]; static RTC_DATA_ATTR char m_locationCity[128];
static RTC_DATA_ATTR double m_locationLat, m_locationLon; static RTC_DATA_ATTR double m_locationLat, m_locationLon;

View file

@ -42,6 +42,7 @@
#define WAKE_ON_ACCEL_EVENTS false // useful if saving battery by not updating every minute #define WAKE_ON_ACCEL_EVENTS false // useful if saving battery by not updating every minute
#define WEATHER_UPDATE_INTERVAL 3600 // seconds #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 DO_GEOLOCATION true // if false then use defaults below
#define DEFAULT_LATITUDE 60.170833 #define DEFAULT_LATITUDE 60.170833