From 7b10b43e34d87328a3ceb5e77f91f854f5f3e8d3 Mon Sep 17 00:00:00 2001 From: 16337 <1633794139@qq.com> Date: Tue, 10 Mar 2026 15:53:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=EF=BC=9Acamera=5Fcode?= =?UTF-8?q?=E7=94=9F=E6=88=90=E9=80=BB=E8=BE=91=E6=8A=BD=E5=8F=96=E4=B8=BA?= =?UTF-8?q?CameraCodeUtil=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将StreamProxyServiceImpl和AiConfigServiceImpl中重复的URL解析+ camera_code生成代码统一到CameraCodeUtil.generate(),消除重复代码。 Co-Authored-By: Claude Opus 4.6 --- .../service/impl/AiConfigServiceImpl.java | 32 +-------------- .../iot/vmp/aiot/util/CameraCodeUtil.java | 37 +++++++++++++++++ .../service/impl/StreamProxyServiceImpl.java | 40 ++----------------- 3 files changed, 42 insertions(+), 67 deletions(-) create mode 100644 src/main/java/com/genersoft/iot/vmp/aiot/util/CameraCodeUtil.java diff --git a/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiConfigServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiConfigServiceImpl.java index ce6f0177e..52ed5be6d 100644 --- a/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiConfigServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiConfigServiceImpl.java @@ -15,9 +15,9 @@ import com.genersoft.iot.vmp.aiot.dao.AiRoiMapper; import com.genersoft.iot.vmp.aiot.service.IAiConfigService; import com.genersoft.iot.vmp.aiot.service.IAiConfigSnapshotService; import com.genersoft.iot.vmp.aiot.service.IAiRedisConfigService; +import com.genersoft.iot.vmp.aiot.util.CameraCodeUtil; import com.genersoft.iot.vmp.streamProxy.bean.StreamProxy; import com.genersoft.iot.vmp.streamProxy.dao.StreamProxyMapper; -import java.util.UUID; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; @@ -104,7 +104,7 @@ public class AiConfigServiceImpl implements IAiConfigService { } int backfilled = 0; for (StreamProxy proxy : nullCodeProxies) { - String cameraCode = generateCameraCodeFromUrl(proxy.getSrcUrl()); + String cameraCode = CameraCodeUtil.generate(proxy.getSrcUrl()); streamProxyMapper.updateCameraCode(proxy.getId(), cameraCode); backfilled++; log.info("[AiConfig] 回填 camera_code: id={}, app={}, stream={} → {}", @@ -116,34 +116,6 @@ public class AiConfigServiceImpl implements IAiConfigService { } } - /** - * 从 RTSP URL 中提取 IP/Host,生成可读的 camera_code - * 格式:cam_{host简写}_{4位随机} - * 例:rtsp://admin:pwd@192.168.1.100:554/stream → cam_192_168_1_100_a3f1 - */ - private String generateCameraCodeFromUrl(String srcUrl) { - String hostPart = "unknown"; - if (srcUrl != null && !srcUrl.isEmpty()) { - try { - String urlBody = srcUrl; - int schemeEnd = urlBody.indexOf("://"); - if (schemeEnd >= 0) { - urlBody = urlBody.substring(schemeEnd + 3); - } - int atIndex = urlBody.indexOf('@'); - if (atIndex >= 0) { - urlBody = urlBody.substring(atIndex + 1); - } - String host = urlBody.split("[:/]")[0]; - hostPart = host.replace(".", "_"); - } catch (Exception e) { - hostPart = "unknown"; - } - } - String suffix = UUID.randomUUID().toString().replace("-", "").substring(0, 4); - return "cam_" + hostPart + "_" + suffix; - } - /** * 修复 ROI 表中仍使用 app/stream 格式的 camera_id,替换为对应的 camera_code * 例如: camera_id="live/camera01" → camera_id="cam_a1b2c3d4e5f6" diff --git a/src/main/java/com/genersoft/iot/vmp/aiot/util/CameraCodeUtil.java b/src/main/java/com/genersoft/iot/vmp/aiot/util/CameraCodeUtil.java new file mode 100644 index 000000000..2cc8f127a --- /dev/null +++ b/src/main/java/com/genersoft/iot/vmp/aiot/util/CameraCodeUtil.java @@ -0,0 +1,37 @@ +package com.genersoft.iot.vmp.aiot.util; + +import java.util.UUID; + +/** + * 摄像头编码生成工具类 + * 从 RTSP URL 中提取 IP/Host 特征,生成可读的 camera_code + * 格式:cam_{host下划线分隔}_{4位随机} + * 例:rtsp://admin:pwd@192.168.1.100:554/stream → cam_192_168_1_100_a3f1 + */ +public class CameraCodeUtil { + + private CameraCodeUtil() {} + + public static String generate(String srcUrl) { + String hostPart = "unknown"; + if (srcUrl != null && !srcUrl.isEmpty()) { + try { + String urlBody = srcUrl; + int schemeEnd = urlBody.indexOf("://"); + if (schemeEnd >= 0) { + urlBody = urlBody.substring(schemeEnd + 3); + } + int atIndex = urlBody.indexOf('@'); + if (atIndex >= 0) { + urlBody = urlBody.substring(atIndex + 1); + } + String host = urlBody.split("[:/]")[0]; + hostPart = host.replace(".", "_"); + } catch (Exception e) { + hostPart = "unknown"; + } + } + String suffix = UUID.randomUUID().toString().replace("-", "").substring(0, 4); + return "cam_" + hostPart + "_" + suffix; + } +} diff --git a/src/main/java/com/genersoft/iot/vmp/streamProxy/service/impl/StreamProxyServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/streamProxy/service/impl/StreamProxyServiceImpl.java index 5d693d271..416df15cc 100755 --- a/src/main/java/com/genersoft/iot/vmp/streamProxy/service/impl/StreamProxyServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/streamProxy/service/impl/StreamProxyServiceImpl.java @@ -39,11 +39,12 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; +import com.genersoft.iot.vmp.aiot.util.CameraCodeUtil; + import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.UUID; /** * 视频代理业务 @@ -79,41 +80,6 @@ public class StreamProxyServiceImpl implements IStreamProxyService { @Autowired private IAiRedisConfigService redisConfigService; - /** - * 从 RTSP URL 中提取 IP/Host 特征,生成可读的 camera_code - * 格式:cam_{host简写}_{4位随机} - * 例:rtsp://admin:pwd@192.168.1.100:554/stream → cam_192_168_1_100_a3f1 - * rtsp://10.0.0.5:554/live → cam_10_0_0_5_b2e4 - * @param srcUrl RTSP 拉流地址 - * @return 唯一的 camera_code - */ - private String generateCameraCode(String srcUrl) { - String hostPart = "unknown"; - if (srcUrl != null && !srcUrl.isEmpty()) { - try { - // 去掉 rtsp:// 前缀 - String urlBody = srcUrl; - int schemeEnd = urlBody.indexOf("://"); - if (schemeEnd >= 0) { - urlBody = urlBody.substring(schemeEnd + 3); - } - // 去掉 user:pass@ 部分 - int atIndex = urlBody.indexOf('@'); - if (atIndex >= 0) { - urlBody = urlBody.substring(atIndex + 1); - } - // 提取 host(去掉端口和路径) - String host = urlBody.split("[:/]")[0]; - // 将 . 替换为 _ 作为可读标识 - hostPart = host.replace(".", "_"); - } catch (Exception e) { - hostPart = "unknown"; - } - } - String suffix = UUID.randomUUID().toString().replace("-", "").substring(0, 4); - return "cam_" + hostPart + "_" + suffix; - } - /** * 流到来的处理 */ @@ -188,7 +154,7 @@ public class StreamProxyServiceImpl implements IStreamProxyService { // 自动生成 camera_code(最多重试3次避免冲突) int retryCount = 0; while (retryCount < 3) { - String cameraCode = generateCameraCode(streamProxy.getSrcUrl()); + String cameraCode = CameraCodeUtil.generate(streamProxy.getSrcUrl()); streamProxy.setCameraCode(cameraCode); streamProxy.setCreateTime(DateUtil.getNow());