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"; + } }