GpsCacheData.java
4.82 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
package com.bsth.data.gps;
import com.bsth.data.geo.GeoCacheData;
import com.bsth.data.gps.util.CircleQueue;
import com.bsth.data.history.HistoryConsumeTimeDataHandler;
import com.bsth.entity.GpsEntity;
import com.bsth.entity.StationRoute;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* GPS 历史缓存 和 实时对照缓存
*/
public class GpsCacheData {
/**
* 每个设备缓存最后 200 个 GPS 点位
*/
private static final int MAX_CACHE_SIZE = 200;
private static ConcurrentMap<String, CircleQueue> prevMap;
/**
* 实时gps对照 K: 设备号
*/
private static ConcurrentMap<String, GpsEntity> realMap;
/**
* 线路索引的设备号K:lineCode_upDown
*/
private static ListMultimap<String, String> lineRealMap;
static {
prevMap = new ConcurrentHashMap<>();
realMap = new ConcurrentHashMap<>();
lineRealMap = Multimaps.synchronizedListMultimap(ArrayListMultimap.create());
}
public static void put(GpsEntity gps) {
String device = gps.getDeviceId();
GpsEntity prev = realMap.get(device);
if (null != prev) {
if (!prev.getCode().equals(gps.getCode())) {
lineRealMap.remove(prev.getCode(), prev.getDeviceId());
lineRealMap.put(gps.getCode(), gps.getDeviceId());
}
putPrev(prev);
} else
lineRealMap.put(gps.getCode(), gps.getDeviceId());
realMap.put(device, gps);
}
private static void putPrev(GpsEntity prev) {
String device = prev.getDeviceId();
CircleQueue queue = prevMap.get(device);
if (null == queue) {
queue = new CircleQueue(MAX_CACHE_SIZE);
prevMap.put(device, queue);
}
queue.add(prev);
}
/**
* 获取指定时间之前的轨迹数据
*
* @param device
* @param maxSecond
* @param minSecond
* @return 如果数据不满足 < minSecond ,则返回空list
*/
public static List<GpsEntity> prev(String device, long maxSecond, long minSecond) {
List<GpsEntity> rs = new ArrayList<>();
CircleQueue queue = prevMap.get(device);
if (null == queue)
return rs;
long t = System.currentTimeMillis();
minSecond = t - (minSecond * 1000);
maxSecond = t - (maxSecond * 1000);
GpsEntity[] array = queue.getQueue();
if (t - array[0].getTimestamp() < minSecond)
return rs;
for (int i = array.length - 1; i > 0; i--) {
if (t - array[i].getTimestamp() > maxSecond)
break;
rs.add(array[i]);
}
return rs;
}
public static GpsEntity findOne(String device) {
return realMap.get(device);
}
public static Map<String, Object> find(String lineCode, int upDown) {
List<String> ds = lineRealMap.get(lineCode + "_" + upDown);
List<GpsEntity> list = new ArrayList<>(ds.size());
for (String d : ds) {
list.add(findOne(d));
}
Map<String, Object> rs = new HashMap<>();
rs.put("list", list);
rs.put("forecast", HistoryConsumeTimeDataHandler.forecastEnd(list, null));//终点时间预测
return rs;
}
/**
* 上一个进出的站点
*
* @param gps
* @return
*/
public static StationRoute prevInStation(GpsEntity gps) {
CircleQueue queue = prevMap.get(gps.getDeviceId());
if (null == queue || queue.size() == 0)
return null;
GpsEntity[] array = queue.getQueue();
GpsEntity prev;
for (int i = array.length - 1; i > 0; i--) {
prev = array[i];
if (prev.getInOut() > 0
&& !prev.getStationCode().equals(gps.getStationCode())) {
return GeoCacheData.findByCode(prev);
}
}
return null;
}
public static void clear(String deviceId) {
CircleQueue queue = prevMap.get(deviceId);
if (null != queue) {
queue = null;
prevMap.remove(deviceId);
}
}
public static List<GpsEntity> findList(String lineCode, int upDown) {
List<String> ds = lineRealMap.get(lineCode + "_" + upDown);
List<GpsEntity> list = new ArrayList<>(ds.size());
for (String d : ds) {
list.add(findOne(d));
}
return list;
}
}