From dd343b83ac99decfed0a79c4185c281d8b7b4013 Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@163.com>
Date: 星期四, 03 九月 2020 13:30:48 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/thread/CrnThread.java |  113 +++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 67 insertions(+), 46 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/CrnThread.java b/src/main/java/com/zy/core/thread/CrnThread.java
index 73e2b60..9a4f4ea 100644
--- a/src/main/java/com/zy/core/thread/CrnThread.java
+++ b/src/main/java/com/zy/core/thread/CrnThread.java
@@ -12,6 +12,7 @@
 import com.zy.core.ThreadHandler;
 import com.zy.core.cache.MessageQueue;
 import com.zy.core.cache.OutputQueue;
+import com.zy.core.enums.CrnStatusType;
 import com.zy.core.enums.CrnTaskModeType;
 import com.zy.core.enums.SlaveType;
 import com.zy.core.model.CrnSlave;
@@ -35,6 +36,7 @@
     private MelsecMcNet melsecMcNet;
     private CrnSlave slave;
     private CrnProtocol crnProtocol;
+    private boolean resetFlag = false;
 
     public CrnThread(CrnSlave slave) {
         this.slave = slave;
@@ -111,13 +113,13 @@
      */
     private void readStatus(){
         try {
-            if (null == crnProtocol) {
-                crnProtocol = new CrnProtocol();
-            }
 
             /* 鏂规硶1 */
             OperateResultExOne<byte[]> result = melsecMcNet.Read("D2231", (short) 62);
             if (result.IsSuccess) {
+                if (null == crnProtocol) {
+                    crnProtocol = new CrnProtocol();
+                }
                 crnProtocol.setMode(melsecMcNet.getByteTransform().TransInt16(result.Content, 0));
                 crnProtocol.setTaskNo(melsecMcNet.getByteTransform().TransInt16(result.Content, 4));
                 crnProtocol.setStatus(melsecMcNet.getByteTransform().TransInt16(result.Content, 6));
@@ -142,8 +144,28 @@
                 crnProtocol.setXDuration(melsecMcNet.getByteTransform().TransSingle(result.Content, 54));
                 crnProtocol.setYDuration(melsecMcNet.getByteTransform().TransSingle(result.Content, 58));
 
+                OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), slave.getId()));
+
+                // 澶嶄綅淇″彿
+                if (crnProtocol.getStatusType().equals(CrnStatusType.WAITING)) {
+                    if (resetFlag) {
+                        if (melsecMcNet.Write("D2218", (short) 1).IsSuccess) {
+                            resetFlag = false;
+                        }
+                    }
+                }
+
+                // 鏍规嵁瀹炴椂淇℃伅鏇存柊鏁版嵁搴�
+                BasCrnpService basCrnpService = SpringUtils.getBean(BasCrnpService.class);
+                BasCrnp basCrnp = new BasCrnp();
+                basCrnp.setCrnNo(slave.getId());
+                if (!basCrnpService.updateById(crnProtocol.toSqlModel(basCrnp))){
+                    log.error("鍫嗗灈鏈簆lc鏁版嵁搴撴洿鏂板け璐� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+                }
+
             } else {
-                throw new CoolException("鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐�");
+                OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆憑1}鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐�",DateUtils.convert(new Date()), slave.getId()));
+                throw new CoolException(MessageFormat.format( "鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐� ===>> [id:{0}] [ip:{1}] [port:{2}]", slave.getId(), slave.getIp(), slave.getPort()));
             }
             /* 鏂规硶2 */
 //            crnProtocol.setMode(melsecMcNet.ReadInt16("D2231").Content);
@@ -173,17 +195,6 @@
             e.printStackTrace();
             OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆戣鍙栧爢鍨涙満plc鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
             log.error("璇诲彇鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
-            return;
-        }
-
-        OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), slave.getId()));
-
-        // 鏍规嵁瀹炴椂淇℃伅鏇存柊鏁版嵁搴�
-        BasCrnpService basCrnpService = SpringUtils.getBean(BasCrnpService.class);
-        BasCrnp basCrnp = new BasCrnp();
-        basCrnp.setCrnNo(slave.getId());
-        if (!basCrnpService.updateById(crnProtocol.toSqlModel(basCrnp))){
-            log.error("鍫嗗灈鏈簆lc鏁版嵁搴撴洿鏂板け璐� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
         }
 
     }
