GpsRealData.java
7.74 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package com.bsth.data.gpsdata;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsth.Application;
import com.bsth.data.BasicData;
import com.bsth.data.forecast.ForecastRealServer;
import com.bsth.data.schedule.DayOfSchedule;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.util.ConfigUtil;
import com.google.common.collect.TreeMultimap;
import org.apache.commons.lang3.StringUtils;
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.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.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
*
* @ClassName: GpsRealData
* @Description: TODO(实时GPS数据集合)
* @author PanZhao
* @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;
// 网关数据接口地址
private static String url;
@Autowired
GpsDataLoader gpsDataLoader;
@Autowired
DayOfSchedule dayOfSchedule;
@Autowired
ForecastRealServer forecastRealServer;
/**
* 构造函数
*/
public GpsRealData(){
gpsMap = new HashMap<>();
lineCode2Devices = TreeMultimap.create();
url = ConfigUtil.get("http.gps.real.url");
}
@Override
public void run(String... arg0) throws Exception {
logger.info("gpsDataLoader,20,6");
Application.mainServices.scheduleWithFixedDelay(gpsDataLoader, 20, 5, TimeUnit.SECONDS);
}
public GpsEntity add(GpsEntity gps) {
String device = gps.getDeviceId();
GpsEntity old = gpsMap.get(device);
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()));
}
}
gpsMap.put(device, gps);
if(StringUtils.isNotBlank(gps.getLineId()))
lineCode2Devices.put(gps.getLineId(), device);
return gps;
}
/**
*
* @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.isAbnormal())
continue;
sch = dayOfSchedule.execPlamMap().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 GpsEntity findByDeviceId(String deviceId) {
return gpsMap.get(deviceId);
}
public Collection<GpsEntity> all(){
return gpsMap.values();
}
public void remove(String device){
gpsMap.remove(device);
}
@Component
public static class GpsDataLoader extends Thread{
Logger logger = LoggerFactory.getLogger(GpsDataLoader.class);
@Autowired
GpsRealData gpsRealData;
@Override
public void run() {
try{
load();
}catch(Exception e){
logger.error("", e);
}
}
public void load() throws Exception {
List<GpsEntity> list = new ArrayList<>();
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
HttpGet get = new HttpGet(url);
response = httpClient.execute(get);
HttpEntity entity = response.getEntity();
if (null != entity) {
// 返回数据量比较大,建议以流的形式读取
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuffer stringBuffer = new StringBuffer();
String str = "";
while ((str = br.readLine()) != null)
stringBuffer.append(str);
JSONObject jsonObj = JSON.parseObject(stringBuffer.toString());
if (jsonObj != null)
list = JSON.parseArray(jsonObj.getString("data"), GpsEntity.class);
String nbbm;
for(GpsEntity gps : list){
//没有设备号
if(StringUtils.isBlank(gps.getDeviceId()))
continue;
nbbm = BasicData.deviceId2NbbmMap.get(gps.getDeviceId());
if(StringUtils.isBlank(nbbm))
gps.setAbnormal(true);//标记为异常数据
else
gps.setNbbm(nbbm);
gps.setStationName(BasicData.stationCode2NameMap.get(gps.getStopNo()));
gpsRealData.add(gps);
//纠正走向
correctUpdown(gps);
/*if(issEPoint(gps))
continue;
//如果走向未知,尝试根据站点纠正走向
if(gps.getUpDown() == -1){
updown=stationUpDownMap.get(gps.getLineId()+"_"+gps.getStopNo());
if(updown != null)
gps.setUpDown(updown);
}
//如果站点编码和走向相反(即上行站点ID,走向为下行),尝试根据站点纠正走向
updown=stationUpDownMap.get(gps.getLineId()+"_"+gps.getStopNo());
if(updown != null && !gps.getUpDown().equals(updown)){
gps.setUpDown(updown);
}*/
}
} else
logger.error("result is null");
} catch(Exception e){
logger.error("", e);
}
finally {
if (null != httpClient)
httpClient.close();
if(null != response)
response.close();
}
}
/**
* 是否是起终点
* @param gps
* @return
*/
public boolean isSEPoint(GpsEntity gps){
String key = gps.getLineId()+"_"+gps.getUpDown()+"_"
,stationCode;
if(BasicData.lineSEPointMap.containsKey(key+"B")){
stationCode = BasicData.lineSEPointMap.get(key+"B");
if(gps.getStopNo().equals(stationCode)){
gps.setsEPoint(true);
return true;
}
}
if(BasicData.lineSEPointMap.containsKey(key+"E")){
stationCode = BasicData.lineSEPointMap.get(key+"E");
if(gps.getStopNo().equals(stationCode)){
gps.setsEPoint(true);
return true;
}
}
return false;
}
/**
* 纠正上下行
* @param gps
*/
public void correctUpdown(GpsEntity gps){
Integer updown=BasicData.lineStationUpDownMap.get(gps.getLineId()+"_"+gps.getStopNo());
if(updown != null && !updown.equals(gps.getUpDown()))
gps.setUpDown(updown);
/*//如果走向未知,尝试根据站点纠正走向
if(gps.getUpDown() == -1){
}*/
if(isSEPoint(gps))
return;
/*//如果站点编码和走向相反(即上行站点ID,走向为下行),尝试根据站点纠正走向
updown=BasicData.lineStationUpDownMap.get(gps.getLineId()+"_"+gps.getStopNo());
if(updown != null && !gps.getUpDown().equals(updown)){
gps.setUpDown(updown);
}*/
}
}
}