From 5461b8ebf2f8bf66a9d34351eb8783484304958a Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: 星期一, 22 八月 2022 16:17:28 +0800 Subject: [PATCH] 支持全局异常和统一返回结果,未完待续 --- src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/playback/PlaybackController.java | 64 +++++++++----------------------- 1 files changed, 18 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/playback/PlaybackController.java b/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/playback/PlaybackController.java index f556175..e9b9ef7 100644 --- a/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/playback/PlaybackController.java +++ b/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/playback/PlaybackController.java @@ -1,10 +1,12 @@ package com.genersoft.iot.vmp.vmanager.gb28181.playback; import com.genersoft.iot.vmp.common.StreamInfo; +import com.genersoft.iot.vmp.conf.exception.ControllerException; import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder; import com.genersoft.iot.vmp.service.IMediaServerService; import com.genersoft.iot.vmp.storager.IRedisCatchStorage; import com.genersoft.iot.vmp.service.IPlayService; +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.tags.Tag; @@ -13,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; @@ -55,14 +58,14 @@ @Parameter(name = "startTime", description = "寮�濮嬫椂闂�", required = true) @Parameter(name = "endTime", description = "缁撴潫鏃堕棿", required = true) @GetMapping("/start/{deviceId}/{channelId}") - public DeferredResult<ResponseEntity<String>> play(@PathVariable String deviceId, @PathVariable String channelId, + public DeferredResult<String> play(@PathVariable String deviceId, @PathVariable String channelId, String startTime,String endTime) { if (logger.isDebugEnabled()) { logger.debug(String.format("璁惧鍥炴斁 API璋冪敤锛宒eviceId锛�%s 锛宑hannelId锛�%s", deviceId, channelId)); } - DeferredResult<ResponseEntity<String>> result = playService.playBack(deviceId, channelId, startTime, endTime, null, wvpResult->{ + DeferredResult<String> result = playService.playBack(deviceId, channelId, startTime, endTime, null, wvpResult->{ resultHolder.invokeResult(wvpResult.getData()); }); @@ -75,67 +78,46 @@ @Parameter(name = "channelId", description = "閫氶亾鍥芥爣缂栧彿", required = true) @Parameter(name = "stream", description = "娴両D", required = true) @GetMapping("/stop/{deviceId}/{channelId}/{stream}") - public ResponseEntity<String> playStop( + public void playStop( @PathVariable String deviceId, @PathVariable String channelId, @PathVariable String stream) { - + if (ObjectUtils.isEmpty(deviceId) || ObjectUtils.isEmpty(channelId) || ObjectUtils.isEmpty(stream)) { + throw new ControllerException(ErrorCode.ERROR400); + } cmder.streamByeCmd(deviceId, channelId, stream, null); - - if (logger.isDebugEnabled()) { - logger.debug(String.format("璁惧褰曞儚鍥炴斁鍋滄 API璋冪敤锛宒eviceId/channelId锛�%s/%s", deviceId, channelId)); - } - if (StringUtils.isEmpty(deviceId) || StringUtils.isEmpty(channelId) || StringUtils.isEmpty(stream)) { - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } - - if (deviceId != null && channelId != null) { - JSONObject json = new JSONObject(); - json.put("deviceId", deviceId); - json.put("channelId", channelId); - return new ResponseEntity<>(json.toString(), HttpStatus.OK); - } else { - logger.warn("璁惧褰曞儚鍥炴斁鍋滄API璋冪敤澶辫触锛�"); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); - } } @Operation(summary = "鍥炴斁鏆傚仠") @Parameter(name = "streamId", description = "鍥炴斁娴両D", required = true) @GetMapping("/pause/{streamId}") - public ResponseEntity<String> playPause(@PathVariable String streamId) { + public void playPause(@PathVariable String streamId) { logger.info("playPause: "+streamId); - JSONObject json = new JSONObject(); StreamInfo streamInfo = redisCatchStorage.queryPlayback(null, null, streamId, null); if (null == streamInfo) { - json.put("msg", "streamId涓嶅瓨鍦�"); logger.warn("streamId涓嶅瓨鍦�!"); - return new ResponseEntity<String>(json.toString(), HttpStatus.BAD_REQUEST); + throw new ControllerException(ErrorCode.ERROR400.getCode(), "streamId涓嶅瓨鍦�"); } Device device = storager.queryVideoDevice(streamInfo.getDeviceID()); cmder.playPauseCmd(device, streamInfo); - json.put("msg", "ok"); - return new ResponseEntity<String>(json.toString(), HttpStatus.OK); } @Operation(summary = "鍥炴斁鎭㈠") @Parameter(name = "streamId", description = "鍥炴斁娴両D", required = true) @GetMapping("/resume/{streamId}") - public ResponseEntity<String> playResume(@PathVariable String streamId) { + public void playResume(@PathVariable String streamId) { logger.info("playResume: "+streamId); JSONObject json = new JSONObject(); StreamInfo streamInfo = redisCatchStorage.queryPlayback(null, null, streamId, null); if (null == streamInfo) { json.put("msg", "streamId涓嶅瓨鍦�"); logger.warn("streamId涓嶅瓨鍦�!"); - return new ResponseEntity<String>(json.toString(), HttpStatus.BAD_REQUEST); + throw new ControllerException(ErrorCode.ERROR400.getCode(), "streamId涓嶅瓨鍦�"); } Device device = storager.queryVideoDevice(streamInfo.getDeviceID()); cmder.playResumeCmd(device, streamInfo); - json.put("msg", "ok"); - return new ResponseEntity<String>(json.toString(), HttpStatus.OK); } @@ -143,43 +125,33 @@ @Parameter(name = "streamId", description = "鍥炴斁娴両D", required = true) @Parameter(name = "seekTime", description = "鎷栧姩鍋忕Щ閲忥紝鍗曚綅s", required = true) @GetMapping("/seek/{streamId}/{seekTime}") - public ResponseEntity<String> playSeek(@PathVariable String streamId, @PathVariable long seekTime) { + public void playSeek(@PathVariable String streamId, @PathVariable long seekTime) { logger.info("playSeek: "+streamId+", "+seekTime); - JSONObject json = new JSONObject(); StreamInfo streamInfo = redisCatchStorage.queryPlayback(null, null, streamId, null); if (null == streamInfo) { - json.put("msg", "streamId涓嶅瓨鍦�"); logger.warn("streamId涓嶅瓨鍦�!"); - return new ResponseEntity<String>(json.toString(), HttpStatus.BAD_REQUEST); + throw new ControllerException(ErrorCode.ERROR400.getCode(), "streamId涓嶅瓨鍦�"); } Device device = storager.queryVideoDevice(streamInfo.getDeviceID()); cmder.playSeekCmd(device, streamInfo, seekTime); - json.put("msg", "ok"); - return new ResponseEntity<String>(json.toString(), HttpStatus.OK); } @Operation(summary = "鍥炴斁鍊嶉�熸挱鏀�") @Parameter(name = "streamId", description = "鍥炴斁娴両D", required = true) @Parameter(name = "speed", description = "鍊嶉��0.25 0.5 1銆�2銆�4", required = true) @GetMapping("/speed/{streamId}/{speed}") - public ResponseEntity<String> playSpeed(@PathVariable String streamId, @PathVariable Double speed) { + public void playSpeed(@PathVariable String streamId, @PathVariable Double speed) { logger.info("playSpeed: "+streamId+", "+speed); - JSONObject json = new JSONObject(); StreamInfo streamInfo = redisCatchStorage.queryPlayback(null, null, streamId, null); if (null == streamInfo) { - json.put("msg", "streamId涓嶅瓨鍦�"); logger.warn("streamId涓嶅瓨鍦�!"); - return new ResponseEntity<String>(json.toString(), HttpStatus.BAD_REQUEST); + throw new ControllerException(ErrorCode.ERROR400.getCode(), "streamId涓嶅瓨鍦�"); } if(speed != 0.25 && speed != 0.5 && speed != 1 && speed != 2.0 && speed != 4.0) { - json.put("msg", "涓嶆敮鎸佺殑speed锛�0.25 0.5 1銆�2銆�4锛�"); logger.warn("涓嶆敮鎸佺殑speed锛� " + speed); - return new ResponseEntity<String>(json.toString(), HttpStatus.BAD_REQUEST); + throw new ControllerException(ErrorCode.ERROR100.getCode(), "涓嶆敮鎸佺殑speed锛�0.25 0.5 1銆�2銆�4锛�"); } Device device = storager.queryVideoDevice(streamInfo.getDeviceID()); cmder.playSpeedCmd(device, streamInfo, speed); - json.put("msg", "ok"); - return new ResponseEntity<String>(json.toString(), HttpStatus.OK); } - } -- Gitblit v1.8.0