GpsBeforeBuffer.java
3.59 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
package com.bsth.client;
import com.bsth.Application;
import com.bsth.data.BasicData;
import com.bsth.data.gpsdata.GpsEntity;
import com.bsth.data.gpsdata.arrival.GpsRealAnalyse;
import com.bsth.data.gpsdata.client.pd.protocol.BasicInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* 从 socket client 到 .. 的缓冲
* Created by panzhao on 2017/5/4.
*/
@Component
public class GpsBeforeBuffer {
static LinkedList<GpsEntity> linkedList = new LinkedList<>();
@Autowired
GpsHandleThread gpsHandleThread;
public void put(BasicInfo basicInfo){
//放弃补发数据
byte cacheData = getCacheState(basicInfo.getServiceState());
if(cacheData == 1)
return;
GpsEntity gps = new GpsEntity();
gps.setDeviceId(basicInfo.getDeviceId());
gps.setTimestamp(basicInfo.getTimestamp());
gps.setLat(basicInfo.getLat());
gps.setLon(basicInfo.getLon());
gps.setDirection((float)basicInfo.getDirection() / 10);
gps.setValid(basicInfo.getGpsValid());
gps.setCompanyCode(basicInfo.getCompanyCode());
gps.setStopNo(basicInfo.getStopNo());
gps.setUpDown(basicInfo.getUpOrDown());
gps.setSpeed((float)basicInfo.getSpeedGps() / 10);
gps.setLineId(String.valueOf(basicInfo.getLineId()));
gps.setState((int) getService(basicInfo.getServiceState()));
//没有设备号
if (StringUtils.isBlank(gps.getDeviceId()))
return;
String nbbm = BasicData.deviceId2NbbmMap.get(gps.getDeviceId());
if (StringUtils.isBlank(nbbm))
gps.setIncomplete(true);
else
gps.setNbbm(nbbm);
linkedList.addLast(gps);
}
public byte getCacheState(long serviceState) {
return (byte)(((serviceState & 0x00100000) == 0x00100000) ? 1 : 0);
}
public void init(){
Application.mainServices.scheduleWithFixedDelay(gpsHandleThread, 20 * 1000, 500, TimeUnit.MILLISECONDS);
}
static int idleCount = 0;
@Component
public static class GpsHandleThread extends Thread{
List<GpsEntity> list;
@Autowired
GpsRealAnalyse gpsRealAnalyse;
@Autowired
ClientApp clientApp;
Logger log = LoggerFactory.getLogger(this.getClass());
@Override
public void run() {
list = new ArrayList<>(200);
GpsEntity gps;
for(int i = 0; i < 4000; i ++){
gps = linkedList.poll();
if(gps == null)
break;
list.add(gps);
}
if(list.size() == 0){
idleCount ++;
//连续40次没有数据,重建socket连接
if(idleCount == 40){
log.info("idleCount == 40");
idleCount = 0;
clientApp.destroy();
clientApp.init();
}
}
gpsRealAnalyse.analyse(list);
}
}
/**
* 获取运营状态
*
* @return -1无效 0运营 1未运营
*/
public static byte getService(long serviceState) {
if ((serviceState & 0x00020000) == 0x00020000 || (serviceState & 0x80000000) == 0x80000000)
return -1;
return (byte) (((serviceState & 0x02000000) == 0x02000000) ? 1 : 0);
}
}