@@ -199,17 +210,35 @@
         command.setCrnNo(slave.getId());
         /* 鏂规硶 1 */
         do {
-            if (!melsecMcNet.Write("D2218", command.getAckFinish()).IsSuccess) { break; }
-            if (!melsecMcNet.Write("D2218", command.getAckFinish()).IsSuccess) { break; }
-            if (!melsecMcNet.Write("D2216", command.getTaskMode()).IsSuccess) { break; }
-            if (!melsecMcNet.Write("D2214", command.getSourcePosX()).IsSuccess) { break; }
-            if (!melsecMcNet.Write("D2212", command.getSourcePosY()).IsSuccess) { break; }
-            if (!melsecMcNet.Write("D2210", command.getSourcePosZ()).IsSuccess) { break; }
-            if (!melsecMcNet.Write("D2208", command.getDestinationPosX()).IsSuccess) { break; }
-            if (!melsecMcNet.Write("D2204", command.getDestinationPosY()).IsSuccess) { break; }
-            if (!melsecMcNet.Write("D2202", command.getDestinationPosZ()).IsSuccess) { break; }
+            if (command.getDestinationPosZ() != null) {
+                if (!melsecMcNet.Write("D2202", command.getDestinationPosZ()).IsSuccess) { break; }
+            }
+            if (command.getDestinationPosY() != null) {
+                if (!melsecMcNet.Write("D2204", command.getDestinationPosY()).IsSuccess) { break; }
+            }
+            if (command.getDestinationPosX() != null) {
+                if (!melsecMcNet.Write("D2208", command.getDestinationPosX()).IsSuccess) { break; }
+            }
+            if (command.getSourcePosZ() != null) {
+                if (!melsecMcNet.Write("D2210", command.getSourcePosZ()).IsSuccess) { break; }
+            }
+            if (command.getSourcePosY() != null) {
+                if (!melsecMcNet.Write("D2212", command.getSourcePosY()).IsSuccess) { break; }
+            }
+            if (command.getDestinationPosX() != null) {
+                if (!melsecMcNet.Write("D2214", command.getSourcePosX()).IsSuccess) { break; }
+            }
+            if (command.getTaskMode() != null) {
+                if (!melsecMcNet.Write("D2216", command.getTaskMode()).IsSuccess) { break; }
+            }
+            if (command.getTaskNo() != null) {
+                if (!melsecMcNet.Write("D2217", command.getTaskNo()).IsSuccess) { break; }
+            }
+            if (command.getAckFinish() != null) {
+                if (!melsecMcNet.Write("D2218", command.getAckFinish()).IsSuccess) { break; }
+            }
 
-            System.out.println(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
+            log.info("鍫嗗灈鏈哄懡浠や笅鍙慬id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
             OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
 
             return;
@@ -240,13 +269,6 @@
         melsecMcNet.ConnectClose();
     }
 
-    public CrnProtocol getCrnProtocol(){
-        if (crnProtocol == null) {
-            readStatus();
-        }
-        return this.crnProtocol;
-    }
-
 
     /******************************************************************************************/
     /**************************************** 娴嬭瘯涓撶敤 *****************************************/
@@ -262,7 +284,6 @@
         crnThread.connect();
         crnThread.readStatus();
         System.out.println(JSON.toJSONString(crnThread.crnProtocol));
-        Thread.sleep(3000L);
 
         // 1.鍏ュ簱 婧愬拰鐩爣閮藉彂
 //        CrnCommand command = new CrnCommand();
@@ -335,19 +356,19 @@
 //        command.setDestinationPosZ((short) 0);     // 鐩爣搴撲綅灞�
 //        crnThread.write(command);
 
-//        // 7.鍧愭爣绉昏   鐩爣鍙�   pass
-//        CrnCommand command = new CrnCommand();
-//        command.setCrnNo(1); // 鍫嗗灈鏈虹紪鍙�
-//        command.setTaskNo((short) 0); // 宸ヤ綔鍙�
-//        command.setAckFinish((short) 0);  // 浠诲姟瀹屾垚纭浣�
-//        command.setTaskMode(CrnTaskModeType.OFFSET_MOVE); // 浠诲姟妯″紡
-//        command.setSourcePosX((short) 0);     // 婧愬簱浣嶆帓
-//        command.setSourcePosY((short) 0);     // 婧愬簱浣嶅垪
-//        command.setSourcePosZ((short) 0);     // 婧愬簱浣嶅眰
-//        command.setDestinationPosX((short) 2);     // 鐩爣搴撲綅鎺�
-//        command.setDestinationPosY((short) 1);     // 鐩爣搴撲綅鍒�
-//        command.setDestinationPosZ((short) 1);     // 鐩爣搴撲綅灞�
-//        crnThread.write(command);
+        // 7.鍧愭爣绉昏   鐩爣鍙�   pass
+        CrnCommand command = new CrnCommand();
+        command.setCrnNo(1); // 鍫嗗灈鏈虹紪鍙�
+        command.setTaskNo((short) 0); // 宸ヤ綔鍙�
+        command.setAckFinish((short) 0);  // 浠诲姟瀹屾垚纭浣�
+        command.setTaskMode(CrnTaskModeType.OFFSET_MOVE); // 浠诲姟妯″紡
+        command.setSourcePosX((short) 0);     // 婧愬簱浣嶆帓
+        command.setSourcePosY((short) 0);     // 婧愬簱浣嶅垪
+        command.setSourcePosZ((short) 0);     // 婧愬簱浣嶅眰
+        command.setDestinationPosX((short) 1);     // 鐩爣搴撲綅鎺�
+        command.setDestinationPosY((short) 3);     // 鐩爣搴撲綅鍒�
+        command.setDestinationPosZ((short) 3);     // 鐩爣搴撲綅灞�
+        crnThread.write(command);
 
         // 鍙湁鍑虹幇鎸囧畾寮傚父鎵嶈繘琛屽浣�
 //        if (crnThread.crnProtocol.getCrnError2().leftTakeNoneErr

--
Gitblit v1.9.1