From 7be636f8b850c2554d9071876820579ce631dc25 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期三, 15 二月 2023 18:30:35 +0800
Subject: [PATCH] 修复更新通道是更新各个坐标系德位置信息

---
 src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java                                                                  |    4 +-
 src/main/java/com/genersoft/iot/vmp/service/IDeviceChannelService.java                                                          |    5 ++
 src/main/java/com/genersoft/iot/vmp/gb28181/utils/SipUtils.java                                                                 |   34 +++++++++++++++++
 src/main/java/com/genersoft/iot/vmp/service/impl/DeviceChannelServiceImpl.java                                                  |   24 ++++++++++++
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java |    2 +
 src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/MobilePosition/MobilePositionController.java                               |   42 ++++++++++++++-------
 src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceChannelMapper.java                                                       |    4 ++
 7 files changed, 99 insertions(+), 16 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java
index 761481b..a907999 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java
@@ -5,6 +5,7 @@
 import com.genersoft.iot.vmp.gb28181.transmit.event.request.SIPRequestProcessorParent;
 import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.IMessageHandler;
 import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.response.ResponseMessageHandler;
+import com.genersoft.iot.vmp.gb28181.utils.SipUtils;
 import com.genersoft.iot.vmp.gb28181.utils.XmlUtil;
 import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
 import gov.nist.javax.sip.message.SIPRequest;
@@ -107,6 +108,7 @@
                                         continue;
                                     }
                                     DeviceChannel deviceChannel = XmlUtil.channelContentHander(itemDevice, device, null);
+                                    deviceChannel = SipUtils.updateGps(deviceChannel, device.getGeoCoordSys());
                                     deviceChannel.setDeviceId(take.getDevice().getDeviceId());
 
                                     channelList.add(deviceChannel);
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/SipUtils.java b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/SipUtils.java
index f3fbbb4..afe1183 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/SipUtils.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/SipUtils.java
@@ -1,5 +1,6 @@
 package com.genersoft.iot.vmp.gb28181.utils;
 
+import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
 import com.genersoft.iot.vmp.gb28181.bean.RemoteAddressInfo;
 import com.genersoft.iot.vmp.utils.GitUtil;
 import gov.nist.javax.sip.address.AddressImpl;
@@ -148,4 +149,37 @@
 
         return new RemoteAddressInfo(remoteAddress, remotePort);
     }
+
+    public static DeviceChannel updateGps(DeviceChannel deviceChannel, String geoCoordSys) {
+        if (deviceChannel.getLongitude()*deviceChannel.getLatitude() > 0) {
+
+            if (geoCoordSys == null) {
+                geoCoordSys = "WGS84";
+            }
+            if ("WGS84".equals(geoCoordSys)) {
+                deviceChannel.setLongitudeWgs84(deviceChannel.getLongitude());
+                deviceChannel.setLatitudeWgs84(deviceChannel.getLatitude());
+                Double[] position = Coordtransform.WGS84ToGCJ02(deviceChannel.getLongitude(), deviceChannel.getLatitude());
+                deviceChannel.setLongitudeGcj02(position[0]);
+                deviceChannel.setLatitudeGcj02(position[1]);
+            }else if ("GCJ02".equals(geoCoordSys)) {
+                deviceChannel.setLongitudeGcj02(deviceChannel.getLongitude());
+                deviceChannel.setLatitudeGcj02(deviceChannel.getLatitude());
+                Double[] position = Coordtransform.GCJ02ToWGS84(deviceChannel.getLongitude(), deviceChannel.getLatitude());
+                deviceChannel.setLongitudeWgs84(position[0]);
+                deviceChannel.setLatitudeWgs84(position[1]);
+            }else {
+                deviceChannel.setLongitudeGcj02(0.00);
+                deviceChannel.setLatitudeGcj02(0.00);
+                deviceChannel.setLongitudeWgs84(0.00);
+                deviceChannel.setLatitudeWgs84(0.00);
+            }
+        }else {
+            deviceChannel.setLongitudeGcj02(deviceChannel.getLongitude());
+            deviceChannel.setLatitudeGcj02(deviceChannel.getLatitude());
+            deviceChannel.setLongitudeWgs84(deviceChannel.getLongitude());
+            deviceChannel.setLatitudeWgs84(deviceChannel.getLatitude());
+        }
+        return deviceChannel;
+    }
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
index 0ea6d87..d51ab9f 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
@@ -1,11 +1,9 @@
 package com.genersoft.iot.vmp.gb28181.utils;
 
-import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
 import com.genersoft.iot.vmp.gb28181.bean.Device;
 import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
-import com.genersoft.iot.vmp.gb28181.bean.TreeType;
 import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
 import com.genersoft.iot.vmp.utils.DateUtil;
 import org.dom4j.Attribute;
@@ -400,6 +398,7 @@
         } else {
             deviceChannel.setLatitude(0.00);
         }
+
         deviceChannel.setGpsTime(DateUtil.getNow());
 
 
@@ -414,6 +413,7 @@
         } else {
             deviceChannel.setPTZType(Integer.parseInt(XmlUtil.getText(itemDevice, "PTZType")));
         }
+
         return deviceChannel;
     }
 
