From 2307db8fc3abd03227f54e24f73d87fb34908dc2 Mon Sep 17 00:00:00 2001
From: chen.llin <1442464845@qq.comm>
Date: 星期四, 15 一月 2026 09:26:00 +0800
Subject: [PATCH] agv可入判断以及分配规则

---
 src/main/java/com/zy/asrs/task/handler/AgvHandler.java |  208 ++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 179 insertions(+), 29 deletions(-)

diff --git a/src/main/java/com/zy/asrs/task/handler/AgvHandler.java b/src/main/java/com/zy/asrs/task/handler/AgvHandler.java
index b82a456..cbada90 100644
--- a/src/main/java/com/zy/asrs/task/handler/AgvHandler.java
+++ b/src/main/java/com/zy/asrs/task/handler/AgvHandler.java
@@ -3,7 +3,6 @@
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
-import com.core.common.R;
 import com.zy.asrs.entity.Task;
 import com.zy.asrs.entity.TaskLog;
 import com.zy.asrs.entity.WrkMast;
@@ -13,20 +12,19 @@
 import com.zy.asrs.service.TaskLogService;
 import com.zy.asrs.service.TaskService;
 import com.zy.common.constant.ApiInterfaceConstant;
+import com.zy.common.properties.AgvProperties;
 import com.zy.common.utils.HttpHandler;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
-import java.util.function.Function;
-import java.util.function.Predicate;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
@@ -53,15 +51,15 @@
     @Resource
     private BasStationMapper basStationMapper;
 
