GpsRealDataBuffer.java
6.19 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
package com.bsth.vehicle.gpsdata.buffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.service.realcontrol.buffer.ScheduleBuffer;
import com.bsth.vehicle.common.CommonMapped;
import com.bsth.vehicle.gpsdata.entity.GpsRealData;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ListMultimap;
/**
*
* @ClassName: GpsRealDataBuffer
* @Description: TODO(GPS 实时数据缓存)
* @author PanZhao
* @date 2016年5月11日 下午4:31:29
*
*/
@Component
public class GpsRealDataBuffer {
private Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 线路和GPS对照
*(K: 线路编码 ,V:GpsRealData)
*/
private static ListMultimap<Integer, GpsRealData> lineGpsMultimap;
/**
* 设备和GPS对照
*(K: 设备编码 ,V:GpsRealData)
*/
private static Map<String, GpsRealData> deviceGpsMap;
/**
* 最后同步时间
*/
private static Long lastUpdateTime;
/**
* 10分钟没有信号则认为掉线
*/
public final static long OFFLINE_TIME = 1000 * 60 * 10;
public boolean isNullEmpty(){
return deviceGpsMap == null;
}
static{
lineGpsMultimap = ArrayListMultimap.create();
}
/**
*
* @Title: initBuffer
* @Description: TODO(初始化缓存)
* @throws
*/
public void initBuffer(List<GpsRealData> list){
lineGpsMultimap = ArrayListMultimap.create();
deviceGpsMap = new HashMap<>();
for(GpsRealData gpsData : list){
lineGpsMultimap.put(gpsData.getLineId(), gpsData);
deviceGpsMap.put(gpsData.getDeviceId(), gpsData);
}
logger.info("init gps buffer gps! list size: " + list.size());
}
public void putLineMultimap(String lineCode, GpsRealData gpsData){
}
public void putDeviceGpsMap(String deviceId, GpsRealData gpsData){
deviceGpsMap.put(deviceId, gpsData);
}
/**
*
* @Title: updateBuffer
* @Description: TODO(更新缓存)
* @param @param upLines 需要更新映射的线路
* @param @param upGpsList 有更新的GPS信号
* @throws
*/
public void updateBuffer(Set<Integer> upLines, List<GpsRealData> upGpsList){
long t = System.currentTimeMillis();
//更新GPS点
for(GpsRealData newGps : upGpsList){
if(t - newGps.getTimestamp() < OFFLINE_TIME
&& !newGps.isOnline()){
logger.info("设备:" + newGps.getDeviceId() + " 上线");
newGps.setOnline(true);
}
deviceGpsMap.put(newGps.getDeviceId(), newGps);
}
Iterator<GpsRealData> iterator = deviceGpsMap.values().iterator();
//重新按线路划分
lineGpsMultimap.clear();
GpsRealData gpsdata;
while(iterator.hasNext()){
gpsdata = iterator.next();
lineGpsMultimap.put(gpsdata.getLineId(), gpsdata);
}
//logger.info("update gps buffer over! new gps list size: " + upGpsList.size() + " - lineGpsMultimap size: " + lineGpsMultimap.size());
}
/**
*
* @Title: getDeviceGpsMap
* @Description: TODO(获取设备号 ——> 实时GPS对照)
* @return ImmutableMap<String,GpsRealData> 不可变的对照表
* @throws
*/
public ImmutableMap<String, GpsRealData> getDeviceGpsMap(){
return ImmutableMap.copyOf(deviceGpsMap);
}
/**
*
* @Title: getListByLineCode
* @Description: TODO(根据线路编码获取该线路所有的GPS点)
* @return List<GpsRealData> 返回类型
* @throws
*/
public List<GpsRealData> getListByLineCode(Integer lineCode){
List<GpsRealData> list = lineGpsMultimap.get(lineCode)
,resList = new ArrayList<>();
//写入车辆自编号
String nbbm;
ScheduleRealInfo current, next;
for(GpsRealData gpsData : list){
nbbm = CommonMapped.vehicDeviceBiMap.get(gpsData.getDeviceId());
if(null != nbbm){
gpsData.setNbbm(nbbm);
gpsData.setStationName(CommonMapped.stationCodeMap.get(gpsData.getStopNo()));
current = ScheduleBuffer.findCurrent(nbbm);
if(null != current){
gpsData.setCurrSchId(current.getId());
next = ScheduleBuffer.getNext(current);
if(next != null)
gpsData.setNextSchId(next.getId());
}
/*if(null == current){
next = ScheduleBuffer.getFirst(nbbm);
}
else
next = ScheduleBuffer.getNext(current);*/
/*if(current != null)
gpsData.setCurrSchId(current.getId());
if(next != null)
gpsData.setNextSchId(next.getId());*/
//gpsData.setCurrSchId(ScheduleBuffer.findCurrent(nbbm));
/*subList = ScheduleBuffer.vehSchListMap.get(nbbm);
if(subList.size() == 0)
continue;*/
/*linkedList = ScheduleBuffer.vehLinkedMap.get(nbbm);
if(null != linkedList){
//为GPS点附加班次信息
size = linkedList.size();
if(size > 0)
gpsData.setCurrSchId(linkedList.peekFirst().getId());
if(size > 1)
gpsData.setNextSchId(linkedList.get(1).getId());
}*/
resList.add(gpsData);
}
}
return resList;
}
/**
*
* @Title: getListByLineCode
* @Description: TODO(根据多个线路编码获取该线路所有的GPS点)
* @return List<GpsRealData> 返回类型
* @throws
*/
public List<GpsRealData> getListByLines(Iterator<String> iterator){
List<GpsRealData> list = new ArrayList<>();
Integer code;
while(iterator.hasNext()){
code = Integer.parseInt(iterator.next());
list.addAll(getListByLineCode(code));
}
return list;
}
public GpsRealData findOneByDeviceId(String deviceId){
return deviceGpsMap.get(deviceId);
}
public void setLastUpdateTime(Long time){
lastUpdateTime = time;
}
public Long getLastUpdateTime(){
return lastUpdateTime;
}
public static Collection<GpsRealData> allGps(){
return lineGpsMultimap.values();
}
public static GpsRealData findByDeviceId(String deviceId){
return deviceGpsMap.get(deviceId);
}
}