diff --git a/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiAlgorithmServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiAlgorithmServiceImpl.java index 109f8678d..7a1360f07 100644 --- a/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiAlgorithmServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiAlgorithmServiceImpl.java @@ -5,6 +5,7 @@ import com.genersoft.iot.vmp.aiot.config.AiServiceConfig; import com.genersoft.iot.vmp.aiot.dao.AiAlgorithmMapper; import com.genersoft.iot.vmp.aiot.service.IAiAlgorithmService; import com.genersoft.iot.vmp.aiot.service.IAiConfigLogService; +import jakarta.annotation.PostConstruct; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -12,6 +13,7 @@ import org.springframework.web.client.RestTemplate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -30,6 +32,57 @@ public class AiAlgorithmServiceImpl implements IAiAlgorithmService { private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + /** + * 预置算法定义,启动时自动校正数据库中的乱码数据 + */ + private static final Map PRESET_ALGORITHMS = new HashMap<>(); + static { + // algoCode -> {algoName, targetClass, description, paramSchema} + PRESET_ALGORITHMS.put("leave_post", new String[]{ + "离岗检测", "person", "检测人员是否在岗,支持工作时间段配置", + "{\"confirm_on_duty_sec\":{\"type\":\"int\",\"default\":10,\"min\":1},\"confirm_leave_sec\":{\"type\":\"int\",\"default\":10,\"min\":1},\"cooldown_sec\":{\"type\":\"int\",\"default\":300,\"min\":0},\"working_hours\":{\"type\":\"list\",\"default\":[]}}" + }); + PRESET_ALGORITHMS.put("intrusion", new String[]{ + "周界入侵检测", "person", "检测人员进入指定区域", + "{\"cooldown_seconds\":{\"type\":\"int\",\"default\":120,\"min\":0},\"confirm_seconds\":{\"type\":\"int\",\"default\":5,\"min\":1}}" + }); + PRESET_ALGORITHMS.put("crowd_detection", new String[]{ + "人群聚集检测", "person", "检测区域内人员数量是否超过阈值", + "{\"max_count\":{\"type\":\"int\",\"default\":10,\"min\":1},\"cooldown_seconds\":{\"type\":\"int\",\"default\":300,\"min\":0}}" + }); + } + + @PostConstruct + public void initPresetAlgorithms() { + String now = LocalDateTime.now().format(FORMATTER); + for (Map.Entry entry : PRESET_ALGORITHMS.entrySet()) { + String code = entry.getKey(); + String[] vals = entry.getValue(); + AiAlgorithm existing = algorithmMapper.queryByCode(code); + if (existing == null) { + AiAlgorithm algo = new AiAlgorithm(); + algo.setAlgoCode(code); + algo.setAlgoName(vals[0]); + algo.setTargetClass(vals[1]); + algo.setDescription(vals[2]); + algo.setParamSchema(vals[3]); + algo.setIsActive(1); + algo.setCreateTime(now); + algo.setUpdateTime(now); + algorithmMapper.add(algo); + log.info("[AI算法] 初始化预置算法: {}", code); + } else { + existing.setAlgoName(vals[0]); + existing.setTargetClass(vals[1]); + existing.setDescription(vals[2]); + existing.setParamSchema(vals[3]); + existing.setUpdateTime(now); + algorithmMapper.updateByCode(existing); + log.info("[AI算法] 校正预置算法数据: {}", code); + } + } + } + @Override public List queryAll() { return algorithmMapper.queryAll();