Adding json weather functions

pull/156/head
Michael-Paul Moore 2022-04-10 09:43:32 -07:00
parent 66a4c9b84e
commit 2b96e25364
2 changed files with 119 additions and 63 deletions

View File

@ -957,3 +957,54 @@ bool WatchyExpanded::syncNTP(long gmt, int dst, String ntpServer){ //NTP sync -
RTC.set(tm);
return true;
}
JSONVar& WatchyExpanded::getWeatherJSON()
{
getWeatherJSON(settings.cityID, settings.weatherUnit, settings.weatherLang, settings.weatherURL,
settings.weatherAPIKey, settings.weatherUpdateInterval);
return m_responseObject;
}
JSONVar& WatchyExpanded::getWeatherJSON(String cityID, String units, String lang, String url, String apiKey, uint8_t updateInterval)
{
if(weatherIntervalCounter < 0) //-1 on first run, set to updateInterval
weatherIntervalCounter = updateInterval;
if(weatherIntervalCounter >= updateInterval) //only update if WEATHER_UPDATE_INTERVAL has elapsed i.e. 30 minutes
{
if(connectWiFi())
{
HTTPClient http; //Use Weather API for live data if WiFi is connected
http.setConnectTimeout(3000);//3 second max timeout
const String& weatherQueryURL = url + cityID + String("&units=") + units + String("&lang=") + lang + String("&appid=") + apiKey;
http.begin(weatherQueryURL.c_str());
int httpResponseCode = http.GET();
if(httpResponseCode == 200)
{
String payload = http.getString();
const JSONVar& jsonNew = JSON.parse(payload);
const JSONVar jsonOld = m_responseObject;
m_responseObject = jsonNew;
if (!json.hasOwnProperty("weather"))
m_responseObject["weather"] = jsonOld["weather"];
}
else
{
//http error
}
http.end();
//turn off radios
WiFi.mode(WIFI_OFF);
btStop();
}
weatherIntervalCounter = 0;
}
else
{
++weatherIntervalCounter;
}
return m_responseObject;
}

View File

@ -71,6 +71,11 @@ class WatchyExpanded
void showWatchFace(bool partialRefresh);
virtual void drawWatchFace(); //override this method for different watch faces
// Expanded
JSONVar& getWeatherJSON();
JSONVar& getWeatherJSON(String cityID, String units, String lang, String url, String apiKey,
uint8_t updateInterval);
private:
void _bmaConfig();
static void _configModeCallback(WiFiManager *myWiFiManager);