PassengerFlowCenter.java
3.48 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
98
99
100
package com.bsth.data.passenger_flow;
import com.bsth.data.BasicData;
import com.bsth.data.alarm.AlarmCenter;
import com.bsth.data.gpsdata_v2.cache.GeoCacheData;
import com.bsth.data.gpsdata_v2.entity.StationRoute;
import com.bsth.message.entity.PassengerFlow;
import com.bsth.repository.realcontrol.PassengerFlowRepository;
import com.google.common.collect.BiMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Component
public class PassengerFlowCenter implements InitializingBean {
/**
* 车辆当前乘客数
*/
private Map<String, Integer> vehicle2num = new ConcurrentHashMap<>();
private static Logger log = LoggerFactory.getLogger(AlarmCenter.class);
private volatile boolean initialized = false;
private List<PassengerFlow> cache = new ArrayList<>();
@Autowired
private PassengerFlowRepository passengerFlowRepository;
public void put(List<PassengerFlow> passengerFlows) {
if (!initialized) {
cache.addAll(passengerFlows);
}
if (cache.size() > 0) {
calc(cache);
cache.clear();
}
calc(passengerFlows);
try {
BiMap<String, String> shangHaiCode2LineCode = BasicData.lineCode2ShangHaiCodeMap.inverse();
for (PassengerFlow passengerFlow : passengerFlows) {
String line = shangHaiCode2LineCode.get(passengerFlow.getLine());
if (line == null) {
log.warn("没有匹配的线路,line= {}", passengerFlow.getLine());
continue;
}
passengerFlow.setLine(line);
StationRoute stationRoute = GeoCacheData.getStationRoute(passengerFlow.getLine(), passengerFlow.getDirection()).get(passengerFlow.getStationSeq() - 1);
passengerFlow.setLineName(BasicData.lineCode2NameMap.get(passengerFlow.getLine()));
passengerFlow.setStationCode(stationRoute.getCode());
passengerFlow.setStationName(stationRoute.getName());
passengerFlow.setStationRouteCode(stationRoute.getRouteSort());
}
passengerFlowRepository.saveAll(passengerFlows);
} catch (Exception e) {
log.error("保存客流信息失败,data={}", passengerFlows, e);
}
}
private void calc(List<PassengerFlow> passengerFlows) {
for (PassengerFlow passengerFlow : passengerFlows) {
Integer num = vehicle2num.get(passengerFlow.getInsideCode());
if (num == null) {
num = 0;
}
num = num + passengerFlow.getUpNum() - passengerFlow.getDownNum();
num = num < 0 ? 0 : num;
vehicle2num.put(passengerFlow.getInsideCode(), num);
}
}
public Integer getNumByVehicle(String vehicle) {
Integer num = vehicle2num.get(vehicle);
return num == null ? 0 : num;
}
public void clearByVehicle(String vehicle) {
vehicle2num.remove(vehicle);
}
public void clear(){
vehicle2num.clear();
log.info("清除车辆乘客数据...");
}
@Override
public void afterPropertiesSet() throws Exception {
// 暂不做故障恢复
initialized = true;
}
}