| | |
| | | package com.zy.ai.utils; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.zy.ai.entity.DeviceConfigsData; |
| | | import com.zy.ai.entity.DeviceRealTimeData; |
| | | import com.zy.ai.entity.WcsDiagnosisRequest; |
| | | import com.zy.ai.log.AiLogAppender; |
| | | import com.zy.asrs.entity.BasCrnp; |
| | | import com.zy.asrs.entity.BasDevp; |
| | | import com.zy.asrs.entity.WrkMast; |
| | | import com.zy.asrs.service.BasCrnpService; |
| | | import com.zy.asrs.service.BasDevpService; |
| | | import com.zy.asrs.service.WrkMastService; |
| | | import com.zy.common.utils.RedisUtil; |
| | | import com.zy.core.cache.SlaveConnection; |
| | | import com.zy.core.enums.SlaveType; |
| | | import com.zy.core.model.StationObjModel; |
| | | import com.zy.core.model.protocol.CrnProtocol; |
| | | import com.zy.core.model.protocol.StationProtocol; |
| | | import com.zy.core.thread.CrnThread; |
| | | import com.zy.core.thread.StationThread; |
| | | import com.zy.system.entity.Config; |
| | | import com.zy.system.service.ConfigService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Component |
| | | public class AiUtils { |
| | | |
| | | @Autowired |
| | | private WrkMastService wrkMastService; |
| | | @Autowired |
| | | private BasCrnpService basCrnpService; |
| | | @Autowired |
| | | private BasDevpService basDevpService; |
| | | @Autowired |
| | | private ConfigService configService; |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | |
| | | public WcsDiagnosisRequest makeAiRequest(int logLimit, String alarmMessage) { |
| | | WcsDiagnosisRequest request = new WcsDiagnosisRequest(); |
| | | |
| | | request.setAlarmMessage(alarmMessage); |
| | | |
| | | List<String> logs = AiLogAppender.getRecentLogs(logLimit); |
| | | request.setLogs(logs); |
| | | |
| | | List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<>()); |
| | | request.setTasks(wrkMasts); |
| | | |
| | | List<DeviceRealTimeData> deviceRealTimeDataList = new ArrayList<>(); |
| | | List<DeviceConfigsData> deviceConfigsDataList = new ArrayList<>(); |
| | | |
| | | List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>()); |
| | | for (BasCrnp basCrnp : basCrnps) { |
| | | CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, basCrnp.getCrnNo()); |
| | | if (crnThread == null) { |
| | | continue; |
| | | } |
| | | |
| | | CrnProtocol protocol = crnThread.getStatus(); |
| | | |
| | | for (StationObjModel stationObjModel : basCrnp.getInStationList$()) { |
| | | StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo()); |
| | | if (stationThread == null) { |
| | | continue; |
| | | } |
| | | |
| | | Map<Integer, StationProtocol> map = stationThread.getStatusMap(); |
| | | StationProtocol stationProtocol = map.get(stationObjModel.getStationId()); |
| | | if (stationProtocol == null) { |
| | | continue; |
| | | } |
| | | |
| | | DeviceRealTimeData stationData = new DeviceRealTimeData(); |
| | | stationData.setDeviceNo(stationObjModel.getDeviceNo()); |
| | | stationData.setDeviceType(String.valueOf(SlaveType.Devp)); |
| | | stationData.setDeviceData(stationProtocol); |
| | | deviceRealTimeDataList.add(stationData); |
| | | } |
| | | |
| | | DeviceRealTimeData deviceRealTimeData = new DeviceRealTimeData(); |
| | | deviceRealTimeData.setDeviceNo(basCrnp.getCrnNo()); |
| | | deviceRealTimeData.setDeviceType(String.valueOf(SlaveType.Crn)); |
| | | deviceRealTimeData.setDeviceData(protocol); |
| | | deviceRealTimeDataList.add(deviceRealTimeData); |
| | | |
| | | DeviceConfigsData deviceConfigsData = new DeviceConfigsData(); |
| | | deviceConfigsData.setDeviceNo(basCrnp.getCrnNo()); |
| | | deviceConfigsData.setDeviceType(String.valueOf(SlaveType.Crn)); |
| | | deviceConfigsData.setDeviceData(basCrnp); |
| | | deviceConfigsDataList.add(deviceConfigsData); |
| | | } |
| | | |
| | | List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>()); |
| | | for (BasDevp basDevp : basDevps) { |
| | | StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo()); |
| | | if (stationThread == null) { |
| | | continue; |
| | | } |
| | | Map<Integer, StationProtocol> map = stationThread.getStatusMap(); |
| | | |
| | | for (StationObjModel stationObjModel : basDevp.getInStationList$()) { |
| | | StationProtocol stationProtocol = map.get(stationObjModel.getStationId()); |
| | | if (stationProtocol == null) { |
| | | continue; |
| | | } |
| | | |
| | | DeviceRealTimeData stationData = new DeviceRealTimeData(); |
| | | stationData.setDeviceNo(stationObjModel.getDeviceNo()); |
| | | stationData.setDeviceType(String.valueOf(SlaveType.Devp)); |
| | | stationData.setDeviceData(stationProtocol); |
| | | deviceRealTimeDataList.add(stationData); |
| | | } |
| | | |
| | | for (StationObjModel stationObjModel : basDevp.getOutStationList$()) { |
| | | StationProtocol stationProtocol = map.get(stationObjModel.getStationId()); |
| | | if (stationProtocol == null) { |
| | | continue; |
| | | } |
| | | |
| | | DeviceRealTimeData stationData = new DeviceRealTimeData(); |
| | | stationData.setDeviceNo(stationObjModel.getDeviceNo()); |
| | | stationData.setDeviceType(String.valueOf(SlaveType.Devp)); |
| | | stationData.setDeviceData(stationProtocol); |
| | | deviceRealTimeDataList.add(stationData); |
| | | } |
| | | |
| | | //剔除全部输送站点信息,以免数据量过大,后期看实际情况是否打开 |
| | | basDevp.setStationList(null); |
| | | |
| | | DeviceConfigsData deviceConfigsData = new DeviceConfigsData(); |
| | | deviceConfigsData.setDeviceNo(basDevp.getDevpNo()); |
| | | deviceConfigsData.setDeviceType(String.valueOf(SlaveType.Devp)); |
| | | deviceConfigsData.setDeviceData(basDevp); |
| | | deviceConfigsDataList.add(deviceConfigsData); |
| | | } |
| | | |
| | | request.setDeviceRealtimeData(deviceRealTimeDataList); |
| | | request.setDeviceConfigs(deviceConfigsDataList); |
| | | |
| | | List<Config> systemConfigList = configService.list(new QueryWrapper<Config>().ne("code", "dingdingReportUrl")); |
| | | request.setSystemConfigs(systemConfigList); |
| | | |
| | | request.setLogLimit(logLimit); |
| | | return request; |
| | | } |
| | | |
| | | public String buildDiagnosisUserContent(WcsDiagnosisRequest request) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | |
| | | if (request.getAlarmMessage() != null && !request.getAlarmMessage().isEmpty()) { |
| | | sb.append("【问题描述】\n"); |
| | | sb.append(request.getAlarmMessage()).append("\n\n"); |
| | | } |
| | | |
| | | sb.append("【设备信息】\n"); |
| | | sb.append("关注设备(如果有指定): ") |
| | | .append(request.getCraneNo() != null ? request.getCraneNo() : "未指定,需整体分析") |
| | | .append("\n\n"); |
| | | |
| | | Object pseudo = redisUtil.get(com.zy.core.enums.RedisKeyType.MAIN_PROCESS_PSEUDOCODE.key); |
| | | if (pseudo != null) { |
| | | sb.append("【主流程伪代码 mainProcessPseudo】\n"); |
| | | sb.append(String.valueOf(pseudo)).append("\n\n"); |
| | | } |
| | | |
| | | if (request.getExtraContext() != null && !request.getExtraContext().isEmpty()) { |
| | | sb.append("【额外上下文 extraContext】\n"); |
| | | sb.append(JSON.toJSONString(request.getExtraContext(), true)).append("\n\n"); |
| | | } |
| | | |
| | | if (request.getTasks() != null && !request.getTasks().isEmpty()) { |
| | | sb.append("【任务信息 tasks】\n"); |
| | | sb.append("下面是当前相关任务列表的 JSON 数据:\n"); |
| | | sb.append(JSON.toJSONString(request.getTasks(), true)).append("\n\n"); |
| | | } else { |
| | | sb.append("【任务信息 tasks】\n"); |
| | | sb.append("当前未提供任务信息。\n\n"); |
| | | } |
| | | |
| | | if (request.getDeviceRealtimeData() != null && !request.getDeviceRealtimeData().isEmpty()) { |
| | | sb.append("【设备实时数据 deviceRealtimeData】\n"); |
| | | sb.append("下面是各设备当前实时状态的 JSON 数据:\n"); |
| | | sb.append(JSON.toJSONString(request.getDeviceRealtimeData(), true)).append("\n\n"); |
| | | } else { |
| | | sb.append("【设备实时数据 deviceRealtimeData】\n"); |
| | | sb.append("当前未提供设备实时数据。\n\n"); |
| | | } |
| | | |
| | | if (request.getDeviceConfigs() != null && !request.getDeviceConfigs().isEmpty()) { |
| | | sb.append("【设备配置信息 deviceConfigs】\n"); |
| | | sb.append("下面是各设备配置的 JSON 数据:\n"); |
| | | sb.append(JSON.toJSONString(request.getDeviceConfigs(), true)).append("\n\n"); |
| | | } else { |
| | | sb.append("【设备配置信息 deviceConfigs】\n"); |
| | | sb.append("当前未提供设备配置信息。\n\n"); |
| | | } |
| | | |
| | | if (request.getLogs() != null && !request.getLogs().isEmpty()) { |
| | | sb.append("【系统日志 logs(按时间顺序)】\n"); |
| | | for (String logLine : request.getLogs()) { |
| | | sb.append(logLine).append("\n"); |
| | | } |
| | | } else { |
| | | sb.append("【系统日志 logs(按时间顺序)】\n"); |
| | | sb.append("当前未提供日志信息。\n"); |
| | | } |
| | | |
| | | if (request.getSystemConfigs() != null && !request.getSystemConfigs().isEmpty()) { |
| | | sb.append("【系统配置 sys_config】\n"); |
| | | sb.append("下面是各系统配置的 JSON 数据:\n"); |
| | | sb.append(JSON.toJSONString(request.getSystemConfigs(), true)).append("\n\n"); |
| | | } |
| | | |
| | | sb.append("\n请根据以上所有信息,结合你的经验进行分析诊断。"); |
| | | |
| | | return sb.toString(); |
| | | } |
| | | |
| | | public String buildAskUserContent(WcsDiagnosisRequest request) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | |
| | | if (request.getExtraContext() != null && !request.getExtraContext().isEmpty()) { |
| | | sb.append("【额外上下文 extraContext】\n"); |
| | | sb.append(JSON.toJSONString(request.getExtraContext(), true)).append("\n\n"); |
| | | } |
| | | |
| | | if (request.getTasks() != null && !request.getTasks().isEmpty()) { |
| | | sb.append("【任务信息 tasks】\n"); |
| | | sb.append("下面是当前相关任务列表的 JSON 数据:\n"); |
| | | sb.append(JSON.toJSONString(request.getTasks(), true)).append("\n\n"); |
| | | } |
| | | |
| | | if (request.getDeviceRealtimeData() != null && !request.getDeviceRealtimeData().isEmpty()) { |
| | | sb.append("【设备实时数据 deviceRealtimeData】\n"); |
| | | sb.append("下面是各设备当前实时状态的 JSON 数据:\n"); |
| | | sb.append(JSON.toJSONString(request.getDeviceRealtimeData(), true)).append("\n\n"); |
| | | } |
| | | |
| | | if (request.getDeviceConfigs() != null && !request.getDeviceConfigs().isEmpty()) { |
| | | sb.append("【设备配置信息 deviceConfigs】\n"); |
| | | sb.append("下面是各设备配置的 JSON 数据:\n"); |
| | | sb.append(JSON.toJSONString(request.getDeviceConfigs(), true)).append("\n\n"); |
| | | } |
| | | |
| | | if (request.getLogs() != null && !request.getLogs().isEmpty()) { |
| | | sb.append("【系统日志 logs(按时间顺序)】\n"); |
| | | for (String logLine : request.getLogs()) { |
| | | sb.append(logLine).append("\n"); |
| | | } |
| | | } |
| | | |
| | | if (request.getSystemConfigs() != null && !request.getSystemConfigs().isEmpty()) { |
| | | sb.append("【系统配置 sys_config】\n"); |
| | | sb.append("下面是各系统配置的 JSON 数据:\n"); |
| | | sb.append(JSON.toJSONString(request.getSystemConfigs(), true)).append("\n\n"); |
| | | } |
| | | |
| | | return sb.toString(); |
| | | } |
| | | |
| | | public String buildDiagnosisUserContentMcp(WcsDiagnosisRequest request) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | |
| | | if (request == null) { |
| | | return ""; |
| | | } |
| | | |
| | | if (request.getAlarmMessage() != null && !request.getAlarmMessage().isEmpty()) { |
| | | sb.append("【问题描述】\n"); |
| | | sb.append(request.getAlarmMessage()).append("\n\n"); |
| | | } |
| | | |
| | | return sb.toString(); |
| | | if (request.getCraneNo() != null) { |
| | | sb.append("【诊断范围】\n"); |
| | | sb.append("当前重点关注堆垛机:").append(request.getCraneNo()).append("\n\n"); |
| | | } |
| | | |
| | | if (request.getLogLimit() != null && request.getLogLimit() > 0) { |
| | | sb.append("【查询建议】\n"); |
| | | sb.append("如需日志,请优先查询最近 ").append(request.getLogLimit()).append(" 行。\n\n"); |
| | | } |
| | | |
| | | if (request.getExtraContext() != null && !request.getExtraContext().isEmpty()) { |
| | | sb.append("【额外上下文 extraContext】\n"); |
| | | sb.append(JSON.toJSONString(request.getExtraContext(), true)).append("\n\n"); |
| | | } |
| | | |
| | | sb.append("请先通过 MCP 工具获取任务、设备、日志和配置等实时数据,再基于事实分析。\n"); |
| | | return sb.toString(); |
| | | } |
| | | } |