重构:camera_code生成逻辑抽取为CameraCodeUtil工具类

将StreamProxyServiceImpl和AiConfigServiceImpl中重复的URL解析+
camera_code生成代码统一到CameraCodeUtil.generate(),消除重复代码。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-10 15:53:29 +08:00
parent 22ee2b92ee
commit 7b10b43e34
3 changed files with 42 additions and 67 deletions

View File

@@ -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"

View File

@@ -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;
}
}

View File

@@ -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());