Files
ink-board/main/external/mtr/line_info.cpp

63 lines
1.9 KiB
C++

#include "external/mtr/line_info.h"
#include "external/mtr/station_info.h"
#include "cJSON.h"
#include "esp_log.h"
LineInfo::LineInfo(cJSON* line_json) {
if (!line_json) {
ESP_LOGE(LINE_INFO_TAG, "line_json is null");
return;
}
// Parse line code
cJSON* code_json = cJSON_GetObjectItem(line_json, "code");
if (code_json && cJSON_IsString(code_json)) {
_code = code_json->valuestring;
} else {
ESP_LOGW(LINE_INFO_TAG, "Missing or invalid 'code' field");
}
// Parse line name
cJSON* name_json = cJSON_GetObjectItem(line_json, "name");
if (name_json && cJSON_IsString(name_json)) {
_name = name_json->valuestring;
} else {
ESP_LOGW(LINE_INFO_TAG, "Missing or invalid 'name' field");
}
// Parse line color (note: field is 'line_color' in JSON, not 'color')
cJSON* color_json = cJSON_GetObjectItem(line_json, "line_color");
if (color_json && cJSON_IsString(color_json)) {
_color = color_json->valuestring;
} else {
ESP_LOGW(LINE_INFO_TAG, "Missing or invalid 'line_color' field");
}
// Parse stations array
cJSON* stations_json = cJSON_GetObjectItem(line_json, "stations");
if (stations_json && cJSON_IsArray(stations_json)) {
int station_count = cJSON_GetArraySize(stations_json);
_stations.reserve(station_count);
for (int i = 0; i < station_count; i++) {
cJSON* station_json = cJSON_GetArrayItem(stations_json, i);
if (station_json) {
_stations.emplace_back(station_json);
}
}
ESP_LOGI(LINE_INFO_TAG, "Created LineInfo: %s with %d stations", _code.c_str(), station_count);
} else {
ESP_LOGW(LINE_INFO_TAG, "Missing or invalid 'stations' array");
}
}
const char* LineInfo::get_station_name(const std::string& station_code) const {
for (const auto& station : _stations) {
if (std::string(station.code()) == station_code) {
return station.name();
}
}
return "";
}