From 98ffac671c19db3ad9f01c8f16e7af281ad97fae Mon Sep 17 00:00:00 2001 From: 16337 <1633794139@qq.com> Date: Sat, 28 Feb 2026 15:47:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(aiot):=20pushConfig=E5=A2=9E=E5=8A=A0device?= =?UTF-8?q?=5Fid=E5=9B=9E=E9=80=80=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E9=85=8D=E7=BD=AE=E6=8E=A8=E9=80=81=E4=B8=8D=E5=88=B0?= =?UTF-8?q?=E8=BE=BEEdge?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:ROI表device_id为空时,writeDeviceAggregatedConfig被跳过, Redis Stream事件不发送,Edge永远收不到配置更新,但前端仍显示推送成功。 修复: - pushConfig/pushAllConfig中device_id为空时回退查询edge设备表, 再回退到硬编码默认值"edge" - 返回值新增redis_sync字段,push_status准确反映实际同步结果 - 新增getDefaultDeviceId()方法 Co-Authored-By: Claude Opus 4.6 --- .../service/impl/AiConfigServiceImpl.java | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) 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 a9112ce48..9dd588dd1 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 @@ -7,7 +7,9 @@ import com.genersoft.iot.vmp.aiot.bean.AiConfigSnapshot; import com.genersoft.iot.vmp.aiot.bean.AiRoi; import com.genersoft.iot.vmp.aiot.bean.AiRoiAlgoBind; import com.genersoft.iot.vmp.aiot.config.AiServiceConfig; +import com.genersoft.iot.vmp.aiot.bean.AiEdgeDevice; import com.genersoft.iot.vmp.aiot.dao.AiAlgorithmMapper; +import com.genersoft.iot.vmp.aiot.dao.AiEdgeDeviceMapper; import com.genersoft.iot.vmp.aiot.dao.AiRoiAlgoBindMapper; import com.genersoft.iot.vmp.aiot.dao.AiRoiMapper; import com.genersoft.iot.vmp.aiot.service.IAiConfigService; @@ -45,6 +47,9 @@ public class AiConfigServiceImpl implements IAiConfigService { @Autowired private AiAlgorithmMapper algorithmMapper; + @Autowired + private AiEdgeDeviceMapper edgeDeviceMapper; + @Autowired private IAiRedisConfigService redisConfigService; @@ -132,10 +137,17 @@ public class AiConfigServiceImpl implements IAiConfigService { // 5. 写入设备聚合配置 + Stream 通知(新格式,对接 Edge config_sync) String deviceId = roiMapper.queryDeviceIdByCameraId(cameraId); + if (deviceId == null || deviceId.isEmpty()) { + // 回退:从 edge 设备表获取默认设备 + deviceId = getDefaultDeviceId(); + log.info("[AiConfig] 摄像头 {} 未关联设备,使用默认设备: {}", cameraId, deviceId); + } + boolean redisSyncOk = false; if (deviceId != null && !deviceId.isEmpty()) { redisConfigService.writeDeviceAggregatedConfig(deviceId, "UPDATE"); + redisSyncOk = true; } else { - log.warn("[AiConfig] 摄像头 {} 未关联边缘设备,跳过聚合配置推送", cameraId); + log.warn("[AiConfig] 无法确定设备ID,跳过 Redis 聚合配置推送。请先注册边缘设备或为摄像头关联 device_id"); } // 6. 本地调试:同步到 Edge HTTP 接口(保留原 Redis 流程) @@ -145,9 +157,12 @@ public class AiConfigServiceImpl implements IAiConfigService { Map result = new LinkedHashMap<>(); result.put("camera_id", cameraId); result.put("version", snapshot.getVersion()); - result.put("push_status", "success"); - result.put("message", "配置已推送到Redis并通知边缘端"); + result.put("push_status", redisSyncOk ? "success" : "partial"); + result.put("message", redisSyncOk + ? "配置已推送到Redis并通知边缘端" + : "配置已保存,但未能同步到边缘端(缺少 device_id)"); result.put("http_sync", httpSyncOk); + result.put("redis_sync", redisSyncOk); log.info("[AiConfig] 配置推送完成: cameraId={}, version={}", cameraId, snapshot.getVersion()); return result; @@ -171,6 +186,14 @@ public class AiConfigServiceImpl implements IAiConfigService { deviceIds.add(deviceId); } } + // 回退:如果没有任何 ROI 关联设备,使用默认设备 + if (deviceIds.isEmpty()) { + String defaultId = getDefaultDeviceId(); + if (defaultId != null && !defaultId.isEmpty()) { + deviceIds.add(defaultId); + log.info("[AiConfig] ROI 未关联设备,使用默认设备: {}", defaultId); + } + } for (String deviceId : deviceIds) { redisConfigService.writeDeviceAggregatedConfig(deviceId, "UPDATE"); } @@ -430,4 +453,20 @@ public class AiConfigServiceImpl implements IAiConfigService { return false; } } + + /** + * 获取默认边缘设备 ID(从设备表查第一个,否则返回 "edge") + */ + private String getDefaultDeviceId() { + try { + List devices = edgeDeviceMapper.queryAll(); + if (devices != null && !devices.isEmpty()) { + return devices.get(0).getDeviceId(); + } + } catch (Exception e) { + log.warn("[AiConfig] 查询默认设备失败: {}", e.getMessage()); + } + // 硬编码回退值,与 Edge 端 EDGE_DEVICE_ID 默认值一致 + return "edge"; + } }