forked from wuspy/portal_calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.cpp
351 lines (326 loc) · 12.6 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
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
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include "weather.h"
#include "global.h"
#include "time.h"
const WeatherEntry EMPTY_WEATHER_ENTRY = {
.condition = WeatherCondition::UNKNOWN,
.temp = INT16_MAX,
.daylight = false,
.clouds = -1,
.pop = -1,
.humidity = -1,
.month = -1,
.mday = -1,
.wday = -1,
.hour = -1,
.minute = -1,
};
const DailyWeather EMPTY_DAILY_WEATHER = {
.condition = WeatherCondition::UNKNOWN,
.highTemp = INT16_MIN,
.lowTemp = INT16_MAX,
.daylight = false,
.month = -1,
.mday = -1,
.wday = -1,
};
RTC_DATA_ATTR time_t lastWeatherSync = 0;
RTC_DATA_ATTR float latitude = WEATHER_LOCATION_LATITUDE;
RTC_DATA_ATTR float longitude = WEATHER_LOCATION_LONGITUDE;
RTC_DATA_ATTR time_t sunriseTime = 0;
RTC_DATA_ATTR time_t sunsetTime = 0;
#define WEATHER_ENTRY_COUNT 40
RTC_DATA_ATTR WeatherEntry weatherEntries[WEATHER_ENTRY_COUNT] = {
EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY,
EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY,
EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY,
EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY,
EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY,
EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY,
EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY,
EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY, EMPTY_WEATHER_ENTRY,
};
time_t getLastWeatherSync()
{
return lastWeatherSync;
}
String urlEncode(String str)
{
const char* hex = "0123456789ABCDEF";
String result;
result.reserve(str.length());
for (char c : str) {
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
result += c;
} else if (c == ' ') {
result += '+';
} else {
result += '%';
result += hex[c >> 4];
result += hex[c & 0xf];
}
}
return result;
}
bool isDaylight(time_t t)
{
// Adjust t to be on the same day as the current sunrise/sunset time
if (sunriseTime > t) {
t += ((sunriseTime - t) / SECONDS_PER_DAY + 1) * SECONDS_PER_DAY;
} else if (sunsetTime < t) {
t -= ((t - sunsetTime) / SECONDS_PER_DAY + 1) * SECONDS_PER_DAY;
}
return t >= sunriseTime && t < sunsetTime;
}
WeatherCondition getWeatherConditionByCloudCover(int clouds)
{
if (clouds < 10) {
return WeatherCondition::CLEAR;
} else if (clouds < 26) {
return WeatherCondition::FEW_CLOUDS;
} else if (clouds < 51) {
return WeatherCondition::SCATTERED_CLOUDS;
} else if (clouds < 85) {
return WeatherCondition::BROKEN_CLOUDS;
} else {
return WeatherCondition::OVERCAST_CLOUDS;
}
}
WeatherCondition parseOWMWeatherConditionId(int id)
{
if (id < 200) {
return WeatherCondition::UNKNOWN;
} else if (id < 300) {
return WeatherCondition::THUNDERSTORM;
} else if (id < 400) {
return WeatherCondition::SHOWERS;
} else if (id < 505) {
return WeatherCondition::SCATTERED_SHOWERS;
} else if (id == 511) {
return WeatherCondition::FREEZING_RAIN;
} else if (id < 600) {
return WeatherCondition::SHOWERS;
} else if (id < 700) {
return WeatherCondition::SNOW;
} else if (id < 800) {
return WeatherCondition::FOG;
} else if (id == 800) {
return WeatherCondition::CLEAR;
} else if (id == 801) {
return WeatherCondition::FEW_CLOUDS;
} else if (id == 802) {
return WeatherCondition::SCATTERED_CLOUDS;
} else if (id == 803) {
return WeatherCondition::BROKEN_CLOUDS;
} else if (id == 804) {
return WeatherCondition::OVERCAST_CLOUDS;
} else {
return WeatherCondition::UNKNOWN;
}
}
void parseOWMWeatherEntry(const JsonVariant& data, WeatherEntry& entry)
{
time_t time = data["dt"].as<time_t>();
tm localtime;
localtime_r(&time, &localtime);
entry.condition = parseOWMWeatherConditionId(data["weather"][0]["id"].as<int>());
entry.temp = (int16_t)round(data["main"]["temp"].as<float>());
entry.daylight = isDaylight(time);
entry.clouds = data["clouds"]["all"].as<uint8_t>();
entry.pop = (uint8_t)round(data["pop"].as<float>() * 100.0);
entry.humidity = data["main"]["humidity"].as<uint8_t>();
entry.month = localtime.tm_mon;
entry.mday = localtime.tm_mday;
entry.wday = localtime.tm_wday;
entry.hour = localtime.tm_hour;
entry.minute = localtime.tm_min;
}
/**
* Gets the index of the weather entry closest to the specified hour on the specified day.
* Returns -1 if no entries exist for the specified day.
*/
int findWeatherEntry(int month, int mday, int hour, int startIndex = 0)
{
const int minuteOfDay = hour * 60;
WeatherEntry *current;
bool foundDay = false;
for (int i = max(startIndex, 0); i < WEATHER_ENTRY_COUNT; ++i) {
current = &weatherEntries[i];
if (current->month == month && current->mday == mday) {
foundDay = true;
// Reach the closest entry to the target hour. Since OWM returns data in 3 hour intervals, the closest
// entry will be at most 90 minutes off. However this also needs to consider the closest entry being far
// ahead of the target hour, since OWM doesn't return past data and it could have been fetched at any time.
if (current->hour >= hour || abs((int)current->hour * 60 + (int)current->minute - minuteOfDay) <= 90) {
return i;
}
} else if (foundDay) {
// Hour is greater than any hour returned by OWM for this day, so return the last entry
return i - 1;
}
}
DEBUG_PRINT("Failed to find weather entry for %d/%d", month + 1, mday);
return -1;
}
void getTodaysWeather(int month, int mday, WeatherEntry (&result)[5])
{
int i = findWeatherEntry(month, mday, WEATHER_START_HOUR);
int j = 0;
if (i != -1) {
for (; j < 5 && i + j < WEATHER_ENTRY_COUNT; ++j) {
result[j] = weatherEntries[i + j];
}
}
DEBUG_PRINT("Found %d weather entries for %d/%d", j, month + 1, mday);
for (; j < 5; ++j) {
result[j] = EMPTY_WEATHER_ENTRY;
}
}
void get5DayWeather(int month, int mday, int year, DailyWeather (&result)[5])
{
DailyWeather *day;
WeatherEntry *entry;
int j = 0;
int conditionStart, conditionEnd;
float clouds, daylight;
int sampleCount;
for (int i = 0; i < 5; ++i) {
day = &result[i];
*day = EMPTY_DAILY_WEATHER;
j = findWeatherEntry(month, mday, 0, j);
if (j == -1) {
j = 0;
advanceDay(month, mday, year);
continue;
}
day->month = month;
day->mday = mday;
day->wday = weatherEntries[j].wday;
clouds = 0.0;
daylight = 0.0;
sampleCount = 0;
conditionStart = findWeatherEntry(month, mday, WEATHER_START_HOUR, j);
conditionEnd = conditionStart + 4;
for (; j < WEATHER_ENTRY_COUNT; ++j) {
entry = &weatherEntries[j];
if (entry->mday != mday) {
break;
}
// Calculate high/low temp for entire 24-hour day
day->highTemp = max(day->highTemp, entry->temp);
day->lowTemp = min(day->lowTemp, entry->temp);
// Calculate overall condition only for the 12 hour period after WEATHER_START_HOUR
if (j >= conditionStart && j <= conditionEnd) {
++sampleCount;
day->condition = max(day->condition, entry->condition);
clouds = (clouds * (sampleCount - 1) + (float)entry->clouds) / sampleCount;
daylight = (daylight * (sampleCount - 1) + (entry->daylight ? 100.0 : 0.0)) / sampleCount;
}
}
DEBUG_PRINT("Found %d weather condition samples for %d/%d/%d", sampleCount, month + 1, mday, year);
day->daylight = daylight >= 50.0;
if (day->condition != WeatherCondition::UNKNOWN && day->condition <= WeatherCondition::OVERCAST_CLOUDS) {
// Use average daily cloud cover for a more representative weather icon
day->condition = getWeatherConditionByCloudCover((int)clouds);
}
advanceDay(month, mday, year);
}
}
OwmResult refreshWeather()
{
char url[200];
if (latitude == 0.0 || longitude == 0.0) {
// Use OWM's geocoding API to lookup the coordinates for the provided location
DEBUG_PRINT("Looking up lat,long for '" WEATHER_LOCATION "' from openweathermap");
HTTPClient http;
unsigned long start = millis();
http.setConnectTimeout(10000);
sprintf(
url,
"http://api.openweathermap.org/geo/1.0/direct?q=%s&limit=1&appid=%s",
urlEncode(WEATHER_LOCATION).c_str(),
urlEncode(OPENWEATHERMAP_API_KEY).c_str()
);
DEBUG_PRINT(url);
http.begin(url);
int status = http.GET();
if (status == 200) {
DynamicJsonDocument response(1000);
DeserializationError error = deserializeJson(response, http.getString());
http.end();
DEBUG_PRINT("Request to openweathermap took %lums", millis() - start);
if (error) {
DEBUG_PRINT("Failed to parse response: %s", error.c_str());
return OwmResult::MALFORMED_RESPONSE;
}
JsonVariant result = response[0];
if (result.isNull()) {
DEBUG_PRINT("No location results from openweathermap");
return OwmResult::INVALID_LOCATION;
}
latitude = result["lat"].as<float>();
longitude = result["lon"].as<float>();
DEBUG_PRINT("Found location %s, %s @ %0.6f,%0.6f", result["name"].as<char*>(), result["country"].as<char*>(), latitude, longitude);
} else {
http.end();
DEBUG_PRINT("Request to openweathermap failed with %d after %lums", status, millis() - start);
return status == 401 ? OwmResult::INVALID_API_KEY : OwmResult::NO_RESPONSE;
}
}
DEBUG_PRINT("Looking up weather for %0.6f,%0.6f from openweathermap", latitude, longitude);
HTTPClient http;
unsigned long start = millis();
http.setConnectTimeout(10000);
sprintf(
url,
"http://api.openweathermap.org/data/2.5/forecast?lat=%0.6f&lon=%0.6f&units=%s&appid=%s",
latitude,
longitude,
urlEncode(WEATHER_UNITS).c_str(),
urlEncode(OPENWEATHERMAP_API_KEY).c_str()
);
DEBUG_PRINT(url);
http.begin(url);
int status = http.GET();
if (status == 200) {
StaticJsonDocument<256> filter;
filter["city"]["sunrise"] = true;
filter["city"]["sunset"] = true;
filter["cnt"] = true;
filter["list"][0]["dt"] = true;
filter["list"][0]["main"]["temp"] = true;
filter["list"][0]["main"]["humidity"] = true;
filter["list"][0]["clouds"]["all"] = true;
filter["list"][0]["weather"][0]["id"] = true;
filter["list"][0]["pop"] = true;
DynamicJsonDocument response(10000);
DeserializationError error = deserializeJson(response, http.getStream(), DeserializationOption::Filter(filter));
http.end();
DEBUG_PRINT("Request to openweathermap took %lums", millis() - start);
if (error) {
DEBUG_PRINT("Failed to parse response: %s", error.c_str());
return OwmResult::MALFORMED_RESPONSE;
}
// Get sunrise and sunet hours for this location
JsonVariant city = response["city"];
sunriseTime = city["sunrise"].as<time_t>();
sunsetTime = city["sunset"].as<time_t>();
const int cnt = response["cnt"].as<int>();
JsonVariant list = response["list"];
int i = 0;
for (; i < min(cnt, WEATHER_ENTRY_COUNT); ++i) {
parseOWMWeatherEntry(list[i], weatherEntries[i]);
}
// In case owm returned less results than expected, clear the rest
for (; i < WEATHER_ENTRY_COUNT; ++i) {
weatherEntries[i] = EMPTY_WEATHER_ENTRY;
}
time(&lastWeatherSync);
return OwmResult::SUCCESS;
} else {
http.end();
DEBUG_PRINT("Request to openweathermap failed with %d after %lums", status, millis() - start);
return status == 401 ? OwmResult::INVALID_API_KEY : OwmResult::NO_RESPONSE;
}
}