From 764d04b497356ba6bcbb75fd42b51eca750f7223 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期三, 29 五月 2024 15:02:51 +0800
Subject: [PATCH] 调整上级观看消息的发送

---
 src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/device/DeviceConfig.java |  119 +++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 67 insertions(+), 52 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/device/DeviceConfig.java b/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/device/DeviceConfig.java
old mode 100644
new mode 100755
index 5aa626e..2f32361
--- a/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/device/DeviceConfig.java
+++ b/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/device/DeviceConfig.java
@@ -7,29 +7,32 @@
 
 package com.genersoft.iot.vmp.vmanager.gb28181.device;
 
-import javax.sip.message.Response;
-
-import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson2.JSONObject;
+import com.genersoft.iot.vmp.conf.exception.ControllerException;
+import com.genersoft.iot.vmp.conf.security.JwtUtils;
 import com.genersoft.iot.vmp.gb28181.bean.Device;
 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.gb28181.utils.XmlUtil;
-import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
-
-import io.swagger.annotations.Api;
-import io.swagger.annotations.ApiImplicitParam;
-import io.swagger.annotations.ApiImplicitParams;
-import io.swagger.annotations.ApiOperation;
+import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
+import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.Parameter;
+import io.swagger.v3.oas.annotations.security.SecurityRequirement;
+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.ResponseEntity;
+import org.springframework.util.ObjectUtils;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.context.request.async.DeferredResult;
 
