feat(aiot): 截图持久化 + ROI 显示修复 + 告警图片代理
截图策略改为截一次持久化到 DB,打开 ROI 页面不再触发 Edge 截图: - 新增 wvp_ai_camera_snapshot 表存储 cos_key - AiScreenshotServiceImpl 回调时持久化 cos_key,图片代理增加 DB 回退 - 前端 getSnapUrl 直接返回代理 URL,force=true 才触发 Edge RoiCanvas 修复: - ResizeObserver 替代 nextTick 初始化 canvas - 图片加载失败时仍初始化 canvas 以显示 ROI 告警图片代理: - AiAlertController 新增 /image 端点通过 presign URL 代理 COS 图片 - 新增告警列表前端页面 alertList Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,8 @@ import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
@@ -60,6 +62,19 @@ public class AiAlertController {
|
||||
return alertService.statistics(startTime);
|
||||
}
|
||||
|
||||
@Operation(summary = "告警图片代理")
|
||||
@GetMapping("/image")
|
||||
public ResponseEntity<byte[]> getAlertImage(@RequestParam String imagePath) {
|
||||
byte[] image = alertService.proxyAlertImage(imagePath);
|
||||
if (image == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.IMAGE_JPEG)
|
||||
.header("Cache-Control", "public, max-age=3600")
|
||||
.body(image);
|
||||
}
|
||||
|
||||
// ==================== Edge 告警上报 ====================
|
||||
|
||||
@Operation(summary = "Edge 告警上报")
|
||||
|
||||
@@ -124,7 +124,7 @@ public class AiRoiController {
|
||||
}
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.IMAGE_JPEG)
|
||||
.header("Cache-Control", "max-age=60")
|
||||
.header("Cache-Control", "public, max-age=300")
|
||||
.body(image);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.genersoft.iot.vmp.aiot.dao;
|
||||
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
@Mapper
|
||||
public interface AiCameraSnapshotMapper {
|
||||
|
||||
@Select("SELECT cos_key FROM wvp_ai_camera_snapshot WHERE camera_code=#{cameraCode}")
|
||||
String getCosKey(@Param("cameraCode") String cameraCode);
|
||||
|
||||
@Insert("INSERT INTO wvp_ai_camera_snapshot (camera_code, cos_key) " +
|
||||
"VALUES (#{cameraCode}, #{cosKey}) " +
|
||||
"ON DUPLICATE KEY UPDATE cos_key=#{cosKey}, updated_at=NOW()")
|
||||
int upsert(@Param("cameraCode") String cameraCode, @Param("cosKey") String cosKey);
|
||||
}
|
||||
@@ -21,4 +21,6 @@ public interface IAiAlertService {
|
||||
Map<String, Object> statistics(String startTime);
|
||||
|
||||
void updateDuration(String alertId, double durationMinutes);
|
||||
|
||||
byte[] proxyAlertImage(String imagePath);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.genersoft.iot.vmp.aiot.service.impl;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.genersoft.iot.vmp.aiot.bean.AiAlert;
|
||||
import com.genersoft.iot.vmp.aiot.dao.AiAlertMapper;
|
||||
import com.genersoft.iot.vmp.aiot.service.IAiAlertService;
|
||||
@@ -7,8 +9,13 @@ import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
@@ -20,6 +27,9 @@ public class AiAlertServiceImpl implements IAiAlertService {
|
||||
@Autowired
|
||||
private AiAlertMapper alertMapper;
|
||||
|
||||
@Value("${ai.alert.presign-url:http://127.0.0.1:8000/admin-api/aiot/storage/presign}")
|
||||
private String presignUrl;
|
||||
|
||||
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
@Override
|
||||
@@ -72,4 +82,20 @@ public class AiAlertServiceImpl implements IAiAlertService {
|
||||
public void updateDuration(String alertId, double durationMinutes) {
|
||||
alertMapper.updateDuration(alertId, durationMinutes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] proxyAlertImage(String imagePath) {
|
||||
try {
|
||||
RestTemplate rest = new RestTemplate();
|
||||
String endpoint = presignUrl + "?objectKey=" +
|
||||
URLEncoder.encode(imagePath, StandardCharsets.UTF_8);
|
||||
String json = rest.getForObject(endpoint, String.class);
|
||||
JSONObject resp = JSON.parseObject(json);
|
||||
String cosUrl = resp.getJSONObject("data").getString("url");
|
||||
return rest.getForObject(URI.create(cosUrl), byte[].class);
|
||||
} catch (Exception e) {
|
||||
log.error("[AiAlert] 代理告警图片失败: path={}, error={}", imagePath, e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.genersoft.iot.vmp.aiot.service.impl;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.genersoft.iot.vmp.aiot.dao.AiCameraSnapshotMapper;
|
||||
import com.genersoft.iot.vmp.aiot.service.IAiScreenshotService;
|
||||
import com.genersoft.iot.vmp.streamProxy.bean.StreamProxy;
|
||||
import com.genersoft.iot.vmp.streamProxy.dao.StreamProxyMapper;
|
||||
@@ -16,6 +17,8 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashMap;
|
||||
@@ -41,15 +44,24 @@ public class AiScreenshotServiceImpl implements IAiScreenshotService {
|
||||
/** 等待 Edge 回调的 pending 请求表 */
|
||||
private final ConcurrentHashMap<String, CompletableFuture<Map<String, Object>>> pendingRequests = new ConcurrentHashMap<>();
|
||||
|
||||
/** requestId → cosPath 映射,用于回调时持久化 cos_key */
|
||||
private final ConcurrentHashMap<String, String> pendingCosKeys = new ConcurrentHashMap<>();
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Autowired
|
||||
private StreamProxyMapper streamProxyMapper;
|
||||
|
||||
@Autowired
|
||||
private AiCameraSnapshotMapper snapshotMapper;
|
||||
|
||||
@Value("${ai.screenshot.callback-url:}")
|
||||
private String callbackUrl;
|
||||
|
||||
@Value("${ai.alert.presign-url:http://127.0.0.1:8000/admin-api/aiot/storage/presign}")
|
||||
private String presignUrl;
|
||||
|
||||
@Override
|
||||
public Map<String, Object> requestScreenshot(String cameraCode, boolean force) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
@@ -100,6 +112,7 @@ public class AiScreenshotServiceImpl implements IAiScreenshotService {
|
||||
try {
|
||||
MapRecord<String, String, String> record = MapRecord.create(SNAP_REQUEST_STREAM, fields);
|
||||
RecordId recordId = stringRedisTemplate.opsForStream().add(record);
|
||||
pendingCosKeys.put(requestId, cosPath);
|
||||
log.info("[AI截图] 发送截图请求: requestId={}, cameraCode={}, streamId={}", requestId, cameraCode, recordId);
|
||||
} catch (Exception e) {
|
||||
log.error("[AI截图] 发送截图请求失败: {}", e.getMessage());
|
||||
@@ -166,6 +179,7 @@ public class AiScreenshotServiceImpl implements IAiScreenshotService {
|
||||
return result;
|
||||
} finally {
|
||||
pendingRequests.remove(requestId);
|
||||
pendingCosKeys.remove(requestId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,6 +196,19 @@ public class AiScreenshotServiceImpl implements IAiScreenshotService {
|
||||
if ("ok".equals(status) && cameraCode != null) {
|
||||
String url = (String) data.get("url");
|
||||
writeCache(cameraCode, url);
|
||||
|
||||
// 持久化 cos_key 到数据库
|
||||
String cosKey = pendingCosKeys.remove(requestId);
|
||||
if (cosKey != null) {
|
||||
try {
|
||||
snapshotMapper.upsert(cameraCode, cosKey);
|
||||
log.info("[AI截图] cos_key 已持久化: cameraCode={}, cosKey={}", cameraCode, cosKey);
|
||||
} catch (Exception e) {
|
||||
log.warn("[AI截图] cos_key 持久化失败: cameraCode={}, error={}", cameraCode, e.getMessage());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
pendingCosKeys.remove(requestId);
|
||||
}
|
||||
|
||||
CompletableFuture<Map<String, Object>> future = pendingRequests.get(requestId);
|
||||
@@ -218,27 +245,49 @@ public class AiScreenshotServiceImpl implements IAiScreenshotService {
|
||||
|
||||
@Override
|
||||
public byte[] proxyScreenshotImage(String cameraCode) {
|
||||
// 1. 先查 Redis 缓存(5分钟内的 presigned URL)
|
||||
String cacheJson = stringRedisTemplate.opsForValue().get(SNAP_CACHE_KEY_PREFIX + cameraCode);
|
||||
if (cacheJson == null) {
|
||||
log.warn("[AI截图] 代理图片: 缓存不存在 cameraCode={}", cameraCode);
|
||||
if (cacheJson != null) {
|
||||
try {
|
||||
JSONObject cached = JSON.parseObject(cacheJson);
|
||||
String cosUrl = cached.getString("url");
|
||||
if (cosUrl != null && !cosUrl.isEmpty()) {
|
||||
RestTemplate rest = new RestTemplate();
|
||||
byte[] bytes = rest.getForObject(URI.create(cosUrl), byte[].class);
|
||||
if (bytes != null) return bytes;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("[AI截图] Redis 缓存 URL 下载失败,尝试 DB: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 查 DB 持久化的 cos_key(永不过期)
|
||||
String cosKey = snapshotMapper.getCosKey(cameraCode);
|
||||
if (cosKey == null) {
|
||||
log.warn("[AI截图] 代理图片: 无缓存也无持久化记录 cameraCode={}", cameraCode);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 3. 调 FastAPI 生成新的 presigned URL
|
||||
try {
|
||||
JSONObject cached = JSON.parseObject(cacheJson);
|
||||
String cosUrl = cached.getString("url");
|
||||
if (cosUrl == null || cosUrl.isEmpty()) {
|
||||
log.warn("[AI截图] 代理图片: 缓存中无 URL cameraCode={}", cameraCode);
|
||||
return null;
|
||||
}
|
||||
RestTemplate rest = new RestTemplate();
|
||||
String endpoint = presignUrl + "?objectKey=" +
|
||||
URLEncoder.encode(cosKey, StandardCharsets.UTF_8);
|
||||
String json = rest.getForObject(endpoint, String.class);
|
||||
JSONObject resp = JSON.parseObject(json);
|
||||
String cosUrl = resp.getJSONObject("data").getString("url");
|
||||
|
||||
// 使用 URI.create 避免 RestTemplate 对已编码的预签名 URL 做二次编码
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
byte[] imageBytes = restTemplate.getForObject(URI.create(cosUrl), byte[].class);
|
||||
log.debug("[AI截图] 代理图片成功: cameraCode={}, size={}", cameraCode, imageBytes != null ? imageBytes.length : 0);
|
||||
// 4. 下载并返回图片
|
||||
byte[] imageBytes = rest.getForObject(URI.create(cosUrl), byte[].class);
|
||||
|
||||
// 5. 更新 Redis 缓存(加速后续请求)
|
||||
if (imageBytes != null) {
|
||||
writeCache(cameraCode, cosUrl);
|
||||
}
|
||||
return imageBytes;
|
||||
} catch (Exception e) {
|
||||
log.error("[AI截图] 代理下载图片失败: cameraCode={}, error={}", cameraCode, e.getMessage());
|
||||
log.error("[AI截图] 通过 DB cos_key 下载图片失败: cameraCode={}, cosKey={}, error={}",
|
||||
cameraCode, cosKey, e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,6 +106,7 @@ public class WebSecurityConfig {
|
||||
defaultExcludes.add("/api/ai/roi/snap/image");
|
||||
defaultExcludes.add("/api/ai/camera/get");
|
||||
defaultExcludes.add("/api/ai/alert/edge/**");
|
||||
defaultExcludes.add("/api/ai/alert/image");
|
||||
defaultExcludes.add("/api/ai/device/edge/**");
|
||||
|
||||
if (userSetting.getInterfaceAuthentication() && !userSetting.getInterfaceAuthenticationExcludes().isEmpty()) {
|
||||
|
||||
@@ -124,6 +124,8 @@ ai:
|
||||
screenshot:
|
||||
# Edge截图回调地址(WVP外部可访问地址,Edge通过此地址回调截图结果)
|
||||
callback-url: http://124.221.55.225:18080
|
||||
alert:
|
||||
presign-url: http://127.0.0.1:8000/admin-api/aiot/storage/presign
|
||||
mqtt:
|
||||
# MQTT推送开关
|
||||
enabled: false
|
||||
|
||||
@@ -86,6 +86,8 @@ ai:
|
||||
enabled: ${AI_SERVICE_ENABLED:false}
|
||||
screenshot:
|
||||
callback-url: ${AI_SCREENSHOT_CALLBACK_URL:}
|
||||
alert:
|
||||
presign-url: ${AI_ALERT_PRESIGN_URL:http://vsp-service:8000/admin-api/aiot/storage/presign}
|
||||
mqtt:
|
||||
enabled: ${AI_MQTT_ENABLED:false}
|
||||
broker: ${AI_MQTT_BROKER:tcp://127.0.0.1:1883}
|
||||
|
||||
Reference in New Issue
Block a user