diff --git a/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsControlController.java b/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsControlController.java index de22e70..2d659a3 100644 --- a/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsControlController.java +++ b/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsControlController.java @@ -7,6 +7,7 @@ import com.inspect.base.redis.service.RedisService; import com.inspect.ivs.base.feign.domain.IvsResult; import com.inspect.ivs.constant.IvsConst; import com.inspect.ivs.service.IvsCommonService; +import com.inspect.ivs.service.IvsResourceRetryableDelegate; import com.inspect.ivs.util.UriUtils; import com.inspect.ivs.view.IvsPresetListView; import com.inspect.ivs.view.IvsPresetView; @@ -27,13 +28,15 @@ public class IvsControlController { private final RedisService redisService; private final IvsCommonService ivsCommonService; + private final IvsResourceRetryableDelegate ivsResourceRetryableDelegate; @Value("${ivs.version:3800}") private String version; public IvsControlController( RedisService redisService, - IvsCommonService ivsCommonService) { + IvsCommonService ivsCommonService, IvsResourceRetryableDelegate ivsResourceRetryableDelegate) { this.redisService = redisService; this.ivsCommonService = ivsCommonService; + this.ivsResourceRetryableDelegate = ivsResourceRetryableDelegate; } @GetMapping({"ptz"}) @@ -45,7 +48,7 @@ public class IvsControlController { jsonObject.put("controlCode", Integer.parseInt(ivsPtzVo.getControlCode())); jsonObject.put("controlPara1", ivsPtzVo.getControlPara1()); jsonObject.put("controlPara2", ivsPtzVo.getControlPara2()); - ivsCommonService.postJson(jsonObject, IvsConst.URI_PTZ_CONTROL); + ivsCommonService.postJson(ivsResourceRetryableDelegate.getLabel(), jsonObject, IvsConst.URI_PTZ_CONTROL); log.debug("[IVS] PTZ END"); return Response.ok(); } @@ -58,7 +61,7 @@ public class IvsControlController { JSONObject paramJsonPreAdd = new JSONObject(); paramJsonPreAdd.put("cameraCode", ivsPresetVo.getCameraCode()); paramJsonPreAdd.put("presetName", ivsPresetVo.getPresetName()); - IvsPresetView ivsPresetView = ivsCommonService.postJson(paramJsonPreAdd, IvsConst.URI_PRESET_SET, IvsPresetView.class); + IvsPresetView ivsPresetView = ivsCommonService.postJson(ivsResourceRetryableDelegate.getLabel(), paramJsonPreAdd, IvsConst.URI_PRESET_SET, IvsPresetView.class); log.debug("[IVS] PRESET SET RES: {}", ivsPresetView); return Response.ok(ivsPresetView); } else if (command.equals("update")) { @@ -68,7 +71,7 @@ public class IvsControlController { ptzPresetInfo.put("presetIndex", Integer.parseInt(ivsPresetVo.getPreset())); ptzPresetInfo.put("presetName", ivsPresetVo.getPresetName()); paramJsonPreUpdate.put("ptzPresetInfo", ptzPresetInfo); - IvsResult ivsResult = ivsCommonService.putJson(paramJsonPreUpdate, IvsConst.URI_PRESET_SET, IvsResult.class); + IvsResult ivsResult = ivsCommonService.putJson(ivsResourceRetryableDelegate.getLabel(), paramJsonPreUpdate, IvsConst.URI_PRESET_SET, IvsResult.class); log.debug("[IVS] PRESET UPDATE RES :{}", ivsResult); redisService.setCacheMapValue(IvsConst.IVS_PRESET_MAP, ivsPresetVo.getPreset(), ivsPresetVo.getPreset()); return Response.ok(); @@ -84,7 +87,7 @@ public class IvsControlController { } paramJson.put("controlPara1", ivsPresetVo.getPreset()); paramJson.put("controlPara2", ""); - ivsCommonService.postJson(paramJson, IvsConst.URI_PTZ_CONTROL, IvsResult.class); + ivsCommonService.postJson(ivsResourceRetryableDelegate.getLabel(), paramJson, IvsConst.URI_PTZ_CONTROL, IvsResult.class); return Response.ok(); } else if (command.equals("remove")) { String code = ivsPresetVo.getCode(); @@ -95,7 +98,7 @@ public class IvsControlController { paramJsonRemove.put("cameraCode", cameraCode); paramJsonRemove.put("domainCode", domainCode); paramJsonRemove.put("presetIndex", Integer.parseInt(ivsPresetVo.getPreset())); - ivsCommonService.delete(UriUtils.parseForReplace(IvsConst.URI_PRESET_REMOVE, paramJsonRemove)); + ivsCommonService.delete(ivsResourceRetryableDelegate.getLabel(), UriUtils.parseForReplace(IvsConst.URI_PRESET_REMOVE, paramJsonRemove)); return Response.ok(); } else { return Response.ok(); @@ -105,7 +108,7 @@ public class IvsControlController { @GetMapping({"presetList"}) public IvsPresetListView presetList(IvsVo ivsVo) { log.debug("[IVS] PRESET LIST: {}", ivsVo); - IvsPresetListView ivsPresetListView = ivsCommonService.get(UriUtils.parseForReplace(IvsConst.URI_PRESET_LIST, ivsVo), IvsPresetListView.class); + IvsPresetListView ivsPresetListView = ivsCommonService.get(ivsResourceRetryableDelegate.getLabel(), UriUtils.parseForReplace(IvsConst.URI_PRESET_LIST, ivsVo), IvsPresetListView.class); log.debug("[IVS] END"); return ivsPresetListView; } diff --git a/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsDeviceController.java b/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsDeviceController.java index bfa7298..5bc6009 100644 --- a/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsDeviceController.java +++ b/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsDeviceController.java @@ -10,7 +10,7 @@ import com.inspect.ivs.constant.IvsConst; import com.inspect.ivs.domain.Temp; import com.inspect.ivs.domain.TempConfiguration; import com.inspect.ivs.service.IvsCommonService; -import com.inspect.ivs.service.PictureDownloadRetryableDelegate; +import com.inspect.ivs.service.IvsResourceRetryableDelegate; import com.inspect.ivs.util.UriUtils; import com.inspect.ivs.view.IvsPlatformSnapshotView; import com.inspect.ivs.view.IvsSnapshotView; @@ -49,7 +49,7 @@ public class IvsDeviceController { private final IvsCommonService ivsCommonService; @Resource - private PictureDownloadRetryableDelegate retryDelegate; + private IvsResourceRetryableDelegate ivsResourceRetryableDelegate; public IvsDeviceController(IvsCommonService ivsCommonService) { this.ivsCommonService = ivsCommonService; @@ -306,6 +306,23 @@ public class IvsDeviceController { // } // } + private void delayBeforeGetPicture() { + if (!testMode) { + log.info("[IVS] prod env get stream delay 9000ms"); + try { + Thread.sleep(9000L); + } catch (Exception e) { + + } + } else { + try { + log.info("[IVS] test env get stream delay 2000ms"); + Thread.sleep(2000L); + } catch (Exception e) { + } + } + } + @GetMapping({"channelsnap"}) public ResponseEntity channelSnap(IvsDevChanSnapVo ivsDevChanSnapVo) { if (version.equals("1800")) { @@ -314,53 +331,56 @@ public class IvsDeviceController { InputStream inputStream = new ByteArrayInputStream(bytes); return ResponseEntity.ok().body(new InputStreamResource(inputStream)); } catch (Exception e) { - return ResponseEntity.ok().body(new InputStreamResource(retryDelegate.generateErrorImage())); + return ResponseEntity.ok().body(new InputStreamResource(ivsResourceRetryableDelegate.generateErrorImage())); } } else { - log.info(com.inspect.base.core.constant.Color.MAGENTA + "[IVS] SNAPSHOT: {}" + com.inspect.base.core.constant.Color.END, ivsDevChanSnapVo.getCameraCode()); - IvsSnapshotView ivsSnapshotView = ivsCommonService.get(UriUtils.parse(IvsConst.URI_PLATFORM_SNAPSHOT, ivsDevChanSnapVo), IvsSnapshotView.class); - IvsChanSnapVo ivsChanSnapVo = new IvsChanSnapVo(ivsDevChanSnapVo.getCameraCode(), - ivsDevChanSnapVo.getDomainCode(), - ivsSnapshotView.getTaskID()); - - if (!testMode) { - log.info("[IVS] prod env get stream delay 9000ms"); + try { + String uuid = ivsResourceRetryableDelegate.getLabel(); + log.info(com.inspect.base.core.constant.Color.MAGENTA + "[IVS] CHANNEL_SNAPSHOT: {}" + com.inspect.base.core.constant.Color.END, ivsDevChanSnapVo.getCameraCode()); + IvsSnapshotView ivsSnapshotView; try { - Thread.sleep(9000L); - } catch (Exception e) { + ivsDevChanSnapVo.setUUID(uuid); + ivsSnapshotView = ivsResourceRetryableDelegate.getSnapshotRetryable(ivsDevChanSnapVo); + } catch (IOException e) { + log.info("ivsSnapshotView {}", e.getMessage() + " final fail"); + return ResponseEntity.ok().body(new InputStreamResource(ivsResourceRetryableDelegate.generateErrorImage())); + } + if(ivsSnapshotView == null) { + log.info("ivsSnapshotView from recover!!!"); + return ResponseEntity.ok().body(new InputStreamResource(ivsResourceRetryableDelegate.generateErrorImage())); } - } else { + + delayBeforeGetPicture(); + + IvsPlatformSnapshotView snapShotView; try { - Thread.sleep(2000L); - } catch (Exception e) {} - } + snapShotView = ivsResourceRetryableDelegate.getSnapshotListRetryable( + new IvsChanSnapVo(ivsDevChanSnapVo.getCameraCode(), + ivsDevChanSnapVo.getDomainCode(), + ivsSnapshotView.getTaskID())); + } catch (IOException e) { + log.info("snapShotView {}", e.getMessage() + " final fail"); + return ResponseEntity.ok().body(new InputStreamResource(ivsResourceRetryableDelegate.generateErrorImage())); + } - IvsPlatformSnapshotView snapShotView = ivsCommonService.postJson( - ivsChanSnapVo, - IvsConst.URI_SNAPSHOT_LIST, - IvsPlatformSnapshotView.class); - - if (snapShotView == null - || snapShotView.getSnapshotInfoList() == null - || snapShotView.getSnapshotInfoList().getSnapshotInfos() == null - || snapShotView.getSnapshotInfoList().getSnapshotInfos().isEmpty()) { - log.info(com.inspect.base.core.constant.Color.RED + "[IVS] GET PIC FAIL cameraCode: {}, channel: {}, domainCode: {}" + com.inspect.base.core.constant.Color.END, - ivsDevChanSnapVo.getCameraCode(), - ivsDevChanSnapVo.getChannel(), - ivsDevChanSnapVo.getDomainCode() - ); - return ResponseEntity.ok().body(new InputStreamResource(retryDelegate.generateErrorImage())); - } + if(snapShotView == null) { + log.info("snapShotView from recover!!!"); + return ResponseEntity.ok().body(new InputStreamResource(ivsResourceRetryableDelegate.generateErrorImage())); + } - String cameraCode = snapShotView.getSnapshotInfoList().getSnapshotInfos().get(0).getCameraCode(); - String pictureUrl = snapShotView.getSnapshotInfoList().getSnapshotInfos().get(0).getPictureUrl(); - log.info(com.inspect.base.core.constant.Color.MAGENTA + "[IVS] SNAPSHOT cameraCode: {}, pictureUrl: {}" + com.inspect.base.core.constant.Color.END, cameraCode, pictureUrl); - try { - return retryDelegate.downloadPicture(pictureUrl); - } catch (IOException e) { - log.info("所有重试失败2, 生成一个默认图片: {}, msg: {}", pictureUrl, e.getMessage()); - return ResponseEntity.ok().body(new InputStreamResource(retryDelegate.generateErrorImage())); + String cameraCode = snapShotView.getSnapshotInfoList().getSnapshotInfos().get(0).getCameraCode(); + String pictureUrl = snapShotView.getSnapshotInfoList().getSnapshotInfos().get(0).getPictureUrl(); + log.info(com.inspect.base.core.constant.Color.MAGENTA + "[IVS] SNAPSHOT cameraCode: {}, pictureUrl: {}" + com.inspect.base.core.constant.Color.END, cameraCode, pictureUrl); + try { + return ivsResourceRetryableDelegate.downloadPictureRetryable(uuid, pictureUrl); + } catch (IOException e) { + log.info("所有重试失败2, 生成一个默认图片: {}, msg: {}", pictureUrl, e.getMessage()); + return ResponseEntity.ok().body(new InputStreamResource(ivsResourceRetryableDelegate.generateErrorImage())); + } + } catch (Exception e) { + log.info("do not know where is the exception: ", e); + return ResponseEntity.ok().body(new InputStreamResource(ivsResourceRetryableDelegate.generateErrorImage())); } } } @@ -378,7 +398,6 @@ public class IvsDeviceController { // } - public byte[] getIvs1800ChannelSnapBuffer(IvsDevChanSnapVo ivsDevChanSnapVo) throws Exception { log.info("++++++++++++++++++++++++执行1800channelsnap截图接口++++++++++++++++++++++++++++++"); String url = address + "/snapshot/manualsnapshot"; @@ -445,7 +464,7 @@ public class IvsDeviceController { if (version.equals("1800")) { ivsDevChanListVo.setDeviceType(2); } - return ivsCommonService.get(UriUtils.parse(IvsConst.URI_DEVICE_LIST, ivsDevChanListVo), SipbDeviceListView.class); + return ivsCommonService.get(ivsResourceRetryableDelegate.getLabel(), UriUtils.parse(IvsConst.URI_DEVICE_LIST, ivsDevChanListVo), SipbDeviceListView.class); } @@ -454,7 +473,7 @@ public class IvsDeviceController { if (version.equals("1800")) { ivsDevChanListVo.setDeviceType(2); } - return ivsCommonService.getResultJson(UriUtils.parse(IvsConst.URI_DEVICE_LIST, ivsDevChanListVo)); + return ivsCommonService.getResultJson(ivsResourceRetryableDelegate.getLabel(), UriUtils.parse(IvsConst.URI_DEVICE_LIST, ivsDevChanListVo)); } private static int calculateSum(int n) { diff --git a/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsPlaybackController.java b/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsPlaybackController.java index d02774b..1d20521 100644 --- a/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsPlaybackController.java +++ b/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsPlaybackController.java @@ -2,6 +2,7 @@ package com.inspect.ivs.controller; import com.alibaba.fastjson.JSONObject; import com.inspect.base.core.domain.Response; +import com.inspect.base.core.utils.StringUtils; import com.inspect.base.redis.service.RedisService; import com.inspect.ivs.constant.IvsConst; import com.inspect.ivs.domain.CameraCodeAndEndTime; @@ -25,6 +26,7 @@ import com.inspect.ivs.vo.IvsStartRecVo; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.UUID; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,7 +61,7 @@ public class IvsPlaybackController { public IvsDevRecListView recordList(IvsPlaybackRecListVo ivsPlaybackRecListVo) { log.debug("[PLAYBACK] playback-recordList ivsPlaybackRecordListVo:{}", ivsPlaybackRecListVo); IvsPlaybackRec2ListVo ivsPlaybackRec2ListVo = new IvsPlaybackRec2ListVo(ivsPlaybackRecListVo.getCode(), ivsPlaybackRecListVo.getStarttime(), ivsPlaybackRecListVo.getEndtime()); - IvsRecListView ivsRecListView = this.ivsCommonService.get(UriUtils.parse(IvsConst.URI_RECORD_LIST, ivsPlaybackRec2ListVo), IvsRecListView.class); + IvsRecListView ivsRecListView = this.ivsCommonService.get(getLabel(), UriUtils.parse(IvsConst.URI_RECORD_LIST, ivsPlaybackRec2ListVo), IvsRecListView.class); log.debug("[PLAYBACK] playback-recordList ivsRecordListView:{}", ivsRecListView); log.debug("[PLAYBACK] playback recordList end"); Map map = new HashMap<>(); @@ -84,11 +86,11 @@ public class IvsPlaybackController { case 1: IvsVideoRtspVo ivsVideoRtspVo = IvsVideoRtspVo.playbackStream(ivsStartRecVo); log.info("[PLAYBACK] json: {}", JSONObject.toJSON(ivsVideoRtspVo)); - ivsVideoRtspView = ivsCommonService.postJson(ivsVideoRtspVo, IvsConst.URI_VIDEO_RTSP, IvsVideoRtspView.class); + ivsVideoRtspView = ivsCommonService.postJson(getLabel(), ivsVideoRtspVo, IvsConst.URI_VIDEO_RTSP, IvsVideoRtspView.class); this.xinTongComponent.convert(ivsVideoRtspView.getRtspURL(), streamId, ivsVideoRtspView); break; default: - ivsVideoRtspView = playbackComponent.postJson(ivsStartRecVo, IvsConst.URI_PLAYBACK_FLV, IvsVideoRtspView.class); + ivsVideoRtspView = playbackComponent.postJson(getLabel(), ivsStartRecVo, IvsConst.URI_PLAYBACK_FLV, IvsVideoRtspView.class); } IvsPlaybackStartView ivsPlaybackStartView = new IvsPlaybackStartView(streamId, ivsVideoRtspView); @@ -100,7 +102,7 @@ public class IvsPlaybackController { @GetMapping({"stop"}) public Response stop(String streamId) { log.debug("==================playback-stop-streamId:{}", streamId); - this.ivsCommonService.get(UriUtils.parse(IvsConst.URI_PLAYBACK_STOP, (new JSONObject()).fluentPut("handle", streamId))); + this.ivsCommonService.get(getLabel(), UriUtils.parse(IvsConst.URI_PLAYBACK_STOP, (new JSONObject()).fluentPut("handle", streamId))); log.debug("==================playback-stop-end"); return Response.ok(); } @@ -113,4 +115,8 @@ public class IvsPlaybackController { log.debug("[PLAYBACK] playback control end"); return Response.ok(); } + + public String getLabel() { + return UUID.randomUUID().toString().trim().replaceAll(StringUtils.DASH, StringUtils.EMPTY); + } } diff --git a/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsRecordController.java b/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsRecordController.java index 349891e..7127031 100644 --- a/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsRecordController.java +++ b/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsRecordController.java @@ -2,6 +2,7 @@ package com.inspect.ivs.controller; import com.inspect.base.core.domain.Response; import com.inspect.base.core.utils.DateUtils; +import com.inspect.base.core.utils.StringUtils; import com.inspect.base.redis.service.RedisService; import com.inspect.ivs.constant.IvsConst; import com.inspect.ivs.service.IvsCommonService; @@ -16,6 +17,7 @@ import com.inspect.ivs.vo.IvsVideoRtspVo; import com.inspect.ivs.vo.IvsVo; import java.util.Iterator; +import java.util.UUID; import java.util.concurrent.TimeUnit; import org.slf4j.Logger; @@ -46,7 +48,7 @@ public class IvsRecordController { ivsRecStartVo.setCode(code); String key = String.format(IvsConst.IVS_RECORD_START_TIME, streamid); redisService.setCacheObject(key, DateUtils.dateTimeNow(), IvsConst.IVS_RECORD_START_TIME_EXPIRE, TimeUnit.MINUTES); - ivsCommonService.get(UriUtils.parse(IvsConst.URI_START_RECORD, ivsRecStartVo)); + ivsCommonService.get(getLabel(), UriUtils.parse(IvsConst.URI_START_RECORD, ivsRecStartVo)); log.debug("[RECORD] record start end"); return Response.ok(); } @@ -58,18 +60,18 @@ public class IvsRecordController { String code = redisService.getCacheObject(key); IvsVo ivsRecordStartVo = new IvsVo(); ivsRecordStartVo.setCode(code); - ivsCommonService.get(UriUtils.parse(IvsConst.URI_STOP_RECORD, ivsRecordStartVo)); + ivsCommonService.get(getLabel(), UriUtils.parse(IvsConst.URI_STOP_RECORD, ivsRecordStartVo)); redisService.deleteObject(key); key = String.format(IvsConst.IVS_RECORD_START_TIME, streamid); String startTime = redisService.getCacheObject(key); redisService.deleteObject(key); IvsPlaybackRec2ListVo ivsPlaybackRec2ListVo = new IvsPlaybackRec2ListVo(code, startTime, DateUtils.dateTimeNow()); - IvsRecListView ivsRecListView = ivsCommonService.get(UriUtils.parse(IvsConst.URI_RECORD_LIST, ivsPlaybackRec2ListVo), IvsRecListView.class); + IvsRecListView ivsRecListView = ivsCommonService.get(getLabel(), UriUtils.parse(IvsConst.URI_RECORD_LIST, ivsPlaybackRec2ListVo), IvsRecListView.class); IvsRecStopView ivsRecStopView = new IvsRecStopView(streamid, ivsRecListView.getRecordInfos().getTotal()); for (IvsRecListView.RecordInfo recordInfo : ivsRecListView.getRecordInfos().getRecordInfoList().getRecordInfo()) { IvsVideoRtspVo ivsVideoRtspVo = IvsVideoRtspVo.download(code, recordInfo); - IvsVideoRtspView ivsVideoRtspView = ivsCommonService.postJson(ivsVideoRtspVo, IvsConst.URI_VIDEO_RTSP, IvsVideoRtspView.class); + IvsVideoRtspView ivsVideoRtspView = ivsCommonService.postJson(getLabel(), ivsVideoRtspVo, IvsConst.URI_VIDEO_RTSP, IvsVideoRtspView.class); zlmediaComponent.convert(ivsVideoRtspView.getRtspURL(), streamid, ivsVideoRtspView); ivsRecStopView.addRecord(recordInfo.getRecordTime().getStartTime(), recordInfo.getRecordTime().getEndTime(), ivsVideoRtspView.getDownloadURL()); } @@ -79,4 +81,8 @@ public class IvsRecordController { log.debug("[RECORD] record stop end"); return ivsRecStopView; } + + public String getLabel() { + return UUID.randomUUID().toString().trim().replaceAll(StringUtils.DASH, StringUtils.EMPTY); + } } diff --git a/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsSessionController.java b/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsSessionController.java index 79623dd..d39c43a 100644 --- a/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsSessionController.java +++ b/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsSessionController.java @@ -1,6 +1,7 @@ package com.inspect.ivs.controller; import com.inspect.base.core.domain.Response; +import com.inspect.base.core.utils.StringUtils; import com.inspect.ivs.constant.IvsConst; import com.inspect.ivs.service.IvsCommonService; import com.inspect.ivs.service.IvsLoginService; @@ -12,6 +13,8 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.UUID; + @RestController @RequestMapping({"/session"}) public class IvsSessionController { @@ -31,8 +34,9 @@ public class IvsSessionController { @GetMapping({"fresh"}) public Response fresh() { if (!"dev".equals(activeProfile)) { + String label = UUID.randomUUID().toString().trim().replaceAll(StringUtils.DASH, StringUtils.EMPTY); log.debug("[IVS SESSION] fresh session start"); - ivsCommonService.get(IvsConst.URI_KEEP_ALIVE); + ivsCommonService.get(label, IvsConst.URI_KEEP_ALIVE); log.debug("[IVS SESSION] fresh session end"); } else { log.info("[IVS SESSION] test profile, no ivs to fresh session"); diff --git a/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsStreamController.java b/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsStreamController.java index e770c28..eb337f9 100644 --- a/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsStreamController.java +++ b/inspect-ivs/src/main/java/com/inspect/ivs/controller/IvsStreamController.java @@ -1,6 +1,7 @@ package com.inspect.ivs.controller; import com.inspect.base.core.domain.Response; +import com.inspect.base.core.utils.StringUtils; import com.inspect.base.core.utils.uuid.UUID; import com.inspect.ivs.constant.IvsConst; import com.inspect.ivs.enums.RtspType; @@ -53,7 +54,9 @@ public class IvsStreamController { ivsVideoRtspView = new IvsVideoRtspView(); ivsVideoRtspView.setRtspURL("rtsp://admin:wd19216811@192.168.1.249:554/h264/ch39/main/av_stream"); } else { + String label = java.util.UUID.randomUUID().toString().trim().replaceAll(StringUtils.DASH, StringUtils.EMPTY); ivsVideoRtspView = ivsCommonService.postJson( + label, ivsVideoRtspVo, IvsConst.URI_VIDEO_RTSP, IvsVideoRtspView.class); diff --git a/inspect-ivs/src/main/java/com/inspect/ivs/service/IvsCommonService.java b/inspect-ivs/src/main/java/com/inspect/ivs/service/IvsCommonService.java index 3dcde04..4587ac5 100644 --- a/inspect-ivs/src/main/java/com/inspect/ivs/service/IvsCommonService.java +++ b/inspect-ivs/src/main/java/com/inspect/ivs/service/IvsCommonService.java @@ -62,7 +62,7 @@ public class IvsCommonService { return webClient.method(httpMethod).uri(getAddress().concat(uri)); } - public T get(String uri, Class clazz) { + public T get(final String label, String uri, Class clazz) { String cookie = this.getCookie(); String res = HttpUtil.createGet(getAddress().concat(uri)) //.cookie(new HttpCookie[]{new HttpCookie(IvsConst.HTTP_JSESSIONID_SESSION_NAME, cookie)}) @@ -70,10 +70,10 @@ public class IvsCommonService { .execute() .body(); log.info("[IVS] GET URI: {}, RES: {}", uri, res); - return checkResultCodeAndConvertResult(JSONObject.parseObject(res), clazz); + return checkResultCodeAndConvertResult(label, uri, JSONObject.parseObject(res), clazz); } - public T getJson(String uri, Class clazz) { + public T getJson(String label, String uri, Class clazz) { String cookie = this.getCookie(); String res = HttpUtil.createGet(getAddress().concat(uri)) .cookie(new HttpCookie[]{new HttpCookie(IvsConst.HTTP_JSESSIONID_SESSION_NAME, cookie)}) @@ -84,10 +84,10 @@ public class IvsCommonService { .contentType(MediaType.APPLICATION_JSON) .cookie("JSESSIONID", cookie).retrieve().bodyToMono(JSONObject.class).block(); log.info("[CALL IVS] getJson POST, Response:{}", t.toJSONString()); - return this.checkResultCodeAndConvertResult(t, clazz); + return this.checkResultCodeAndConvertResult(label, uri, t, clazz); } - public JSONObject getResultJson(String uri) { + public JSONObject getResultJson(String label, String uri) { String cookie = this.getCookie(); JSONObject result = this.http(HttpMethod.GET, uri) .cookie(IvsConst.HTTP_JSESSIONID_SESSION_NAME, cookie) @@ -97,7 +97,7 @@ public class IvsCommonService { String resultCode = result.getString(IvsConst.RESULT_CODE); if (IvsConst.HTTP_STATUS_LOGIN_EXPIRE.equals(resultCode)) { (new Thread(() -> { - this.get(IvsConst.LOCAL_LOGIN_URL); + this.get(label, IvsConst.LOCAL_LOGIN_URL); })).start(); } @@ -108,7 +108,7 @@ public class IvsCommonService { return result; } - public IvsResult delete(String uri) { + public IvsResult delete(String label, String uri) { String cookie = getCookie(); JSONObject jsonObject = http(HttpMethod.DELETE, uri) @@ -117,15 +117,15 @@ public class IvsCommonService { .retrieve() .bodyToMono(JSONObject.class) .block(); - return checkResultCodeAndConvertResult(jsonObject, IvsResult.class); + return checkResultCodeAndConvertResult(label, uri, jsonObject, IvsResult.class); } - public IvsResult get(String uri) { - return this.get(uri, IvsResult.class); + public IvsResult get(String label, String uri) { + return this.get(label, uri, IvsResult.class); } - private T checkResultCodeAndConvertResult(JSONObject result, Class clazz) { - log.info("!!!!!!checkResultCodeAndConvertResult: {}", result.toJSONString()); + private T checkResultCodeAndConvertResult(final String label, final String uri, JSONObject result, Class clazz) { + log.info("CHECK_IVS_RET LABEL: {}, URI: {}, RES: {}", label, uri, result.toJSONString()); String resultCode = result.getString(IvsConst.RESULT_CODE); if (IvsConst.HTTP_STATUS_LOGIN_EXPIRE.equals(resultCode)) { (new Thread(() -> HttpUtil.get(IvsConst.LOCAL_LOGIN_URL))).start(); @@ -138,7 +138,7 @@ public class IvsCommonService { return result.toJavaObject(clazz); } - public T putJson(Object requestParam, String uri, Class clazz) { + public T putJson(String label, Object requestParam, String uri, Class clazz) { String cookie = this.getCookie(); JSONObject t = http(HttpMethod.PUT, uri) .contentType(MediaType.APPLICATION_JSON) @@ -149,7 +149,7 @@ public class IvsCommonService { .block(); log.debug("[IVS] PUT PARAM: {}\n, RES: {}", JSONObject.toJSONString(requestParam), t.toJSONString()); - return checkResultCodeAndConvertResult(t, clazz); + return checkResultCodeAndConvertResult(label, uri, t, clazz); } // public T postJson(Object requestParam, String uri, Class clazz) { @@ -166,16 +166,16 @@ public class IvsCommonService { // return checkResultCodeAndConvertResult(t, clazz); // } - public T postJson(Object requestParam, String uri, Class clazz) { + public T postJson(String label, Object requestParam, String uri, Class clazz) { String cookie = this.getCookie(); JSONObject t = (JSONObject) ((RequestBodySpec) this.http(HttpMethod.POST, uri).contentType(MediaType.APPLICATION_JSON).cookie("JSESSIONID", cookie)).bodyValue(requestParam).retrieve().bodyToMono(JSONObject.class).block(); log.info("[CALL IVS] POST, Parameter:{}", JSONObject.toJSONString(requestParam)); log.info("[CALL IVS] POST, Response:{}", t.toJSONString()); - return this.checkResultCodeAndConvertResult(t, clazz); + return this.checkResultCodeAndConvertResult(label, uri, t, clazz); } - public IvsResult postJson(Object requestParam, String uri) { - return this.postJson(requestParam, uri, IvsResult.class); + public IvsResult postJson(String label, Object requestParam, String uri) { + return this.postJson(label, requestParam, uri, IvsResult.class); } public byte[] sendSSLPost(String url, String param) throws Exception { diff --git a/inspect-ivs/src/main/java/com/inspect/ivs/service/IvsResourceRetryableDelegate.java b/inspect-ivs/src/main/java/com/inspect/ivs/service/IvsResourceRetryableDelegate.java new file mode 100644 index 0000000..f2a7bba --- /dev/null +++ b/inspect-ivs/src/main/java/com/inspect/ivs/service/IvsResourceRetryableDelegate.java @@ -0,0 +1,168 @@ +package com.inspect.ivs.service; + +import com.inspect.base.core.utils.StringUtils; +import com.inspect.ivs.constant.IvsConst; +import com.inspect.ivs.util.UriUtils; +import com.inspect.ivs.view.IvsPlatformSnapshotView; +import com.inspect.ivs.view.IvsSnapshotView; +import com.inspect.ivs.vo.IvsChanSnapVo; +import com.inspect.ivs.vo.IvsDevChanSnapVo; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.InputStreamResource; +import org.springframework.http.ResponseEntity; +import org.springframework.retry.annotation.Backoff; +import org.springframework.retry.annotation.Recover; +import org.springframework.retry.annotation.Retryable; +import org.springframework.retry.support.RetrySynchronizationManager; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.UUID; + +@Component +public class IvsResourceRetryableDelegate { + private static final Logger log = LoggerFactory.getLogger(IvsResourceRetryableDelegate.class); + + private static final int RETRYABLE_MAX = 10; + + @Resource + private IvsCommonService ivsCommonService; + + @Retryable( + value = IOException.class, + maxAttempts = RETRYABLE_MAX, + backoff = @Backoff(delay = 2000)) // 每次重试间隔 2 秒 + public IvsSnapshotView getSnapshotRetryable(IvsDevChanSnapVo ivsDevChanSnapVo) throws IOException { + int retryCount = RetrySynchronizationManager.getContext() != null + ? RetrySynchronizationManager.getContext().getRetryCount() + 1 + : 1; + log.info("URI_PLATFORM_SNAPSHOT retryCount: {}, UUID: {}", retryCount, ivsDevChanSnapVo.getUUID()); + IvsSnapshotView ivsSnapshotView; + try { + ivsSnapshotView = ivsCommonService.get(getLabel(), UriUtils.parse(IvsConst.URI_PLATFORM_SNAPSHOT, ivsDevChanSnapVo), IvsSnapshotView.class); + log.info("URI_PLATFORM_SNAPSHOT SUCCESS retryCount: {}, UUID: {}", retryCount, ivsDevChanSnapVo.getUUID()); + } catch (Exception e) { + log.info("URI_PLATFORM_SNAPSHOT FAIL retryCount: {}, UUID: {}", retryCount, ivsDevChanSnapVo.getUUID()); + throw new IOException("URI_PLATFORM_SNAPSHOT FAIL UUID: " + ivsDevChanSnapVo.getUUID()); + } + + return ivsSnapshotView; + } + + @SuppressWarnings("unused") + @Recover + public IvsSnapshotView recover(IOException e, IvsDevChanSnapVo ivsDevChanSnapVo) { + log.info("URI_PLATFORM_SNAPSHOT RECOVER UUID: {}, MSG: {}", ivsDevChanSnapVo.getUUID(), e.getMessage()); + return null; + } + + @Retryable( + value = IOException.class, + maxAttempts = RETRYABLE_MAX, + backoff = @Backoff(delay = 2000)) // 每次重试间隔 2 秒 + public IvsPlatformSnapshotView getSnapshotListRetryable(final IvsChanSnapVo ivsChanSnapVo) throws IOException { + int retryCount = RetrySynchronizationManager.getContext() != null + ? RetrySynchronizationManager.getContext().getRetryCount() + 1 + : 1; + log.info("URI_SNAPSHOT_LIST retryCount: {}, UUID: {}", retryCount, ivsChanSnapVo.getUUID()); + IvsPlatformSnapshotView snapShotView; + try { + snapShotView = ivsCommonService.postJson(getLabel(), ivsChanSnapVo, IvsConst.URI_SNAPSHOT_LIST, IvsPlatformSnapshotView.class); + log.info("URI_SNAPSHOT_LIST SUCCESS retryCount: {}, UUID: {}", retryCount, ivsChanSnapVo.getUUID()); + } catch (Exception e) { + log.info("URI_SNAPSHOT_LIST FAIL retryCount: {}, UUID: {}", retryCount, ivsChanSnapVo.getUUID()); + throw new IOException("URI_SNAPSHOT_LIST FAIL UUID: " + ivsChanSnapVo.getUUID()); + } + + return snapShotView; + } + + @SuppressWarnings("unused") + @Recover + public IvsPlatformSnapshotView recover(IOException e, IvsChanSnapVo ivsChanSnapVo) { + log.info("URI_SNAPSHOT_LIST RECOVER UUID: {}, MSG: {}", ivsChanSnapVo.getUUID(), e.getMessage()); + return null; + } + + @Retryable( + value = IOException.class, + maxAttempts = RETRYABLE_MAX, + backoff = @Backoff(delay = 2000)) // 每次重试间隔 2 秒 + public ResponseEntity downloadPictureRetryable(String uuid, String pictureUrl) throws IOException { + int retryCount = RetrySynchronizationManager.getContext() != null + ? RetrySynchronizationManager.getContext().getRetryCount() + 1 + : 1; + + log.info("DOWNLOAD_PICTURE retryCount: {}, pictureUrl: {}, UUID: {}", retryCount, pictureUrl, uuid); + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + HttpResponse httpResponse = httpClient.execute(new HttpGet(pictureUrl)); + InputStream content = httpResponse.getEntity().getContent(); + byte[] bytes = readStream(content); + log.info("DOWNLOAD_PICTURE retryCount: {}, SIZE: {}, UUID: {}", retryCount, bytes.length, uuid); + if (bytes.length > 0) { + InputStream inputStream = new ByteArrayInputStream(bytes); + return ResponseEntity.ok().body(new InputStreamResource(inputStream)); + } else { + throw new IOException("DOWNLOAD_PICTURE PIC NULL: " + pictureUrl + ", UUID: " + uuid); + } + } + } + + @SuppressWarnings("unused") + @Recover + public ResponseEntity recover(IOException e, String uuid, String pictureUrl) { + log.info("DOWNLOAD_PICTURE RECOVER {}, UUID: {}, MSG: {}", pictureUrl, uuid, e.getMessage()); + return ResponseEntity.ok().body(new InputStreamResource(generateErrorImage())); + } + + public static byte[] readStream(InputStream inputStream) throws IOException { + try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) { + byte[] buffer = new byte[4096]; // 4KB 缓冲区 + int len; + while ((len = inputStream.read(buffer)) != -1) { + outStream.write(buffer, 0, len); + } + return outStream.toByteArray(); + } + } + + public InputStream generateErrorImage() { + int width = 400, height = 200; + BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics2D g = bufferedImage.createGraphics(); + + // 背景 + g.setColor(Color.WHITE); + g.fillRect(0, 0, width, height); + + // 字体 + g.setColor(Color.RED); + g.setFont(new Font("Arial", Font.BOLD, 20)); + g.drawString("Get Picture Fail", 120, 100); + g.dispose(); + + try { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + ImageIO.write(bufferedImage, "png", os); + return new ByteArrayInputStream(os.toByteArray()); + } catch (IOException e) { + throw new RuntimeException("生成错误图片失败", e); + } + } + + public String getLabel() { + return UUID.randomUUID().toString().trim().replaceAll(StringUtils.DASH, StringUtils.EMPTY); + } +} diff --git a/inspect-ivs/src/main/java/com/inspect/ivs/service/PictureDownloadRetryableDelegate.java b/inspect-ivs/src/main/java/com/inspect/ivs/service/PictureDownloadRetryableDelegate.java deleted file mode 100644 index ca39cc7..0000000 --- a/inspect-ivs/src/main/java/com/inspect/ivs/service/PictureDownloadRetryableDelegate.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.inspect.ivs.service; - -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.core.io.InputStreamResource; -import org.springframework.http.ResponseEntity; -import org.springframework.retry.annotation.Backoff; -import org.springframework.retry.annotation.Recover; -import org.springframework.retry.annotation.Retryable; -import org.springframework.retry.support.RetrySynchronizationManager; -import org.springframework.stereotype.Component; - -import javax.imageio.ImageIO; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; - -@Component -public class PictureDownloadRetryableDelegate { - private static final Logger log = LoggerFactory.getLogger(PictureDownloadRetryableDelegate.class); - - @Retryable( - value = IOException.class, - maxAttempts = 4, - backoff = @Backoff(delay = 2000)) // 每次重试间隔 2 秒 - public ResponseEntity downloadPicture(String pictureUrl) throws IOException { - int retryCount = RetrySynchronizationManager.getContext() != null - ? RetrySynchronizationManager.getContext().getRetryCount() + 1 - : 1; - - log.info("downloadPicture 尝试第 {} 次下载: {}", retryCount, pictureUrl); - try (CloseableHttpClient httpClient = HttpClients.createDefault()) { - HttpResponse httpResponse = httpClient.execute(new HttpGet(pictureUrl)); - InputStream content = httpResponse.getEntity().getContent(); - byte[] bytes = readStream(content); - - if (bytes.length > 0) { - InputStream inputStream = new ByteArrayInputStream(bytes); - return ResponseEntity.ok().body(new InputStreamResource(inputStream)); - } else { - throw new IOException("图片为空: " + pictureUrl); - } - } - } - - @SuppressWarnings("unused") - @Recover - public ResponseEntity recover(IOException e, String pictureUrl) { - log.info("所有重试失败, 生成一个默认图片: {}, msg: {}", pictureUrl, e.getMessage()); - return ResponseEntity.ok().body(new InputStreamResource(generateErrorImage())); - } - - public static byte[] readStream(InputStream inputStream) throws IOException { - try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) { - byte[] buffer = new byte[4096]; // 4KB 缓冲区 - int len; - while ((len = inputStream.read(buffer)) != -1) { - outStream.write(buffer, 0, len); - } - return outStream.toByteArray(); - } - } - - public InputStream generateErrorImage() { - int width = 400, height = 200; - BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); - Graphics2D g = bufferedImage.createGraphics(); - - // 背景 - g.setColor(Color.WHITE); - g.fillRect(0, 0, width, height); - - // 字体 - g.setColor(Color.RED); - g.setFont(new Font("Arial", Font.BOLD, 20)); - g.drawString("Get Picture Fail", 120, 100); - g.dispose(); - - try { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - ImageIO.write(bufferedImage, "png", os); - return new ByteArrayInputStream(os.toByteArray()); - } catch (IOException e) { - throw new RuntimeException("生成错误图片失败", e); - } - } -} diff --git a/inspect-ivs/src/main/java/com/inspect/ivs/vo/IvsChanSnapVo.java b/inspect-ivs/src/main/java/com/inspect/ivs/vo/IvsChanSnapVo.java index 44224a3..6b76ec4 100644 --- a/inspect-ivs/src/main/java/com/inspect/ivs/vo/IvsChanSnapVo.java +++ b/inspect-ivs/src/main/java/com/inspect/ivs/vo/IvsChanSnapVo.java @@ -6,6 +6,7 @@ public class IvsChanSnapVo implements Serializable { private String cameraCode; private String domainCode; private String taskID; + private String UUID; public String getCameraCode() { return this.cameraCode; @@ -31,6 +32,14 @@ public class IvsChanSnapVo implements Serializable { this.taskID = taskID; } + public String getUUID() { + return UUID; + } + + public void setUUID(String UUID) { + this.UUID = UUID; + } + public boolean equals(Object o) { if(o == this) { return true; diff --git a/inspect-ivs/src/main/java/com/inspect/ivs/vo/IvsDevChanSnapVo.java b/inspect-ivs/src/main/java/com/inspect/ivs/vo/IvsDevChanSnapVo.java index d63c794..ba2b056 100644 --- a/inspect-ivs/src/main/java/com/inspect/ivs/vo/IvsDevChanSnapVo.java +++ b/inspect-ivs/src/main/java/com/inspect/ivs/vo/IvsDevChanSnapVo.java @@ -6,6 +6,7 @@ public class IvsDevChanSnapVo extends IvsVo { private boolean realtime; private boolean check_outputs; private boolean timeout; + private String UUID; public String getSerial() { return this.serial; @@ -47,6 +48,14 @@ public class IvsDevChanSnapVo extends IvsVo { this.timeout = timeout; } + public String getUUID() { + return UUID; + } + + public void setUUID(String UUID) { + this.UUID = UUID; + } + public boolean equals(Object o) { if(o == this) { return true;