Browse Source

根据gps判断最近站点逻辑修改。

zhengqiang 4 years ago
parent
commit
6cbc351581

+ 2 - 2
common/pom.xml

@@ -250,9 +250,9 @@
         </dependency>
         <!--gps-->
         <dependency>
-            <groupId>com.jpsoft</groupId>
+            <groupId>com.jpsoft.bus</groupId>
             <artifactId>gps</artifactId>
-            <version>1.0.0</version>
+            <version>1.1.0</version>
         </dependency>
         <dependency>
             <groupId>org.gavaghan</groupId>

+ 16 - 14
common/src/main/java/com/jpsoft/bus/modules/bus/callback/GpsDataCallbackImpl.java

@@ -105,19 +105,20 @@ public class GpsDataCallbackImpl implements GpsDataCallback {
                 }
 
                 v.setLatestAddress(address);
-
                 v.setLongitude(longitude);//经度
                 v.setLatitude(latitude);//纬度
 
                 vehicleInfoService.updateGps(v);
 
-
                 //更新车辆的当前站点
                 try{
                     List<ShiftInfo> shiftInfoList = shiftInfoService.findByVehicleIdAndStatus(v.getId(),"1");
+
                     if (shiftInfoList.size()>0){
                         ShiftInfo shiftInfo = shiftInfoList.get(0);
                         String stationId = gpsService.getLatelyStation(v.getId());
+
+                        //站点发生变化时
                         if (!shiftInfo.getCurrentStationId().equals(stationId)){
                             shiftInfo.setCurrentStationId(stationId);
                             shiftInfoService.update(shiftInfo);
@@ -127,41 +128,42 @@ public class GpsDataCallbackImpl implements GpsDataCallback {
                             //此线路的所有站点
                             List<StationInfo> stationInfoList = stationInfoService.findByRouteId(shiftInfo.getRouteId());
 
-                            //站点集合大小
-                            int stationSize = stationInfoList.size();
-
-                            int currentStationIndex = stationInfoList.indexOf(currentStation);
+                            int currentStationIndex = currentStation.getSortNo();
 
+                            //线路终点站
+                            StationInfo endStation = stationInfoService.get(shiftInfo.getEndStationId());
+                            boolean frontToEnd = true;
 
+                            if(endStation.getId().equals(stationInfoList.get(0).getId())){
+                                //反向行驶
+                                frontToEnd = false;
+                            }
 
                             //查询乘客表是否有过站的情况
-
                             List<PassengerInfo> passengerInfoList = passengerInfoService.findByShiftStatusPayStatusNotTicketDown(shiftInfo.getId(),"1","1",stationId);
+
                             if (passengerInfoList.size()>0){
                                 for (PassengerInfo passengerInfo : passengerInfoList){
-
                                     //乘客购票的下车站点
                                     StationInfo downStation = stationInfoService.get(passengerInfo.getTicketDownStationId());
-                                    int downStationIndex = stationInfoList.indexOf(downStation);
+
                                     //按照线路顺行并且当前站点大于乘客购票站点,则过站
-                                    if (shiftInfo.getStartStationId().equals(stationInfoList.get(0).getId()) && currentStationIndex > downStationIndex){
+                                    if (frontToEnd && currentStationIndex > downStation.getSortNo()){
                                         passengerInfo.setPayStatus("2");
                                         passengerInfoService.update(passengerInfo);
                                     }
 
                                     //按照线路逆行并且当前站点小于乘客购票站点,则过站
-                                    if (!shiftInfo.getStartStationId().equals(stationInfoList.get(0).getId()) && currentStationIndex < downStationIndex){
+                                    if (!frontToEnd && currentStationIndex < downStation.getSortNo()){
                                         passengerInfo.setPayStatus("2");
                                         passengerInfoService.update(passengerInfo);
                                     }
-
-
                                 }
                             }
                         }
                     }
                 }catch (Exception ex){
-
+                    log.error(ex.getMessage(),ex);
                 }
             }
         }

+ 53 - 24
common/src/main/java/com/jpsoft/bus/modules/bus/service/impl/GpsServiceImpl.java

@@ -297,11 +297,9 @@ public class GpsServiceImpl implements GpsService {
 
         String currentStationId = null;
         List<ShiftInfo> shiftInfoList = shiftInfoService.findByVehicleIdAndStatus(id, "1");
-        if (shiftInfoList.size() > 0) {
-
 
+        if (shiftInfoList.size() > 0) {
             ShiftInfo shiftInfo = shiftInfoList.get(0);
-
             RouteInfo routeInfo = routeInfoService.get(shiftInfo.getRouteId());
 
             List<StationInfo> stationInfoList = stationInfoService.findByRouteId(routeInfo.getId());
@@ -309,43 +307,74 @@ public class GpsServiceImpl implements GpsService {
 
             //当前站点
             StationInfo currentStation = stationInfoService.get(currentStationId);
+            int currentIndex = currentStation.getSortNo();
 
-            int currentIndex = stationInfoList.indexOf(currentStation);
-            //线路始站
-            StationInfo startStation = stationInfoService.get(shiftInfo.getStartStationId());
             //线路终点站
             StationInfo endStation = stationInfoService.get(shiftInfo.getEndStationId());
 
+            boolean frontToEnd = true;
 
             if (stationInfoList.size() > 0) {
-                for (StationInfo stationInfo : stationInfoList) {
-                    GlobalCoordinates source = new GlobalCoordinates(Double.valueOf(vehicleInfo.getLatitude()), Double.valueOf(vehicleInfo.getLongitude()));
-                    GlobalCoordinates target = new GlobalCoordinates(Double.valueOf(stationInfo.getLatitude()), Double.valueOf(stationInfo.getLongitude()));
-
-                    double meter1 = CommonUtil.getDistanceMeter(source, target, Ellipsoid.Sphere);
-                    if (meter1 <= Double.valueOf(50)) {
-                        int indexStation = stationInfoList.indexOf(stationInfo);
-                        //线路的开始站是站点集合的第一个站点则是顺线路运行
-                        if (shiftInfo.getStartStationId().equals(stationInfoList.get(0).getId())){
-                            if (indexStation > currentIndex){
-                                currentStationId = stationInfo.getId();
-                                break;
-                            }
+                if(endStation.getId().equals(stationInfoList.get(0).getId())){
+                    //反向行驶
+                    frontToEnd = false;
+                }
+
+                if(frontToEnd){
+                    for (int i=0;i<stationInfoList.size();i++) {
+                        StationInfo stationInfo = stationInfoList.get(i);
+
+                        if(stationInfo.getSortNo() <= currentIndex){
+                            //正向行驶时,排序比当前站小的站点不用计算
+                            continue;
                         }
-                        if (shiftInfo.getEndStationId().equals(stationInfoList.get(0).getId())){
-                            if (indexStation < currentIndex){
-                                currentStationId = stationInfo.getId();
-                                break;
-                            }
+
+                        if(matchDistance(stationInfo,vehicleInfo,50)){
+                           currentStationId = stationInfo.getId();
                         }
+                    }
+                }
+                else{
+                    for (int i=stationInfoList.size() - 1;i>=0;i--) {
+                        StationInfo stationInfo = stationInfoList.get(i);
 
+                        if (stationInfo.getSortNo() >= currentIndex){
+                            //反向行驶时,排序比当前站大的站点不用计算
+                            continue;
+                        }
+
+                        if(matchDistance(stationInfo,vehicleInfo,50)){
+                            currentStationId = stationInfo.getId();
+                        }
                     }
                 }
+
             }
         }
         return currentStationId;
     }
 
+    /**
+     * 查询指定范围内的站点
+     * @param stationInfo
+     * @param vehicleInfo
+     * @param distance
+     * @return
+     */
+    private boolean matchDistance(StationInfo stationInfo,VehicleInfo vehicleInfo,int distance){
+        GlobalCoordinates source = new GlobalCoordinates(Double.valueOf(vehicleInfo.getLatitude()), Double.valueOf(vehicleInfo.getLongitude()));
+        GlobalCoordinates target = new GlobalCoordinates(Double.valueOf(stationInfo.getLatitude()), Double.valueOf(stationInfo.getLongitude()));
+
+        double meter1 = CommonUtil.getDistanceMeter(source, target, Ellipsoid.Sphere);
+
+        if (meter1 <= Double.valueOf(distance)) {
+            return true;
+        }
+        else{
+            return false;
+        }
+    }
+
     public String encode(String text) {
         try {
             text = URLEncoder.encode(text, "UTF-8");

+ 1 - 2
gps/pom.xml

@@ -8,9 +8,8 @@
         <version>1.0.0</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
-
     <artifactId>gps</artifactId>
-    <version>1.0.0</version>
+    <version>1.1.0</version>
 
     <properties>
         <netty-all.version>4.1.6.Final</netty-all.version>

+ 17 - 9
gps/src/main/java/com/jpsoft/gps/handler/ProcessHandler.java

@@ -25,19 +25,27 @@ public class ProcessHandler extends SimpleChannelInboundHandler<String> {
             String deviceNo = arr[0];
             String[] subArr = arr[1].split(";");
 
-            double wd = Double.valueOf(subArr[0]);
-            double jd = Double.valueOf(subArr[1]);
+            if (subArr.length==3){
+                //测试用不进行转换
+                if (this.callback != null) {
+                    this.callback.receive(deviceNo, subArr[1], subArr[0]);
+                }
+            }
+            else {
+                double wd = Double.valueOf(subArr[0]);
+                double jd = Double.valueOf(subArr[1]);
 
-            DecimalFormat df = new DecimalFormat("#.######");
+                DecimalFormat df = new DecimalFormat("#.######");
 
-            if (wd > 0 && jd > 0) {
-                //高德地图坐标系 gcj02
-                double[] gcj = GPSUtil.gps84_To_Gcj02(wd, jd);//坐标转换
+                if (wd > 0 && jd > 0) {
+                    //gps坐标转高德地图坐标系 gcj02
+                    double[] gcj = GPSUtil.gps84_To_Gcj02(wd, jd);//坐标转换
 
-                System.out.println(deviceNo + "," + df.format(gcj[1]) + "," + df.format(gcj[0]));
+                    System.out.println(deviceNo + "," + df.format(gcj[1]) + "," + df.format(gcj[0]));
 
-                if (this.callback != null) {
-                    this.callback.receive(deviceNo, df.format(gcj[1]), df.format(gcj[0]));
+                    if (this.callback != null) {
+                        this.callback.receive(deviceNo, df.format(gcj[1]), df.format(gcj[0]));
+                    }
                 }
             }
         }

+ 4 - 4
gps/src/main/java/com/jpsoft/gps/utils/GPSUtil.java

@@ -191,8 +191,8 @@ public class GPSUtil {
     }
 
     public static void main(String[] args) {
-        double[] points = GPSUtil.bd09_To_Gcj02(30.308866F,112.270896F);
-
+//        double[] points = GPSUtil.bd09_To_Gcj02(30.308866F,112.270896F);
+        double[] points = GPSUtil.gcj02_To_Gps84(30.307475F,112.2757F);
         DecimalFormat df = new DecimalFormat("#.######");
 
         String longitude = df.format(points[1]);
@@ -200,8 +200,8 @@ public class GPSUtil {
 
         System.out.println(longitude + "," + latitude);
 
-        String address = GPSUtil.regeo(longitude,latitude,"a71a4fa458ed085246ea75ebe096bbea");
+//        String address = GPSUtil.regeo(longitude,latitude,"a71a4fa458ed085246ea75ebe096bbea");
 
-        System.out.println(address);
+//        System.out.println(address);
     }
 }

+ 0 - 1
web/src/main/java/com/jpsoft/bus/modules/driver/controller/DriverApiController.java

@@ -256,7 +256,6 @@ public class DriverApiController {
         MessageResult<Map> messageResult = new MessageResult<>();
 
         try {
-
             VehicleInfo vehicleInfo = vehicleInfoService.get(subject);
             if (vehicleInfo == null){
                 throw new Exception("当前车辆不存在");