fix(aiot): pushConfig增加device_id回退逻辑,修复配置推送不到达Edge

问题: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 <noreply@anthropic.com>
This commit is contained in:
2026-02-28 15:47:58 +08:00
parent 6ca8a38fae
commit 98ffac671c

View File

@@ -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.AiRoi;
import com.genersoft.iot.vmp.aiot.bean.AiRoiAlgoBind; import com.genersoft.iot.vmp.aiot.bean.AiRoiAlgoBind;
import com.genersoft.iot.vmp.aiot.config.AiServiceConfig; 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.AiAlgorithmMapper;
import com.genersoft.iot.vmp.aiot.dao.AiEdgeDeviceMapper;
import com.genersoft.iot.vmp.aiot.dao.AiRoiAlgoBindMapper; import com.genersoft.iot.vmp.aiot.dao.AiRoiAlgoBindMapper;
import com.genersoft.iot.vmp.aiot.dao.AiRoiMapper; import com.genersoft.iot.vmp.aiot.dao.AiRoiMapper;
import com.genersoft.iot.vmp.aiot.service.IAiConfigService; import com.genersoft.iot.vmp.aiot.service.IAiConfigService;
@@ -45,6 +47,9 @@ public class AiConfigServiceImpl implements IAiConfigService {
@Autowired @Autowired
private AiAlgorithmMapper algorithmMapper; private AiAlgorithmMapper algorithmMapper;
@Autowired
private AiEdgeDeviceMapper edgeDeviceMapper;
@Autowired @Autowired
private IAiRedisConfigService redisConfigService; private IAiRedisConfigService redisConfigService;
@@ -132,10 +137,17 @@ public class AiConfigServiceImpl implements IAiConfigService {
// 5. 写入设备聚合配置 + Stream 通知(新格式,对接 Edge config_sync // 5. 写入设备聚合配置 + Stream 通知(新格式,对接 Edge config_sync
String deviceId = roiMapper.queryDeviceIdByCameraId(cameraId); 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()) { if (deviceId != null && !deviceId.isEmpty()) {
redisConfigService.writeDeviceAggregatedConfig(deviceId, "UPDATE"); redisConfigService.writeDeviceAggregatedConfig(deviceId, "UPDATE");
redisSyncOk = true;
} else { } else {
log.warn("[AiConfig] 摄像头 {} 未关联边缘设备,跳过聚合配置推送", cameraId); log.warn("[AiConfig] 无法确定设备ID跳过 Redis 聚合配置推送。请先注册边缘设备或为摄像头关联 device_id");
} }
// 6. 本地调试:同步到 Edge HTTP 接口(保留原 Redis 流程) // 6. 本地调试:同步到 Edge HTTP 接口(保留原 Redis 流程)
@@ -145,9 +157,12 @@ public class AiConfigServiceImpl implements IAiConfigService {
Map<String, Object> result = new LinkedHashMap<>(); Map<String, Object> result = new LinkedHashMap<>();
result.put("camera_id", cameraId); result.put("camera_id", cameraId);
result.put("version", snapshot.getVersion()); result.put("version", snapshot.getVersion());
result.put("push_status", "success"); result.put("push_status", redisSyncOk ? "success" : "partial");
result.put("message", "配置已推送到Redis并通知边缘端"); result.put("message", redisSyncOk
? "配置已推送到Redis并通知边缘端"
: "配置已保存,但未能同步到边缘端(缺少 device_id");
result.put("http_sync", httpSyncOk); result.put("http_sync", httpSyncOk);
result.put("redis_sync", redisSyncOk);
log.info("[AiConfig] 配置推送完成: cameraId={}, version={}", cameraId, snapshot.getVersion()); log.info("[AiConfig] 配置推送完成: cameraId={}, version={}", cameraId, snapshot.getVersion());
return result; return result;
@@ -171,6 +186,14 @@ public class AiConfigServiceImpl implements IAiConfigService {
deviceIds.add(deviceId); 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) { for (String deviceId : deviceIds) {
redisConfigService.writeDeviceAggregatedConfig(deviceId, "UPDATE"); redisConfigService.writeDeviceAggregatedConfig(deviceId, "UPDATE");
} }
@@ -430,4 +453,20 @@ public class AiConfigServiceImpl implements IAiConfigService {
return false; return false;
} }
} }
/**
* 获取默认边缘设备 ID从设备表查第一个否则返回 "edge"
*/
private String getDefaultDeviceId() {
try {
List<AiEdgeDevice> 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";
}
} }