-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherData.cpp
More file actions
342 lines (284 loc) · 11.9 KB
/
WeatherData.cpp
File metadata and controls
342 lines (284 loc) · 11.9 KB
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
/**
* @file WeatherData.cpp
* @brief Historical weather data management implementation
*
* Fetches historical data via HTTP, parses JSON responses, and converts to
* chart-ready format. Uses static PSRAM buffer for large HTTP responses and
* caches last successful fetch for resilience against temporary failures.
*/
#include "WeatherData.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <cstdlib>
#include <cstring>
extern "C" {
#include "cJSON.h"
#include "esp_task_wdt.h"
#include "wifi_manager.h"
}
const char* WeatherData::TAG = "WeatherData";
// Static storage for HTTP response
char* WeatherData::http_response_buffer = nullptr;
int WeatherData::http_response_length = 0;
WeatherData& WeatherData::getInstance() {
static WeatherData instance;
// Allocate buffer on first use (in PSRAM if available)
if (http_response_buffer == nullptr) {
http_response_buffer = (char*)heap_caps_malloc(max_response_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (http_response_buffer == nullptr) {
// Fall back to regular heap if PSRAM not available
http_response_buffer = (char*)malloc(max_response_size);
ESP_LOGW(TAG, "Allocated HTTP buffer in regular heap");
} else {
//ESP_LOGI(TAG, "Allocated HTTP buffer in PSRAM");
}
}
return instance;
}
void WeatherData::httpResponseCallback(const char *data, int len) {
if (http_response_buffer == nullptr) {
ESP_LOGE(TAG, "HTTP response buffer not allocated");
return;
}
// Copy data to our static buffer
if (len > 0 && len < max_response_size) {
memcpy(http_response_buffer, data, len);
http_response_buffer[len] = '\0'; // Null terminate
http_response_length = len;
//ESP_LOGI(TAG, "Received and copied HTTP response: %d bytes", len);
} else if (len >= max_response_size) {
ESP_LOGE(TAG, "HTTP response too large: %d bytes (max: %d)", len, max_response_size);
http_response_length = 0;
} else {
ESP_LOGW(TAG, "Empty HTTP response");
http_response_length = 0;
}
}
void WeatherData::parseHistoryJson(const char* json_data, HistoryData& data) {
if (json_data == nullptr) {
ESP_LOGE(TAG, "NULL JSON data provided");
return;
}
cJSON *root = cJSON_Parse(json_data);
if (root == NULL) {
ESP_LOGE(TAG, "Failed to parse HTTP history JSON");
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL) {
ESP_LOGE(TAG, "JSON Error before: %s", error_ptr);
}
return;
}
if (!cJSON_IsArray(root)) {
ESP_LOGE(TAG, "HTTP history JSON is not an array");
cJSON_Delete(root);
return;
}
int array_size = cJSON_GetArraySize(root);
//ESP_LOGI(TAG, "Parsing %d historical data points", array_size);
int point_count = 0;
// Parse each history point
for (int i = 0; i < array_size; i++) {
// Reset watchdog periodically during long parsing
if (i % 50 == 0) {
esp_task_wdt_reset();
}
cJSON *item = cJSON_GetArrayItem(root, i);
if (!cJSON_IsObject(item)) {
continue;
}
// Parse timestamp
cJSON *timestamp = cJSON_GetObjectItem(item, "timestamp");
uint32_t ts = 0;
if (cJSON_IsNumber(timestamp)) {
ts = (uint32_t)timestamp->valueint;
}
// Parse temperature (use 'temperature' field for combined data, 'value' for individual metric)
cJSON *temp = cJSON_GetObjectItem(item, "temperature");
if (!cJSON_IsNumber(temp)) {
temp = cJSON_GetObjectItem(item, "value"); // Fallback for individual endpoint
}
float temperature = 0.0f;
if (cJSON_IsNumber(temp)) {
temperature = temp->valuedouble;
}
// Parse wind speed
cJSON *wind_speed = cJSON_GetObjectItem(item, "wind_speed");
float ws = 0.0f;
if (cJSON_IsNumber(wind_speed)) {
ws = wind_speed->valuedouble;
}
// Parse air pressure
cJSON *pressure = cJSON_GetObjectItem(item, "air_pressure");
if (!cJSON_IsNumber(pressure)) {
pressure = cJSON_GetObjectItem(item, "pressure"); // Alternative field name
}
float press = 0.0f;
if (cJSON_IsNumber(pressure)) {
press = pressure->valuedouble;
}
// Add to vectors
data.temperatures.push_back(temperature);
data.wind_speeds.push_back(ws);
data.pressures.push_back(press);
data.timestamps.push_back(ts);
point_count++;
}
//ESP_LOGI(TAG, "Successfully parsed and processed %d history points", point_count);
cJSON_Delete(root);
}
WeatherData::HistoryData WeatherData::generateSampleData(int points) {
HistoryData sample_data;
// Get current weather data for baseline values
const CurrentWeather& current = getCurrentWeather();
if (!current.valid) {
ESP_LOGW(TAG, "No valid current weather data, using defaults for sample data");
// Use default values
float base_temp = 20.0f;
float base_wind = 5.0f;
float base_pressure = 1013.0f;
uint32_t current_time = xTaskGetTickCount() / configTICK_RATE_HZ;
for (int i = 0; i < points; i++) {
sample_data.temperatures.push_back(base_temp + (rand() % 10 - 5) / 2.0f);
sample_data.wind_speeds.push_back(base_wind + (rand() % 30 - 15) / 10.0f);
sample_data.pressures.push_back(base_pressure + (rand() % 20 - 10) / 2.0f);
sample_data.timestamps.push_back(current_time - (points - i) * 3600);
}
} else {
uint32_t current_time = xTaskGetTickCount() / configTICK_RATE_HZ;
for (int i = 0; i < points; i++) {
sample_data.temperatures.push_back(current.temperature + (rand() % 10 - 5) / 2.0f);
sample_data.wind_speeds.push_back(current.wind_speed + (rand() % 30 - 15) / 10.0f);
sample_data.pressures.push_back(current.pressure + (rand() % 20 - 10) / 2.0f);
sample_data.timestamps.push_back(current_time - (points - i) * 3600);
}
}
//ESP_LOGI(TAG, "Generated %d sample data points", points);
return sample_data;
}
WeatherData::HistoryData WeatherData::fetchHistoricalData() {
//ESP_LOGI(TAG, "Fetching historical weather data from HTTP API...");
HistoryData history_data;
// Clear response buffer and length
http_response_length = 0;
if (http_response_buffer != nullptr) {
http_response_buffer[0] = '\0';
}
// Fetch data from HTTP API
esp_err_t ret = http_client_fetch_history(httpResponseCallback);
//ESP_LOGI(TAG, "HTTP fetch returned: %s, buffer=%p, length=%d",
//esp_err_to_name(ret), http_response_buffer, http_response_length);
if (ret == ESP_OK && http_response_length > 0) {
//ESP_LOGI(TAG, "Successfully fetched historical data, parsing JSON...");
// Parse the JSON response
parseHistoryJson(http_response_buffer, history_data);
if (history_data.isEmpty()) {
ESP_LOGW(TAG, "Parsing resulted in no data points, keeping cached data");
// Return cached data if available, otherwise empty
return cached_data;
} else {
//ESP_LOGI(TAG, "Successfully loaded %zu historical data points", history_data.size());
// Update cache with new data
cached_data = history_data;
return history_data;
}
} else {
ESP_LOGE(TAG, "Failed to fetch historical data: %s (length=%d)",
esp_err_to_name(ret), http_response_length);
//ESP_LOGI(TAG, "Returning cached data (%zu points)", cached_data.size());
// Return cached data (may be empty if this is the first fetch)
return cached_data;
}
}
bool WeatherData::fetchCurrentWeather() {
//ESP_LOGI(TAG, "Fetching current weather data from HTTP API...");
// Clear response buffer and length
http_response_length = 0;
if (http_response_buffer != nullptr) {
http_response_buffer[0] = '\0';
}
// Fetch data from HTTP API
esp_err_t ret = http_client_fetch_current(httpResponseCallback);
//ESP_LOGI(TAG, "HTTP fetch returned: %s, buffer=%p, length=%d",
//esp_err_to_name(ret), http_response_buffer, http_response_length);
if (ret == ESP_OK && http_response_length > 0) {
//ESP_LOGI(TAG, "Successfully fetched current weather data, parsing JSON...");
// Parse the JSON response
bool parsed = parseCurrentJson(http_response_buffer);
if (!parsed) {
ESP_LOGE(TAG, "Failed to parse current weather JSON");
return false;
}
//ESP_LOGI(TAG, "Current weather: T=%.1f°C H=%.1f%% P=%.1fmb WS=%.1fm/s WD=%d°",
//current_weather.temperature, current_weather.humidity,
//current_weather.pressure, current_weather.wind_speed,
//current_weather.wind_direction);
return true;
} else {
ESP_LOGE(TAG, "Failed to fetch current weather data: %s (length=%d)",
esp_err_to_name(ret), http_response_length);
return false;
}
}
bool WeatherData::parseCurrentJson(const char* json_data) {
if (json_data == nullptr) {
ESP_LOGE(TAG, "NULL JSON data provided");
return false;
}
ESP_LOGD(TAG, "Parsing current weather JSON: %s", json_data);
cJSON *root = cJSON_Parse(json_data);
if (root == NULL) {
ESP_LOGE(TAG, "Failed to parse JSON: %s", json_data);
return false;
}
// Parse according to API spec: /current returns a single object
// {
// "timestamp": 1733313982,
// "temperature": 7.0,
// "humidity": 92.2,
// "pressure": 1002.7,
// "wind_speed": 0.0,
// "wind_speed_max": 0.0,
// "wind_speed_min": 0.0,
// "wind_direction": 275,
// "rainfall": 0.0
// }
bool success = false;
cJSON *timestamp = cJSON_GetObjectItem(root, "timestamp");
cJSON *temp = cJSON_GetObjectItem(root, "temperature");
cJSON *humidity = cJSON_GetObjectItem(root, "humidity");
cJSON *pressure = cJSON_GetObjectItem(root, "pressure");
cJSON *wind_speed = cJSON_GetObjectItem(root, "wind_speed");
cJSON *wind_speed_max = cJSON_GetObjectItem(root, "wind_speed_max");
cJSON *wind_speed_min = cJSON_GetObjectItem(root, "wind_speed_min");
cJSON *wind_dir = cJSON_GetObjectItem(root, "wind_direction");
cJSON *rainfall = cJSON_GetObjectItem(root, "rainfall");
if (cJSON_IsNumber(timestamp) && cJSON_IsNumber(temp) &&
cJSON_IsNumber(humidity) && cJSON_IsNumber(pressure)) {
current_weather.timestamp = (time_t)timestamp->valueint;
current_weather.temperature = temp->valuedouble;
current_weather.humidity = humidity->valuedouble;
current_weather.pressure = pressure->valuedouble;
if (cJSON_IsNumber(wind_speed)) {
current_weather.wind_speed = wind_speed->valuedouble;
}
if (cJSON_IsNumber(wind_speed_max)) {
current_weather.wind_speed_max = wind_speed_max->valuedouble;
}
if (cJSON_IsNumber(wind_speed_min)) {
current_weather.wind_speed_min = wind_speed_min->valuedouble;
}
if (cJSON_IsNumber(wind_dir)) {
current_weather.wind_direction = (uint16_t)wind_dir->valueint;
}
if (cJSON_IsNumber(rainfall)) {
current_weather.rainfall = rainfall->valuedouble;
}
current_weather.valid = true;
success = true;
} else {
ESP_LOGE(TAG, "Missing required fields in current weather JSON");
}
cJSON_Delete(root);
return success;
}