Commit 70bf91fe08f3809059d5759cfea4af66983d7b3e
1 parent
fbb5769f
up
Showing
1 changed file
with
267 additions
and
0 deletions
src/main/java/com/bsth/vehicle/gpsdata/GpsArrivalStationThread_old.java
0 → 100644
| 1 | +package com.bsth.vehicle.gpsdata; | |
| 2 | + | |
| 3 | +import java.sql.Connection; | |
| 4 | +import java.sql.PreparedStatement; | |
| 5 | +import java.sql.ResultSet; | |
| 6 | +import java.text.ParseException; | |
| 7 | +import java.text.SimpleDateFormat; | |
| 8 | +import java.util.ArrayList; | |
| 9 | +import java.util.Calendar; | |
| 10 | +import java.util.Date; | |
| 11 | +import java.util.Iterator; | |
| 12 | +import java.util.List; | |
| 13 | +import java.util.Set; | |
| 14 | + | |
| 15 | +import org.slf4j.Logger; | |
| 16 | +import org.slf4j.LoggerFactory; | |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 18 | +import org.springframework.stereotype.Component; | |
| 19 | + | |
| 20 | +import com.alibaba.fastjson.JSONObject; | |
| 21 | +import com.bsth.entity.realcontrol.ScheduleRealInfo; | |
| 22 | +import com.bsth.service.realcontrol.buffer.ScheduleBuffer; | |
| 23 | +import com.bsth.util.DateUtils; | |
| 24 | +import com.bsth.util.db.DBUtils_MS; | |
| 25 | +import com.bsth.vehicle.directive.service.DirectiveService; | |
| 26 | +import com.bsth.vehicle.gpsdata.buffer.GpsArrivalDataBuffer; | |
| 27 | +import com.bsth.vehicle.gpsdata.entity.ArrivalInfo; | |
| 28 | +import com.bsth.websocket.handler.RealControlSocketHandler; | |
| 29 | + | |
| 30 | +/** | |
| 31 | + * | |
| 32 | + * @ClassName: GpsArrivalStationThread | |
| 33 | + * @Description: TODO(GPS到离站) | |
| 34 | + * @author PanZhao | |
| 35 | + * @date 2016年6月27日 上午10:58:13 | |
| 36 | + * | |
| 37 | + */ | |
| 38 | +@Component | |
| 39 | +public class GpsArrivalStationThread_old extends Thread{ | |
| 40 | + | |
| 41 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 42 | + SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); | |
| 43 | + | |
| 44 | + @Autowired | |
| 45 | + DirectiveService directiveService; | |
| 46 | + | |
| 47 | + @Autowired | |
| 48 | + RealControlSocketHandler socketHandler; | |
| 49 | + | |
| 50 | + private static int diff = 1000 * 60 * 20; | |
| 51 | + | |
| 52 | + @Override | |
| 53 | + public void run() { | |
| 54 | + List<ArrivalInfo> list = null; | |
| 55 | + try { | |
| 56 | + list = loadData(); | |
| 57 | + } catch (ParseException e) { | |
| 58 | + e.printStackTrace(); | |
| 59 | + } | |
| 60 | + GpsArrivalDataBuffer.putAll(list); | |
| 61 | + //实际到离站和计划排班相匹配 | |
| 62 | + | |
| 63 | + Set<String> keySet = GpsArrivalDataBuffer.allMap.keySet(); | |
| 64 | + System.out.println("开始..."); | |
| 65 | + List<ScheduleRealInfo> schList; | |
| 66 | + for(String key : keySet){ | |
| 67 | + schList = extractSched(ScheduleBuffer.vehLinkedMap.get(key)); | |
| 68 | + if(null != schList) | |
| 69 | + match(GpsArrivalDataBuffer.allMap.get(key), schList); | |
| 70 | + } | |
| 71 | + System.out.println("结束..."); | |
| 72 | + } | |
| 73 | + | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * | |
| 77 | + * @Title: match | |
| 78 | + * @Description: TODO(实际和计划进行匹配) | |
| 79 | + * @param @param arrList 实际GPS到离站链表 | |
| 80 | + * @param @param schList 计划排班链表 | |
| 81 | + * @throws | |
| 82 | + */ | |
| 83 | + public void match(List<ArrivalInfo> arrList, List<ScheduleRealInfo> schList){ | |
| 84 | + Iterator<ScheduleRealInfo> schIterator = schList.iterator(); | |
| 85 | + | |
| 86 | + while(schIterator.hasNext()) | |
| 87 | + match(schIterator.next(), arrList); | |
| 88 | + } | |
| 89 | + | |
| 90 | + | |
| 91 | + public void match(ScheduleRealInfo scInfo, List<ArrivalInfo> arrList){ | |
| 92 | + for(ArrivalInfo arr : arrList){ | |
| 93 | + match(scInfo, arr); | |
| 94 | + } | |
| 95 | + } | |
| 96 | + | |
| 97 | + public void match(ScheduleRealInfo scInfo, ArrivalInfo arr){ | |
| 98 | + try{ | |
| 99 | + //匹配起点 | |
| 100 | + matchStart(scInfo, arr); | |
| 101 | + | |
| 102 | + //匹配终点 | |
| 103 | + matchEnd(scInfo, arr); | |
| 104 | + }catch(Exception e){ | |
| 105 | + e.printStackTrace(); | |
| 106 | + } | |
| 107 | + } | |
| 108 | + | |
| 109 | + /** | |
| 110 | + * | |
| 111 | + * @Title: matchStart | |
| 112 | + * @Description: TODO(匹配起点 出站时间) | |
| 113 | + * @param @param scInfo | |
| 114 | + * @throws | |
| 115 | + */ | |
| 116 | + public void matchStart(ScheduleRealInfo scInfo, ArrivalInfo arr){ | |
| 117 | + if(scInfo.getFcsjT() == null | |
| 118 | + || arr.getInOut() != 1 || scInfo.getFcsjActual() != null) | |
| 119 | + return; | |
| 120 | + | |
| 121 | + Long ts = arr.getTs(); | |
| 122 | + //起点站和发车时间比比较 | |
| 123 | + if(scInfo.getQdzCode().equals(arr.getStopNo()) | |
| 124 | + && Math.abs(scInfo.getFcsjT() - ts) < diff){ | |
| 125 | + scInfo.setFcsjActualTime(ts); | |
| 126 | + scInfo.setFcsjActual(sdf.format(ts)); | |
| 127 | + | |
| 128 | + System.out.println("成功匹配一个起点..."); | |
| 129 | + //班次状态改为正在执行 | |
| 130 | + scInfo.setStatus(1); | |
| 131 | + ScheduleBuffer.persistentList.add(scInfo); | |
| 132 | + //推送到页面 | |
| 133 | + sendFcsj(scInfo); | |
| 134 | + } | |
| 135 | + } | |
| 136 | + | |
| 137 | + /** | |
| 138 | + * @Title: sendFcsj | |
| 139 | + * @Description: TODO(推送发车信息) | |
| 140 | + * @param @param schedule 班次 | |
| 141 | + * @throws | |
| 142 | + */ | |
| 143 | + public void sendFcsj(ScheduleRealInfo schedule){ | |
| 144 | + JSONObject json = new JSONObject(); | |
| 145 | + json.put("fn", "faChe"); | |
| 146 | + json.put("t", schedule); | |
| 147 | + json.put("dataStr", sdf.format(new Date())); | |
| 148 | + socketHandler.sendMessageToLine(Integer.parseInt(schedule.getXlBm()), json.toJSONString()); | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 152 | + * | |
| 153 | + * @Title: matchEnd | |
| 154 | + * @Description: TODO(匹配终点 进站时间) | |
| 155 | + * @throws | |
| 156 | + */ | |
| 157 | + public void matchEnd(ScheduleRealInfo scInfo, ArrivalInfo arr){ | |
| 158 | + if(scInfo.getZdsjT() == null | |
| 159 | + || arr.getInOut() != 0 || scInfo.getZdsjActual() != null) | |
| 160 | + return; | |
| 161 | + | |
| 162 | + Long ts = arr.getTs(); | |
| 163 | + //终点站和发车时间比较 | |
| 164 | + if(scInfo.getZdzCode().equals(arr.getStopNo()) | |
| 165 | + && Math.abs(scInfo.getZdsjT() - ts) < diff){ | |
| 166 | + scInfo.setZdsjActualTime(ts); | |
| 167 | + scInfo.setZdsjActual(sdf.format(ts)); | |
| 168 | + | |
| 169 | + System.out.println("成功匹配一个终点..."); | |
| 170 | + //完成当前班次 | |
| 171 | + ScheduleRealInfo nextSch = ScheduleBuffer.finishSch(scInfo); | |
| 172 | + //到达终点,发送下一班次的调度指令 | |
| 173 | + int finish = ScheduleBuffer.getFinishSchNo(nextSch.getClZbh()); | |
| 174 | + directiveService.send60Dispatch(nextSch, finish); | |
| 175 | + //推送到页面 | |
| 176 | + sendZdsj(scInfo, nextSch, finish); | |
| 177 | + } | |
| 178 | + } | |
| 179 | + | |
| 180 | + /** | |
| 181 | + * @Title: sendFcsj | |
| 182 | + * @Description: TODO(推送到达终点时间) | |
| 183 | + * @param @param schedule 班次 | |
| 184 | + * @throws | |
| 185 | + */ | |
| 186 | + public void sendZdsj(ScheduleRealInfo schedule,ScheduleRealInfo nextSch, int finish){ | |
| 187 | + JSONObject json = new JSONObject(); | |
| 188 | + json.put("fn", "zhongDian"); | |
| 189 | + json.put("t", schedule); | |
| 190 | + json.put("nt", nextSch); | |
| 191 | + json.put("finish", finish); | |
| 192 | + json.put("dataStr", sdf.format(new Date())); | |
| 193 | + | |
| 194 | + socketHandler.sendMessageToLine(Integer.parseInt(schedule.getXlBm()), json.toJSONString()); | |
| 195 | + } | |
| 196 | + | |
| 197 | + /** | |
| 198 | + * @throws ParseException | |
| 199 | + * | |
| 200 | + * @Title: loadData | |
| 201 | + * @Description: TODO(从数据库加载到离站信息) | |
| 202 | + * @return List<ArrivalInfo> 返回类型 | |
| 203 | + * @throws | |
| 204 | + */ | |
| 205 | + private List<ArrivalInfo> loadData() throws ParseException{ | |
| 206 | + Calendar cal = Calendar.getInstance(); | |
| 207 | + //周数,表分区字段 | |
| 208 | + int weeks_year = cal.get(Calendar.WEEK_OF_YEAR); | |
| 209 | + //按时间标记增量加载 | |
| 210 | + if(null == GpsArrivalDataBuffer.markTime){ | |
| 211 | + //第一次从当天0点开始 | |
| 212 | + GpsArrivalDataBuffer.markTime = DateUtils.getTimesmorning() * 1000L; | |
| 213 | + } | |
| 214 | + | |
| 215 | + String sql = "select * from bsth_c_arrival_info where weeks_year=? and create_date > ? order by ts"; | |
| 216 | + | |
| 217 | + List<ArrivalInfo> list = new ArrayList<>(); | |
| 218 | + Connection conn = null; | |
| 219 | + PreparedStatement ps = null; | |
| 220 | + ResultSet rs = null; | |
| 221 | + try { | |
| 222 | + conn = DBUtils_MS.getConnection(); | |
| 223 | + ps = conn.prepareStatement(sql); | |
| 224 | + ps.setInt(1, /*weeks_year*/28); | |
| 225 | + ps.setLong(2, /*GpsArrivalDataBuffer.markTime*/1467853749000L); | |
| 226 | + | |
| 227 | + Long t = System.currentTimeMillis(); | |
| 228 | + rs = ps.executeQuery(); | |
| 229 | + | |
| 230 | + while(rs.next()){ | |
| 231 | + list.add(new ArrivalInfo(rs.getString("device_id"), rs.getLong("ts"), rs.getString("line_id") | |
| 232 | + , rs.getInt("up_down"), rs.getString("stop_no"), rs.getInt("in_out"), rs.getDate("create_date"), rs.getInt("weeks_year"))); | |
| 233 | + } | |
| 234 | + | |
| 235 | + //重新打时间标记 | |
| 236 | + GpsArrivalDataBuffer.markTime = t; | |
| 237 | + | |
| 238 | + } catch (Exception e) { | |
| 239 | + logger.error("", e); | |
| 240 | + }finally { | |
| 241 | + DBUtils_MS.close(rs, ps, conn); | |
| 242 | + } | |
| 243 | + return list; | |
| 244 | + } | |
| 245 | + | |
| 246 | + Long rang = 1000 * 60 * 60L; | |
| 247 | + /** | |
| 248 | + * | |
| 249 | + * @Title: extractSched | |
| 250 | + * @Description: TODO(提取当前时间前后一小时的计划) | |
| 251 | + * @param @param allList | |
| 252 | + * @throws | |
| 253 | + */ | |
| 254 | + public List<ScheduleRealInfo> extractSched(List<ScheduleRealInfo> allList){ | |
| 255 | + List<ScheduleRealInfo> subList = new ArrayList<>(); | |
| 256 | + System.out.println("原计划:" + allList.size()); | |
| 257 | + Long t = System.currentTimeMillis(); | |
| 258 | + for(ScheduleRealInfo sch : allList){ | |
| 259 | + if(Math.abs(sch.getFcsjT() - t) < rang | |
| 260 | + || (sch.getZdsjT() != null && Math.abs(sch.getZdsjT()) - t < rang)){ | |
| 261 | + subList.add(sch); | |
| 262 | + } | |
| 263 | + } | |
| 264 | + System.out.println("按时间提取:" + subList.size()); | |
| 265 | + return subList; | |
| 266 | + } | |
| 267 | +} | ... | ... |