forked from rszimm/sprinklers_pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeather.cpp
214 lines (205 loc) · 4.58 KB
/
Weather.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
// Weather.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 "Weather.h"
#include "core.h"
#include "port.h"
#include <string.h>
#include <stdlib.h>
Weather::Weather(void)
{
}
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())
break;
else
continue; //TODO: implement a timeout here. Same in testing parse headers.
}
else
{
recvbufptr = recvbuf;
recvbufend = recvbuf + len;
}
}
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);
}
else if (strcmp(key, "minhumidity") == 0)
{
ret->minhumidity = atoi(val);
}
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)
}
int Weather::GetScale(const IPAddress & ip, const char * key, uint32_t zip, const char * pws, bool usePws) const
{
ReturnVals vals = GetVals(ip, key, zip, pws, usePws);
return GetScale(vals);
}
int Weather::GetScale(const ReturnVals & vals) const
{
if (!vals.valid)
return 100;
const int humid_factor = 30 - (vals.maxhumidity + vals.minhumidity) / 2;
const int temp_factor = (vals.meantempi - 70) * 4;
const int rain_factor = (vals.precipi + vals.precip_today) * -2;
const int adj = min(max(0, 100+humid_factor+temp_factor+rain_factor), 200);
trace(F("Adjusting H(%d)T(%d)R(%d):%d\n"), humid_factor, temp_factor, rain_factor, adj);
return adj;
}
Weather::ReturnVals Weather::GetVals(const IPAddress & ip, const char * key, uint32_t zip, const char * pws, bool usePws) const
{
ReturnVals vals = {0};
EthernetClient client;
if (client.connect(ip, 80))
{
char getstring[90];
trace(F("Connected\n"));
if (usePws)
snprintf(getstring, sizeof(getstring), "GET /api/%s/yesterday/conditions/q/pws:%s.json HTTP/1.0\n\n", key, pws);
else
snprintf(getstring, sizeof(getstring), "GET /api/%s/yesterday/conditions/q/%ld.json HTTP/1.0\n\n", key, (long) zip);
//trace(getstring);
client.write((uint8_t*) getstring, strlen(getstring));
ParseResponse(client, &vals);
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;
}