重构: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:
@@ -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.IAiConfigService;
|
||||||
import com.genersoft.iot.vmp.aiot.service.IAiConfigSnapshotService;
|
import com.genersoft.iot.vmp.aiot.service.IAiConfigSnapshotService;
|
||||||
import com.genersoft.iot.vmp.aiot.service.IAiRedisConfigService;
|
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.bean.StreamProxy;
|
||||||
import com.genersoft.iot.vmp.streamProxy.dao.StreamProxyMapper;
|
import com.genersoft.iot.vmp.streamProxy.dao.StreamProxyMapper;
|
||||||
import java.util.UUID;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Lazy;
|
import org.springframework.context.annotation.Lazy;
|
||||||
@@ -104,7 +104,7 @@ public class AiConfigServiceImpl implements IAiConfigService {
|
|||||||
}
|
}
|
||||||
int backfilled = 0;
|
int backfilled = 0;
|
||||||
for (StreamProxy proxy : nullCodeProxies) {
|
for (StreamProxy proxy : nullCodeProxies) {
|
||||||
String cameraCode = generateCameraCodeFromUrl(proxy.getSrcUrl());
|
String cameraCode = CameraCodeUtil.generate(proxy.getSrcUrl());
|
||||||
streamProxyMapper.updateCameraCode(proxy.getId(), cameraCode);
|
streamProxyMapper.updateCameraCode(proxy.getId(), cameraCode);
|
||||||
backfilled++;
|
backfilled++;
|
||||||
log.info("[AiConfig] 回填 camera_code: id={}, app={}, stream={} → {}",
|
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
|
* 修复 ROI 表中仍使用 app/stream 格式的 camera_id,替换为对应的 camera_code
|
||||||
* 例如: camera_id="live/camera01" → camera_id="cam_a1b2c3d4e5f6"
|
* 例如: camera_id="live/camera01" → camera_id="cam_a1b2c3d4e5f6"
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,11 +39,12 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
|
import com.genersoft.iot.vmp.aiot.util.CameraCodeUtil;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 视频代理业务
|
* 视频代理业务
|
||||||
@@ -79,41 +80,6 @@ public class StreamProxyServiceImpl implements IStreamProxyService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private IAiRedisConfigService redisConfigService;
|
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次避免冲突)
|
// 自动生成 camera_code(最多重试3次避免冲突)
|
||||||
int retryCount = 0;
|
int retryCount = 0;
|
||||||
while (retryCount < 3) {
|
while (retryCount < 3) {
|
||||||
String cameraCode = generateCameraCode(streamProxy.getSrcUrl());
|
String cameraCode = CameraCodeUtil.generate(streamProxy.getSrcUrl());
|
||||||
streamProxy.setCameraCode(cameraCode);
|
streamProxy.setCameraCode(cameraCode);
|
||||||
|
|
||||||
streamProxy.setCreateTime(DateUtil.getNow());
|
streamProxy.setCreateTime(DateUtil.getNow());
|
||||||
|
|||||||
Reference in New Issue
Block a user