-
Notifications
You must be signed in to change notification settings - Fork 100
/
Wunderground.cpp
226 lines (211 loc) · 4.99 KB
/
Wunderground.cpp
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
// NO LONGER WORKING due to API shutdown/changes
// Wunderground.cpp
// This file manages the retrieval of Weather related information and adjustment of durations
// from Weather Underground
// Author: Richard Zimmerman
// Copyright (c) 2013 Richard Zimmerman
//
#include "config.h"
#ifdef WEATHER_WUNDERGROUND
#include "Wunderground.h"
#include "core.h"
#include "port.h"
#include <string.h>
#include <stdlib.h>
Wunderground::Wunderground(void)
{
m_wundergroundAPIHost="api.wunderground.com";
}
static void ParseResponse(EthernetClient & client, Weather::ReturnVals * ret)
{
freeMemory();
ret->valid = false;
enum
{
FIND_QUOTE1 = 0, PARSING_KEY, FIND_QUOTE2, PARSING_VALUE, PARSING_QVALUE, ERROR
} current_state = FIND_QUOTE1;
char recvbuf[100];
char * recvbufptr = recvbuf;
char * recvbufend = recvbuf;
char key[30], val[30];
char * keyptr = key;
char * valptr = val;
while (true)
{
if (recvbufptr >= recvbufend)
{
int len = client.read((uint8_t*) recvbuf, sizeof(recvbuf));
// trace(F("Received Bytes:%d\n"), len);
if (len <= 0)
{
if (!client.connected()) {
trace("Client Disconnected\n");
break;
} else {
continue; //TODO: implement a timeout here. Same in testing parse headers.
}
}
else
{
recvbufptr = recvbuf;
recvbufend = recvbuf + len;
//trace(recvbuf);
}
}
char c = *(recvbufptr++);
switch (current_state)
{
case FIND_QUOTE1:
if (c == '"')
{
current_state = PARSING_KEY;
keyptr = key;
}
break;
case PARSING_KEY:
if (c == '"')
{
current_state = FIND_QUOTE2;
*keyptr = 0;
}
else
{
if ((keyptr - key) < (long)(sizeof(key) - 1))
{
*keyptr = c;
keyptr++;
}
}
break;
case FIND_QUOTE2:
if (c == '"')
{
current_state = PARSING_QVALUE;
valptr = val;
}
else if (c == '{')
{
current_state = FIND_QUOTE1;
}
else if ((c >= '0') && (c <= '9'))
{
current_state = PARSING_VALUE;
valptr = val;
*valptr = c;
valptr++;
}
break;
case PARSING_VALUE:
if (((c >= '0') && (c <= '9')) || (c == '.'))
{
*valptr = c;
valptr++;
}
else
{
current_state = FIND_QUOTE1;
*valptr = 0;
}
break;
case PARSING_QVALUE:
if (c == '"')
{
current_state = FIND_QUOTE1;
*valptr = 0;
//trace("%s:%s\n", key, val);
if (strcmp(key, "maxhumidity") == 0)
{
ret->valid = true;
ret->keynotfound = false;
ret->maxhumidity = atoi(val);
// prevent invalid humidity from getting through
if (ret->maxhumidity > 100 || ret->maxhumidity < 0) {
ret->maxhumidity = NEUTRAL_HUMIDITY;
}
}
else if (strcmp(key, "minhumidity") == 0)
{
ret->minhumidity = atoi(val);
// prevent invalid humidity from getting through
if (ret->minhumidity > 100 || ret->minhumidity < 0) {
ret->minhumidity = NEUTRAL_HUMIDITY;
}
}
else if (strcmp(key, "meantempi") == 0)
{
ret->meantempi = atoi(val);
}
else if (strcmp(key, "precip_today_in") == 0)
{
ret->precip_today = (atof(val) * 100.0);
}
else if (strcmp(key, "precipi") == 0)
{
ret->precipi = (atof(val) * 100.0);
}
else if (strcmp(key, "UV") == 0)
{
ret->UV = (atof(val) * 10.0);
}
else if (strcmp(key, "meanwindspdi") == 0)
{
ret->windmph = (atof(val) * 10.0);
}
else if (strcmp(key, "type") == 0)
{
if (strcmp(val, "keynotfound") == 0)
ret->keynotfound = true;
}
}
else
{
if ((valptr - val) < (long)(sizeof(val) - 1))
{
*valptr = c;
valptr++;
}
}
break;
case ERROR:
break;
} // case
} // while (true)
}
Weather::ReturnVals Wunderground::InternalGetVals(const Weather::Settings & settings) const
{
ReturnVals vals = {0};
EthernetClient client;
//trace("Settings:\nKey: %s\nPWS: %s\nZIP: %ld\nUse PWS: %d\n", settings.key, settings.pws, settings.zip, settings.usePws);
if (client.connect(m_wundergroundAPIHost, 80))
{
char getstring[255];
trace(F("Connected\n"));
if (settings.usePws)
snprintf(getstring, sizeof(getstring), "GET https://%s/api/%s/yesterday/conditions/q/pws:%s.json HTTP/1.1\r\n",m_wundergroundAPIHost, settings.key, settings.pws);
else
snprintf(getstring, sizeof(getstring), "GET https://%s/api/%s/yesterday/conditions/q/%ld.json HTTP/1.1\r\n",m_wundergroundAPIHost, settings.key, (long) settings.zip);
//trace("GetString: %s\n",getstring);
client.write((uint8_t*) getstring, strlen(getstring));
//send host header
snprintf(getstring, sizeof(getstring), "Host: %s\r\nConnection: close\r\n\r\n",m_wundergroundAPIHost);
//trace("GetString: %s\n",getstring);
client.write((uint8_t*) getstring, strlen(getstring));
ParseResponse(client, &vals);
vals.resolvedIP=client.GetIpAddress();
client.stop();
if (!vals.valid)
{
if (vals.keynotfound)
trace("Invalid WUnderground Key\n");
else
trace("Bad WUnderground Response\n");
}
}
else
{
trace(F("connection failed\n"));
client.stop();
}
return vals;
}
#endif