GpsRealDataRefreshThread.java
3.98 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
package com.bsth.vehicle.gpsdata;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsth.util.ConfigUtil;
import com.bsth.vehicle.gpsdata.buffer.GpsRealDataBuffer;
import com.bsth.vehicle.gpsdata.entity.GpsRealData;
import com.google.common.collect.ImmutableMap;
/**
*
* @ClassName: RefreshBufferThread
* @Description: TODO(刷新GPS实时缓存数据 线程)
* @author PanZhao
* @date 2016年5月11日 下午8:56:44
*
*/
@Component
public class GpsRealDataRefreshThread extends Thread{
private static Logger logger = LoggerFactory.getLogger(GpsRealDataRefreshThread.class);
//接口地址
private static String url;
public GpsRealDataRefreshThread() {
url = ConfigUtil.get("http.gps.real.url");
}
@Autowired
GpsRealDataBuffer gpsBuffer;
@Override
public void run() {
try{
//Long t = System.currentTimeMillis();
List<GpsRealData> newList = getterRealGpsData();
if(gpsBuffer.isNullEmpty())
gpsBuffer.initBuffer(newList);
else{
ImmutableMap<String, GpsRealData> deviceMap = gpsBuffer.getDeviceGpsMap();
GpsRealData oldGps;
//新的GPS信号
List<GpsRealData> upGpsList = new ArrayList<>();
//需要更新映射的线路
Set<Integer> upLines = new HashSet<>();
for(GpsRealData newData : newList){
oldGps = deviceMap.get(newData.getDeviceId());
//新的GPS点
if(null == oldGps
|| oldGps.getTimestamp() < newData.getTimestamp()){
upGpsList.add(newData);
upLines.add(newData.getLineId());
}
}
//更新缓存
gpsBuffer.updateBuffer(upLines, upGpsList);
gpsBuffer.setLastUpdateTime(System.currentTimeMillis());
}
//logger.info("本次刷新GPS实时数据耗时:" + (System.currentTimeMillis() - t) + "毫秒");
}catch(Exception e){
//logger.error("", e);
logger.error("加载gps数据失败," + e.getMessage());
}
}
/**
*
* @Title: getterRealGpsData
* @Description: TODO(请求实时GPS数据)
* @return List<GpsRealData>
* @throws
*/
public List<GpsRealData> getterRealGpsData() throws Exception{
List<GpsRealData> list = new ArrayList<>()
,rs = new ArrayList<>();
CloseableHttpClient httpClient = null;
try {
httpClient = HttpClients.createDefault();
HttpGet get = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(get);
try {
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"), GpsRealData.class);
//将无效的经纬度过滤掉
for(GpsRealData gps : list){
if(gps.getLat() != 0 && gps.getLon() != 0)
rs.add(gps);
}
}
}
else
logger.error("result is null");
} finally {
response.close();
}
} finally{
if(null != httpClient)
httpClient.close();
}
return rs;
}
}