diff --git a/src/main/java/com/genersoft/iot/vmp/service/IDeviceChannelService.java b/src/main/java/com/genersoft/iot/vmp/service/IDeviceChannelService.java
index 31e568a..57605bb 100644
--- a/src/main/java/com/genersoft/iot/vmp/service/IDeviceChannelService.java
+++ b/src/main/java/com/genersoft/iot/vmp/service/IDeviceChannelService.java
@@ -46,4 +46,9 @@
      * @return
      */
     List<ChannelReduce> queryAllChannelList(String platformId);
+
+    /**
+     * 鏁版嵁浣嶇疆淇℃伅鏍煎紡澶勭悊
+     */
+    boolean updateAllGps(Device device);
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceChannelServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceChannelServiceImpl.java
index 880b697..7527631 100644
--- a/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceChannelServiceImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceChannelServiceImpl.java
@@ -176,5 +176,29 @@
         return channelMapper.queryChannelListInAll(null, null, null, platformId, null);
     }
 
+    @Override
+    public boolean updateAllGps(Device device) {
+        List<DeviceChannel> deviceChannels = channelMapper.getChannelsWithoutTransform(device.getDeviceId());
+        List<DeviceChannel> result = new ArrayList<>();
+        if (deviceChannels.size() == 0) {
+            return true;
+        }
+        deviceChannels.parallelStream().forEach(deviceChannel -> {
+            result.add(updateGps(deviceChannel, device));
+        });
+        int limitCount = 300;
+        if (result.size() > limitCount) {
+            for (int i = 0; i < result.size(); i += limitCount) {
+                int toIndex = i + limitCount;
+                if (i + limitCount > result.size()) {
+                    toIndex = result.size();
+                }
+                channelMapper.batchUpdate(result.subList(i, toIndex));
+            }
+        }else {
+            channelMapper.batchUpdate(result);
+        }
 
+        return true;
+    }
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceChannelMapper.java b/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceChannelMapper.java
index 8da1a09..4fe0a22 100644
--- a/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceChannelMapper.java
+++ b/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceChannelMapper.java
@@ -357,4 +357,8 @@
 
     @Select("select count(1) as total, sum(status) as online from device_channel")
     ResourceBaceInfo getOverview();
+
+    @Select("select * from device_channel where deviceId = #{deviceId} " +
+            "and latitude * longitude > 0 and latitudeGcj02 * latitudeWgs84 * longitudeWgs84 * longitudeGcj02 = 0")
+    List<DeviceChannel> getChannelsWithoutTransform(String deviceId);
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/MobilePosition/MobilePositionController.java b/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/MobilePosition/MobilePositionController.java
index f399f30..305d488 100644
--- a/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/MobilePosition/MobilePositionController.java
+++ b/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/MobilePosition/MobilePositionController.java
@@ -1,39 +1,30 @@
 package com.genersoft.iot.vmp.vmanager.gb28181.MobilePosition;
 
-import java.text.ParseException;
-import java.util.List;
-import java.util.UUID;
-
 import com.genersoft.iot.vmp.conf.exception.ControllerException;
 import com.genersoft.iot.vmp.gb28181.bean.Device;
 import com.genersoft.iot.vmp.gb28181.bean.MobilePosition;
 import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
 import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage;
 import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
+import com.genersoft.iot.vmp.service.IDeviceChannelService;
 import com.genersoft.iot.vmp.service.IDeviceService;
 import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
 import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
-import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
 import com.github.pagehelper.util.StringUtil;
-
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.Parameter;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.CrossOrigin;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.context.request.async.DeferredResult;
 
 import javax.sip.InvalidArgumentException;
 import javax.sip.SipException;
+import java.text.ParseException;
+import java.util.List;
+import java.util.UUID;
 
 /**
  *  浣嶇疆淇℃伅绠$悊
@@ -57,6 +48,9 @@
 
 	@Autowired
 	private IDeviceService deviceService;
+
+	@Autowired
+	private IDeviceChannelService deviceChannelService;
 
     /**
      * 鏌ヨ鍘嗗彶杞ㄨ抗
@@ -162,4 +156,24 @@
             throw new ControllerException(ErrorCode.ERROR100);
         }
     }
+
+    /**
+     * 鏁版嵁浣嶇疆淇℃伅鏍煎紡澶勭悊
+     * @param deviceId 璁惧ID
+     * @return true = 鍛戒护鍙戦�佹垚鍔�
+     */
+    @Operation(summary = "鏁版嵁浣嶇疆淇℃伅鏍煎紡澶勭悊")
+    @Parameter(name = "deviceId", description = "璁惧鍥芥爣缂栧彿", required = true)
+    @GetMapping("/transform/{deviceId}")
+    public void positionTransform(@PathVariable String deviceId) {
+
+        Device device = deviceService.getDevice(deviceId);
+        if (device == null) {
+            throw new ControllerException(ErrorCode.ERROR400.getCode(), "鏈壘鍒拌澶囷細 " + deviceId);
+        }
+        boolean result = deviceChannelService.updateAllGps(device);
+        if (!result) {
+            throw new ControllerException(ErrorCode.ERROR100);
+        }
+    }
 }

--
Gitblit v1.8.0