BusCongestionChecker.java 3.63 KB
package com.bsth.data.zndd.dify;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import java.util.ArrayList;
import java.util.List;

public class BusCongestionChecker {

    static Logger logger = LoggerFactory.getLogger(BusCongestionChecker.class);

    @Value("${baidu.ak}")
    private static String ak; //百度api秘钥*/
    //道路拥堵情况 // 起点坐标 // 终点坐标
    public static String DL_YDQK(String origin, String destination,String city,String line){
        String resultString = "";
        try {
            String apiUrl = String.format(
                    "http://api.map.baidu.com/direction/v2/transit?" +
                            "origin=%s&destination=%s&ak=%s&city=%s",
                    origin, destination, ak, city
            );

            // 发送HTTP请求
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet request = new HttpGet(apiUrl);
            CloseableHttpResponse response = httpClient.execute(request);

            // 解析JSON响应
            HttpEntity entity = response.getEntity();
            String jsonResponse = EntityUtils.toString(entity);
            ObjectMapper mapper = new ObjectMapper();
            JsonNode root = mapper.readTree(jsonResponse);

            // 检查API调用状态
            if (root.path("status").asInt() != 0) {
                logger.info("API请求失败: " + root.path("message").asText());
            }

            // 遍历所有公交路线
            JsonNode routes = root.path("result").path("routes");
            for (JsonNode route : routes) {
                // 检查是否为目标线路
                JsonNode steps = route.path("steps");
                boolean isTargetLine = false;
                List<String> congestionSections = new ArrayList<>();

                for (JsonNode step : steps) {
                    // 匹配公交线路名称
                    JsonNode vehicle = step.path("vehicle");
                    if (vehicle.path("name").asText().equals(line)) {
                        isTargetLine = true;
                    }

                    // 解析交通状况(具体拥堵路段)
                    JsonNode trafficConditions = step.path("traffic_condition");
                    for (JsonNode condition : trafficConditions) {
                        int status = condition.path("status").asInt();
                        String sectionDesc = condition.path("desc").asText(); // 拥堵路段描述
                        if (status == 3) { // 3表示拥堵
                            congestionSections.add(sectionDesc);
                        }
                    }
                }

                // 输出结果
                if (isTargetLine && !congestionSections.isEmpty()) {
                    resultString += ("公交线路 " + line + " 存在拥堵路段:");
                    for (String section : congestionSections){
                        resultString +=(" - 拥堵路段: " + section);
                    }
                }
            }

            resultString +=("公交线路 " + line + " 当前畅通。");

        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultString;
    }
}