Skip to content

Commit c71bc61

Browse files
authored
Changes to the Weather Underground client
Changes to the Weather Underground client components to add feels-like temp, UV index, month/day of the forecast, forecast text, and observation date/time text.
1 parent fdfb94e commit c71bc61

File tree

2 files changed

+156
-5
lines changed

2 files changed

+156
-5
lines changed

WundergroundClient.cpp

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ void WundergroundClient::updateAstronomy(String apiKey, String language, String
5757
}
5858
// end JJG add ////////////////////////////////////////////////////////////////////
5959

60+
// fowlerk added
61+
void WundergroundClient::updateAlerts(String apiKey, String language, String country, String city) {
62+
isForecast = true;
63+
doUpdate("/api/" + apiKey + "/alerts/lang:" + language + "/q/" + country + "/" + city + ".json");
64+
}
65+
// end fowlerk add
66+
6067
void WundergroundClient::doUpdate(String url) {
6168
JsonStreamingParser parser;
6269
parser.setListener(this);
@@ -112,12 +119,39 @@ void WundergroundClient::startDocument() {
112119

113120
void WundergroundClient::key(String key) {
114121
currentKey = String(key);
122+
// Restructured following logic to accomodate the multiple types of JSON returns based on the API. This was necessary since several
123+
// keys are reused between various types of API calls, resulting in confusing returns in the original function. Various booleans
124+
// now indicate whether the JSON stream being processed is part of the text forecast (txt_forecast), the first section of the 10-day
125+
// forecast API that contains detailed text for the forecast period; the simple forecast (simpleforecast), the second section of the
126+
// 10-day forecast API that contains such data as forecast highs/lows, conditions, precipitation / probabilities; the current
127+
// observations (current_observation), from the observations API call; or alerts (alerts), for the future) weather alerts API call.
128+
// Added by fowlerk...18-Dec-2016
115129
if (currentKey == "txt_forecast") {
116-
isSimpleForecast = false;
130+
isForecast = true;
131+
isCurrentObservation = false; // fowlerk
132+
isSimpleForecast = false; // fowlerk
133+
isAlerts = false; // fowlerk
117134
}
118135
if (currentKey == "simpleforecast") {
119136
isSimpleForecast = true;
137+
isCurrentObservation = false; // fowlerk
138+
isForecast = false; // fowlerk
139+
isAlerts = false; // fowlerk
120140
}
141+
// Added by fowlerk...
142+
if (currentKey == "current_observation") {
143+
isCurrentObservation = true;
144+
isSimpleForecast = false;
145+
isForecast = false;
146+
isAlerts = false;
147+
}
148+
if (currentKey == "alerts") {
149+
isCurrentObservation = false;
150+
isSimpleForecast = false;
151+
isForecast = false;
152+
isAlerts = true;
153+
}
154+
// end fowlerk add
121155
}
122156

123157
void WundergroundClient::value(String value) {
@@ -218,6 +252,12 @@ void WundergroundClient::value(String value) {
218252
if (currentKey == "observation_time_rfc822") {
219253
date = value.substring(0, 16);
220254
}
255+
// Begin add, fowlerk...04-Dec-2016
256+
if (currentKey == "observation_time") {
257+
observationTime = value;
258+
}
259+
// end add, fowlerk
260+
221261
if (currentKey == "temp_f" && !isMetric) {
222262
currentTemp = value;
223263
}
@@ -229,7 +269,8 @@ void WundergroundClient::value(String value) {
229269
Serial.println(String(currentForecastPeriod) + ": " + value + ":" + currentParent);
230270
forecastIcon[currentForecastPeriod] = value;
231271
}
232-
if (!isForecast) {
272+
// if (!isForecast) { // Removed by fowlerk
273+
if (isCurrentObservation && !(isForecast || isSimpleForecast)) { // Added by fowlerk
233274
weatherIcon = value;
234275
}
235276
}
@@ -245,6 +286,21 @@ void WundergroundClient::value(String value) {
245286
if (currentKey == "pressure_in" && !isMetric) {
246287
pressure = value + "in";
247288
}
289+
// fowlerk added...
290+
if (currentKey == "feelslike_f" && !isMetric) {
291+
feelslike = value;
292+
}
293+
294+
if (currentKey == "feelslike_c" && isMetric) {
295+
feelslike = value;
296+
}
297+
298+
if (currentKey == "UV") {
299+
UV = value;
300+
}
301+
302+
// end fowlerk add
303+
248304
if (currentKey == "dewpoint_f" && !isMetric) {
249305
dewPoint = value;
250306
}
@@ -260,10 +316,22 @@ void WundergroundClient::value(String value) {
260316
if (currentKey == "period") {
261317
currentForecastPeriod = value.toInt();
262318
}
263-
if (currentKey == "title" && currentForecastPeriod < MAX_FORECAST_PERIODS) {
319+
// Modified below line to add check to ensure we are processing the 10-day forecast
320+
// before setting the forecastTitle (day of week of the current forecast day).
321+
// (The keyword title is used in both the current observation and the 10-day forecast.)
322+
// Modified by fowlerk
323+
// if (currentKey == "title" && currentForecastPeriod < MAX_FORECAST_PERIODS) { // Removed, fowlerk
324+
if (currentKey == "title" && isForecast && currentForecastPeriod < MAX_FORECAST_PERIODS) {
264325
Serial.println(String(currentForecastPeriod) + ": " + value);
265326
forecastTitle[currentForecastPeriod] = value;
266327
}
328+
329+
// Added forecastText key following...fowlerk, 12/3/16
330+
if (currentKey == "fcttext" && isForecast && currentForecastPeriod < MAX_FORECAST_PERIODS) {
331+
forecastText[currentForecastPeriod] = value;
332+
}
333+
// end fowlerk add, 12/3/16
334+
267335
// The detailed forecast period has only one forecast per day with low/high for both
268336
// night and day, starting at index 1.
269337
int dailyForecastPeriod = (currentForecastPeriod - 1) * 2;
@@ -287,6 +355,28 @@ void WundergroundClient::value(String value) {
287355
forecastLowTemp[dailyForecastPeriod] = value;
288356
}
289357
}
358+
// fowlerk added...to pull month/day from the forecast period
359+
if (currentKey == "month" && isSimpleForecast && currentForecastPeriod < MAX_FORECAST_PERIODS) {
360+
// Added by fowlerk to handle transition from txtforecast to simpleforecast, as
361+
// the key "period" doesn't appear until after some of the key values needed and is
362+
// used as an array index.
363+
if (isSimpleForecast && currentForecastPeriod == 19) {
364+
currentForecastPeriod = 0;
365+
}
366+
forecastMonth[currentForecastPeriod] = value;
367+
}
368+
369+
if (currentKey == "day" && isSimpleForecast && currentForecastPeriod < MAX_FORECAST_PERIODS) {
370+
// Added by fowlerk to handle transition from txtforecast to simpleforecast, as
371+
// the key "period" doesn't appear until after some of the key values needed and is
372+
// used as an array index.
373+
if (isSimpleForecast && currentForecastPeriod == 19) {
374+
currentForecastPeriod = 0;
375+
}
376+
forecastDay[currentForecastPeriod] = value;
377+
}
378+
// end fowlerk add
379+
290380
}
291381

292382
void WundergroundClient::endArray() {
@@ -410,6 +500,21 @@ String WundergroundClient::getPressure() {
410500
String WundergroundClient::getDewPoint() {
411501
return dewPoint;
412502
}
503+
// fowlerk added...
504+
String WundergroundClient::getFeelsLike() {
505+
return feelslike;
506+
}
507+
508+
String WundergroundClient::getUV() {
509+
return UV;
510+
}
511+
512+
// Added by fowlerk, 04-Dec-2016
513+
String WundergroundClient::getObservationTime() {
514+
return observationTime;
515+
}
516+
// end fowlerk add
517+
413518

414519
String WundergroundClient::getPrecipitationToday() {
415520
return precipitationToday;
@@ -438,6 +543,23 @@ String WundergroundClient::getForecastLowTemp(int period) {
438543
String WundergroundClient::getForecastHighTemp(int period) {
439544
return forecastHighTemp[period];
440545
}
546+
// fowlerk added...
547+
String WundergroundClient::getForecastDay(int period) {
548+
// Serial.print("Day period: "); Serial.println(period);
549+
return forecastDay[period];
550+
}
551+
552+
String WundergroundClient::getForecastMonth(int period) {
553+
// Serial.print("Month period: "); Serial.println(period);
554+
return forecastMonth[period];
555+
}
556+
557+
String WundergroundClient::getForecastText(int period) {
558+
Serial.print("Forecast period: "); Serial.println(period);
559+
return forecastText[period];
560+
}
561+
// end fowlerk add
562+
441563

442564
String WundergroundClient::getMeteoconIcon(String iconText) {
443565
if (iconText == "chanceflurries") return "F";

WundergroundClient.h

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ See more at http://blog.squix.ch
2828
#include <JsonListener.h>
2929
#include <JsonStreamingParser.h>
3030

31-
#define MAX_FORECAST_PERIODS 12 // Changed from 7 to 12 to support 6 day / 2 screen forecast (Neptune)
31+
#define MAX_FORECAST_PERIODS 20 // Changed from 7 to 12 to support 6 day / 2 screen forecast (Neptune)
32+
// Changed to 20 to support max 10-day forecast returned from 'forecast10day' API (fowlerk)
3233

3334
class WundergroundClient: public JsonListener {
3435
private:
@@ -57,23 +58,37 @@ class WundergroundClient: public JsonListener {
5758
String pressure;
5859
String dewPoint;
5960
String precipitationToday;
61+
// fowlerk added...
62+
String feelslike;
63+
String UV;
64+
String observationTime; // fowlerk add, 04-Dec-2016
65+
// end fowlerk add
66+
6067
void doUpdate(String url);
6168

6269
// forecast
6370
boolean isForecast = false;
64-
boolean isSimpleForecast = true;
71+
boolean isSimpleForecast = false; // true; fowlerk
72+
boolean isCurrentObservation = false; // Added by fowlerk
73+
boolean isAlerts = false; // Added by fowlerk
6574
int currentForecastPeriod;
6675
String forecastIcon [MAX_FORECAST_PERIODS];
6776
String forecastTitle [MAX_FORECAST_PERIODS];
6877
String forecastLowTemp [MAX_FORECAST_PERIODS];
6978
String forecastHighTemp [MAX_FORECAST_PERIODS];
79+
// fowlerk added...
80+
String forecastDay [MAX_FORECAST_PERIODS/2];
81+
String forecastMonth [MAX_FORECAST_PERIODS/2];
82+
String forecastText [MAX_FORECAST_PERIODS];
83+
// end fowlerk add
7084

7185
public:
7286
WundergroundClient(boolean isMetric);
7387
void updateConditions(String apiKey, String language, String country, String city);
7488
void updateConditions(String apiKey, String language, String zmwCode);
7589
void updateForecast(String apiKey, String language, String country, String city);
7690
void updateAstronomy(String apiKey, String language, String country, String city);
91+
void updateAlerts(String apiKey, String language, String country, String city); // Added by fowlerk, 18-Dec-2016
7792
// JJG added
7893
String getHours();
7994
String getMinutes();
@@ -109,6 +124,13 @@ class WundergroundClient: public JsonListener {
109124
String getDewPoint();
110125

111126
String getPrecipitationToday();
127+
// fowlerk added...
128+
String getFeelsLike();
129+
130+
String getUV();
131+
132+
String getObservationTime(); // fowlerk add, 04-Dec-2016
133+
// end fowlerk add
112134

113135
String getForecastIcon(int period);
114136

@@ -117,6 +139,13 @@ class WundergroundClient: public JsonListener {
117139
String getForecastLowTemp(int period);
118140

119141
String getForecastHighTemp(int period);
142+
// fowlerk added...
143+
String getForecastDay(int period);
144+
145+
String getForecastMonth(int period);
146+
147+
String getForecastText(int period);
148+
// end fowlerk add
120149

121150
virtual void whitespace(char c);
122151

0 commit comments

Comments
 (0)