BusCongestionChecker.java
3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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 {
private static final String AK = "baidu.akyd=WnSDHZgtPbSbw2LfsH3KO3DDKWONmlYK"; // 替换为百度AK
private static final String ORIGIN = "40.056878,116.30815"; // 起点坐标
private static final String DESTINATION = "39.915285,116.403857"; // 终点坐标
private static final String CITY = "上海"; // 城市名
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;
}
}