ArrivalThread.java
7.91 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package com.bsth.vehicle.gpsdata;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.bsth.util.DateUtils;
import com.bsth.util.db.DBUtils_MS;
import com.bsth.vehicle.directive.service.DirectiveService;
import com.bsth.vehicle.gpsdata.arrival.MatchService;
import com.bsth.vehicle.gpsdata.buffer.ArrivalDataBuffer;
import com.bsth.vehicle.gpsdata.entity.ArrivalInfo;
import com.bsth.websocket.handler.RealControlSocketHandler;
/**
*
* @ClassName: GpsArrivalStationThread
* @Description: TODO(定时从数据库加载到离站数据)
* @author PanZhao
* @date 2016年6月27日 上午10:58:13
*
*/
@Component
public class ArrivalThread extends Thread{
Logger logger = LoggerFactory.getLogger(this.getClass());
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
@Autowired
DirectiveService directiveService;
@Autowired
RealControlSocketHandler socketHandler;
Long diff = 1000 * 60 * 20L;
@Override
public void run() {
List<ArrivalInfo> list = null;
try {
list = loadData();
} catch (ParseException e) {
e.printStackTrace();
}
//缓存
ArrivalDataBuffer.putAll(list);
//有新到离站数据的线路
Set<String> newSet = new HashSet<>();
for(ArrivalInfo arr : list)
newSet.add(arr.getLineCode());
//启动对应的匹配线程
for(String lineCode : newSet){
MatchService.addService(lineCode);
}
/*try {
Bootstrap bs = Bootstrap.instance();
bs.start();
} catch (Exception e) {
e.printStackTrace();
}*/
/*//车辆
Set<String> set = new HashSet<>();
for(ArrivalInfo arr : list)
set.add(CommonMapped.vehicDeviceBiMap.get(arr.getDeviceId()));
List<ArrivalInfo> rsList = null;
List<ScheduleRealInfo> subList = null;
ArrivalInfo[] status;
for(String nbbm : set){
rsList = GpsArrivalDataBuffer.pops(nbbm);
//和该车辆班次进行匹配
subList = ScheduleBuffer.vehSchListMap.get(nbbm);
if(null == subList || subList.size() == 0)
continue;
try{
for(ScheduleRealInfo schedule : subList){
//匹配结果
status = match(rsList, schedule);
//发车
if(status[0] != null){
//班次状态改为正在执行
schedule.setStatus(1);
ScheduleBuffer.persistentList.add(schedule);
//推送到页面
sendFcsj(schedule);
rsList.remove(status[0]);
}
//到达终点
if(status[1] != null){
//完成当前班次
ScheduleRealInfo nextSch = ScheduleBuffer.finishSch(schedule);
//到达终点,发送下一班次的调度指令
int finish = ScheduleBuffer.getFinishSchNo(nextSch.getClZbh());
directiveService.send60Dispatch(nextSch, finish);
//推送到页面
sendZdsj(schedule, nextSch, finish);
rsList.remove(status[1]);
}
}
}catch(Exception e){
e.printStackTrace();
}
}*/
}
/* public ArrivalInfo[] match(List<ArrivalInfo> arrList, ScheduleRealInfo sch){
//进出分组
List<ArrivalInfo> in = new ArrayList<>(), out = new ArrayList<>();
for(ArrivalInfo arr : arrList){
if(arr.getInOut() == 0)
in.add(arr);
else
out.add(arr);
}
ArrivalInfo a1 = null, a2 = null;
//匹配发车时间
if(sch.getFcsjActual() == null && sch.getFcsj() != null)
a1 = matchOut(out, sch);
//匹配终点时间
if(sch.getZdsjActual() == null && sch.getZdsj() != null)
a2 = matchIn(in, sch);
return new ArrivalInfo[]{a1,a2};
}
public ArrivalInfo matchOut(List<ArrivalInfo> arrList, ScheduleRealInfo sch){
Long space = ScheduleBuffer.schSpaceMap.get(sch.getXlBm() + "_" + sch.getXlDir())diff;
for(ArrivalInfo arr : arrList){
if(arr.getStopNo().equals(sch.getQdzCode())
&& Math.abs((arr.getTs() - sch.getFcsjT())) < space){
sch.setFcsjActualAll(arr.getTs());
return arr;
}
}
return null;
}
public ArrivalInfo matchIn(List<ArrivalInfo> arrList, ScheduleRealInfo sch){
Long space = ScheduleBuffer.schSpaceMap.get(sch.getXlBm() + "_" + sch.getXlDir())diff;
for(ArrivalInfo arr : arrList){
if(arr.getStopNo().equals(sch.getZdzCode())
&& Math.abs((arr.getTs() - sch.getZdsjT())) < space){
sch.setZdsjActualAll(arr.getTs());
return arr;
}
}
return null;
}*/
/**
* @Title: sendFcsj
* @Description: TODO(推送发车信息)
* @param @param schedule 班次
* @throws
*/
/* public void sendFcsj(ScheduleRealInfo schedule){
JSONObject json = new JSONObject();
json.put("fn", "faChe");
json.put("t", schedule);
json.put("dataStr", sdf.format(new Date()));
socketHandler.sendMessageToLine(Integer.parseInt(schedule.getXlBm()), json.toJSONString());
}
*//**
* @Title: sendFcsj
* @Description: TODO(推送到达终点时间)
* @param @param schedule 班次
* @throws
*//*
public void sendZdsj(ScheduleRealInfo schedule,ScheduleRealInfo nextSch, int finish){
JSONObject json = new JSONObject();
json.put("fn", "zhongDian");
json.put("t", schedule);
json.put("nt", nextSch);
json.put("finish", finish);
json.put("dataStr", sdf.format(new Date()));
socketHandler.sendMessageToLine(Integer.parseInt(schedule.getXlBm()), json.toJSONString());
}*/
/**
* @throws ParseException
*
* @Title: loadData
* @Description: TODO(从数据库加载到离站信息)
* @return List<ArrivalInfo> 返回类型
* @throws
*/
private List<ArrivalInfo> loadData() throws ParseException{
Calendar cal = Calendar.getInstance();
//周数,表分区字段
int weeks_year = cal.get(Calendar.WEEK_OF_YEAR);
//按时间标记增量加载
if(null == ArrivalDataBuffer.markTime){
//第一次从当天0点开始
ArrivalDataBuffer.markTime = DateUtils.getTimesmorning() * 1000L;
}
Long t = System.currentTimeMillis();
String sql = "select * from bsth_c_arrival_info where weeks_year=? and create_timestamp > ? and create_timestamp <=? order by create_date";
List<ArrivalInfo> list = new ArrayList<>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtils_MS.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, weeks_year/*30*/);
ps.setLong(2, ArrivalDataBuffer.markTime/*1461361377000L*/);
ps.setLong(3, t/*30*/);
rs = ps.executeQuery();
while(rs.next()){
list.add(new ArrivalInfo(rs.getString("device_id"), rs.getLong("ts"), rs.getString("line_id")
, rs.getInt("up_down"), rs.getString("stop_no"), rs.getInt("in_out"), rs.getLong("create_date")
, rs.getInt("weeks_year")));
}
logger.info("获取 到离站 数量:" + list.size() + ", st: " + ArrivalDataBuffer.markTime + " , et: " + t);
//时间戳标记
ArrivalDataBuffer.markTime = t;
} catch (Exception e) {
logger.error("", e);
}finally {
DBUtils_MS.close(rs, ps, conn);
}
return list;
}
Long rang = 1000 * 60 * 60L;
/**
*
* @Title: extractSched
* @Description: TODO(提取当前时间前后一小时的计划)
* @param @param allList
* @throws
public List<ScheduleRealInfo> extractSched(List<ScheduleRealInfo> allList){
List<ScheduleRealInfo> subList = new ArrayList<>();
Long t = System.currentTimeMillis();
for(ScheduleRealInfo sch : allList){
if(Math.abs(sch.getFcsjT() - t) < rang
|| (sch.getZdsjT() != null && Math.abs(sch.getZdsjT()) - t < rang)){
subList.add(sch);
}
}
return subList;
} */
}