2
2
3
3
import com .alibaba .fastjson2 .JSON ;
4
4
import com .alibaba .fastjson2 .JSONObject ;
5
+ import com .baomidou .mybatisplus .core .conditions .query .QueryWrapper ;
5
6
import com .ruoyi .common .utils .StringUtils ;
6
7
import com .ruoyi .simulation .call .CallUE4Engine ;
8
+ import com .ruoyi .simulation .dao .JunctionMapper ;
9
+ import com .ruoyi .simulation .dao .RoadMapper ;
7
10
import com .ruoyi .simulation .dao .SpeedMapper ;
8
11
import com .ruoyi .simulation .domain .Junction ;
9
12
import com .ruoyi .simulation .domain .Road ;
@@ -35,21 +38,25 @@ public class TrafficIndirectionListener implements ApplicationListener<Applicati
35
38
public RedisTemplate <String ,String > redisTemplate ;
36
39
@ Resource
37
40
public SpeedMapper speedMapper ;
41
+ @ Resource
42
+ public RoadMapper roadMapper ;
43
+ @ Resource
44
+ public JunctionMapper junctionMapper ;
38
45
//每天定时获取历史平均速度
39
46
private Map <String ,Double > speedMap = new HashMap <>();
40
47
//一天内的平均速度
41
48
private Map <String , List <Double >> daySpeedMap = new HashMap <>();
42
49
private Map <String , Double > hourSpeedMap = new HashMap <>();
43
50
private Map <String , Double > indirectionTrendMap = new HashMap <>();
44
- private Map <String , Double > mileageTrendMap = new HashMap <>();
51
+ private Map <Integer , Double > mileageTrendMap = new HashMap <>();
45
52
private Map <String , Double > trafficTrendMap = new HashMap <>();
46
53
private int acquiredCount = 0 ;
47
54
private List <Road > roadList = new ArrayList <>();
48
55
private List <Junction > junctionList = new ArrayList <>();
49
56
@ Override
50
57
public void onApplicationEvent (ApplicationStartedEvent e ) {
51
58
executionIndirectionScript ();
52
- getJunctionRoad ();
59
+ getMapBasicData ();
53
60
new Thread (new Runnable () {
54
61
@ Override
55
62
public void run () {
@@ -71,17 +78,28 @@ private void executionIndirectionScript(){
71
78
//获得总拥堵里程
72
79
this .callUE4Engine .getTrafficIndirection ("congestion_mileage.py" );
73
80
//获得不同道路的拥堵里程
74
- this .callUE4Engine .getTrafficIndirection ("road_congestion_mileage .py" );
81
+ this .callUE4Engine .getTrafficIndirection ("junction_congestion_mileage .py" );
75
82
//获得服务车次
76
83
this .callUE4Engine .getTrafficIndirection ("serviced_vehicle.py" );
77
84
//获得路网车辆
78
85
this .callUE4Engine .getTrafficIndirection ("total_vehicle.py" );
79
86
}
80
87
81
88
/**
82
- * 获取道路及路口的字典信息
89
+ * 获取道路及路口的地图基础数据信息
83
90
*/
84
- private void getJunctionRoad (){
91
+ private void getMapBasicData (){
92
+ this .roadList = this .roadMapper .selectList (new QueryWrapper <>());
93
+ Map <Integer ,Integer > roadMap = new HashMap <>();
94
+ //设置carla中roadId与道路主键之间的对应关系
95
+ for (Road road : this .roadList ){
96
+ List <String > relatedList = Arrays .asList (road .getRelatedRoadId ().split ("," ));
97
+ for (String related : relatedList ){
98
+ roadMap .put (Integer .parseInt (related ), road .getRoadId ());
99
+ }
100
+ }
101
+ this .redisTemplate .opsForValue ().set ("roadMap" , JSON .toJSONString (roadMap ));
102
+ this .junctionList = this .junctionMapper .selectList (new QueryWrapper <>());
85
103
}
86
104
/**
87
105
* 每天零点整执行读取数据和写入数据的操作
@@ -125,6 +143,8 @@ public void getRedisIndirection(){
125
143
LoggerUtil .printLoggerStace (e );
126
144
}
127
145
Map <String ,Object > indirectionMap = new HashMap <>();
146
+ //设置交通趋势变化数据
147
+ this .setTrafficTrend ();
128
148
//获取实时的平均车速
129
149
double averageSpeed = this .getAverageSpeed ();
130
150
indirectionMap .put ("averageSpeed" , averageSpeed );
@@ -139,13 +159,11 @@ public void getRedisIndirection(){
139
159
double congestionMileage = this .getCongestionMileage ();
140
160
indirectionMap .put ("congestionMileage" , congestionMileage );
141
161
//获取不同路口的拥堵里程
142
- this .setJunctionCongestionMileage (indirectionMap );
162
+ this .setJunctionIndirection (indirectionMap );
143
163
//获取服务车次
144
164
this .setServicedVehicle (indirectionMap );
145
165
//获取路网车辆
146
166
this .setTotalVehicle (indirectionMap );
147
- //设置交通趋势变化数据
148
- this .setTrafficTrend ();
149
167
for (String sessionId : WebSocketServer .webSocketMap .keySet ()){
150
168
SendResponseUtil .sendIndirectionResponse (indirectionMap , sessionId );
151
169
}
@@ -199,6 +217,13 @@ public void setRoadAverageSpeed(String roadId, double averageSpeed) {
199
217
road .setAverageSpeed (averageSpeed );
200
218
double congestionIndirection = speedMap .get (roadId )/averageSpeed ;
201
219
road .setCongestionIndirection (congestionIndirection );
220
+ double trend = indirectionTrendMap .get (roadId );
221
+ //计算拥堵指数趋势变化
222
+ double trendRate = 1d ;
223
+ if (trend !=0 ){
224
+ trendRate = (congestionIndirection -trend )/trend ;
225
+ }
226
+ road .setCongestionIndirectionTrend (trendRate );
202
227
}
203
228
}.getRoadAverageSpeed ();
204
229
indirectionMap .put ("roadIndirection" , this .roadList );
@@ -234,10 +259,10 @@ public double getCongestionMileage(){
234
259
* 设置不同路口的拥堵里程
235
260
* @param indirectionMap
236
261
*/
237
- public void setJunctionCongestionMileage (Map <String ,Object > indirectionMap ){
238
- Map <String , Junction > junctionMap = new HashMap <>();
262
+ public void setJunctionIndirection (Map <String ,Object > indirectionMap ){
263
+ Map <Integer , Junction > junctionMap = new HashMap <>();
239
264
for (Junction junction : this .junctionList ){
240
- junctionMap .put (String . valueOf ( junction .getJunctionRoadId () ), junction );
265
+ junctionMap .put (junction .getTrafficLightId ( ), junction );
241
266
//初始化所有的道路拥堵里程
242
267
junction .setCongestionMileage (0d );
243
268
//初始化拥堵里程趋势变化
@@ -246,10 +271,17 @@ public void setJunctionCongestionMileage(Map<String,Object> indirectionMap){
246
271
//保存不同路口拥堵里程数据
247
272
new ParseJunctionIndirection (){
248
273
@ Override
249
- protected void setJunctionCongestionMileage (String junctionRoadId , double congestionMileage ) {
250
- Junction junction = junctionMap .get (junctionRoadId );
274
+ protected void setJunctionCongestionMileage (int trafficLightId , double congestionMileage ) {
275
+ Junction junction = junctionMap .get (trafficLightId );
251
276
//设置拥堵里程
252
277
junction .setCongestionMileage (congestionMileage );
278
+ double trend = mileageTrendMap .get (trafficLightId );
279
+ //计算拥堵里程趋势变化
280
+ double trendRate = 1d ;
281
+ if (trend !=0 ){
282
+ trendRate = (congestionMileage -trend )/trend ;
283
+ }
284
+ junction .setCongestionMileageTrend (trendRate );
253
285
}
254
286
}.getJunctionCongestionMileage ();
255
287
indirectionMap .put ("junctionCongestionMileage" , junctionList );
@@ -265,7 +297,7 @@ private void setTrafficTrend(){
265
297
this .indirectionTrendMap .put (String .valueOf (road .getRoadId ()), 1d );
266
298
}
267
299
for (Junction junction : this .junctionList ){
268
- this .mileageTrendMap .put (String . valueOf ( junction .getJunctionRoadId () ), 0d );
300
+ this .mileageTrendMap .put (junction .getTrafficLightId ( ), 0d );
269
301
}
270
302
//保存不同道路的拥堵指数变化趋势
271
303
new ParseRoadIndirection (){
@@ -281,11 +313,10 @@ public void setRoadAverageSpeed(String roadId, double averageSpeed) {
281
313
//保存不同路口的拥堵里程变化趋势
282
314
new ParseJunctionIndirection (){
283
315
@ Override
284
- public void setJunctionCongestionMileage (String junctionRoadId , double congestionMileage ) {
285
- mileageTrendMap .put (junctionRoadId , congestionMileage );
316
+ public void setJunctionCongestionMileage (int trafficLightId , double congestionMileage ) {
317
+ mileageTrendMap .put (trafficLightId , congestionMileage );
286
318
}
287
319
}.getJunctionCongestionMileage ();
288
-
289
320
//保存平均延误时间变化趋势
290
321
trafficTrendMap .put ("averageDelay" , this .getAverageDelay ());
291
322
//保存总拥堵里程变化趋势
@@ -392,18 +423,20 @@ public void getJunctionCongestionMileage(){
392
423
String roadCongestionMileageStr = redisTemplate .opsForValue ().get ("road_congestion_mileage" );
393
424
if (!StringUtils .isEmpty (roadCongestionMileageStr )) {
394
425
JSONObject jsonObject = JSON .parseObject (roadCongestionMileageStr );
395
- for (String junctionRoadId : jsonObject .keySet ()) {
426
+ for (String identity : jsonObject .keySet ()) {
427
+ int trafficLightId = Integer .parseInt (identity .split ("_" )[0 ]);
428
+ System .out .println (identity );
396
429
//获取不同路口的拥堵里程
397
- double congestionMileage = Double .valueOf (String .valueOf (jsonObject .get (junctionRoadId )));
398
- this .setJunctionCongestionMileage (junctionRoadId , congestionMileage );
430
+ double congestionMileage = Double .valueOf (String .valueOf (jsonObject .get (identity )));
431
+ this .setJunctionCongestionMileage (trafficLightId , congestionMileage );
399
432
}
400
433
}
401
434
}
402
435
/**
403
436
* 设置实时拥堵里程数据
404
- * @param junctionRoadId
437
+ * @param trafficLightId
405
438
* @param congestionMileage
406
439
*/
407
- protected abstract void setJunctionCongestionMileage (String junctionRoadId , double congestionMileage );
440
+ protected abstract void setJunctionCongestionMileage (int trafficLightId , double congestionMileage );
408
441
}
409
442
}
0 commit comments