Skip to content

Commit 0a36a53

Browse files
committed
Refactored astronomy to get the object to have better control over memory
1 parent 89e1d4b commit 0a36a53

File tree

3 files changed

+41
-67
lines changed

3 files changed

+41
-67
lines changed

examples/WundergroundAstronomyDemo/WundergroundAstronomyDemo.ino

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

8484
Serial.println();
8585
Serial.println("\n\nNext Loop-Step: " + String(millis()) + ":");
86-
87-
wunderground.updateAstronomy(WUNDERGRROUND_API_KEY, WUNDERGRROUND_LANGUAGE, WUNDERGR_UND_STATE_OR_COUNTRY, WUNDERGR_UND_CITY);
88-
89-
Serial.println("wundergroundMoonPctIlum: " + wunderground.getMoonPctIlum());
90-
Serial.println("wundergroundMoonAge: " + wunderground.getMoonAge());
91-
Serial.println("wundergroundMoonPhase: " + wunderground.getMoonPhase());
92-
Serial.println("wundergroundSunriseTime: " + wunderground.getSunriseTime());
93-
Serial.println("wundergroundSunsetTime: " + wunderground.getSunsetTime());
94-
Serial.println("wundergroundMoonriseTime: " + wunderground.getMoonriseTime());
95-
Serial.println("wundergroundMoonsetTime: " + wunderground.getMoonsetTime());
86+
WGAstronomy astronomy;
87+
wunderground.updateAstronomy(&astronomy, WUNDERGRROUND_API_KEY, WUNDERGRROUND_LANGUAGE, WUNDERGR_UND_STATE_OR_COUNTRY, WUNDERGR_UND_CITY);
88+
89+
Serial.println("moonPctIlum: " + astronomy.moonPctIlum);
90+
Serial.println("moonAge: " + astronomy.moonAge);
91+
Serial.println("moonPhase: " + astronomy.moonPhase);
92+
Serial.println("sunriseTime: " + astronomy.sunriseTime);
93+
Serial.println("sunsetTime: " + astronomy.sunsetTime);
94+
Serial.println("moonriseTime: " + astronomy.moonriseTime);
95+
Serial.println("moonsetTime: " + astronomy.moonsetTime);
9696

9797

9898
Serial.println();

