From 6c679ea7d1acdd67c7c2d4c7302b6420bd3fbcd5 Mon Sep 17 00:00:00 2001
From: lsh <lsh@163.com>
Date: 星期四, 22 五月 2025 13:09:03 +0800
Subject: [PATCH] *

---
 src/main/java/com/zy/core/thread/RgvThread.java |  261 +++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 237 insertions(+), 24 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/RgvThread.java b/src/main/java/com/zy/core/thread/RgvThread.java
index d354a5b..365d9b9 100644
--- a/src/main/java/com/zy/core/thread/RgvThread.java
+++ b/src/main/java/com/zy/core/thread/RgvThread.java
@@ -14,6 +14,7 @@
 import com.zy.asrs.service.BasRgvService;
 import com.zy.asrs.utils.RouteUtils;
 import com.zy.asrs.utils.TrackRangeUtils;
+import com.zy.core.DevpThread;
 import com.zy.core.ThreadHandler;
 import com.zy.core.cache.*;
 import com.zy.core.enums.RgvModeType;
@@ -25,6 +26,7 @@
 import com.zy.core.model.command.RgvCommand;
 import com.zy.core.model.protocol.RgvProtocol;
 import com.zy.core.model.protocol.RgvTaskProtocol;
+import com.zy.core.model.protocol.StaProtocol;
 import com.zy.core.model.protocol.TaskProtocol;
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
@@ -48,11 +50,11 @@
     //    private RgvProtocol rgvProtocol;
     private TaskProtocolCache taskProtocolCache = new TaskProtocolCache();
     // # 杞ㄩ亾鎬婚暱
-    private Long trackEntireLength = 100L;
+    private Long trackEntireLength = 224000L;
     //# 杞ㄩ亾鍩哄噯鐐�
-    private Long trackBenchmark = 100L;
+    private Long trackBenchmark = 1L;
     //  # 閬胯璺濈
-    private Long avoidDistance = 100L;
+    private Long avoidDistance = 5000L;
 
     /**
      * 宸ヤ綅1澶嶄綅淇″彿
@@ -63,6 +65,9 @@
      */
     private boolean resetFlag2 = false;
 
+    private boolean connectRgv = false;
+    private boolean delRgvTask = false;
+
     public RgvThread(RgvSlave slave) {
         this.slave = slave;
     }
