- Added MainUIHandler class to manage the main UI and polling for arrival data. - Introduced SettingsUI class for displaying QR code and configuration options. - Created SettingsUIHandler to manage settings UI lifecycle and web server interactions. - Developed WebHandler to handle HTTP requests for MTR route settings, including adding and removing routes. - Implemented web endpoints for fetching MTR lines, routes, and saving settings. - Enhanced UI with responsive design for e-ink displays and added error handling for web interactions.
101 lines
3.1 KiB
C++
101 lines
3.1 KiB
C++
#include "external/mtr/arrival.h"
|
|
#include "cJSON.h"
|
|
#include "esp_log.h"
|
|
#include <string>
|
|
|
|
static const char* TAG = "StationArrivalInfo";
|
|
|
|
StationArrivalInfo::StationArrivalInfo(
|
|
cJSON* mtr_line_station_json,
|
|
cJSON* arrival_json,
|
|
const std::string& train_line_code,
|
|
const std::string& train_station_code
|
|
) : _status(UNKNOWN_STATUS)
|
|
, _train_line(train_line_code)
|
|
, _train_station(train_station_code) {
|
|
|
|
if (!arrival_json) {
|
|
ESP_LOGE(TAG, "arrival_json is null");
|
|
_status = FAILED_WITH_MESSAGE;
|
|
_message = "No arrival data received";
|
|
return;
|
|
}
|
|
|
|
ESP_LOGD(TAG, "Parsing arrival JSON for %s-%s", train_line_code.c_str(), train_station_code.c_str());
|
|
|
|
// Parse status
|
|
cJSON* status_json = cJSON_GetObjectItem(arrival_json, "status");
|
|
if (status_json && cJSON_IsNumber(status_json)) {
|
|
int status_value = status_json->valueint;
|
|
if (status_value >= 0 && status_value <= 3) {
|
|
_status = static_cast<StatusEnum>(status_value);
|
|
}
|
|
}
|
|
|
|
ESP_LOGD(TAG, "Status: %d, Message: %s", (int)_status, _message.c_str());
|
|
|
|
// Parse message (if present)
|
|
cJSON* message_json = cJSON_GetObjectItem(arrival_json, "message");
|
|
if (message_json && cJSON_IsString(message_json)) {
|
|
_message = message_json->valuestring;
|
|
}
|
|
|
|
// Parse UP direction arrivals
|
|
cJSON* up_json = cJSON_GetObjectItem(arrival_json, "UP");
|
|
if (up_json && cJSON_IsArray(up_json)) {
|
|
int up_count = cJSON_GetArraySize(up_json);
|
|
for (int i = 0; i < up_count; i++) {
|
|
cJSON* arrival_item = cJSON_GetArrayItem(up_json, i);
|
|
if (arrival_item) {
|
|
std::string time_str = "";
|
|
std::string dest_str = "";
|
|
|
|
cJSON* time_json = cJSON_GetObjectItem(arrival_item, "time");
|
|
if (time_json && cJSON_IsString(time_json)) {
|
|
time_str = time_json->valuestring;
|
|
}
|
|
|
|
cJSON* dest_json = cJSON_GetObjectItem(arrival_item, "dest");
|
|
if (dest_json && cJSON_IsString(dest_json)) {
|
|
dest_str = dest_json->valuestring;
|
|
}
|
|
|
|
if (!time_str.empty()) {
|
|
_up_arrivals.emplace_back(time_str, dest_str);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Parse DOWN direction arrivals
|
|
cJSON* down_json = cJSON_GetObjectItem(arrival_json, "DOWN");
|
|
if (down_json && cJSON_IsArray(down_json)) {
|
|
int down_count = cJSON_GetArraySize(down_json);
|
|
for (int i = 0; i < down_count; i++) {
|
|
cJSON* arrival_item = cJSON_GetArrayItem(down_json, i);
|
|
if (arrival_item) {
|
|
std::string time_str = "";
|
|
std::string dest_str = "";
|
|
|
|
cJSON* time_json = cJSON_GetObjectItem(arrival_item, "time");
|
|
if (time_json && cJSON_IsString(time_json)) {
|
|
time_str = time_json->valuestring;
|
|
}
|
|
|
|
cJSON* dest_json = cJSON_GetObjectItem(arrival_item, "dest");
|
|
if (dest_json && cJSON_IsString(dest_json)) {
|
|
dest_str = dest_json->valuestring;
|
|
}
|
|
|
|
if (!time_str.empty()) {
|
|
_down_arrivals.emplace_back(time_str, dest_str);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Parsed arrival info for %s/%s: %zu UP, %zu DOWN trains",
|
|
train_line_code.c_str(), train_station_code.c_str(),
|
|
_up_arrivals.size(), _down_arrivals.size());
|
|
}
|