GpsRealData.java
4.39 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.bsth.data.gpsdata;
import com.bsth.data.BasicData;
import com.bsth.data.forecast.ForecastRealServer;
import com.bsth.data.gpsdata.thread.GpsDataLoaderThread;
import com.bsth.data.schedule.DayOfSchedule;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.google.common.collect.TreeMultimap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.*;
/**
* @author PanZhao
* @ClassName: GpsRealData
* @Description: TODO(实时GPS数据集合)
* @date 2016年8月12日 下午2:04:41
*/
@Component
public class GpsRealData implements CommandLineRunner {
static Logger logger = LoggerFactory.getLogger(GpsRealData.class);
private static Map<String, GpsEntity> gpsMap;
//按线路分组设备号
private static TreeMultimap<String, String> lineCode2Devices;
@Autowired
GpsDataLoaderThread gpsDataLoader;
@Autowired
DayOfSchedule dayOfSchedule;
@Autowired
ForecastRealServer forecastRealServer;
/**
* 构造函数
*/
public GpsRealData() {
gpsMap = new HashMap<>();
lineCode2Devices = TreeMultimap.create();
}
@Override
public void run(String... arg0) throws Exception {
logger.info("gpsDataLoader,20,5");
//定时从网关获取GPS数据
//Application.mainServices.scheduleWithFixedDelay(gpsDataLoader, 20, 15, TimeUnit.SECONDS);
//定时扫描掉离线
}
public void put(GpsEntity gps) {
String device = gps.getDeviceId();
GpsEntity old = gpsMap.get(device);
try {
if (!StringUtils.isEmpty(gps.getStopNo())) {
//站点编码改变
if (null == old || !gps.getStopNo().equals(old.getStopNo())) {
gps.setArrTime(gps.getTimestamp());
//预测到达终点时间
forecastRealServer.forecast(gps.getNbbm(), gps);
} else {
gps.setArrTime(old.getArrTime());
//不预测, 重新计算终点时间
gps.setExpectStopTime(forecastRealServer.expectStopTime(gps.getNbbm()));
}
}
} catch (Exception e) {
logger.error("", e);
}
//刷新对照
gpsMap.put(device, gps);
if (StringUtils.isNotBlank(gps.getLineId())) {
//站点名称
gps.setStationName(getStationName(gps));
lineCode2Devices.put(gps.getLineId(), device);
if(old != null && !gps.getLineId().equals(old.getLineId()))
lineCode2Devices.remove(old.getLineId(), device);
}
}
public String getStationName(GpsEntity gps) {
return BasicData.getStationNameByCode(gps.getStopNo(), gps.getLineId() + "_" + gps.getUpDown() + "_");
}
/**
* @Title: get @Description: TODO(设备号获取GPS)
*/
public GpsEntity get(String deviceId) {
return gpsMap.get(deviceId);
}
/**
* @Title: get @Description: TODO(线路编码获取GPS集合) @throws
*/
public List<GpsEntity> getByLine(String lineCode) {
NavigableSet<String> set = lineCode2Devices.get(lineCode);
List<GpsEntity> rs = new ArrayList<>();
GpsEntity gps;
ScheduleRealInfo sch;
for (String device : set) {
gps = gpsMap.get(device);
//过滤异常GPS数据
if (gps == null || gps.isIncomplete())
continue;
sch = dayOfSchedule.execPlanMap().get(gps.getNbbm());
if (null != sch)
gps.setSchId(sch.getId());
rs.add(gps);
}
return rs;
}
public List<GpsEntity> get(List<String> pArray) {
List<GpsEntity> list = new ArrayList<>();
for (String code : pArray)
list.addAll(getByLine(code));
return list;
}
public Set<String> allDevices() {
return gpsMap.keySet();
}
public Collection<GpsEntity> all() {
return gpsMap.values();
}
public void remove(String device) {
gpsMap.remove(device);
}
}