-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wunderground.pde
358 lines (330 loc) · 11 KB
/
Wunderground.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details. */
public class Wunderground {
// processing.xmlweather.xmlweatherElement weatherData;
// processing.xmlweather.xmlweatherElement currentObservations;
Gif loopingGif;
String[] wgXML;
String temp_c; //getTemp
String high_c; //getTemp
String low_c; //getTemp
String wind_degrees; //getWindDirection
String wind_gust_kph; //getWindGust
String wind_kph; //getWindSpeed
String pressure_mb; //getAirPressure
String dewpoint_c; //getDewPoint
String heat_index_c; //getHeatIndex
String windchill_c; //getWindChill
String icon = "Offline"; //getCondition
String forecast_url = "www.example.com"; //getForecastLink
String observation_epoch = "8975640"; //getUpdateTime
String relative_humidity; //getHumidity
public int hitCount;
public int hitCountPerMinute;
public int date = 0;
public int minutes = 0;
String updateSuccess;
Gif APIfail = new Gif(getPapplet(), dataPath("") + "APIrate.gif");
void setHitCounter(int dayCount, int minCount) {
int hitCount = dayCount;
println("hitcount:"+ hitCount);
int hitCountPerMinute = minCount;
}
public String getCondition() {
String formatedIcon = "";
String[] temp = new String [2];
temp[0] = icon.substring(0, 1).toUpperCase();
temp[1] = icon.substring(1, icon.length());
formatedIcon = join(temp, "");
return formatedIcon;
}
public String getUpdateTime() {
String date = "";
date = new java.text.SimpleDateFormat("MMM d, h:mm zz").format(new java.util.Date (int(observation_epoch)*1000L));
return date;
}
public String getHumidity() {
String[] humidityOutput = new String[2];
humidityOutput[0] = new String("");
humidityOutput[1] = new String("");
//accepts celsius, fahrenheit, kelvin as valid units
humidityOutput = split (relative_humidity, "%") ;
return humidityOutput[0];
}
public String getForecastLink() {
String forecast;
forecast = forecast_url;
return forecast_url;
}
public float getWindChill(String units) {
//accepts celsius, fahrenheit, kelvin as valid units
float windChill;
float windChillOutput = 0;
windChill = float(windchill_c);
if (units.equals("celsius")) {
windChillOutput = windChill;
}
if (units.equals("fahrenheit")) {
windChillOutput = ((windChill * (9/5)) + 32) ;
}
if (units.equals("kelvin")) {
windChillOutput = windChill + 273.15;
}
return windChillOutput;
}
public float getHeatIndex(String units) {
//accepts celsius, fahrenheit, kelvin as valid units
float heatIndex ;
float heatIndexOutput = 0;
heatIndex = float(heat_index_c);
if (units.equals("celsius")) {
heatIndexOutput = heatIndex;
}
if (units.equals("fahrenheit")) {
heatIndexOutput = ((heatIndex * (9/5)) + 32) ;
}
if (units.equals("kelvin")) {
heatIndexOutput = heatIndex + 273.15;
}
return heatIndexOutput;
}
public float getDewPoint(String units) {
//accepts celsius, fahrenheit, kelvin as valid units
float dewpoint;
float dewPointOutput = 0;
dewpoint = float(dewpoint_c);
if (units.equals("celsius")) {
dewPointOutput = dewpoint;
}
if (units.equals("fahrenheit")) {
dewPointOutput = ((dewpoint * (9/5)) + 32) ;
}
if (units.equals("kelvin")) {
dewPointOutput = dewpoint + 273.15;
}
return dewPointOutput;
}
public float getAirPressure(String units) {
//accepts mbar, mmHg, or inHg as units
float airPressureFloat;
float airPressureOutput = 0;
airPressureFloat = float(pressure_mb);
if (units.equals("mbar")) {
airPressureOutput = airPressureFloat;
}
if (units.equals("inHg")) {
airPressureOutput = (airPressureFloat * .0295333727) ;
}
if (units.equals("mmHg")) {
airPressureOutput = (airPressureFloat * .75006375541921);
}
return airPressureOutput;
}
public float getWindSpeed(String units) {
//accepts kmh,mph, or kts as units
float windSpeed;
float windSpeedOutput = 0;
windSpeed = float(wind_kph);
if (units.equals("kmh")) {
windSpeedOutput = windSpeed;
}
if (units.equals("mph")) {
windSpeedOutput = (windSpeed * .621371) ;
}
if (units.equals("kts")) {
windSpeedOutput = (windSpeed * .539956803);
}
return windSpeedOutput;
}
public float getWindGust(String units) {
//accepts kmh,mph, or kts as units
float windGustOutput = 0;
float windGust;
windGust = float(wind_gust_kph);
if (units.equals("kmh")) {
windGustOutput = windGust;
}
if (units.equals("mph")) {
windGustOutput = (windGust * .621371) ;
}
if (units.equals("kts")) {
windGustOutput = (windGust * .539956803);
}
return windGustOutput;
}
public float getWindDirection(String units) {
//accepts degrees or radians as units
float directionOutput = 0;
float windDirectionFloat;
windDirectionFloat = float(wind_degrees);
if (units.equals("degrees")) {
directionOutput = windDirectionFloat;
}
if (units.equals("radians")) {
directionOutput = ((windDirectionFloat * PI) / 180);
}
return directionOutput;
}
public float getTemp(String units) {
//accepts celsius, fahrenheit, kelvin as valid units
float tempOutput = 0;
float temperature;
temperature = float(temp_c);
if (units.equals("celsius")) {
tempOutput = temperature;
}
if (units.equals("fahrenheit")) {
tempOutput = (temperature*1.8 + 32) ;
}
if (units.equals("kelvin")) {
tempOutput = temperature + 273.15;
}
return tempOutput;
}
Gif updateRadar(String zipCode, String key, int imgHeight, int imgWidth) {
if (date != day()) {
println("date:"+day());
println("reseting hitcount");
hitCount = 0;
date = day();
}
if (minutes != minute()) {
hitCountPerMinute = 0;
minutes = minute();
}
if (hitCount <= 490) {
if (hitCountPerMinute <= 3) {
println(hitCountPerMinute);
//Request the current conditions from Wunderground in xmlweather format
hitCount++;
hitCountPerMinute++;
String [] urlParts = new String [5];
urlParts[0] = "http://api.wunderground.com/api/";
urlParts[1] = key;
urlParts[2] = "/animatedradar/q/";
urlParts[3] = zipCode;
urlParts[4] = ".gif?newmaps=1&timelabel=1&timelabel.y=10&num=8&delay=50"+"&width="+str(imgWidth)+"&height="+str(imgHeight);
String updateURL = join(urlParts, "");
println(updateURL);
loopingGif = new Gif(getPapplet(), updateURL);
loopingGif.loop();
}
else {
loopingGif = APIfail;
loopingGif.noLoop();
}
}
else {
loopingGif = APIfail;
loopingGif.noLoop();
}
return loopingGif;
}
public String update(String zipCode, String key) {
if (date != day()) {
println("date:"+day());
println("reseting hitcount");
hitCount = 0;
date = day();
}
if (minutes != minute()) {
hitCountPerMinute = 0;
minutes = minute();
}
if (hitCount <= 490) {
if (hitCountPerMinute <= 3) {
//Construct URL to fetch from the zipcode provided
String [] urlParts = new String [5];
urlParts[0] = "http://api.wunderground.com/api/";
urlParts[1] = key;
urlParts[2] = "/conditions/q/";
urlParts[3] = zipCode;
urlParts[4] = ".xml";
String updateURL = join(urlParts, "");
// println(updateURL);
//Request the current conditions from Wunderground in xmlweather format
try {
XMLTag xmlweather = XMLDoc.from(new URL(updateURL), true);
hitCount++;
hitCountPerMinute++;
xmlweather.gotoRoot();
xmlweather.gotoChild("current_observation");
xmlweather.gotoChild("temp_c");
temp_c = xmlweather.getText().toString();
xmlweather.gotoParent();
xmlweather.gotoChild("wind_degrees");
wind_degrees = xmlweather.getText().toString();
xmlweather.gotoParent();
xmlweather.gotoChild("wind_gust_kph");
wind_gust_kph = xmlweather.getText().toString();
xmlweather.gotoParent();
xmlweather.gotoChild("wind_kph");
wind_kph = xmlweather.getText().toString();
xmlweather.gotoParent();
xmlweather.gotoChild("pressure_mb");
pressure_mb = xmlweather.getText().toString();
xmlweather.gotoParent();
xmlweather.gotoChild("dewpoint_c");
dewpoint_c = xmlweather.getText().toString();
xmlweather.gotoParent();
xmlweather.gotoChild("heat_index_c");
heat_index_c = xmlweather.getText().toString();
xmlweather.gotoParent();
xmlweather.gotoChild("windchill_c");
windchill_c = xmlweather.getText().toString();
xmlweather.gotoParent();
xmlweather.gotoChild("icon");
icon = xmlweather.getText().toString();
xmlweather.gotoParent();
xmlweather.gotoChild("forecast_url");
forecast_url = xmlweather.getText().toString();
xmlweather.gotoParent();
xmlweather.gotoChild("observation_epoch");
observation_epoch = xmlweather.getText().toString();
xmlweather.gotoParent();
xmlweather.gotoChild("relative_humidity");
relative_humidity = xmlweather.getText().toString();
xmlweather.gotoRoot();
if (xmlweather.hasTag("forecast")) {
xmlweather.gotoChild("forecast");
xmlweather.gotoChild("simpleforecast");
xmlweather.gotoChild("forecastdays");
xmlweather.gotoChild(1);
xmlweather.gotoChild("date");
xmlweather.gotoChild("day");
if (xmlweather.getText().toString().equals(str(day()))) {
xmlweather.gotoParent();
xmlweather.gotoChild("high");
xmlweather.gotoChild("high_c");
high_c = xmlweather.getText().toString();
xmlweather.gotoParent();
xmlweather.gotoParent();
xmlweather.gotoChild("low");
xmlweather.gotoChild("low_c");
low_c = xmlweather.getText().toString();
}
}
updateSuccess = "Update successful";
}
catch (Exception e) {
println(e);
updateSuccess = "update failed";
}
}
else {
updateSuccess = "per minute api limit exceeded";
}
}
else {
updateSuccess = "daily api limit exceeded";
}
return updateSuccess;
}
}
PApplet getPapplet ()
{
return this;
}