Commit 22f2cea617feca12295da5d8b3cbfa49fc2beb7b
1 parent
71fdaf28
update...
Showing
5 changed files
with
239 additions
and
25 deletions
src/main/java/com/bsth/server_rs/gps/GpsRestService.java
| @@ -3,7 +3,6 @@ package com.bsth.server_rs.gps; | @@ -3,7 +3,6 @@ package com.bsth.server_rs.gps; | ||
| 3 | import com.bsth.server_rs.gps.buffer.GpsRealDataBuffer; | 3 | import com.bsth.server_rs.gps.buffer.GpsRealDataBuffer; |
| 4 | import com.bsth.server_rs.gps.dao.HistoryGpsDao; | 4 | import com.bsth.server_rs.gps.dao.HistoryGpsDao; |
| 5 | import com.bsth.server_rs.gps.entity.GpsEntity; | 5 | import com.bsth.server_rs.gps.entity.GpsEntity; |
| 6 | -import com.bsth.server_rs.gps.entity.HistoryGpsEntity; | ||
| 7 | import org.springframework.beans.factory.annotation.Autowired; | 6 | import org.springframework.beans.factory.annotation.Autowired; |
| 8 | import org.springframework.stereotype.Component; | 7 | import org.springframework.stereotype.Component; |
| 9 | 8 | ||
| @@ -13,6 +12,7 @@ import javax.ws.rs.PathParam; | @@ -13,6 +12,7 @@ import javax.ws.rs.PathParam; | ||
| 13 | import javax.ws.rs.Produces; | 12 | import javax.ws.rs.Produces; |
| 14 | import javax.ws.rs.core.MediaType; | 13 | import javax.ws.rs.core.MediaType; |
| 15 | import java.util.Collection; | 14 | import java.util.Collection; |
| 15 | +import java.util.Map; | ||
| 16 | 16 | ||
| 17 | /** | 17 | /** |
| 18 | * Created by panzhao on 2017/3/28. | 18 | * Created by panzhao on 2017/3/28. |
| @@ -39,7 +39,7 @@ public class GpsRestService { | @@ -39,7 +39,7 @@ public class GpsRestService { | ||
| 39 | 39 | ||
| 40 | @GET | 40 | @GET |
| 41 | @Path("/history/{nbbm}/{st}/{et}") | 41 | @Path("/history/{nbbm}/{st}/{et}") |
| 42 | - public Collection<HistoryGpsEntity> history(@PathParam("nbbm") String nbbm | 42 | + public Collection<Map<String, Object>> history(@PathParam("nbbm") String nbbm |
| 43 | , @PathParam("st") String st | 43 | , @PathParam("st") String st |
| 44 | , @PathParam("et") String et) { | 44 | , @PathParam("et") String et) { |
| 45 | return historyGpsDao.query(nbbm, Long.parseLong(st), Long.parseLong(et)); | 45 | return historyGpsDao.query(nbbm, Long.parseLong(st), Long.parseLong(et)); |
src/main/java/com/bsth/server_rs/gps/dao/HistoryGpsDao.java
| @@ -3,10 +3,15 @@ package com.bsth.server_rs.gps.dao; | @@ -3,10 +3,15 @@ package com.bsth.server_rs.gps.dao; | ||
| 3 | import com.bsth.redis.util.DateUtils; | 3 | import com.bsth.redis.util.DateUtils; |
| 4 | import com.bsth.server_rs.base_info.car.Car; | 4 | import com.bsth.server_rs.base_info.car.Car; |
| 5 | import com.bsth.server_rs.base_info.car.buffer.CarBufferData; | 5 | import com.bsth.server_rs.base_info.car.buffer.CarBufferData; |
| 6 | +import com.bsth.server_rs.gps.entity.DeviceChange; | ||
| 6 | import com.bsth.server_rs.gps.entity.HistoryGpsEntity; | 7 | import com.bsth.server_rs.gps.entity.HistoryGpsEntity; |
| 7 | import com.bsth.util.DBUtils_MS; | 8 | import com.bsth.util.DBUtils_MS; |
| 9 | +import org.apache.commons.lang3.StringUtils; | ||
| 8 | import org.joda.time.format.DateTimeFormat; | 10 | import org.joda.time.format.DateTimeFormat; |
| 9 | import org.joda.time.format.DateTimeFormatter; | 11 | import org.joda.time.format.DateTimeFormatter; |
| 12 | +import org.slf4j.Logger; | ||
| 13 | +import org.slf4j.LoggerFactory; | ||
| 14 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 10 | import org.springframework.jdbc.core.BeanPropertyRowMapper; | 15 | import org.springframework.jdbc.core.BeanPropertyRowMapper; |
| 11 | import org.springframework.jdbc.core.JdbcTemplate; | 16 | import org.springframework.jdbc.core.JdbcTemplate; |
| 12 | import org.springframework.stereotype.Component; | 17 | import org.springframework.stereotype.Component; |
| @@ -19,42 +24,171 @@ import java.util.*; | @@ -19,42 +24,171 @@ import java.util.*; | ||
| 19 | @Component | 24 | @Component |
| 20 | public class HistoryGpsDao { | 25 | public class HistoryGpsDao { |
| 21 | 26 | ||
| 27 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
| 28 | + | ||
| 29 | + @Autowired | ||
| 30 | + JdbcTemplate jdbcTemplate; | ||
| 22 | 31 | ||
| 23 | /** | 32 | /** |
| 24 | * 最大查询间隔 | 33 | * 最大查询间隔 |
| 25 | */ | 34 | */ |
| 26 | private static final long GPS_RANGE = 1000 * 60 * 60 * 24; | 35 | private static final long GPS_RANGE = 1000 * 60 * 60 * 24; |
| 27 | 36 | ||
| 28 | - public Collection<HistoryGpsEntity> query(String nbbm, Long st, Long et) { | ||
| 29 | - //分区字段 day_years | 37 | + private static DateTimeFormatter fmtyyyy = DateTimeFormat.forPattern("yyyy"); |
| 38 | + | ||
| 39 | + public Collection<Map<String, Object>> query(String nbbm, Long st, Long et) { | ||
| 40 | + List<Map<String, Object>> list = new ArrayList<>(); | ||
| 41 | + if (et - st > GPS_RANGE) | ||
| 42 | + return list; | ||
| 43 | + | ||
| 44 | + // day_of_year 分区字段 | ||
| 30 | Calendar sCal = Calendar.getInstance(); | 45 | Calendar sCal = Calendar.getInstance(); |
| 31 | sCal.setTime(new Date(st)); | 46 | sCal.setTime(new Date(st)); |
| 47 | + int sDayOfYear = sCal.get(Calendar.DAY_OF_YEAR); | ||
| 32 | Calendar eCal = Calendar.getInstance(); | 48 | Calendar eCal = Calendar.getInstance(); |
| 33 | eCal.setTime(new Date(et)); | 49 | eCal.setTime(new Date(et)); |
| 50 | + int eDayOfYear = eCal.get(Calendar.DAY_OF_YEAR); | ||
| 34 | 51 | ||
| 35 | - if (null == st) | ||
| 36 | - return null; | ||
| 37 | - if (null == et) | ||
| 38 | - return null; | ||
| 39 | - if (et < st) | ||
| 40 | - return null; | 52 | + List<DeviceChange> dcs = findDeviceChangeLogs(nbbm, et, st); |
| 41 | 53 | ||
| 42 | - if (et - st > GPS_RANGE) | ||
| 43 | - return null; | 54 | + //按年分表 |
| 55 | + String tableName = "bsth_c_gps_info_" + fmtyyyy.print(st); | ||
| 44 | 56 | ||
| 45 | - Car c = CarBufferData.findOne(nbbm); | ||
| 46 | - if (null == c) | ||
| 47 | - return null; | 57 | + StringBuilder sql = new StringBuilder(""); |
| 58 | + long t1,t2; | ||
| 59 | + DeviceChange dc; | ||
| 60 | + for(int i = 0,len=dcs.size(); i < len; i++){ | ||
| 61 | + t1 = st; | ||
| 62 | + t2 = et; | ||
| 63 | + dc = dcs.get(i); | ||
| 64 | + if(dc.getSt() > st) | ||
| 65 | + t1 = dc.getSt(); | ||
| 66 | + if(dc.getEt() < et && dc.getEt()!=0) | ||
| 67 | + t2 = dc.getEt(); | ||
| 48 | 68 | ||
| 49 | - List<HistoryGpsEntity> list = query(c.getEquipmentCode(), sCal, eCal); | ||
| 50 | - for (HistoryGpsEntity gps : list) { | ||
| 51 | - gps.setNbbm(nbbm); | ||
| 52 | - gps.setUpDown(getUpOrDown(gps.getServiceState())); | ||
| 53 | - gps.setState(getService(gps.getServiceState())); | 69 | + sql.append("select DEVICE_ID,LON,LAT,TS,INOUT_STOP,SERVICE_STATE ,STOP_NO,DIRECTION,LINE_ID,SPEED_GPS,SECTION_CODE from "+tableName+" where days_year in ("+sDayOfYear+","+eDayOfYear+") " + |
| 70 | + " and device_id='"+dc.getDevice()+"' and ts >= "+t1+" and ts <= "+t2+" "); | ||
| 71 | + | ||
| 72 | + if(i == len - 1) | ||
| 73 | + sql.append(" ORDER BY device_id,ts,stop_no"); | ||
| 74 | + else | ||
| 75 | + sql.append(" UNION "); | ||
| 54 | } | 76 | } |
| 77 | + | ||
| 78 | + logger.info("轨迹回放 nbbm: " + nbbm + " -st: " + st + " -et: " + et + " -sql: " + sql.toString()); | ||
| 79 | + | ||
| 80 | + //查询GPS数据 | ||
| 81 | + JdbcTemplate jdbcTemplate_ms = new JdbcTemplate(DBUtils_MS.getDataSource()); | ||
| 82 | + List<Map<String, Object>> dataList = jdbcTemplate_ms.queryForList(sql.toString()); | ||
| 83 | + | ||
| 84 | + Float lon, lat; | ||
| 85 | + int inOutStop; | ||
| 86 | + long serviceState; | ||
| 87 | + | ||
| 88 | + Map<String, Object> map; | ||
| 89 | + for(Map<String, Object> rs : dataList){ | ||
| 90 | + serviceState = map_get_long(rs, "SERVICE_STATE"); | ||
| 91 | + if(getGpsValid(serviceState) == 1) | ||
| 92 | + continue; | ||
| 93 | + | ||
| 94 | + map = new HashMap<>(); | ||
| 95 | + | ||
| 96 | + lon = map_get_float(rs, "LON"); | ||
| 97 | + lat = map_get_float(rs, "LAT"); | ||
| 98 | + //原始坐标 | ||
| 99 | + map.put("lon", lon); | ||
| 100 | + map.put("lat", lat); | ||
| 101 | + | ||
| 102 | + map.put("deviceId", map_get_str(rs, "DEVICE_ID")); | ||
| 103 | + map.put("ts", map_get_long(rs, "TS")); | ||
| 104 | + map.put("timestamp", map_get_long(rs, "TS")); | ||
| 105 | + map.put("stopNo", map_get_str(rs, "STOP_NO")); | ||
| 106 | + map.put("direction", map_get_float(rs,"DIRECTION")); | ||
| 107 | + | ||
| 108 | + map.put("lineId", map_get_str(rs, "LINE_ID")); | ||
| 109 | + map.put("speed", map_get_float(rs,"SPEED_GPS")); | ||
| 110 | + | ||
| 111 | + inOutStop = Integer.parseInt(rs.get("INOUT_STOP").toString()); | ||
| 112 | + map.put("inout_stop", inOutStop); | ||
| 113 | + | ||
| 114 | + | ||
| 115 | + map.put("nbbm", nbbm); | ||
| 116 | + map.put("state", getService(serviceState)); | ||
| 117 | + // 上下行 | ||
| 118 | + map.put("upDown", getUpOrDown(serviceState)); | ||
| 119 | + //路段编码 | ||
| 120 | + map.put("section_code", map_get_str(rs,"SECTION_CODE")); | ||
| 121 | + list.add(map); | ||
| 122 | + } | ||
| 123 | + // 按时间排序 | ||
| 124 | + Collections.sort(list, new Comparator<Map<String, Object>>() { | ||
| 125 | + | ||
| 126 | + @Override | ||
| 127 | + public int compare(Map<String, Object> o1, Map<String, Object> o2) { | ||
| 128 | + return (int) (Long.parseLong(o1.get("ts").toString()) - Long.parseLong(o2.get("ts").toString())); | ||
| 129 | + } | ||
| 130 | + }); | ||
| 131 | + | ||
| 55 | return list; | 132 | return list; |
| 56 | } | 133 | } |
| 57 | 134 | ||
| 135 | + | ||
| 136 | + private List<DeviceChange> findDeviceChangeLogs(String nbbm, long et, long st){ | ||
| 137 | + List<DeviceChange> dcs = null; | ||
| 138 | + List<DeviceChange> rs = new ArrayList<>(); | ||
| 139 | + try{ | ||
| 140 | + | ||
| 141 | + dcs = jdbcTemplate.query("select cl_zbh as nbbm,new_device_no as device,old_device_no as old_device,UNIX_TIMESTAMP(qyrq) * 1000 as st from bsth_c_car_device where is_cancel=0 and cl_zbh='"+nbbm+"' order by qyrq" | ||
| 142 | + , BeanPropertyRowMapper.newInstance(DeviceChange.class)); | ||
| 143 | + | ||
| 144 | + | ||
| 145 | + //生成一条初始记录 | ||
| 146 | + if(dcs.size() > 0){ | ||
| 147 | + DeviceChange first = dcs.get(0); | ||
| 148 | + | ||
| 149 | + DeviceChange initDv = new DeviceChange(); | ||
| 150 | + initDv.setDevice(first.getOldDevice()); | ||
| 151 | + if(StringUtils.isNotEmpty(initDv.getDevice())){ | ||
| 152 | + initDv.setNbbm(first.getNbbm()); | ||
| 153 | + initDv.setSt(0); | ||
| 154 | + initDv.setEt(first.getSt()); | ||
| 155 | + dcs.add(0, initDv); | ||
| 156 | + } | ||
| 157 | + } | ||
| 158 | + for(int i = 0,len=dcs.size(); i < len - 1; i++){ | ||
| 159 | + dcs.get(i).setEt(dcs.get(i + 1).getSt()); | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + for(DeviceChange dc : dcs){ | ||
| 163 | + if(dc.getEt() < st && dc.getEt() != 0) | ||
| 164 | + continue; | ||
| 165 | + if(dc.getSt() > et) | ||
| 166 | + continue; | ||
| 167 | + | ||
| 168 | + rs.add(dc); | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + //没有设备变更记录,则参考车辆信息上的设备号 | ||
| 172 | + if(null == rs || rs.size() == 0){ | ||
| 173 | + Car car = CarBufferData.findOne(nbbm); | ||
| 174 | + | ||
| 175 | + if(null != car){ | ||
| 176 | + DeviceChange dc = new DeviceChange(); | ||
| 177 | + dc.setNbbm(nbbm); | ||
| 178 | + dc.setDevice(car.getEquipmentCode()); | ||
| 179 | + dc.setSt(st); | ||
| 180 | + dc.setEt(et); | ||
| 181 | + dc.setType(1); | ||
| 182 | + | ||
| 183 | + rs.add(dc); | ||
| 184 | + } | ||
| 185 | + } | ||
| 186 | + }catch (Exception e){ | ||
| 187 | + logger.error("", e); | ||
| 188 | + } | ||
| 189 | + return rs; | ||
| 190 | + } | ||
| 191 | + | ||
| 58 | private static DateTimeFormatter fmtyyyyMMdd = DateTimeFormat.forPattern("yyyy-MM-dd"); | 192 | private static DateTimeFormatter fmtyyyyMMdd = DateTimeFormat.forPattern("yyyy-MM-dd"); |
| 59 | private List<HistoryGpsEntity> query(String deviceId, Calendar sCal, Calendar eCal) { | 193 | private List<HistoryGpsEntity> query(String deviceId, Calendar sCal, Calendar eCal) { |
| 60 | int sDayOfYear = sCal.get(Calendar.DAY_OF_YEAR); | 194 | int sDayOfYear = sCal.get(Calendar.DAY_OF_YEAR); |
| @@ -120,4 +254,20 @@ public class HistoryGpsDao { | @@ -120,4 +254,20 @@ public class HistoryGpsDao { | ||
| 120 | return -1; | 254 | return -1; |
| 121 | return (byte) (((serviceState & 0x02000000) == 0x02000000) ? 1 : 0); | 255 | return (byte) (((serviceState & 0x02000000) == 0x02000000) ? 1 : 0); |
| 122 | } | 256 | } |
| 257 | + | ||
| 258 | + private String map_get_str(Map<String, Object> map, String key){ | ||
| 259 | + return map.containsKey(key)?map.get(key).toString():""; | ||
| 260 | + } | ||
| 261 | + | ||
| 262 | + private Long map_get_long(Map<String, Object> map, String key){ | ||
| 263 | + return map.containsKey(key)?Long.parseLong(map.get(key).toString()):-1; | ||
| 264 | + } | ||
| 265 | + | ||
| 266 | + private Float map_get_float(Map<String, Object> map, String key){ | ||
| 267 | + return map.containsKey(key)?Float.parseFloat(map.get(key).toString()):-1; | ||
| 268 | + } | ||
| 269 | + | ||
| 270 | + public static byte getGpsValid(long serviceState) { | ||
| 271 | + return (byte)(((serviceState & 0x80000000) == 0x80000000) ? 1 : 0); | ||
| 272 | + } | ||
| 123 | } | 273 | } |
src/main/java/com/bsth/server_rs/gps/entity/DeviceChange.java
0 → 100644
| 1 | +package com.bsth.server_rs.gps.entity; | ||
| 2 | + | ||
| 3 | +/** | ||
| 4 | + * 车辆设备变更记录 | ||
| 5 | + * Created by panzhao on 2018/1/16. | ||
| 6 | + */ | ||
| 7 | +public class DeviceChange { | ||
| 8 | + | ||
| 9 | + private String nbbm; | ||
| 10 | + private String oldDevice; | ||
| 11 | + private String device; | ||
| 12 | + private long st; | ||
| 13 | + private long et; | ||
| 14 | + | ||
| 15 | + //为1 表示没有设备变更记录,直接从车辆信息上关联的设备号 | ||
| 16 | + private int type=0; | ||
| 17 | + | ||
| 18 | + public String getNbbm() { | ||
| 19 | + return nbbm; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + public void setNbbm(String nbbm) { | ||
| 23 | + this.nbbm = nbbm; | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + public String getDevice() { | ||
| 27 | + return device; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + public void setDevice(String device) { | ||
| 31 | + this.device = device; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + public long getSt() { | ||
| 35 | + return st; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + public void setSt(long st) { | ||
| 39 | + this.st = st; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + public long getEt() { | ||
| 43 | + return et; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + public void setEt(long et) { | ||
| 47 | + this.et = et; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + public int getType() { | ||
| 51 | + return type; | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + public void setType(int type) { | ||
| 55 | + this.type = type; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + public String getOldDevice() { | ||
| 59 | + return oldDevice; | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + public void setOldDevice(String oldDevice) { | ||
| 63 | + this.oldDevice = oldDevice; | ||
| 64 | + } | ||
| 65 | +} |
src/main/java/com/bsth/server_rs/schedule/real/ScheduleRealService.java
| @@ -14,7 +14,6 @@ import org.slf4j.Logger; | @@ -14,7 +14,6 @@ import org.slf4j.Logger; | ||
| 14 | import org.slf4j.LoggerFactory; | 14 | import org.slf4j.LoggerFactory; |
| 15 | import org.springframework.beans.factory.annotation.Autowired; | 15 | import org.springframework.beans.factory.annotation.Autowired; |
| 16 | import org.springframework.stereotype.Component; | 16 | import org.springframework.stereotype.Component; |
| 17 | -import org.springframework.web.bind.annotation.RequestParam; | ||
| 18 | 17 | ||
| 19 | import javax.ws.rs.GET; | 18 | import javax.ws.rs.GET; |
| 20 | import javax.ws.rs.Path; | 19 | import javax.ws.rs.Path; |
| @@ -145,8 +144,8 @@ public class ScheduleRealService { | @@ -145,8 +144,8 @@ public class ScheduleRealService { | ||
| 145 | * @return | 144 | * @return |
| 146 | */ | 145 | */ |
| 147 | @GET | 146 | @GET |
| 148 | - @Path("/pz_vip/readByLine/{rq}") | ||
| 149 | - public List<ScheduleRealInfo> readByLine(@PathParam("rq") String rq,@RequestParam String lineCode){ | 147 | + @Path("/pz_vip/readByRqAndLine/{rq}/{lineCode}") |
| 148 | + public List<ScheduleRealInfo> readByLine(@PathParam("rq") String rq,@PathParam("lineCode") String lineCode){ | ||
| 150 | List<ScheduleRealInfo> all = redisService.read(rq, lineCode), rs = new ArrayList<>(); | 149 | List<ScheduleRealInfo> all = redisService.read(rq, lineCode), rs = new ArrayList<>(); |
| 151 | 150 | ||
| 152 | for(ScheduleRealInfo sch : all){ | 151 | for(ScheduleRealInfo sch : all){ |
src/main/resources/ms-jdbc.properties
| @@ -4,6 +4,6 @@ | @@ -4,6 +4,6 @@ | ||
| 4 | #ms.mysql.password= panzhao | 4 | #ms.mysql.password= panzhao |
| 5 | 5 | ||
| 6 | ms.mysql.driver= com.mysql.jdbc.Driver | 6 | ms.mysql.driver= com.mysql.jdbc.Driver |
| 7 | -ms.mysql.url= jdbc:mysql://10.10.150.21:3306/ms?useUnicode=true&characterEncoding=utf-8 | 7 | +ms.mysql.url= jdbc:mysql://10.10.200.226:3306/ms?useUnicode=true&characterEncoding=utf-8 |
| 8 | ms.mysql.username= root | 8 | ms.mysql.username= root |
| 9 | -ms.mysql.password= root2jsp@JSP | ||
| 10 | \ No newline at end of file | 9 | \ No newline at end of file |
| 10 | +ms.mysql.password= root2jsp | ||
| 11 | \ No newline at end of file | 11 | \ No newline at end of file |