From dfaf0332889a83f9c8437befdfff32866cf29d5e Mon Sep 17 00:00:00 2001
From: yxFwq <1>
Date: 星期五, 13 六月 2025 15:16:12 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/thread/RgvThread.java |  153 ++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 121 insertions(+), 32 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/RgvThread.java b/src/main/java/com/zy/core/thread/RgvThread.java
index b3d3f21..4c7cdcf 100644
--- a/src/main/java/com/zy/core/thread/RgvThread.java
+++ b/src/main/java/com/zy/core/thread/RgvThread.java
@@ -43,6 +43,8 @@
      * 宸ヤ綅1澶嶄綅淇″彿
      */
     private boolean resetFlag1 = false;
+    private boolean connectRgv = false;
+
     /**
      * 宸ヤ綅2澶嶄綅淇″彿
      */
@@ -55,9 +57,89 @@
     @Override
     @SuppressWarnings("InfiniteLoopStatement")
     public void run() {
-        this.connect();
+        connectRgv = this.connect();
+        while(!connectRgv){
+            try {
+                connectRgv = this.connect();
+                Thread.sleep(100);
+            } catch (Exception e){
+
+            }
+        }
+
+        // 鍚姩绾跨▼鑷姩閲嶈繛
+        new Thread(this::rgvConnect).start();
+
+        // 鍚姩璇绘暟鎹嚎绋�
+        new Thread(this::readStatusRgv).start();
+
+        // 鍚姩浠诲姟涓嬪彂绾跨▼
+        new Thread(this::taskIssued).start();
+    }
+
+    private void rgvConnect() {
         while (true) {
             try {
+                Thread.sleep(1000);
+                if(!connectRgv){
+                    try {
+                        connectRgv = this.connect();
+                        Thread.sleep(100);
+                    } catch (Exception e){
+
+                    }
+                }
+            } catch (Exception e) {
+
+                log.error("rgv杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+                initRgv();
+//                e.printStackTrace();
+            }
+        }
+    }
+
+    private void readStatusRgv() {
+        while (true) {
+            try {
+                if(!connectRgv){
+                    try {
+                        Thread.sleep(1000L);
+                    } catch (Exception e){
+
+                    }
+                    initRgv();
+                    continue;
+                }
+                Thread.sleep(50);
+                readStatus();
+
+            } catch (Exception e) {
+                log.error("RGV璇荤嚎绋嬪紓甯�"+e.getMessage());
+
+                log.error("RGV鏁版嵁璇诲彇绾跨▼寮傚父锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+                initRgv();
+//                e.printStackTrace();
+            }
+
+        }
+
+    }
+
+
+    /**
+     * 浠诲姟涓嬪彂
+     */
+    private void taskIssued() {
+        while (true) {
+            try {
+                if(!connectRgv){
+                    try {
+                        Thread.sleep(1000L);
+                    } catch (Exception e){
+
+                    }
+                    continue;
+                }
                 int step = 1;
                 Task task = MessageQueue.poll(SlaveType.Rgv, slave.getId());
                 if (task != null) {
@@ -66,7 +148,7 @@
                 switch (step) {
                     // 璇绘暟鎹�
                     case 1:
-                        readStatus();
+//                        readStatus();
                         break;
                     // 宸ヤ綅1銆�2鍐欏叆鏁版嵁
                     case 2:
@@ -123,8 +205,10 @@
                     default:
                         break;
                 }
-                Thread.sleep(500);
+                Thread.sleep(200);
             } catch (Exception e) {
+                log.error("RGV鍐欑嚎绋嬪紓甯�"+e.getMessage());
+
 //                e.printStackTrace();
             }
 
@@ -135,23 +219,27 @@
      * 鍒濆鍖朢GV鐘舵��
      */
     private void initRgv() {
-        if (null == rgvProtocol) {
-            rgvProtocol = new RgvProtocol();
+        try{
+            if (null == rgvProtocol) {
+                rgvProtocol = new RgvProtocol();
+            }
+            rgvProtocol.setMode((short) -1);
+            rgvProtocol.setStatus((short)-1);
+            rgvProtocol.setTaskNo1((short)0);
+            rgvProtocol.setStatus1((short)-1);
+            rgvProtocol.setLoaded1((short)0);
+            rgvProtocol.setWalkPos((short)0);
+            rgvProtocol.setRgvPos((short)0);
+            rgvProtocol.setTaskNo2((short)0);
+            rgvProtocol.setStatus2((short)-1);
+            rgvProtocol.setLoaded2((short)0);
+            rgvProtocol.setAlarm((short)0);
+            rgvProtocol.setxSpeed((short) 0);
+            rgvProtocol.setxDistance((short) 0);
+            rgvProtocol.setxDuration((short) 0);
+        }catch (Exception e){
+            log.error("鍒濆鍖朢GV澶辫触"+e.getMessage());
         }
-        rgvProtocol.setMode((short) -1);
-        rgvProtocol.setStatus((short)-1);
-        rgvProtocol.setTaskNo1((short)0);
-        rgvProtocol.setStatus1((short)-1);
-        rgvProtocol.setLoaded1((short)0);
-        rgvProtocol.setWalkPos((short)0);
-        rgvProtocol.setRgvPos((short)0);
-        rgvProtocol.setTaskNo2((short)0);
-        rgvProtocol.setStatus2((short)-1);
-        rgvProtocol.setLoaded2((short)0);
-        rgvProtocol.setAlarm((short)0);
-        rgvProtocol.setxSpeed((short) 0);
-        rgvProtocol.setxDistance((short) 0);
-        rgvProtocol.setxDuration((short) 0);
     }
 
     @Override
@@ -185,6 +273,7 @@
                     rgvProtocol = new RgvProtocol();
                     rgvProtocol.setRgvNo(slave.getId());
                 }
+                rgvProtocol.setRgvNo(slave.getId());
                 rgvProtocol.setMode(siemensNet.getByteTransform().TransInt16(result.Content, 0));
                 rgvProtocol.setStatus(siemensNet.getByteTransform().TransInt16(result.Content, 2));
                 rgvProtocol.setTaskNo1(siemensNet.getByteTransform().TransInt16(result.Content, 4));
@@ -283,7 +372,7 @@
 
         if (command.getAckFinish1() == 0 && command.getAckFinish2() == 0) {
             short commandFinish = 3;  //宸ヤ綅1銆�2浠诲姟鍚屾椂鍐欏叆
-            Thread.sleep(100L);
+            Thread.sleep(50L);
             result = siemensNet.Write("DB100.20", commandFinish);
         }
 
@@ -308,8 +397,8 @@
         } catch (Exception ignore) {}
 
         if (result != null && result.IsSuccess) {
-            Thread.sleep(200);
-            this.readStatus();
+//            Thread.sleep(200);
+//            this.readStatus();
             log.info("RGV 鍛戒护涓嬪彂[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
             OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
             return true;
@@ -344,7 +433,7 @@
 
         if (command.getAckFinish1() == 0) {
             short commandFinish = 1;  //宸ヤ綅1浠诲姟鍐欏叆
-            Thread.sleep(100L);
+            Thread.sleep(20L);
             result = siemensNet.Write("DB100.20", commandFinish);
         }
 
@@ -369,10 +458,10 @@
         } catch (Exception ignore) {}
 
         if (result != null && result.IsSuccess) {
-            Thread.sleep(200);
-            this.readStatus();
-            log.info("RGV 宸ヤ綅1鍛戒护涓嬪彂[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
-            OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 宸ヤ綅1鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
+//            Thread.sleep(200);
+//            this.readStatus();
+            log.info("RGV 宸ヤ綅1鍛戒护涓嬪彂[id:{}] >>>>> {}", slave.getId(), JSON.toJSONString(command));
+            OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 宸ヤ綅1鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSONString(command)));
             return true;
         } else {
             OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆戝啓鍏GV plc宸ヤ綅1鏁版嵁澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
@@ -403,7 +492,7 @@
 
         if (command.getAckFinish2() == 0) {
             short commandFinish = 2;  //宸ヤ綅2浠诲姟鍐欏叆
-            Thread.sleep(100L);
+            Thread.sleep(20L);
             result = siemensNet.Write("DB100.20", commandFinish);
         }
 
@@ -428,10 +517,10 @@
         } catch (Exception ignore) {}
 
         if (result != null && result.IsSuccess) {
-            Thread.sleep(200);
-            this.readStatus();
-            log.info("RGV 宸ヤ綅2鍛戒护涓嬪彂[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
-            OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 宸ヤ綅2鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
+//            Thread.sleep(200);
+//            this.readStatus();
+            log.info("RGV 宸ヤ綅2鍛戒护涓嬪彂[id:{}] >>>>> {}", slave.getId(), JSON.toJSONString(command));
+            OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 宸ヤ綅2鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSONString(command)));
             return true;
         } else {
             OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆戝啓鍏GV plc宸ヤ綅2鏁版嵁澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));

--
Gitblit v1.9.1