feat(travel): Refactor route handling to use direction instead of destination

This commit is contained in:
GW_MC
2026-02-03 20:11:16 +08:00
parent c4635948e4
commit 3617a206ff
9 changed files with 137 additions and 57 deletions

View File

@@ -256,8 +256,8 @@ esp_err_t WebHandler::settings_page_handler_(httpd_req_t* req) {
</select>
</div>
<div class="form-group">
<label></label>
<select id="dest_select">
<label></label>
<select id="direction_select">
<option value=""></option>
</select>
</div>
@@ -327,11 +327,11 @@ esp_err_t WebHandler::settings_page_handler_(httpd_req_t* req) {
function updateStations() {
const lineCode = document.getElementById('line_select').value;
const stationSelect = document.getElementById('station_select');
const destSelect = document.getElementById('dest_select');
const directionSelect = document.getElementById('direction_select');
if (!lineCode) {
stationSelect.innerHTML = '<option value=""></option>';
destSelect.innerHTML = '<option value=""></option>';
directionSelect.innerHTML = '<option value=""></option>';
return;
}
@@ -343,7 +343,18 @@ esp_err_t WebHandler::settings_page_handler_(httpd_req_t* req) {
).join('');
stationSelect.innerHTML = '<option value=""></option>' + stationsHtml;
destSelect.innerHTML = '<option value=""></option>' + stationsHtml;
const stations = line.stations;
if (stations.length >= 2) {
const firstStation = stations[0];
const lastStation = stations[stations.length - 1];
const directionsHtml =
`<option value="${firstStation.code}">${firstStation.name}</option>` +
`<option value="${lastStation.code}">${lastStation.name}</option>`;
directionSelect.innerHTML = '<option value=""></option>' + directionsHtml;
} else {
directionSelect.innerHTML = '<option value=""></option>';
}
}
function renderRoutes() {
@@ -357,7 +368,7 @@ esp_err_t WebHandler::settings_page_handler_(httpd_req_t* req) {
<div class="route-item">
<div class="route-info">
<span class="route-line" style="background: ${route.line_color}">${route.line_name}</span>
${route.station_name} ${route.dest_name}
${route.station_name} ${route.direction_name}
</div>
<button class="danger" style="width: auto; padding: 5px 10px;"
onclick="removeRoute(${index})"></button>
@@ -368,21 +379,21 @@ esp_err_t WebHandler::settings_page_handler_(httpd_req_t* req) {
async function addRoute() {
const lineCode = document.getElementById('line_select').value;
const stationCode = document.getElementById('station_select').value;
const destCode = document.getElementById('dest_select').value;
const directionCode = document.getElementById('direction_select').value;
if (!lineCode || !stationCode || !destCode) {
showStatus('', 'error');
if (!lineCode || !stationCode || !directionCode) {
showStatus('', 'error');
return;
}
if (stationCode === destCode) {
showStatus('', 'error');
if (stationCode === directionCode) {
showStatus('', 'error');
return;
}
const line = linesData.find(l => l.code === lineCode);
const station = line.stations.find(s => s.code === stationCode);
const dest = line.stations.find(s => s.code === destCode);
const direction = line.stations.find(s => s.code === directionCode);
const route = {
line_code: lineCode,
@@ -390,8 +401,8 @@ esp_err_t WebHandler::settings_page_handler_(httpd_req_t* req) {
line_color: line.color,
station_code: stationCode,
station_name: station.name,
dest_code: destCode,
dest_name: dest.name
direction: directionCode,
direction_name: direction.name + ''
};
try {
@@ -536,8 +547,8 @@ esp_err_t WebHandler::get_routes_handler_(httpd_req_t* req) {
cJSON_AddStringToObject(route_obj, "line_color", route.line_color.c_str());
cJSON_AddStringToObject(route_obj, "station_code", route.station_code.c_str());
cJSON_AddStringToObject(route_obj, "station_name", route.station_name.c_str());
cJSON_AddStringToObject(route_obj, "dest_code", route.dest_code.c_str());
cJSON_AddStringToObject(route_obj, "dest_name", route.dest_name.c_str());
cJSON_AddStringToObject(route_obj, "direction", route.direction.c_str());
cJSON_AddStringToObject(route_obj, "direction_name", route.direction_name.c_str());
cJSON_AddItemToArray(routes_arr, route_obj);
}
cJSON_AddItemToObject(root, "routes", routes_arr);
@@ -603,15 +614,15 @@ esp_err_t WebHandler::add_route_handler_(httpd_req_t* req) {
item = cJSON_GetObjectItem(root, "station_name");
if (item && cJSON_IsString(item)) route.station_name = item->valuestring;
item = cJSON_GetObjectItem(root, "dest_code");
if (item && cJSON_IsString(item)) route.dest_code = item->valuestring;
item = cJSON_GetObjectItem(root, "direction");
if (item && cJSON_IsString(item)) route.direction = item->valuestring;
item = cJSON_GetObjectItem(root, "dest_name");
if (item && cJSON_IsString(item)) route.dest_name = item->valuestring;
item = cJSON_GetObjectItem(root, "direction_name");
if (item && cJSON_IsString(item)) route.direction_name = item->valuestring;
cJSON_Delete(root);
if (route.line_code.empty() || route.station_code.empty() || route.dest_code.empty()) {
if (route.line_code.empty() || route.station_code.empty() || route.direction.empty()) {
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing required fields");
return ESP_FAIL;
}