GpsRealAnalyse.java
4.3 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
package com.bsth.data.gpsdata.arrival;
import com.bsth.data.gpsdata.GpsEntity;
import com.bsth.data.gpsdata.GpsRealData;
import com.bsth.data.gpsdata.arrival.handlers.*;
import com.bsth.data.gpsdata.arrival.utils.CircleQueue;
import com.bsth.data.gpsdata.recovery.GpsDataRecovery;
import com.google.common.collect.ArrayListMultimap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* gps 实时数据分析
* Created by panzhao on 2016/12/27.
*/
@Component
public class GpsRealAnalyse {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
OfflineSignalHandle offlineSignalHandle;
@Autowired
CorrectSignalHandle correctSignalHandle;
@Autowired
StationInsideHandle stationInsideHandle;
@Autowired
InOutStationSignalHandle inOutStationSignalHandle;
@Autowired
ReverseSignalHandle reverseSignalHandle;
@Autowired
AbnormalStateHandle abnormalStateHandle;
@Autowired
GpsRealData gpsRealData;
static ExecutorService threadPool = Executors.newFixedThreadPool(100);
public void analyse(List<GpsEntity> list) {
//如果正在恢复数据
if (GpsDataRecovery.run)
return;
long t = System.currentTimeMillis();
logger.info("analyse gps size: " + list.size());
//按线路分组gps
ArrayListMultimap multimap = ArrayListMultimap.create();
for(GpsEntity gps : list){
multimap.put(gps.getLineId(), gps);
}
Set<String> ks = multimap.keySet();
CountDownLatch count = new CountDownLatch(ks.size());
for(String lineCode : ks){
threadPool.execute(new SignalHandleThread(multimap.get(lineCode), count));
}
try {
//等待子线程结束
count.await();
//加入实时gps对照
for(GpsEntity gps: list)
gpsRealData.put(gps);
logger.info("time , " + (System.currentTimeMillis() - t));
} catch (InterruptedException e) {
logger.error("", e);
}
}
static GpsComp comp = new GpsComp();
public class SignalHandleThread implements Runnable {
List<GpsEntity> list;
CountDownLatch count;
SignalHandleThread(List<GpsEntity> gpsList, CountDownLatch count) {
this.list = gpsList;
this.count = count;
}
@Override
public void run() {
try {
Collections.sort(list, comp);
for(GpsEntity gps : list){
//是否有任务
boolean task;
CircleQueue<GpsEntity> prevs = GeoCacheData.getGps(gps.getNbbm());
//掉线处理
offlineSignalHandle.handle(gps, prevs);
//状态处理
task = correctSignalHandle.handle(gps, prevs);
//场,站内外判断
stationInsideHandle.handle(gps, prevs);
//异常判定(越界/超速)
abnormalStateHandle.handle(gps, prevs);
if(!task)
return; //无任务的,到这里就结束
//反向处理
reverseSignalHandle.handle(gps, prevs);
//进出站动作处理
inOutStationSignalHandle.handle(gps, prevs);
GeoCacheData.putGps(gps);
}
} catch (Exception e) {
logger.error("", e);
} finally {
if(count != null)
count.countDown();
}
}
}
public static class GpsComp implements Comparator<GpsEntity> {
@Override
public int compare(GpsEntity g1, GpsEntity g2) {
return g1.getTimestamp().compareTo(g2.getTimestamp());
}
}
}