-    @Value("${Agv.sendTask}")
-    private boolean agvSendTask;
+    @Resource
+    private AgvProperties agvProperties;
 
     /**
      * 鍛煎彨agv鎼繍
      */
     public void callAgv(List<Task> taskList) {
 
-        if (!agvSendTask) {
+        if (!agvProperties.isSendTask()) {
             return;
         }
 
@@ -69,7 +67,7 @@
             // 鍛煎彨agv
             String response = "";
             boolean success = false;
-            String url = ApiInterfaceConstant.AGV_IP + ApiInterfaceConstant.AGV_CALL_CARRY_PATH;
+            String url = ApiInterfaceConstant.AGV_IP + ApiInterfaceConstant.AGV_CREATE_TASK_PATH;
             String namespace = "";
             switch (task.getIoType()) {
                 case 1:
@@ -90,24 +88,46 @@
                 default:
             }
             String body = getRequest(task,namespace);
+            // 鎵撳嵃璇锋眰淇℃伅
+            log.info("{}鍛煎彨agv鎼繍 - 璇锋眰鍦板潃锛歿}", namespace, url);
+            log.info("{}鍛煎彨agv鎼繍 - 璇锋眰鍙傛暟锛歿}", namespace, body);
             try {
+                // 浣跨敤浠欏伐M4鎺ュ彛
                 response = new HttpHandler.Builder()
                         .setUri(ApiInterfaceConstant.AGV_IP)
-                        .setPath(ApiInterfaceConstant.AGV_CALL_CARRY_PATH)
+                        .setPath(ApiInterfaceConstant.AGV_CREATE_TASK_PATH)
                         .setJson(body)
                         .build()
                         .doPost();
+                // 鎵撳嵃杩斿洖鍙傛暟
+                log.info("{}鍛煎彨agv鎼繍 - 杩斿洖鍙傛暟锛歿}", namespace, response);
+                
+                // 妫�鏌ュ搷搴旀槸鍚︿负绌�
+                if (response == null || response.trim().isEmpty()) {
+                    log.error("{}鍛煎彨agv鎼繍澶辫触 - 浠诲姟ID锛歿}锛孉GV鎺ュ彛杩斿洖涓虹┖", namespace, task.getId());
+                    continue;
+                }
+                
                 JSONObject jsonObject = JSON.parseObject(response);
-                if (jsonObject.getInteger("code").equals(200)) {
+                if (jsonObject == null) {
+                    log.error("{}鍛煎彨agv鎼繍澶辫触 - 浠诲姟ID锛歿}锛屽搷搴擩SON瑙f瀽澶辫触锛屽搷搴斿唴瀹癸細{}", namespace, task.getId(), response);
+                    continue;
+                }
+                
+                Integer code = jsonObject.getInteger("code");
+                if (code != null && code.equals(200)) {
                     success = true;
                     task.setWrkSts(8L);
                     taskService.updateById(task);
-                    log.info(namespace + "鍛煎彨agv鎼繍鎴愬姛锛歿}", task.getId());
+                    log.info("{}鍛煎彨agv鎼繍鎴愬姛 - 浠诲姟ID锛歿}", namespace, task.getId());
                 } else {
-                    log.error(namespace + "鍛煎彨agv鎼繍澶辫触锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", url, body, response);
+                    String message = jsonObject.getString("message");
+                    log.error("{}鍛煎彨agv鎼繍澶辫触 - 浠诲姟ID锛歿}锛岄敊璇爜锛歿}锛岄敊璇俊鎭細{}", 
+                            namespace, task.getId(), code, message);
                 }
             } catch (Exception e) {
-                log.error(namespace + "鍛煎彨agv鎼繍寮傚父", e);
+                log.error("{}鍛煎彨agv鎼繍寮傚父 - 浠诲姟ID锛歿}锛岃姹傚湴鍧�锛歿}锛岃姹傚弬鏁帮細{}锛屽紓甯镐俊鎭細{}", 
+                        namespace, task.getId(), url, body, e.getMessage(), e);
             } finally {
                 try {
                     // 淇濆瓨鎺ュ彛鏃ュ織
@@ -128,35 +148,77 @@
     }
 
     /**
-     * 鏋勯�犺姹傚唴瀹�
+     * 鏋勯�犺姹傚唴瀹癸紙浠欏伐M4鏍煎紡锛�
      */
     private String getRequest(Task task, String nameSpace) {
         JSONObject object = new JSONObject();
-        object.put("entityName", "ContainerTransportOrder");
-        JSONObject entityValue = new JSONObject();
-        entityValue.put("id", task.getId());
+        // taskId浣跨敤浠诲姟ID锛屾牸寮忥細T + 浠诲姟ID
+        object.put("taskId", "T" + task.getId());
+        // fromBin浣跨敤婧愬簱浣嶇紪鍙凤紙sourceLocNo锛夛紝濡傛灉涓虹┖鍒欎娇鐢ㄦ簮绔欑偣缂栧彿锛坰ourceStaNo锛変綔涓哄閫�
+        String fromBin = task.getSourceLocNo();
+        if (fromBin == null || fromBin.isEmpty()) {
+            fromBin = task.getSourceStaNo();
+        }
+        if (fromBin == null || fromBin.isEmpty() || "0".equals(fromBin)) {
+            log.warn("浠诲姟{}鐨勬簮搴撲綅鍜屾簮绔欑偣閮戒负绌猴紝浣跨敤榛樿鍊�", task.getId());
+            fromBin = "0";
+        }
+        object.put("fromBin", fromBin);
+        // toBin浣跨敤鐩爣绔欑偣缂栧彿
+        object.put("toBin", task.getStaNo());
+        // robotGroup浠巌nvWh瀛楁鑾峰彇锛屽鏋滄病鏈夊垯鏍规嵁绔欑偣缂栧彿鍒ゆ柇
+        String robotGroup = task.getInvWh();
+        if (robotGroup == null || robotGroup.isEmpty()) {
+            robotGroup = determineRobotGroupByStation(task.getStaNo());
+        }
+        object.put("robotGroup", robotGroup);
+        // kind鏍规嵁浠诲姟绫诲瀷鏄犲皠
         String kind = "";
         switch (nameSpace) {
             case "鍏ュ簱":
-                kind = "inBound";
+                // 鍒ゆ柇鏄惁涓虹┖鎵樺叆搴擄細ioType=10 鎴� emptyMk="Y"
+                if (task.getIoType() == 10 || "Y".equals(task.getEmptyMk())) {
+                    kind = "绌烘墭鍏ュ簱";
+                } else {
+                    kind = "瀹炴墭鍏ュ簱";
+                }
                 break;
             case "鍑哄簱":
-                kind = "outBound";
+                kind = "瀹炴墭鍑哄簱";
                 break;
             case "杞Щ":
-                kind = "moveBound";
+                kind = "璐х墿杞繍";
                 break;
             default:
+                kind = "璐х墿杞繍";
         }
-        entityValue.put("kind", kind);
-        entityValue.put("status", "Created");
-        entityValue.put("priority", 0);
-        entityValue.put("container", task.getBarcode());
-        entityValue.put("fromBin", task.getSourceStaNo());
-        entityValue.put("toBin", task.getStaNo());
-        entityValue.put("emptyFlag", task.getIoType() == 10 ? 1 : 2); // 绌烘墭1锛屾弧鎵�2 agv甯︾潃鍛婅瘔杈撻�佺嚎plc
-        object.put("entityValue", entityValue.toJSONString());
+        object.put("kind", kind);
         return object.toJSONString();
+    }
+
+    /**
+     * 鏍规嵁绔欑偣缂栧彿鍒ゆ柇鏈哄櫒浜虹粍
+     * @param staNo 绔欑偣缂栧彿
+     * @return 鏈哄櫒浜虹粍鍚嶇О
+     */
+    private String determineRobotGroupByStation(String staNo) {
+        if (staNo == null || staNo.isEmpty()) {
+            return agvProperties.getRobotGroupEast(); // 榛樿浣跨敤涓滀晶鏈哄櫒浜虹粍
+        }
+        
+        // 浠庨厤缃腑鑾峰彇绔欑偣鍒楄〃
+        Set<String> eastStations = new HashSet<>(agvProperties.getEastStations());
+        Set<String> westStations = new HashSet<>(agvProperties.getWestStations());
+        
+        // 鍒ゆ柇绔欑偣灞炰簬鍝竴渚�
+        if (eastStations.contains(staNo)) {
+            return agvProperties.getRobotGroupEast(); // 涓滀晶鏈哄櫒浜�
+        } else if (westStations.contains(staNo)) {
+            return agvProperties.getRobotGroupWest(); // 瑗夸晶鏈哄櫒浜�
+        } else {
+            log.warn("绔欑偣缂栧彿涓嶅湪閰嶇疆鍒楄〃涓紝浣跨敤榛樿鏈哄櫒浜虹粍锛歿}", staNo);
+            return agvProperties.getRobotGroupEast(); // 榛樿浣跨敤涓滀晶鏈哄櫒浜虹粍
+        }
     }
 
     /**
@@ -225,4 +287,92 @@
             basStationMapper.updateLocStsBatch( Collections.singletonList(String.valueOf(endSite)), "S");
         }
     }
+
+    /**
+     * 鍙栨秷AGV浠诲姟锛堜粰宸4鎺ュ彛锛�
+     * @param task 浠诲姟瀵硅薄
+     * @return 鏄惁鎴愬姛
+     */
+    public boolean cancelAgvTask(Task task) {
+        if (!agvProperties.isSendTask()) {
+            return false;
+        }
+
+        if (task == null || task.getId() == null) {
+            log.error("鍙栨秷AGV浠诲姟澶辫触锛氫换鍔℃垨浠诲姟ID涓虹┖");
+            return false;
+        }
+
+        String response = "";
+        boolean success = false;
+        String url = ApiInterfaceConstant.AGV_IP + ApiInterfaceConstant.AGV_CANCEL_TASK_PATH;
+        String namespace = "";
+        String kind = "";
+        
+        // 鏍规嵁浠诲姟绫诲瀷纭畾kind鍜宯amespace
+        switch (task.getIoType()) {
+            case 1:
+            case 10:
+            case 53:
+            case 57:
+                namespace = "鍏ュ簱";
+                kind = "瀹炴墭鍏ュ簱";
+                break;
+            case 3:
+                namespace = "杞Щ";
+                kind = "璐х墿杞繍";
+                break;
+            case 101:
+            case 110:
+            case 103:
+            case 107:
+                namespace = "鍑哄簱";
+                kind = "瀹炴墭鍑哄簱";
+                break;
+            default:
+                kind = "璐х墿杞繍";
+        }
+
+        // 鏋勯�犲彇娑堜换鍔¤姹�
+        JSONObject cancelRequest = new JSONObject();
+        cancelRequest.put("taskId", "T" + task.getId());
+        cancelRequest.put("kind", kind);
+        String body = cancelRequest.toJSONString();
+
+        try {
+            response = new HttpHandler.Builder()
+                    .setUri(ApiInterfaceConstant.AGV_IP)
+                    .setPath(ApiInterfaceConstant.AGV_CANCEL_TASK_PATH)
+                    .setJson(body)
+                    .build()
+                    .doPost();
+            
+            JSONObject jsonObject = JSON.parseObject(response);
+            if (jsonObject.getInteger("code") != null && jsonObject.getInteger("code").equals(200)) {
+                success = true;
+                log.info(namespace + "鍙栨秷AGV浠诲姟鎴愬姛锛歿}", task.getId());
+            } else {
+                log.error(namespace + "鍙栨秷AGV浠诲姟澶辫触锛侊紒锛乽rl锛歿}锛況equest锛歿}锛況esponse锛歿}", url, body, response);
+            }
+        } catch (Exception e) {
+            log.error(namespace + "鍙栨秷AGV浠诲姟寮傚父", e);
+        } finally {
+            try {
+                // 淇濆瓨鎺ュ彛鏃ュ織
+                apiLogService.save(
+                        namespace + "鍙栨秷AGV浠诲姟",
+                        url,
+                        null,
+                        "127.0.0.1",
+                        body,
+                        response,
+                        success
+                );
+            } catch (Exception e) {
+                log.error(namespace + "鍙栨秷AGV浠诲姟淇濆瓨鎺ュ彛鏃ュ織寮傚父:", e);
+            }
+        }
+
+        return success;
+    }
 }

--
Gitblit v1.9.1