@@ -70,20 +75,48 @@
     @Override
     @SuppressWarnings("InfiniteLoopStatement")
     public void run() {
-        boolean connect = this.connect();
-        if (connect) {
+        connectRgv = this.connect();
+        while(!connectRgv){
+            try {
+                connectRgv = this.connect();
+                Thread.sleep(100);
+            } catch (Exception e){
 
-            // 鍚姩璇绘暟鎹嚎绋�
-            new Thread(this::readStatusRgv).start();
+            }
+        }
 
-            // 鍚姩浠诲姟涓嬪彂绾跨▼
-            new Thread(this::taskIssued).start();
+        // 鍚姩璇绘暟鎹嚎绋�
+        new Thread(this::rgvConnect).start();
 
-            // 鍚姩婕父绾跨▼
-            new Thread(this::taskWalkIssued).start();
+        new Thread(this::readStatusRgv).start();
 
-            // 鍚姩浠诲姟瀹屾垚绾跨▼
-            new Thread(this::taskComplete).start();
+        // 鍚姩浠诲姟涓嬪彂绾跨▼
+        new Thread(this::taskIssued).start();
+
+        // 鍚姩婕父绾跨▼
+        new Thread(this::taskWalkIssued).start();
+        new Thread(this::taskWalkIssued2).start();
+
+        // 鍚姩浠诲姟瀹屾垚绾跨▼
+        new Thread(this::taskComplete).start();
+    }
+
+    private void rgvConnect() {
+        while (true) {
+            try {
+                if(!connectRgv){
+                    try {
+                        connectRgv = this.connect();
+                        Thread.sleep(100);
+                    } catch (Exception e){
+
+                    }
+                }
+            } catch (Exception e) {
+                log.error("RGV鏁版嵁璇诲彇绾跨▼寮傚父锛侊紒锛�" + e.getMessage());
+                initRgv();
+//                e.printStackTrace();
+            }
         }
     }
 
@@ -110,11 +143,16 @@
     private void taskComplete() {
         while (true) {
             try {
+                if (delRgvTask){
+                    writeDelRgvTask();
+                    delRgvTask = false;
+                    continue;
+                }
                 Thread.sleep(100);
                 OperateResultExOne<byte[]> result = siemensNet.Read("DB100.12", (short) 1);
                 boolean[] status = siemensNet.getByteTransform().TransBool(result.Content, 0, 1);
                 if (status[0]){
-                    OperateResult result4 = siemensNet.Write("DB100.12.0", true);
+                    OperateResult result4 = siemensNet.Write("DB100.12.0", false);
                 }
             } catch (Exception e) {
                 log.error("RGV鏁版嵁璇诲彇绾跨▼寮傚父锛侊紒锛�" + e.getMessage());
@@ -124,6 +162,63 @@
 
         }
 
+    }
+
+    /**
+     * 浠诲姟涓嬪彂
+     */
+    private void taskWalkIssued2() {
+        while (true) {
+            try {
+                // 浼戠湢 1 绉�
+                Thread.sleep(100);
+                if (!deviceDetection()) {
+                    continue;
+                }
+                RgvProtocol rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
+                RgvTaskProtocol rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
+                if (rgvProtocol == null || rgvTaskProtocol == null) {
+                    initRgv();
+                    rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
+                    rgvTaskProtocol = RgvTaskCache.getRgvStatus(slave.getId());
+                }
+                if (rgvTaskProtocol.getAvoid() != 0) {
+                    continue;
+                }
+                if (!rgvProtocol.getStatusType().equals(RgvStatusType.IDLE)){
+                    continue;
+                }
+                if (rgvProtocol.getLoaded() == -1){
+                    continue;
+                }
+
+                List<TaskProtocol> allTakeTaskProtocol = taskProtocolCache.getAllWalkTaskProtocol();
+                for (TaskProtocol taskProtocol : allTakeTaskProtocol) {
+                    if (taskProtocol.getIsRunning() == 1) {//鍑嗗涓嬪彂
+                        // 鍙岃溅
+                        if (rgvOtherStatusEnable()) {
+                            //鍙︿竴鍙拌溅鏄惁鍏佽姝ゅ彴杞︽墽琛�
+                            if (!otherRgvAvoid(taskProtocol.getTargetPosition())) {
+                                continue;
+                            }
+                        }
+                        if (taskProtocol.getTargetPosition()<=rgvProtocol.getRgvPos()+50
+                                && taskProtocol.getTargetPosition()>=rgvProtocol.getRgvPos()-50){
+                            taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
+                            break;
+                        } else {
+                            TaskProtocol issued = new TaskProtocol(taskProtocol,true);
+                            write(issued);
+                            taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
+                            break;
+                        }
+                    }
+                }
+            } catch (Exception e) {
+                log.error("RGV鍙栬揣浠诲姟涓嬪彂绾跨▼寮傚父锛侊紒锛�" + e.getMessage());
+//                e.printStackTrace();
+            }
+        }
     }
 
     /**
@@ -149,6 +244,8 @@
                 TaskProtocol issued = new TaskProtocol();
                 issued.setTaskNo(32222L);
                 issued.setTargetPosition(rgvTaskProtocol.getAvoidingTheDestination());
+                issued.setTaskStatus(1);
+                issued.setDirection(true);
                 write(issued);
                 rgvTaskProtocol.setAvoid(0);
 
@@ -186,11 +283,29 @@
                 if (!rgvProtocol.getStatusType().equals(RgvStatusType.IDLE)){
                     continue;
                 }
+                if (rgvProtocol.getLoaded() == -1){
+                    continue;
+                }
 
                 List<TaskProtocol> allTakeTaskProtocol = taskProtocolCache.getTakeOrPutTaskProtocol(rgvProtocol.getLoaded());
                 for (TaskProtocol taskProtocol : allTakeTaskProtocol) {
                     if (taskProtocol.getIsRunning() == 1) {//鍑嗗涓嬪彂
-                        RgvProtocol rgvProtocolOther = RgvStatusCache.getRgvStatus(slave.getOtherId());
+//                        RgvProtocol rgvProtocolOther = RgvStatusCache.getRgvStatus(slave.getOtherId());
+                        if (taskProtocol.getTaskStatus()==3){
+                            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, taskProtocol.getTargetPositionStaNoPlcId());
+                            StaProtocol staProtocol = devpThread.getStation().get(taskProtocol.getTargetPositionStaNo());
+                            if (staProtocol == null ) {
+                                continue;
+                            }
+                            // 鍒ゆ柇鏄惁婊¤冻鍏ュ簱鏉′欢
+                            if (!staProtocol.isAutoing() || staProtocol.isLoading()
+                                    || staProtocol.getWorkNo() != 0
+                            ){
+                                Thread.sleep(500);
+//                                    System.out.println("<鏀捐揣鐩爣绔�>" + taskProtocol.getTargetPositionStaNo() + "<闈炶嚜鍔ㄦ垨鑰呮湁鐗╂垨鑰呭瓨鍦ㄥ伐浣滃彿锛侊紒锛�>");
+//                                continue;
+                            }
+                        }
                         //鍙岃溅
                         if (rgvOtherStatusEnable()) {
                             //鍙︿竴鍙拌溅鏄惁鍏佽姝ゅ彴杞︽墽琛�
@@ -200,18 +315,28 @@
                         }
                         if (taskProtocol.getTargetPosition()<=rgvProtocol.getRgvPos()+50
                                 && taskProtocol.getTargetPosition()>=rgvProtocol.getRgvPos()-50){
-                            Thread.sleep(100);
+                            if (taskProtocol.getTaskStatus()==3){
+                                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, taskProtocol.getTargetPositionStaNoPlcId());
+                                StaProtocol staProtocol = devpThread.getStation().get(taskProtocol.getTargetPositionStaNo());
+                                if (staProtocol == null ) {
+                                    continue;
+                                }
+                                // 鍒ゆ柇鏄惁婊¤冻鍏ュ簱鏉′欢
+                                if (!staProtocol.isAutoing() || staProtocol.isLoading()
+                                        || staProtocol.getWorkNo() != 0
+                                ){
+                                    Thread.sleep(500);
+//                                    System.out.println("<鏀捐揣鐩爣绔�>" + taskProtocol.getTargetPositionStaNo() + "<闈炶嚜鍔ㄦ垨鑰呮湁鐗╂垨鑰呭瓨鍦ㄥ伐浣滃彿锛侊紒锛�>");
+                                    continue;
+                                }
+                            }
                             TaskProtocol issued = new TaskProtocol(taskProtocol);
                             write(issued);
-                            taskProtocol.setIsRunning(4);
-                            taskProtocolCache.updateTaskProtocol(taskProtocol);
+                            taskProtocolCache.removeTaskProtocol(taskProtocol.getTaskNoDirection());
                             break;
                         } else {
-                            Thread.sleep(100);
                             TaskProtocol issued = new TaskProtocol(taskProtocol,true);
                             write(issued);
-//                            taskProtocol.setIsRunning(4);
-//                            taskProtocolCache.updateTaskProtocol(taskProtocol);
                             break;
                         }
                     }
@@ -282,6 +407,10 @@
                         log.error("琛岃蛋瓒呭嚭鑼冨洿锛侊紒锛佷换鍔″紓甯�  鑱旂郴绠$悊鍛橈紒锛侊紒");
                         return false;
                     }
+                    long avoidAbs = Math.abs(avoid - rgvProtocolOther.getRgvPos());
+                    if (avoidAbs<51){
+                        return true;
+                    }
                     rgvTaskProtocolOther.setAvoid(1);
                     rgvTaskProtocolOther.setAvoidingTheDestination(avoid);
                     RgvTaskCache.updateRgvStatus(rgvTaskProtocolOther);
@@ -298,6 +427,10 @@
                         log.error("琛岃蛋瓒呭嚭鑼冨洿锛侊紒锛佷换鍔″紓甯�  鑱旂郴绠$悊鍛橈紒锛侊紒");
                         return false;
                     }
+                    long avoidAbs = Math.abs(avoid - rgvProtocolOther.getRgvPos());
+                    if (avoidAbs<51){
+                        return true;
+                    }
                     rgvTaskProtocolOther.setAvoid(1);
                     rgvTaskProtocolOther.setAvoidingTheDestination(avoid);
                     RgvTaskCache.updateRgvStatus(rgvTaskProtocolOther);
@@ -309,10 +442,14 @@
                         > avoidDistance) {//鏃犻渶閬胯
                     return true;
                 } else {
-                    if (rgvProtocolOther.getRgvPosDestination() >= rgvProtocolOther.getRgvPos()) {
+                    if (rgvProtocolOther.getRgvPosDestination() >= rgvProtocolOther.getRgvPos()-50) {
                         long avoid = rgvProtocolOther.getRgvPos() - rgvProtocolOther.getCarBodyJiaoMing() - avoidDistance - rgvProtocol.getCarBodyKunPeng();
                         if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[1])) {
                             log.error("琛岃蛋瓒呭嚭鑼冨洿锛侊紒锛佷换鍔″紓甯�  鑱旂郴绠$悊鍛橈紒锛侊紒");
+                            return false;
+                        }
+                        long avoidAbs = Math.abs(avoid - rgvProtocol.getRgvPos());
+                        if (avoidAbs<51){
                             return false;
                         }
                         rgvTaskProtocol.setAvoid(1);
@@ -321,6 +458,12 @@
                         return false;
                     }
                     return false;
+                }
+            } else {
+                if ((rgvProtocolOther.getRgvPosDestinationOrPos(true) - rgvProtocolOther.getCarBodyJiaoMing())
+                        - (targetPosition + rgvProtocol.getCarBodyKunPeng())
+                        > avoidDistance) {//鏃犻渶閬胯
+                    return true;
                 }
             }
         } else {
@@ -334,6 +477,10 @@
                     if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[1])) {
                         log.error("琛岃蛋瓒呭嚭鑼冨洿锛侊紒锛佷换鍔″紓甯�  鑱旂郴绠$悊鍛橈紒锛侊紒");
                         return false;
+                    }
+                    long avoidAbs = Math.abs(avoid - rgvProtocolOther.getRgvPos());
+                    if (avoidAbs<51){
+                        return true;
                     }
                     rgvTaskProtocolOther.setAvoid(1);
                     rgvTaskProtocolOther.setAvoidingTheDestination(avoid);
@@ -351,6 +498,10 @@
                         log.error("琛岃蛋瓒呭嚭鑼冨洿锛侊紒锛佷换鍔″紓甯�  鑱旂郴绠$悊鍛橈紒锛侊紒");
                         return false;
                     }
+                    long avoidAbs = Math.abs(avoid - rgvProtocolOther.getRgvPos());
+                    if (avoidAbs<51){
+                        return true;
+                    }
                     rgvTaskProtocolOther.setAvoid(1);
                     rgvTaskProtocolOther.setAvoidingTheDestination(avoid);
                     RgvTaskCache.updateRgvStatus(rgvTaskProtocolOther);
@@ -363,10 +514,14 @@
                         > avoidDistance) {//鏃犻渶閬胯
                     return true;
                 } else {
-                    if (rgvProtocolOther.getRgvPosDestination() <= rgvProtocolOther.getRgvPos()) {
+                    if (rgvProtocolOther.getRgvPosDestination() <= rgvProtocolOther.getRgvPos() + 50) {
                         long avoid = rgvProtocolOther.getRgvPos() + rgvProtocolOther.getCarBodyKunPeng() + avoidDistance + rgvProtocol.getCarBodyJiaoMing();
                         if (!new TrackRangeUtils().avoidRange(avoid, avoidRange[0])) {
                             log.error("琛岃蛋瓒呭嚭鑼冨洿锛侊紒锛佷换鍔″紓甯�  鑱旂郴绠$悊鍛橈紒锛侊紒");
+                            return false;
+                        }
+                        long avoidAbs = Math.abs(avoid - rgvProtocol.getRgvPos());
+                        if (avoidAbs<51){
                             return false;
                         }
                         rgvTaskProtocol.setAvoid(1);
@@ -375,6 +530,12 @@
                         return false;
                     }
                     return false;
+                }
+            } else {
+                if ((rgvProtocolOther.getRgvPosDestinationOrPos(false) - rgvProtocolOther.getCarBodyJiaoMing())
+                        - (targetPosition + rgvProtocol.getCarBodyKunPeng())
+                        > avoidDistance) {//鏃犻渶閬胯
+                    return true;
                 }
             }
         }
@@ -459,8 +620,10 @@
                 RgvProtocol rgvProtocol = RgvStatusCache.getRgvStatus(slave.getId());
                 if (rgvProtocol == null) {
                     rgvProtocol = new RgvProtocol();
-                    rgvProtocol.setRgvNo(slave.getId());
                 }
+                rgvProtocol.setRgvNo(slave.getId());
+                rgvProtocol.setCarBodyJiaoMing(slave.getCarBodyJiaoMing());
+                rgvProtocol.setCarBodyKunPeng(slave.getCarBodyKunPeng());
 
                 rgvProtocol.setRgvPos((long)siemensNet.getByteTransform().TransInt32(result.Content, 0));
                 rgvProtocol.setRgvPosDestination((long)siemensNet.getByteTransform().TransInt32(result.Content, 4));
@@ -519,6 +682,7 @@
 
             } else {
                 initRgv();
+                connectRgv = false;
 //                OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆戣鍙朢GV plc鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
                 log.error("璇诲彇RGV plc鐘舵�佷俊鎭け璐� ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
             }
@@ -541,6 +705,8 @@
 //        convertRow(command);
 //        taskProtocol.setRgvNo(slave.getId());
         Long[] array = new Long[11];
+        OperateResult result41 = siemensNet.Write("DB100.12.0", false);
+
 //        array[0] = taskProtocol.getAckFinish1();
 //        array[1] = taskProtocol.getTaskNo();
 //        array[2] = taskProtocol.getTaskMode();
@@ -594,6 +760,53 @@
         }
     }
 
+    private boolean writeDelRgvTask() throws InterruptedException {
+//
+        OperateResult result = siemensNet.Write("DB100.12.0", false);
+        OperateResult result0 = siemensNet.Write("DB100.0", (int) 0);
+        OperateResult result1 = siemensNet.Write("DB100.4", (short) 0);
+        OperateResult result2 = siemensNet.Write("DB100.6", (int) 0);
+        OperateResult result3 = siemensNet.Write("DB100.10", (short) 0); // 鎵ц鏂瑰悜锛堥潰鏈濊建閬� 瀹氫綅鍊煎乏灏忓彸澶э級    true:宸�   false:鍙�
+
+        try {
+            // 鏃ュ織璁板綍
+            BasRgvOptService bean = SpringUtils.getBean(BasRgvOptService.class);
+            BasRgvOpt basRgvOpt = new BasRgvOpt(
+                    0,
+                    0,
+                    slave.getId(),
+                    new Date(),
+                    String.valueOf(0),
+                    null,
+                    null,
+                    null,
+                    result.IsSuccess ? 1 : 0,
+                    null,
+                    new Date(),
+                    null
+            );
+            bean.insert(basRgvOpt);
+        } catch (Exception ignore) {
+        }
+
+        if (result != null && result.IsSuccess) {
+            Thread.sleep(200);
+            this.readStatus();
+            log.info("RGV 鍛戒护涓嬪彂[id:{}] >>>>> {}", slave.getId(), JSON.toJSON("null"));
+            OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON("null")));
+            return true;
+        } else {
+            OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆戝啓鍏GV plc鏁版嵁澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+            log.error("鍐欏叆RGV plc鏁版嵁澶辫触 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+            return false;
+        }
+    }
+
+
+    public void setDelRgvTask() {
+        delRgvTask = true;
+    }
+
     @Override
     public void close() {
         siemensNet.ConnectClose();

--
Gitblit v1.9.1