#include "external/mtr/arrival.h" #include "cJSON.h" #include "esp_log.h" #include 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; } // 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(status_value); } } // TODO: verify the arrival json parsing // 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()); }