From 1b8a4677f362d234d834120deac4880d7ae89a50 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期四, 12 三月 2026 17:03:59 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/ai/utils/AiUtils.java | 274 ++++--------------------------------------------------
1 files changed, 21 insertions(+), 253 deletions(-)
diff --git a/src/main/java/com/zy/ai/utils/AiUtils.java b/src/main/java/com/zy/ai/utils/AiUtils.java
index 5c5c83b..1d6af95 100644
--- a/src/main/java/com/zy/ai/utils/AiUtils.java
+++ b/src/main/java/com/zy/ai/utils/AiUtils.java
@@ -1,279 +1,47 @@
package com.zy.ai.utils;
import com.alibaba.fastjson.JSON;
-import com.baomidou.mybatisplus.mapper.EntityWrapper;
-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.selectList(new EntityWrapper<>());
- request.setTasks(wrkMasts);
-
- List<DeviceRealTimeData> deviceRealTimeDataList = new ArrayList<>();
- List<DeviceConfigsData> deviceConfigsDataList = new ArrayList<>();
-
- List<BasCrnp> basCrnps = basCrnpService.selectList(new EntityWrapper<>());
- 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.selectList(new EntityWrapper<>());
- 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.selectList(new EntityWrapper<Config>().notIn("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");
}
+ 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();
}
-
}
--
Gitblit v1.9.1