Skip to content

Commit 0945310

Browse files
committed
Refactored Alerts client to take array object
1 parent 0a36a53 commit 0945310

File tree

3 files changed

+85
-120
lines changed

3 files changed

+85
-120
lines changed

examples/WundergroundAlertsDemo/WundergroundAlertsDemo.ino

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,21 @@ void setup() {
8383

8484
Serial.println();
8585
Serial.println("\n\nNext Loop-Step: " + String(millis()) + ":");
86-
87-
wunderground.updateAlerts(WUNDERGRROUND_API_KEY, WUNDERGRROUND_LANGUAGE, WUNDERGR_UND_STATE_OR_COUNTRY, WUNDERGR_UND_CITY);
86+
uint8_t maxAlerts = 3;
87+
WGAlert alerts[maxAlerts];
88+
wunderground.updateAlerts(alerts, maxAlerts, WUNDERGRROUND_API_KEY, WUNDERGRROUND_LANGUAGE, WUNDERGR_UND_STATE_OR_COUNTRY, WUNDERGR_UND_CITY);
8889

8990
for (int i = 0; i < wunderground.getActiveAlertsCnt(); i++) {
9091
Serial.println("------------------------------------");
91-
92-
Serial.println("getActiveAlerts: " + wunderground.getActiveAlerts(i));
93-
Serial.println("getActiveAlertsText: " + wunderground.getActiveAlertsText(i));
94-
Serial.println("getActiveAlertsMessage: " + wunderground.getActiveAlertsMessage(i));
95-
Serial.println("getActiveAlertsMessageTrunc: " + wunderground.getActiveAlertsMessageTrunc(i));
96-
Serial.println("getActiveAlertsStart: " + wunderground.getActiveAlertsStart(i));
97-
Serial.println("getActiveAlertsEnd: " + wunderground.getActiveAlertsEnd(i));
98-
Serial.println("getActiveAlertsPhenomena: " + wunderground.getActiveAlertsPhenomena(i));
99-
Serial.println("getActiveAlertsSignificance: " + wunderground.getActiveAlertsSignificance(i));
100-
Serial.println("getActiveAlertsAttribution: " + wunderground.getActiveAlertsAttribution(i));
92+
Serial.println("activeAlerts: " + alerts[i].activeAlerts);
93+
Serial.println("activeAlertsMessage: " + alerts[i].activeAlertsMessage);
94+
Serial.println("activeAlertsMessageTrunc: " + alerts[i].activeAlertsMessageTrunc);
95+
Serial.println("activeAlertsText: " + alerts[i].activeAlertsText);
96+
Serial.println("activeAlertsStart: " + alerts[i].activeAlertsStart);
97+
Serial.println("activeAlertsEnd: " + alerts[i].activeAlertsEnd);
98+
Serial.println("activeAlertsPhenomena: " + alerts[i].activeAlertsPhenomena);
99+
Serial.println("activeAlertsSignificance: " + alerts[i].activeAlertsSignificance);
100+
Serial.println("activeAlertsAttribution: " + alerts[i].activeAlertsAttribution);
101101
}
102102

103103

src/WundergroundAlerts.cpp

Lines changed: 48 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ See more at http://blog.squix.ch
3030
WundergroundAlerts::WundergroundAlerts() {
3131

3232
}
33-
void WundergroundAlerts::updateAlerts(String apiKey, String language, String country, String city) {
33+
void WundergroundAlerts::updateAlerts(WGAlert *alerts, uint8_t maxAlerts, String apiKey, String language, String country, String city) {
3434
currentAlert = 0;
3535
activeAlertsCnt = 0;
3636
isAlerts = true;
@@ -41,11 +41,11 @@ void WundergroundAlerts::updateAlerts(String apiKey, String language, String cou
4141
isAlertUS = false;
4242
isAlertEU = true;
4343
}
44-
doUpdate("/api/" + apiKey + "/alerts/lang:" + language + "/q/" + country + "/" + city + ".json");
44+
doUpdate(alerts, maxAlerts, "/api/" + apiKey + "/alerts/lang:" + language + "/q/" + country + "/" + city + ".json");
4545
}
4646
// end fowlerk add
4747

48-
void WundergroundAlerts::updateAlertsPWS(String apiKey, String language, String country, String pws) {
48+
void WundergroundAlerts::updateAlertsPWS(WGAlert *alert, uint8_t maxAlerts, String apiKey, String language, String country, String pws) {
4949
currentAlert = 0;
5050
activeAlertsCnt = 0;
5151
isAlerts = true;
@@ -56,10 +56,12 @@ void WundergroundAlerts::updateAlertsPWS(String apiKey, String language, String
5656
isAlertUS = false;
5757
isAlertEU = true;
5858
}
59-
doUpdate("/api/" + apiKey + "/alerts/lang:" + language + "/q/pws:" + pws + ".json");
59+
doUpdate(alerts, maxAlerts, "/api/" + apiKey + "/alerts/lang:" + language + "/q/pws:" + pws + ".json");
6060
}
6161

62-
void WundergroundAlerts::doUpdate(String url) {
62+
void WundergroundAlerts::doUpdate(WGAlert *alerts, uint8_t maxAlerts, String url) {
63+
this->alerts = alerts;
64+
this->maxAlerts = maxAlerts;
6365
JsonStreamingParser parser;
6466
parser.setListener(this);
6567
WiFiClient client;
@@ -102,6 +104,7 @@ void WundergroundAlerts::doUpdate(String url) {
102104
}
103105
}
104106
}
107+
this->alerts = nullptr;
105108
}
106109

107110
void WundergroundAlerts::whitespace(char c) {
@@ -120,64 +123,74 @@ void WundergroundAlerts::value(String value) {
120123
if (currentKey == "type" && isAlerts) {
121124
activeAlertsCnt++;
122125
currentAlert++;
123-
activeAlerts[currentAlert-1] = value;
124-
Serial.print("Alert type processed, value: "); Serial.println(activeAlerts[currentAlert-1]);
126+
alerts[currentAlert-1].activeAlerts = value;
127+
Serial.print("Alert type processed, value: ");
128+
Serial.println(alerts[currentAlert-1].activeAlerts);
125129
}
126130
if (currentKey == "description" && isAlerts && isAlertUS) {
127-
activeAlertsText[currentAlert-1] = value;
128-
Serial.print("Alert description processed, value: "); Serial.println(activeAlertsText[currentAlert-1]);
131+
alerts[currentAlert-1].activeAlertsText = value;
132+
Serial.print("Alert description processed, value: ");
133+
Serial.println(alerts[currentAlert-1].activeAlertsText);
129134
}
130135
if (currentKey == "wtype_meteoalarm_name" && isAlerts && isAlertEU) {
131-
activeAlertsText[currentAlert-1] = value;
132-
Serial.print("Alert description processed, value: "); Serial.println(activeAlertsText[currentAlert-1]);
136+
alerts[currentAlert-1].activeAlertsText = value;
137+
Serial.print("Alert description processed, value: ");
138+
Serial.println(alerts[currentAlert-1].activeAlertsText);
133139
}
134140
if (currentKey == "message" && isAlerts) {
135-
activeAlertsMessage[currentAlert-1] = value;
136-
Serial.print("Alert msg length: "); Serial.println(activeAlertsMessage[currentAlert-1].length());
137-
if(activeAlertsMessage[currentAlert-1].length() >= 511) {
138-
activeAlertsMessageTrunc[currentAlert-1] = true;
141+
alerts[currentAlert-1].activeAlertsMessage = value;
142+
Serial.print("Alert msg length: ");
143+
Serial.println(alerts[currentAlert-1].activeAlertsMessage.length());
144+
if(alerts[currentAlert-1].activeAlertsMessage.length() >= 511) {
145+
alerts[currentAlert-1].activeAlertsMessageTrunc = true;
139146
} else {
140-
activeAlertsMessageTrunc[currentAlert-1] = false;
147+
alerts[currentAlert-1].activeAlertsMessageTrunc = false;
141148
}
142-
Serial.print("Alert message processed, value: "); Serial.println(activeAlertsMessage[currentAlert-1]);
149+
Serial.print("Alert message processed, value: ");
150+
Serial.println(alerts[currentAlert-1].activeAlertsMessage);
143151
}
144152
if (currentKey == "date" && isAlerts) {
145-
activeAlertsStart[currentAlert-1] = value;
153+
alerts[currentAlert-1].activeAlertsStart = value;
146154
// Check last char for a "/"; the returned value sometimes includes this; if so, strip it (47 is a "/" char)
147-
if (activeAlertsStart[currentAlert-1].charAt(activeAlertsStart[currentAlert-1].length()-1) == 47) {
155+
if (alerts[currentAlert-1].activeAlertsStart.charAt(alerts[currentAlert-1].activeAlertsStart.length()-1) == 47) {
148156
Serial.println("...last char is a slash...");
149-
activeAlertsStart[currentAlert-1] = activeAlertsStart[currentAlert-1].substring(0,(activeAlertsStart[currentAlert-1].length()-1));
157+
alerts[currentAlert-1].activeAlertsStart = alerts[currentAlert-1].activeAlertsStart.substring(0,(alerts[currentAlert-1].activeAlertsStart.length()-1));
150158
}
151159
// For meteoalarms, the start field is returned with the UTC=0 by default (not used?)
152-
if (isAlertEU && activeAlertsStart[currentAlert-1] == "1970-01-01 00:00:00 GMT") {
153-
activeAlertsStart[currentAlert-1] = "<Not specified>";
160+
if (isAlertEU && alerts[currentAlert-1].activeAlertsStart == "1970-01-01 00:00:00 GMT") {
161+
alerts[currentAlert-1].activeAlertsStart = "<Not specified>";
154162
}
155-
Serial.print("Alert start processed, value: "); Serial.println(activeAlertsStart[currentAlert-1]);
163+
Serial.print("Alert start processed, value: ");
164+
Serial.println(alerts[currentAlert-1].activeAlertsStart);
156165
}
157166
if (currentKey == "expires" && isAlerts) {
158-
activeAlertsEnd[currentAlert-1] = value;
159-
Serial.print("Alert expiration processed, value: "); Serial.println(activeAlertsEnd[currentAlert-1]);
167+
alerts[currentAlert-1].activeAlertsEnd = value;
168+
Serial.print("Alert expiration processed, value: ");
169+
Serial.println(alerts[currentAlert-1].activeAlertsEnd);
160170
}
161171
if (currentKey == "phenomena" && isAlerts) {
162-
activeAlertsPhenomena[currentAlert-1] = value;
163-
Serial.print("Alert phenomena processed, value: "); Serial.println(activeAlertsPhenomena[currentAlert-1]);
172+
alerts[currentAlert-1].activeAlertsPhenomena = value;
173+
Serial.print("Alert phenomena processed, value: ");
174+
Serial.println(alerts[currentAlert-1].activeAlertsPhenomena);
164175
}
165176
if (currentKey == "significance" && isAlerts && isAlertUS) {
166-
activeAlertsSignificance[currentAlert-1] = value;
167-
Serial.print("Alert significance processed, value: "); Serial.println(activeAlertsSignificance[currentAlert-1]);
177+
alerts[currentAlert-1].activeAlertsSignificance = value;
178+
Serial.print("Alert significance processed, value: ");
179+
Serial.println(alerts[currentAlert-1].activeAlertsSignificance);
168180
}
169181
// Map meteoalarm level to the field for significance for consistency (used for European alerts)
170182
if (currentKey == "level_meteoalarm" && isAlerts && isAlertEU) {
171-
activeAlertsSignificance[currentAlert-1] = value;
172-
Serial.print("Meteo alert significance processed, value: "); Serial.println(activeAlertsSignificance[currentAlert-1]);
183+
alerts[currentAlert-1].activeAlertsSignificance = value;
184+
Serial.print("Meteo alert significance processed, value: ");
185+
Serial.println(alerts[currentAlert-1].activeAlertsSignificance);
173186
}
174187
// For meteoalarms only (European alerts); attribution must be displayed according to the T&C's of use
175188
if (currentKey == "attribution" && isAlerts) {
176-
activeAlertsAttribution[currentAlert-1] = value;
189+
alerts[currentAlert-1].activeAlertsAttribution = value;
177190
// Remove some of the markup in the attribution
178-
activeAlertsAttribution[currentAlert-1].replace(" <a href='"," ");
179-
activeAlertsAttribution[currentAlert-1].replace("</a>","");
180-
activeAlertsAttribution[currentAlert-1].replace("/'>"," ");
191+
alerts[currentAlert-1].activeAlertsAttribution.replace(" <a href='"," ");
192+
alerts[currentAlert-1].activeAlertsAttribution.replace("</a>","");
193+
alerts[currentAlert-1].activeAlertsAttribution.replace("/'>"," ");
181194
}
182195

183196
}
@@ -203,42 +216,6 @@ void WundergroundAlerts::startArray() {
203216

204217
}
205218

206-
String WundergroundAlerts::getActiveAlerts(int alertIndex) {
207-
return activeAlerts[alertIndex];
208-
}
209-
210-
String WundergroundAlerts::getActiveAlertsText(int alertIndex) {
211-
return activeAlertsText[alertIndex];
212-
}
213-
214-
String WundergroundAlerts::getActiveAlertsMessage(int alertIndex) {
215-
return activeAlertsMessage[alertIndex];
216-
}
217-
218-
bool WundergroundAlerts::getActiveAlertsMessageTrunc(int alertIndex) {
219-
return activeAlertsMessageTrunc[alertIndex];
220-
}
221-
222-
String WundergroundAlerts::getActiveAlertsStart(int alertIndex) {
223-
return activeAlertsStart[alertIndex];
224-
}
225-
226-
String WundergroundAlerts::getActiveAlertsEnd(int alertIndex) {
227-
return activeAlertsEnd[alertIndex];
228-
}
229-
230-
String WundergroundAlerts::getActiveAlertsPhenomena(int alertIndex) {
231-
return activeAlertsPhenomena[alertIndex];
232-
}
233-
234-
String WundergroundAlerts::getActiveAlertsSignificance(int alertIndex) {
235-
return activeAlertsSignificance[alertIndex];
236-
}
237-
238-
String WundergroundAlerts::getActiveAlertsAttribution(int alertIndex) {
239-
return activeAlertsAttribution[alertIndex];
240-
}
241-
242219
int WundergroundAlerts::getActiveAlertsCnt() {
243220
return activeAlertsCnt;
244221
}

src/WundergroundAlerts.h

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,53 +30,41 @@ See more at http://blog.squix.ch
3030

3131
#define MAX_WEATHER_ALERTS 6 // The maximum number of concurrent weather alerts supported by the library
3232

33+
struct WGAlert {
34+
String activeAlerts;
35+
String activeAlertsMessage;
36+
bool activeAlertsMessageTrunc;
37+
String activeAlertsText;
38+
String activeAlertsStart;
39+
String activeAlertsEnd;
40+
String activeAlertsPhenomena;
41+
String activeAlertsSignificance;
42+
String activeAlertsAttribution;
43+
};
44+
3345
class WundergroundAlerts: public JsonListener {
3446
private:
3547
String currentKey;
3648
String currentParent = "";
49+
WGAlert *alerts;
50+
uint8_t maxAlerts;
3751

3852

39-
void doUpdate(String url);
40-
41-
boolean isAlerts = false; // Added by fowlerk
42-
boolean isAlertUS = false; // Added by fowlerk
43-
boolean isAlertEU = false; // Added by fowlerk
44-
String activeAlerts [MAX_WEATHER_ALERTS]; // For a max of 6 currently-active alerts
45-
String activeAlertsMessage [MAX_WEATHER_ALERTS]; // Alert full-text message
46-
bool activeAlertsMessageTrunc [MAX_WEATHER_ALERTS]; // Alert full-text message truncation flag
47-
String activeAlertsText [MAX_WEATHER_ALERTS]; // Alerts description text
48-
String activeAlertsStart [MAX_WEATHER_ALERTS]; // Start of alert date/time
49-
String activeAlertsEnd [MAX_WEATHER_ALERTS]; // Expiration of alert date/time
50-
String activeAlertsPhenomena [MAX_WEATHER_ALERTS]; // Alert phenomena code
51-
String activeAlertsSignificance [MAX_WEATHER_ALERTS]; // Alert significance code
52-
String activeAlertsAttribution [MAX_WEATHER_ALERTS]; // Alert significance code
53-
int activeAlertsCnt; // Number of active alerts
54-
int currentAlert; // For indexing the current active alert
55-
// end fowlerk add
56-
57-
public:
58-
WundergroundAlerts();
59-
void updateAlerts(String apiKey, String language, String country, String city); // Added by fowlerk, 18-Dec-2016
60-
void updateAlertsPWS(String apiKey, String language, String country, String pws);
61-
int getActiveAlertsCnt();
62-
63-
String getActiveAlerts(int alertIndex);
64-
65-
String getActiveAlertsText(int alertIndex);
66-
67-
String getActiveAlertsMessage(int alertIndex);
68-
69-
bool getActiveAlertsMessageTrunc(int alertIndex);
53+
void doUpdate(WGAlert *alerts, uint8_t maxAlerts, String url);
7054

71-
String getActiveAlertsStart(int alertIndex);
55+
boolean isAlerts = false;
56+
boolean isAlertUS = false;
57+
boolean isAlertEU = false;
7258

73-
String getActiveAlertsEnd(int alertIndex);
59+
int activeAlertsCnt; // Number of active alerts
60+
int currentAlert; // For indexing the current active alert
7461

75-
String getActiveAlertsPhenomena(int alertIndex);
7662

77-
String getActiveAlertsSignificance(int alertIndex);
78-
79-
String getActiveAlertsAttribution(int alertIndex);
63+
public:
64+
WundergroundAlerts();
65+
void updateAlerts(WGAlert *alert, uint8_t maxAlerts, String apiKey, String language, String country, String city); // Added by fowlerk, 18-Dec-2016
66+
void updateAlertsPWS(WGAlert *alert, uint8_t maxAlerts, String apiKey, String language, String country, String pws);
67+
int getActiveAlertsCnt();
8068

8169
virtual void whitespace(char c);
8270

0 commit comments

Comments
 (0)