Commit c3a6754a3f143ecf83fe22eade7ca041c9ece14f

Authored by 王通
1 parent 6260e175

1.博协GPS访问接口

src/main/java/com/bsth/server_rs/gps/GpsRestService.java
1 -package com.bsth.server_rs.gps;  
2 -  
3 -import java.text.ParseException;  
4 -import java.util.ArrayList;  
5 -import java.util.Collection;  
6 -import java.util.Date;  
7 -import java.util.HashMap;  
8 -import java.util.List;  
9 -import java.util.Map;  
10 -import java.util.Set;  
11 -  
12 -import javax.ws.rs.GET;  
13 -import javax.ws.rs.Path;  
14 -import javax.ws.rs.PathParam;  
15 -import javax.ws.rs.Produces;  
16 -import javax.ws.rs.core.MediaType;  
17 -  
18 -import org.springframework.beans.factory.annotation.Autowired;  
19 -import org.springframework.stereotype.Component;  
20 -  
21 -import com.bsth.server_rs.gps.buffer.BasicDataBuffer;  
22 -import com.bsth.server_rs.gps.buffer.GpsRealDataBuffer;  
23 -import com.bsth.server_rs.gps.dao.HistoryGpsDao;  
24 -import com.bsth.server_rs.gps.entity.GpsEntity;  
25 -import com.bsth.server_rs.gps.entity.LineInfo;  
26 -import com.bsth.server_rs.gps.entity.StopInfo;  
27 -import com.bsth.util.ThreadLocalDateUtil;  
28 -  
29 -/**  
30 - * Created by panzhao on 2017/3/28.  
31 - */  
32 -@Component  
33 -@Path("/gps")  
34 -@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})  
35 -public class GpsRestService {  
36 -  
37 - @Autowired  
38 - HistoryGpsDao historyGpsDao;  
39 -  
40 - @GET  
41 - @Path("/{deviceId}")  
42 - public GpsEntity findOne(@PathParam("deviceId") String deviceId) {  
43 - return GpsRealDataBuffer.get(deviceId);  
44 - }  
45 -  
46 - @GET  
47 - @Path("/all")  
48 - public Collection<GpsEntity> findAll() {  
49 - return GpsRealDataBuffer.all();  
50 - }  
51 -  
52 - @GET  
53 - @Path("/history/{nbbm}/{st}/{et}")  
54 - public Collection<Map<String, Object>> history(@PathParam("nbbm") String nbbm  
55 - , @PathParam("st") String st  
56 - , @PathParam("et") String et) {  
57 - return historyGpsDao.query(nbbm, Long.parseLong(st), Long.parseLong(et));  
58 - }  
59 -  
60 - @GET  
61 - @Path("/gpsReport")  
62 - public List<Map<String, Object>> gpsReport() {  
63 - List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();  
64 - Collection<GpsEntity> gpss = GpsRealDataBuffer.all();  
65 - Set<Integer> lines = BasicDataBuffer.getAllLine();  
66 - for (GpsEntity gps : gpss) {  
67 - String device = gps.getDeviceId();  
68 - if (lines.contains(Integer.parseInt(gps.getLineId()))) {  
69 - Map<String, Object> map = new HashMap<String, Object>();  
70 - map.put("vehicleNumberPlate", BasicDataBuffer.getPlateByDevice(device));  
71 - try {  
72 - map.put("gpsDateTime", ThreadLocalDateUtil.formatDate(new Date(gps.getTimestamp())));  
73 - } catch (ParseException e) {  
74 - // TODO Auto-generated catch block  
75 - e.printStackTrace();  
76 - }  
77 - map.put("gpsLongitude", gps.getLon());  
78 - map.put("gpsLatitude", gps.getLat());  
79 - map.put("gpsDirection", gps.getDirection());  
80 - map.put("gpsSpeed", gps.getSpeed());  
81 - map.put("gpsIsValid", gps.getValid());  
82 - map.put("lineId", gps.getLineId());  
83 - map.put("lineVersion", 0);  
84 - map.put("upDownMark", gps.getUpDown());  
85 - map.put("stationOrder", getCurrStop(gps));  
86 - map.put("inOutStationState", gps.getInOrOutStation());  
87 - map.put("operationalState", gps.getState());  
88 -  
89 - result.add(map);  
90 - }  
91 - }  
92 -  
93 - return result;  
94 - }  
95 -  
96 - private static int getCurrStop(GpsEntity gps) {  
97 - int res = 0;  
98 -  
99 - LineInfo line = BasicDataBuffer.getLineById(Integer.parseInt(gps.getLineId()));  
100 - if (line != null) {  
101 - List<StopInfo> upStops = new ArrayList<>(line.getStopsUp()), downStops = new ArrayList<>(line.getStopsDown());  
102 - int updown = gps.getUpDown();  
103 - // 环线、内外换的线路  
104 - boolean isRing = line.getLinePlayType() == 1;  
105 - if (isRing) {  
106 - updown = 0;  
107 - downStops.clear();  
108 - }  
109 - List<StopInfo> stops = new ArrayList<StopInfo>();  
110 - stops.addAll(upStops);  
111 - stops.addAll(downStops);  
112 -  
113 - int idx = 1;  
114 - if (updown == 0) {  
115 - for (StopInfo stop : upStops) {  
116 - if (gps.getStopNo().trim().equals(stop.getStationCod())) break;  
117 - idx++;  
118 - }  
119 - } else {  
120 - for (StopInfo stop : downStops) {  
121 - if (gps.getStopNo().trim().equals(stop.getStationCod())) break;  
122 - idx++;  
123 - }  
124 - }  
125 - if (idx <= stops.size()) {  
126 - res = idx;  
127 - }  
128 - }  
129 -  
130 - return res;  
131 - }  
132 -} 1 +package com.bsth.server_rs.gps;
  2 +
  3 +import java.text.ParseException;
  4 +import java.util.ArrayList;
  5 +import java.util.Collection;
  6 +import java.util.Date;
  7 +import java.util.HashMap;
  8 +import java.util.List;
  9 +import java.util.Map;
  10 +import java.util.Set;
  11 +
  12 +import javax.ws.rs.GET;
  13 +import javax.ws.rs.Path;
  14 +import javax.ws.rs.PathParam;
  15 +import javax.ws.rs.Produces;
  16 +import javax.ws.rs.core.MediaType;
  17 +
  18 +import com.bsth.server_rs.base_info.car.Car;
  19 +import com.bsth.server_rs.base_info.car.buffer.CarBufferData;
  20 +import org.springframework.beans.factory.annotation.Autowired;
  21 +import org.springframework.stereotype.Component;
  22 +
  23 +import com.bsth.server_rs.gps.buffer.BasicDataBuffer;
  24 +import com.bsth.server_rs.gps.buffer.GpsRealDataBuffer;
  25 +import com.bsth.server_rs.gps.dao.HistoryGpsDao;
  26 +import com.bsth.server_rs.gps.entity.GpsEntity;
  27 +import com.bsth.server_rs.gps.entity.LineInfo;
  28 +import com.bsth.server_rs.gps.entity.StopInfo;
  29 +import com.bsth.util.ThreadLocalDateUtil;
  30 +
  31 +/**
  32 + * Created by panzhao on 2017/3/28.
  33 + */
  34 +@Component
  35 +@Path("/gps")
  36 +@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  37 +public class GpsRestService {
  38 +
  39 + @Autowired
  40 + HistoryGpsDao historyGpsDao;
  41 +
  42 + @GET
  43 + @Path("/{deviceId}")
  44 + public GpsEntity findOne(@PathParam("deviceId") String deviceId) {
  45 + return GpsRealDataBuffer.get(deviceId);
  46 + }
  47 +
  48 + @GET
  49 + @Path("/all")
  50 + public Collection<GpsEntity> findAll() {
  51 + return GpsRealDataBuffer.all();
  52 + }
  53 +
  54 + @GET
  55 + @Path("/history/{nbbm}/{st}/{et}")
  56 + public Collection<Map<String, Object>> history(@PathParam("nbbm") String nbbm
  57 + , @PathParam("st") String st
  58 + , @PathParam("et") String et) {
  59 + return historyGpsDao.query(nbbm, Long.parseLong(st), Long.parseLong(et));
  60 + }
  61 +
  62 + @GET
  63 + @Path("/gpsReport")
  64 + public List<Map<String, Object>> gpsReport() {
  65 + List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
  66 + Collection<GpsEntity> gpss = GpsRealDataBuffer.all();
  67 + Set<Integer> lines = BasicDataBuffer.getAllLine();
  68 + for (GpsEntity gps : gpss) {
  69 + String device = gps.getDeviceId();
  70 + if (lines.contains(Integer.parseInt(gps.getLineId()))) {
  71 + Map<String, Object> map = new HashMap<String, Object>();
  72 + map.put("vehicleNumberPlate", BasicDataBuffer.getPlateByDevice(device));
  73 + try {
  74 + map.put("gpsDateTime", ThreadLocalDateUtil.formatDate(new Date(gps.getTimestamp())));
  75 + } catch (ParseException e) {
  76 + // TODO Auto-generated catch block
  77 + e.printStackTrace();
  78 + }
  79 + map.put("gpsLongitude", gps.getLon());
  80 + map.put("gpsLatitude", gps.getLat());
  81 + map.put("gpsDirection", gps.getDirection());
  82 + map.put("gpsSpeed", gps.getSpeed());
  83 + map.put("gpsIsValid", gps.getValid());
  84 + map.put("lineId", gps.getLineId());
  85 + map.put("lineVersion", 0);
  86 + map.put("upDownMark", gps.getUpDown());
  87 + map.put("stationOrder", getCurrStop(gps));
  88 + map.put("inOutStationState", gps.getInOrOutStation());
  89 + map.put("operationalState", gps.getState());
  90 +
  91 + result.add(map);
  92 + }
  93 + }
  94 +
  95 + return result;
  96 + }
  97 +
  98 + @GET
  99 + @Path("/bx/all")
  100 + public List<Map<String, Object>> bx() {
  101 + List<Car> cars = CarBufferData.findAll();
  102 + System.out.println(111);
  103 + Map<String, String> device2vehicle = new HashMap<>(cars.size());
  104 + for (Car car : cars) {
  105 + device2vehicle.put(car.getEquipmentCode(), car.getNbbm());
  106 + }
  107 + List<Map<String, Object>> result = new ArrayList<>();
  108 + Collection<GpsEntity> gpss = GpsRealDataBuffer.all();
  109 + for (GpsEntity gps : gpss) {
  110 + String device = gps.getDeviceId();
  111 + Map<String, Object> map = new HashMap<>();
  112 + map.put("vehicleCode", device2vehicle.get(device));
  113 + try {
  114 + map.put("gpsDateTime", ThreadLocalDateUtil.formatDate(new Date(gps.getTimestamp())));
  115 + } catch (ParseException e) {
  116 + // TODO Auto-generated catch block
  117 + e.printStackTrace();
  118 + }
  119 + map.put("gpsLongitude", gps.getLon());
  120 + map.put("gpsLatitude", gps.getLat());
  121 +
  122 + result.add(map);
  123 + }
  124 +
  125 + return result;
  126 + }
  127 +
  128 + private static int getCurrStop(GpsEntity gps) {
  129 + int res = 0;
  130 +
  131 + LineInfo line = BasicDataBuffer.getLineById(Integer.parseInt(gps.getLineId()));
  132 + if (line != null) {
  133 + List<StopInfo> upStops = new ArrayList<>(line.getStopsUp()), downStops = new ArrayList<>(line.getStopsDown());
  134 + int updown = gps.getUpDown();
  135 + // 环线、内外换的线路
  136 + boolean isRing = line.getLinePlayType() == 1;
  137 + if (isRing) {
  138 + updown = 0;
  139 + downStops.clear();
  140 + }
  141 + List<StopInfo> stops = new ArrayList<StopInfo>();
  142 + stops.addAll(upStops);
  143 + stops.addAll(downStops);
  144 +
  145 + int idx = 1;
  146 + if (updown == 0) {
  147 + for (StopInfo stop : upStops) {
  148 + if (gps.getStopNo().trim().equals(stop.getStationCod())) break;
  149 + idx++;
  150 + }
  151 + } else {
  152 + for (StopInfo stop : downStops) {
  153 + if (gps.getStopNo().trim().equals(stop.getStationCod())) break;
  154 + idx++;
  155 + }
  156 + }
  157 + if (idx <= stops.size()) {
  158 + res = idx;
  159 + }
  160 + }
  161 +
  162 + return res;
  163 + }
  164 +}