-@Api(tags = "鍥芥爣璁惧閰嶇疆")
-@CrossOrigin
+import javax.sip.InvalidArgumentException;
+import javax.sip.SipException;
+import java.text.ParseException;
+import java.util.UUID;
+
+@Tag(name = "鍥芥爣璁惧閰嶇疆")
 @RestController
 @RequestMapping("/api/device/config")
 public class DeviceConfig {
@@ -37,7 +40,7 @@
     private final static Logger logger = LoggerFactory.getLogger(DeviceQuery.class);
 
     @Autowired
-    private IVideoManagerStorager storager;
+    private IVideoManagerStorage storager;
 
     @Autowired
     private SIPCommander cmder;
@@ -55,18 +58,16 @@
 	 * @param heartBeatCount 蹇冭烦璁℃暟
 	 * @return
 	 */
-	@ApiOperation("鐪嬪畧浣嶆帶鍒跺懡浠�")
 	@GetMapping("/basicParam/{deviceId}")
-	@ApiImplicitParams({
-			@ApiImplicitParam(name = "deviceId", value ="璁惧ID" ,dataTypeClass = String.class),
-			@ApiImplicitParam(name = "channelId", value ="閫氶亾ID",dataTypeClass = String.class ),
-			@ApiImplicitParam(name = "name", value ="鍚嶇О" ,dataTypeClass = String.class),
-			@ApiImplicitParam(name = "expiration", value ="鍒版湡鏃堕棿" ,dataTypeClass = String.class),
-			@ApiImplicitParam(name = "heartBeatInterval", value ="蹇冭烦闂撮殧" ,dataTypeClass = String.class),
-			@ApiImplicitParam(name = "heartBeatCount", value ="蹇冭烦璁℃暟" ,dataTypeClass = String.class),
-	})
-	public DeferredResult<ResponseEntity<String>> homePositionApi(@PathVariable String deviceId,
-                                                                @RequestParam(required = false) String channelId,
+	@Operation(summary = "鍩烘湰閰嶇疆璁剧疆鍛戒护", security = @SecurityRequirement(name = JwtUtils.HEADER))
+	@Parameter(name = "deviceId", description = "璁惧鍥芥爣缂栧彿", required = true)
+	@Parameter(name = "channelId", description = "閫氶亾鍥芥爣缂栧彿", required = true)
+	@Parameter(name = "name", description = "鍚嶇О")
+	@Parameter(name = "expiration", description = "鍒版湡鏃堕棿")
+	@Parameter(name = "heartBeatInterval", description = "蹇冭烦闂撮殧")
+	@Parameter(name = "heartBeatCount", description = "蹇冭烦璁℃暟")
+	public DeferredResult<String> homePositionApi(@PathVariable String deviceId,
+                                                               	String channelId,
                                                                 @RequestParam(required = false) String name,
 																@RequestParam(required = false) String expiration,
 																@RequestParam(required = false) String heartBeatInterval,
@@ -75,19 +76,27 @@
 			logger.debug("鎶ヨ澶嶄綅API璋冪敤");
 		}
 		Device device = storager.queryVideoDevice(deviceId);
-		cmder.deviceBasicConfigCmd(device, channelId, name, expiration, heartBeatInterval, heartBeatCount, event -> {
-			Response response = event.getResponse();
-			RequestMessage msg = new RequestMessage();
-			msg.setId(DeferredResultHolder.CALLBACK_CMD_DEVICECONFIG + (XmlUtil.isEmpty(channelId) ? deviceId : channelId));
-			msg.setData(String.format("璁惧閰嶇疆鎿嶄綔澶辫触锛岄敊璇爜锛� %s, %s", response.getStatusCode(), response.getReasonPhrase()));
-			resultHolder.invokeResult(msg);
-		});
-        DeferredResult<ResponseEntity<String>> result = new DeferredResult<ResponseEntity<String>>(3 * 1000L);
+		String uuid = UUID.randomUUID().toString();
+		String key = DeferredResultHolder.CALLBACK_CMD_DEVICECONFIG + deviceId + channelId;
+		try {
+			cmder.deviceBasicConfigCmd(device, channelId, name, expiration, heartBeatInterval, heartBeatCount, event -> {
+				RequestMessage msg = new RequestMessage();
+				msg.setId(uuid);
+				msg.setKey(key);
+				msg.setData(String.format("璁惧閰嶇疆鎿嶄綔澶辫触锛岄敊璇爜锛� %s, %s", event.statusCode, event.msg));
+				resultHolder.invokeResult(msg);
+			});
+		} catch (InvalidArgumentException | SipException | ParseException e) {
+			logger.error("[鍛戒护鍙戦�佸け璐 璁惧閰嶇疆: {}", e.getMessage());
+			throw new ControllerException(ErrorCode.ERROR100.getCode(), "鍛戒护鍙戦�佸け璐�: " + e.getMessage());
+		}
+		DeferredResult<String> result = new DeferredResult<String>(3 * 1000L);
 		result.onTimeout(() -> {
 			logger.warn(String.format("璁惧閰嶇疆鎿嶄綔瓒呮椂, 璁惧鏈繑鍥炲簲绛旀寚浠�"));
 			// 閲婃斁rtpserver
 			RequestMessage msg = new RequestMessage();
-			msg.setId(DeferredResultHolder.CALLBACK_CMD_DEVICECONFIG + (XmlUtil.isEmpty(channelId) ? deviceId : channelId));
+			msg.setId(uuid);
+			msg.setKey(key);
 			JSONObject json = new JSONObject();
 			json.put("DeviceID", deviceId);
 			json.put("Status", "Timeout");
@@ -95,7 +104,7 @@
 			msg.setData(json); //("鐪嬪畧浣嶆帶鍒舵搷浣滆秴鏃�, 璁惧鏈繑鍥炲簲绛旀寚浠�");
 			resultHolder.invokeResult(msg);
 		});
-		resultHolder.put(DeferredResultHolder.CALLBACK_CMD_DEVICECONFIG + (XmlUtil.isEmpty(channelId) ? deviceId : channelId), result);
+		resultHolder.put(key, uuid, result);
 		return result;
 	}
 
@@ -106,37 +115,43 @@
 	 * @param channelId 閫氶亾ID
 	 * @return
 	 */
-	@ApiOperation("璁惧閰嶇疆鏌ヨ璇锋眰")
-	@ApiImplicitParams({
-			@ApiImplicitParam(name = "deviceId", value ="璁惧ID" ,dataTypeClass = String.class),
-			@ApiImplicitParam(name = "channelId", value ="閫氶亾ID" ,dataTypeClass = String.class),
-			@ApiImplicitParam(name = "configType", value ="閰嶇疆绫诲瀷" ,dataTypeClass = String.class),
-	})
+	@Operation(summary = "璁惧閰嶇疆鏌ヨ璇锋眰", security = @SecurityRequirement(name = JwtUtils.HEADER))
+	@Parameter(name = "deviceId", description = "璁惧鍥芥爣缂栧彿", required = true)
+	@Parameter(name = "channelId", description = "閫氶亾鍥芥爣缂栧彿", required = true)
+	@Parameter(name = "configType", description = "閰嶇疆绫诲瀷")
 	@GetMapping("/query/{deviceId}/{configType}")
-    public DeferredResult<ResponseEntity<String>> configDownloadApi(@PathVariable String deviceId, 
+    public DeferredResult<String> configDownloadApi(@PathVariable String deviceId,
                                                                 @PathVariable String configType,
                                                                 @RequestParam(required = false) String channelId) {
 		if (logger.isDebugEnabled()) {
 			logger.debug("璁惧鐘舵�佹煡璇PI璋冪敤");
 		}
+		String key = DeferredResultHolder.CALLBACK_CMD_CONFIGDOWNLOAD + (ObjectUtils.isEmpty(channelId) ? deviceId : channelId);
+		String uuid = UUID.randomUUID().toString();
 		Device device = storager.queryVideoDevice(deviceId);
-		cmder.deviceConfigQuery(device, channelId, configType, event -> {
-			Response response = event.getResponse();
-			RequestMessage msg = new RequestMessage();
-			msg.setId(DeferredResultHolder.CALLBACK_CMD_CONFIGDOWNLOAD + (XmlUtil.isEmpty(channelId) ? deviceId : channelId));
-			msg.setData(String.format("鑾峰彇璁惧閰嶇疆澶辫触锛岄敊璇爜锛� %s, %s", response.getStatusCode(), response.getReasonPhrase()));
-			resultHolder.invokeResult(msg);
-		});
-        DeferredResult<ResponseEntity<String>> result = new DeferredResult<ResponseEntity<String >> (3 * 1000L);
+		try {
+			cmder.deviceConfigQuery(device, channelId, configType, event -> {
+				RequestMessage msg = new RequestMessage();
+				msg.setId(uuid);
+				msg.setKey(key);
+				msg.setData(String.format("鑾峰彇璁惧閰嶇疆澶辫触锛岄敊璇爜锛� %s, %s", event.statusCode, event.msg));
+				resultHolder.invokeResult(msg);
+			});
+		} catch (InvalidArgumentException | SipException | ParseException e) {
+			logger.error("[鍛戒护鍙戦�佸け璐 鑾峰彇璁惧閰嶇疆: {}", e.getMessage());
+			throw new ControllerException(ErrorCode.ERROR100.getCode(), "鍛戒护鍙戦�佸け璐�: " + e.getMessage());
+		}
+		DeferredResult<String> result = new DeferredResult<String > (3 * 1000L);
 		result.onTimeout(()->{
 			logger.warn(String.format("鑾峰彇璁惧閰嶇疆瓒呮椂"));
 			// 閲婃斁rtpserver
 			RequestMessage msg = new RequestMessage();
-			msg.setId(DeferredResultHolder.CALLBACK_CMD_CONFIGDOWNLOAD + (XmlUtil.isEmpty(channelId) ? deviceId : channelId));
+			msg.setId(uuid);
+			msg.setKey(key);
 			msg.setData("Timeout. Device did not response to this command.");
 			resultHolder.invokeResult(msg);
 		});
-		resultHolder.put(DeferredResultHolder.CALLBACK_CMD_CONFIGDOWNLOAD + (XmlUtil.isEmpty(channelId) ? deviceId : channelId), result);
+		resultHolder.put(key, uuid, result);
 		return result;
 	}
 

--
Gitblit v1.8.0