src/WundergroundAstronomy.cpp

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@ See more at http://blog.squix.ch
3131
WundergroundAstronomy::WundergroundAstronomy(boolean _usePM) {
3232
usePM = _usePM;
3333
}
34-
void WundergroundAstronomy::updateAstronomy(String apiKey, String language, String country, String city) {
35-
doUpdate("/api/" + apiKey + "/astronomy/lang:" + language + "/q/" + country + "/" + city + ".json");
34+
void WundergroundAstronomy::updateAstronomy(WGAstronomy *astronomy, String apiKey, String language, String country, String city) {
35+
doUpdate(astronomy, "/api/" + apiKey + "/astronomy/lang:" + language + "/q/" + country + "/" + city + ".json");
3636
}
3737
// end JJG add ////////////////////////////////////////////////////////////////////
3838

39-
void WundergroundAstronomy::updateAstronomyPWS(String apiKey, String language, String pws) {
40-
doUpdate("/api/" + apiKey + "/astronomy/lang:" + language + "/q/pws:" + pws + ".json");
39+
void WundergroundAstronomy::updateAstronomyPWS(WGAstronomy *astronomy, String apiKey, String language, String pws) {
40+
doUpdate(astronomy, "/api/" + apiKey + "/astronomy/lang:" + language + "/q/pws:" + pws + ".json");
4141
}
4242

43-
void WundergroundAstronomy::doUpdate(String url) {
43+
void WundergroundAstronomy::doUpdate(WGAstronomy *astronomy, String url) {
44+
this->astronomy = astronomy;
4445
JsonStreamingParser parser;
4546
parser.setListener(this);
4647
WiFiClient client;
@@ -100,15 +101,15 @@ void WundergroundAstronomy::key(String key) {
100101
void WundergroundAstronomy::value(String value) {
101102

102103
if (currentKey == "ageOfMoon") {
103-
moonAge = value;
104+
astronomy->moonAge = value;
104105
}
105106

106107
if (currentKey == "phaseofMoon") {
107-
moonPhase = value;
108+
astronomy->moonPhase = value;
108109
}
109110

110111
if (currentKey == "percentIlluminated") {
111-
moonPctIlum = value;
112+
astronomy->moonPctIlum = value;
112113
}
113114

114115

@@ -122,13 +123,13 @@ void WundergroundAstronomy::value(String value) {
122123
else isPM = false;
123124
char tempHourBuff[3] = ""; // fowlerk add for formatting, 12/22/16
124125
sprintf(tempHourBuff, "%2d", tempHour); // fowlerk add for formatting, 12/22/16
125-
sunriseTime = String(tempHourBuff); // fowlerk add for formatting, 12/22/16
126+
astronomy->sunriseTime = String(tempHourBuff); // fowlerk add for formatting, 12/22/16
126127
//sunriseTime = value;
127128
}
128129
if (currentKey == "minute") {
129130
char tempMinBuff[3] = ""; // fowlerk add for formatting, 12/22/16
130131
sprintf(tempMinBuff, "%02d", value.toInt()); // fowlerk add for formatting, 12/22/16
131-
sunriseTime += ":" + String(tempMinBuff); // fowlerk add for formatting, 12/22/16
132+
astronomy->sunriseTime += ":" + String(tempMinBuff); // fowlerk add for formatting, 12/22/16
132133
if (isPM) sunriseTime += "pm";
133134
else if (usePM) sunriseTime += "am";
134135
}
@@ -145,13 +146,13 @@ void WundergroundAstronomy::value(String value) {
145146
else isPM = false;
146147
char tempHourBuff[3] = ""; // fowlerk add for formatting, 12/22/16
147148
sprintf(tempHourBuff, "%2d", tempHour); // fowlerk add for formatting, 12/22/16
148-
sunsetTime = String(tempHourBuff); // fowlerk add for formatting, 12/22/16
149+
astronomy->sunsetTime = String(tempHourBuff); // fowlerk add for formatting, 12/22/16
149150
// sunsetTime = value;
150151
}
151152
if (currentKey == "minute") {
152153
char tempMinBuff[3] = ""; // fowlerk add for formatting, 12/22/16
153154
sprintf(tempMinBuff, "%02d", value.toInt()); // fowlerk add for formatting, 12/22/16
154-
sunsetTime += ":" + String(tempMinBuff); // fowlerk add for formatting, 12/22/16
155+
astronomy->sunsetTime += ":" + String(tempMinBuff); // fowlerk add for formatting, 12/22/16
155156
if (isPM) sunsetTime += "pm";
156157
else if(usePM) sunsetTime += "am";
157158
}
@@ -167,13 +168,13 @@ void WundergroundAstronomy::value(String value) {
167168
else isPM = false;
168169
char tempHourBuff[3] = ""; // fowlerk add for formatting, 12/22/16
169170
sprintf(tempHourBuff, "%2d", tempHour); // fowlerk add for formatting, 12/22/16
170-
moonriseTime = String(tempHourBuff); // fowlerk add for formatting, 12/22/16
171+
astronomy->moonriseTime = String(tempHourBuff); // fowlerk add for formatting, 12/22/16
171172
// moonriseTime = value;
172173
}
173174
if (currentKey == "minute") {
174175
char tempMinBuff[3] = ""; // fowlerk add for formatting, 12/22/16
175176
sprintf(tempMinBuff, "%02d", value.toInt()); // fowlerk add for formatting, 12/22/16
176-
moonriseTime += ":" + String(tempMinBuff); // fowlerk add for formatting, 12/22/16
177+
astronomy->moonriseTime += ":" + String(tempMinBuff); // fowlerk add for formatting, 12/22/16
177178
if (isPM) moonriseTime += "pm";
178179
else if (usePM) moonriseTime += "am";
179180
}
@@ -183,12 +184,12 @@ void WundergroundAstronomy::value(String value) {
183184
if (currentKey == "hour") {
184185
char tempHourBuff[3] = ""; // fowlerk add for formatting, 12/22/16
185186
sprintf(tempHourBuff, "%2d", value.toInt()); // fowlerk add for formatting, 12/22/16
186-
moonsetTime = String(tempHourBuff); // fowlerk add for formatting, 12/22/16
187+
astronomy->moonsetTime = String(tempHourBuff); // fowlerk add for formatting, 12/22/16
187188
}
188189
if (currentKey == "minute") {
189190
char tempMinBuff[3] = ""; // fowlerk add for formatting, 12/22/16
190191
sprintf(tempMinBuff, "%02d", value.toInt()); // fowlerk add for formatting, 12/22/16
191-
moonsetTime += ":" + String(tempMinBuff); // fowlerk add for formatting, 12/22/16
192+
astronomy->moonsetTime += ":" + String(tempMinBuff); // fowlerk add for formatting, 12/22/16
192193
}
193194
}
194195

@@ -214,33 +215,3 @@ void WundergroundAstronomy::endDocument() {
214215
void WundergroundAstronomy::startArray() {
215216

216217
}
217-
218-
219-
// JJG added ... /////////////////////////////////////////////////////////////////////////////////////////
220-
String WundergroundAstronomy::getMoonPctIlum() {
221-
return moonPctIlum;
222-
}
223-
224-
String WundergroundAstronomy::getMoonAge() {
225-
return moonAge;
226-
}
227-
228-
String WundergroundAstronomy::getMoonPhase() {
229-
return moonPhase;
230-
}
231-
232-
String WundergroundAstronomy::getSunriseTime() {
233-
return sunriseTime;
234-
}
235-
236-
String WundergroundAstronomy::getSunsetTime() {
237-
return sunsetTime;
238-
}
239-
240-
String WundergroundAstronomy::getMoonriseTime() {
241-
return moonriseTime;
242-
}
243-
244-
String WundergroundAstronomy::getMoonsetTime() {
245-
return moonsetTime;
246-
}

src/WundergroundAstronomy.h

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

31+
struct WGAstronomy {
32+
String moonPctIlum;
33+
String moonAge;
34+
String moonPhase;
35+
String sunriseTime;
36+
String sunsetTime;
37+
String moonriseTime;
38+
String moonsetTime;
39+
};
40+
3141
class WundergroundAstronomy: public JsonListener {
3242
private:
43+
WGAstronomy *astronomy;
3344
String currentKey;
3445
String currentParent = "";
3546
String moonPctIlum; // not used
@@ -43,21 +54,13 @@ class WundergroundAstronomy: public JsonListener {
4354
boolean usePM;
4455
boolean isPM;
4556

46-
void doUpdate(String url);
57+
void doUpdate(WGAstronomy *astronomy, String url);
4758

4859

4960
public:
5061
WundergroundAstronomy(boolean usePM);
51-
void updateAstronomy(String apiKey, String language, String country, String city);
52-
void updateAstronomyPWS(String apiKey, String language, String pws);
53-
54-
String getMoonPctIlum();
55-
String getMoonAge();
56-
String getMoonPhase();
57-
String getSunriseTime();
58-
String getSunsetTime();
59-
String getMoonriseTime();
60-
String getMoonsetTime();
62+
void updateAstronomy(WGAstronomy *astronomy, String apiKey, String language, String country, String city);
63+
void updateAstronomyPWS(WGAstronomy *astronomy, String apiKey, String language, String pws);
6164

6265
virtual void whitespace(char c);
6366

0 commit comments

Comments
 (0)