From 6db4a007c34ece48ffac0a2b6c2f700d7b15c6e5 Mon Sep 17 00:00:00 2001
From: lsh <lsh>
Date: 星期五, 12 七月 2024 09:01:40 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/thread/SiemensCrnThread.java |  180 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 167 insertions(+), 13 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/SiemensCrnThread.java b/src/main/java/com/zy/core/thread/SiemensCrnThread.java
index b6dea72..0adeb16 100644
--- a/src/main/java/com/zy/core/thread/SiemensCrnThread.java
+++ b/src/main/java/com/zy/core/thread/SiemensCrnThread.java
@@ -19,6 +19,7 @@
 import com.zy.core.model.CrnSlave;
 import com.zy.core.model.Task;
 import com.zy.core.model.command.CrnCommand;
+import com.zy.core.model.cpmmandParam.CrnCommandParam;
 import com.zy.core.model.protocol.CrnProtocol;
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
@@ -48,6 +49,7 @@
      * 鍫嗗灈鏈烘槸鍚﹀湪鍥炲師鐐硅繍鍔ㄤ腑鏍囪
      */
     private boolean backHpFlag = false;
+    private boolean backHpFlagTwo = false;
 
     public SiemensCrnThread(CrnSlave slave) {
         this.slave = slave;
@@ -57,6 +59,7 @@
     @SuppressWarnings("InfiniteLoopStatement")
     public void run() {
         this.connect();
+        sign = System.currentTimeMillis();
         while (isRunning) {
             try {
                 int step = 1;
@@ -112,6 +115,10 @@
                         command2.setDestinationPosY((short)0);     // 鐩爣搴撲綅鍒�
                         command2.setDestinationPosZ((short)0);     // 鐩爣搴撲綅灞�
                         write5(command2);
+                        break;
+                    // 澶嶄綅
+                    case 7:
+                        write9((CrnCommandParam) task.getData());
                         break;
                     default:
                         break;
@@ -252,8 +259,8 @@
 
                 // 澶嶄綅淇″彿
                 if (!Cools.isEmpty(crnProtocol.getStatusType()) && (crnProtocol.getStatusType().equals(CrnStatusType.WAITING) || crnProtocol.getStatusType().equals(CrnStatusType.SEPARATE_WAITING))) {
-                    log.error("-------------------------------------------绗竴姝ャ�乕鍫嗗灈鏈哄彿锛歿}, 宸ヤ綔鍙凤細{}, 杞借揣鍙�:{}]==>> 鐘舵�佷负10锛岀瓑寰呯‘璁わ紒锛�",
-                            slave.getId(),crnProtocol.getTaskNo(), crnProtocol.getLoaded()==1 ? "鏈夌墿" : "鏃犵墿");
+                    log.error("-------------------------宸ヤ綅1-------------------------绗竴姝ャ�乕鍫嗗灈鏈哄彿锛歿}, 宸ヤ綔鍙凤細{}, 杞借揣鍙�:{}]==>> 鐘舵�佷负{}锛岀瓑寰呯‘璁わ紒锛�",
+                            slave.getId(),crnProtocol.getTaskNo(), crnProtocol.getLoaded()==1 ? "鏈夌墿" : "鏃犵墿",crnProtocol.getStatusType());
                     if (resetFlag) {
                         if(crnProtocol.getTaskNo()==9999){
                             backHpFlag = false;
@@ -264,14 +271,20 @@
                             resetFlag = false;
                         }
                     }
+                }
+
+                // 澶嶄綅淇″彿
+                if (!Cools.isEmpty(crnProtocol.getStatusTypeTwo()) && (crnProtocol.getStatusTypeTwo().equals(CrnStatusType.WAITING) || crnProtocol.getStatusTypeTwo().equals(CrnStatusType.SEPARATE_WAITING))) {
+                    log.error("-------------------------宸ヤ綅2-------------------------绗竴姝ャ�乕鍫嗗灈鏈哄彿锛歿}, 宸ヤ綔鍙凤細{}, 杞借揣鍙�:{}]==>> 鐘舵�佷负{}锛岀瓑寰呯‘璁わ紒锛�",
+                            slave.getId(),crnProtocol.getTaskNoTwo(), crnProtocol.getLoadedTwo()==1 ? "鏈夌墿" : "鏃犵墿",crnProtocol.getStatusTypeTwo());
                     if (resetFlagTwo) {
-                        if(crnProtocol.getTaskNo()==9999){
-                            backHpFlag = false;
+                        if(crnProtocol.getTaskNoTwo()==9999){
+                            backHpFlagTwo = false;
                         }
                         CrnCommand crnCommand = new CrnCommand();
                         crnCommand.setAckFinish((short)1);
                         if (write5(crnCommand)) {
-                            resetFlag = false;
+                            resetFlagTwo = false;
                         }
                     }
                 }
@@ -344,7 +357,7 @@
         do {
             try{
                 if(!result.IsSuccess){
-                    log.error("鍐欏叆鍫嗗灈鏈簆lc鏁版嵁澶辫触锛岄噸鏂颁笅鍙戜换鍔�  鍐欏叆鐩存帴澶辫触 ===>> [id:{}],{},[鍐欏叆娆℃暟:{}]", slave.getId(), JSON.toJSON(command),writeCount);
+                    log.error("鍐欏叆鍫嗗灈鏈簆lc鏁版嵁澶辫触锛岄噸鏂颁笅鍙戜换鍔�  鍐欏叆鐩存帴澶辫触 ===>> [id:{}],{},[鍐欏叆娆℃暟:{}]", slave.getId(), JSON.toJSONString(command),writeCount);
                     result = siemensNet.Write("DB100.0", array);
                     Thread.sleep(100);
                     writeCount++;
@@ -367,12 +380,12 @@
                             || !command.getDestinationPosY().equals(one.getDestinationPosY()) || !command.getDestinationPosZ().equals(one.getDestinationPosZ())
                     ){
                         try{
-                            log.error("鍫嗗灈鏈哄懡浠ゅ湴鍧�鍐欏叆鍚庡洖璇诲け璐�==>涓嶄竴鑷碵id:{}] >>>>> 鍐欏叆[{}],===>>鍥炶[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one));
+                            log.error("鍫嗗灈鏈哄懡浠ゅ湴鍧�鍐欏叆鍚庡洖璇诲け璐�==>涓嶄竴鑷碵id:{}] >>>>> 鍐欏叆[{}],===>>鍥炶[{}]", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(one));
                         }catch (Exception e){
                             try{
-                                log.error("鏃ュ織鎵撳嵃澶辫触锛�===>>鍙傛暟one鎶ラ敊 [id:{}],{}", slave.getId(), JSON.toJSON(command),JSON.toJSON(resultRead));
+                                log.error("鏃ュ織鎵撳嵃澶辫触锛�===>>鍙傛暟one鎶ラ敊 [id:{}],{}", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(resultRead));
                             }catch (Exception e1){
-                                log.error("鏃ュ織鎵撳嵃澶辫触锛�===>> [id:{}],{}", slave.getId(), JSON.toJSON(command));
+                                log.error("鏃ュ織鎵撳嵃澶辫触锛�===>> [id:{}],{}", slave.getId(), JSON.toJSONString(command));
                             }
                         }
                         try{
@@ -380,12 +393,12 @@
                         }catch (Exception e){
 
                         }
-                        log.error("鍐欏叆鍫嗗灈鏈簆lc鏁版嵁澶辫触锛岄噸鏂颁笅鍙戜换鍔�  鍥炶涓嶄竴鑷� ===>> [id:{}],{},[鍐欏叆娆℃暟:{}]", slave.getId(), JSON.toJSON(command),writeCount);
+                        log.error("鍐欏叆鍫嗗灈鏈簆lc鏁版嵁澶辫触锛岄噸鏂颁笅鍙戜换鍔�  鍥炶涓嶄竴鑷� ===>> [id:{}],{},[鍐欏叆娆℃暟:{}]", slave.getId(), JSON.toJSONString(command),writeCount);
                         result = siemensNet.Write("DB100.0", array);
                         writeCount++;
                         continue;
                     } else {
-                        log.info("鍫嗗灈鏈哄懡浠ゅ湴鍧�鍐欏叆鍚庡洖璇绘垚鍔焄id:{}] >>>>> 鍐欏叆[{}],===>>鍥炶[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one));
+                        log.info("鍫嗗灈鏈哄懡浠ゅ湴鍧�鍐欏叆鍚庡洖璇绘垚鍔焄id:{}] >>>>> 鍐欏叆[{}],===>>鍥炶[{}]", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(one));
                         break;
                     }
                 }
@@ -442,8 +455,8 @@
         if (result != null && result.IsSuccess) {
             Thread.sleep(200);
             this.readStatus();
-            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)));
+            log.info("鍫嗗灈鏈哄懡浠や笅鍙慬id:{}] >>>>> {}", slave.getId(), JSON.toJSONString(command));
+            OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSONString(command)));
             return true;
         } else {
             OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆戝啓鍏ュ爢鍨涙満plc鏁版嵁澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
@@ -485,6 +498,147 @@
         do {
             try{
                 if(!result.IsSuccess){
+                    log.error("鍐欏叆鍫嗗灈鏈簆lc鏁版嵁澶辫触锛岄噸鏂颁笅鍙戜换鍔�  鍐欏叆鐩存帴澶辫触 ===>> [id:{}],{},[鍐欏叆娆℃暟:{}]", slave.getId(), JSON.toJSONString(command),writeCount);
+                    result = siemensNet.Write("DB100.20", array);
+                    Thread.sleep(100);
+                    writeCount++;
+                    continue;
+                }
+                OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.20", (short) 24);
+                if (resultRead.IsSuccess) {
+                    CrnCommand one = new CrnCommand();
+                    one.setTaskNo(siemensNet.getByteTransform().TransInt16(resultRead.Content, 2));
+                    one.setTaskMode(siemensNet.getByteTransform().TransInt16(resultRead.Content, 4));
+                    one.setSourcePosX(siemensNet.getByteTransform().TransInt16(resultRead.Content, 6));
+                    one.setSourcePosY(siemensNet.getByteTransform().TransInt16(resultRead.Content, 8));
+                    one.setSourcePosZ(siemensNet.getByteTransform().TransInt16(resultRead.Content, 10));
+                    one.setDestinationPosX(siemensNet.getByteTransform().TransInt16(resultRead.Content, 12));
+                    one.setDestinationPosY(siemensNet.getByteTransform().TransInt16(resultRead.Content, 14));
+                    one.setDestinationPosZ(siemensNet.getByteTransform().TransInt16(resultRead.Content, 16));
+                    if (!command.getTaskNo().equals(one.getTaskNo()) || !command.getTaskMode().equals(one.getTaskMode())
+                            || !command.getSourcePosX().equals(one.getSourcePosX()) || !command.getSourcePosY().equals(one.getSourcePosY())
+                            || !command.getSourcePosZ().equals(one.getSourcePosZ()) || !command.getDestinationPosX().equals(one.getDestinationPosX())
+                            || !command.getDestinationPosY().equals(one.getDestinationPosY()) || !command.getDestinationPosZ().equals(one.getDestinationPosZ())
+                    ){
+                        try{
+                            log.error("鍫嗗灈鏈哄懡浠ゅ湴鍧�鍐欏叆鍚庡洖璇诲け璐�==>涓嶄竴鑷碵id:{}] >>>>> 鍐欏叆[{}],===>>鍥炶[{}]", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(one));
+                        }catch (Exception e){
+                            try{
+                                log.error("鏃ュ織鎵撳嵃澶辫触锛�===>>鍙傛暟one鎶ラ敊 [id:{}],{}", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(resultRead));
+                            }catch (Exception e1){
+                                log.error("鏃ュ織鎵撳嵃澶辫触锛�===>> [id:{}],{}", slave.getId(), JSON.toJSONString(command));
+                            }
+                        }
+                        try{
+                            Thread.sleep(100);
+                        }catch (Exception e){
+
+                        }
+                        log.error("鍐欏叆鍫嗗灈鏈簆lc鏁版嵁澶辫触锛岄噸鏂颁笅鍙戜换鍔�  鍥炶涓嶄竴鑷� ===>> [id:{}],{},[鍐欏叆娆℃暟:{}]", slave.getId(), JSON.toJSONString(command),writeCount);
+                        result = siemensNet.Write("DB100.20", array);
+                        writeCount++;
+                        continue;
+                    } else {
+                        log.info("鍫嗗灈鏈哄懡浠ゅ湴鍧�鍐欏叆鍚庡洖璇绘垚鍔焄id:{}] >>>>> 鍐欏叆[{}],===>>鍥炶[{}]", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(one));
+                        break;
+                    }
+                }
+            }catch (Exception e){
+                log.error("鍫嗗灈鏈哄懡浠ゅ湴鍧�鍐欏叆鍚庡洖璇诲嚭閿�,寮傚父锛�"+e);
+            }
+            writeCount++;
+        } while (writeCount<6);
+
+        if (command.getAckFinish() == 0) {
+            short commandFinish = 1;
+            Thread.sleep(100L);
+            result = siemensNet.Write("DB100.38", commandFinish);
+            int signFinish = 1;
+            while (signFinish<5){
+                OperateResultExOne<byte[]> result10018 = siemensNet.Read("DB100.38", (short) 2);
+                short transInt16 = siemensNet.getByteTransform().TransInt16(result10018.Content, 0);
+                if (transInt16 != commandFinish){
+                    log.info("涓嬪彂DB100.38  鍥炶澶辫触" + "commandFinish:"+commandFinish);
+                    log.info("涓嬪彂DB100.38  鍥炶澶辫触" + "array:"+ JSON.toJSONString(array));
+                    result = siemensNet.Write("DB100.38", commandFinish);
+                    signFinish++;
+                }else {
+                    log.info("涓嬪彂DB100.38" + "commandFinish:"+commandFinish);
+                    log.info("涓嬪彂DB100.38" + "array:"+ JSON.toJSONString(array));
+                    break;
+                }
+            }
+        }
+
+        try {
+            // 鏃ュ織璁板綍
+            BasCrnOptService bean = SpringUtils.getBean(BasCrnOptService.class);
+            BasCrnOpt basCrnOpt = new BasCrnOpt(
+                    command.getTaskNo().intValue(),    // 浠诲姟鍙�
+                    command.getCrnNo(),    // 鍫嗗灈鏈篬闈炵┖]
+                    new Date(),    // 涓嬪彂鏃堕棿
+                    command.getTaskModeType().toString(),    // 妯″紡
+                    command.getSourcePosX().intValue(),    // 婧愭帓
+                    command.getSourcePosY().intValue(),    // 婧愬垪
+                    command.getSourcePosZ().intValue(),    // 婧愬眰
+                    null,    // 婧愮珯
+                    command.getDestinationPosX().intValue(),    // 鐩爣鎺�
+                    command.getDestinationPosY().intValue(),    // 鐩爣鍒�
+                    command.getDestinationPosZ().intValue(),    // 鐩爣灞�
+                    null,    // 鐩爣绔�
+                    null,    // 鍝嶅簲缁撴灉
+                    null,    // 淇敼鏃堕棿
+                    null    // 淇敼浜哄憳
+            );
+            bean.insert(basCrnOpt);
+        } catch (Exception ignore) {}
+
+        if (result != null && result.IsSuccess) {
+            Thread.sleep(200);
+            this.readStatus();
+            log.info("鍫嗗灈鏈哄懡浠や笅鍙慬id:{}] >>>>> {}", slave.getId(), JSON.toJSONString(command));
+            OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSONString(command)));
+            return true;
+        } else {
+            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 false;
+        }
+    }
+
+    /**
+     * 鍐欏叆鏁版嵁  宸ヤ綅2
+     */
+    private boolean write9(CrnCommandParam command) throws InterruptedException {
+        if (null == command) {
+            log.error("鍫嗗灈鏈哄啓鍏ュ懡浠や负绌�");
+            return false;
+        }
+//        convertRow(command);
+        command.setCrnNo(slave.getId());
+        short[] array = new short[10];
+        array[0] = command.getAckFinish();
+        array[1] = command.getTaskNo();
+        array[2] = command.getTaskMode();
+        array[3] = command.getSourcePosX();
+        array[4] = command.getSourcePosY();
+        array[5] = command.getSourcePosZ();
+        array[6] = command.getDestinationPosX();
+        array[7] = command.getDestinationPosY();
+        array[8] = command.getDestinationPosZ();
+//        array[9] = command.getSourceStaNo();
+//        array[10] = command.getDestinationStaNo();
+        array[9] = command.getCommand();
+        OperateResult result18 = siemensNet.Write("DB100.38", (short)0);
+        OperateResult result = siemensNet.Write("DB100.20", array);
+        log.info("鍫嗗灈鏈哄懡浠や笅鍙慬id:{}] >>>>> {}", slave.getId(), array);
+
+        //鍫嗗灈鏈轰换鍔″啓鍏ュ悗锛屽洖璇讳竴娆★紝鐪嬫槸鍚︽垚鍔�
+        Thread.sleep(200);
+        int writeCount = 1;
+        do {
+            try{
+                if(!result.IsSuccess){
                     log.error("鍐欏叆鍫嗗灈鏈簆lc鏁版嵁澶辫触锛岄噸鏂颁笅鍙戜换鍔�  鍐欏叆鐩存帴澶辫触 ===>> [id:{}],{},[鍐欏叆娆℃暟:{}]", slave.getId(), JSON.toJSON(command),writeCount);
                     result = siemensNet.Write("DB100.20", array);
                     Thread.sleep(100);

--
Gitblit v1.9.1