From 475447f26fc2f4c534236e65f6f947125462d643 Mon Sep 17 00:00:00 2001
From: lty <876263681@qq.com>
Date: 星期二, 25 三月 2025 11:59:02 +0800
Subject: [PATCH] #堆垛机写入优化

---
 src/main/java/com/zy/core/thread/SiemensCrnThread.java |  226 ++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 154 insertions(+), 72 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/SiemensCrnThread.java b/src/main/java/com/zy/core/thread/SiemensCrnThread.java
index 1ae702e..cb14968 100644
--- a/src/main/java/com/zy/core/thread/SiemensCrnThread.java
+++ b/src/main/java/com/zy/core/thread/SiemensCrnThread.java
@@ -83,7 +83,7 @@
                         break;
                     // 鍐欏叆鏁版嵁
                     case 2:
-                        write2((CrnCommand) task.getData());
+                        write5((CrnCommand) task.getData());
                         break;
                     // 澶嶄綅
                     case 3:
@@ -154,7 +154,7 @@
 //            deviceErrorService.addDeviceError("crn", slave.getId(), "鍫嗗灈鏈簆lc杩炴帴澶辫触");
             initCrnProtocol();
         }
-        siemensNet.ConnectClose();
+//        siemensNet.ConnectClose();
         return result;
     }
 
@@ -227,7 +227,7 @@
                         taskOverToWms.setTaskNo(taskWrk.getTaskNo()); //浠诲姟鍙�
                         taskOverToWms.setTaskType(tasktype); // 浠诲姟绫诲瀷
                         taskOverToWms.setContainerCode(taskWrk.getBarcode()); // 瀹瑰櫒缂栫爜
-                        if (taskWrk.getIoType() ==1 || taskWrk.getIoType() == 3){
+                        if (taskWrk.getIoType() ==1){
                             taskOverToWms.setEquipmentCode(String.valueOf(taskWrk.getCrnNo())); //璁惧缂栫爜
                             taskOverToWms.setTargetLocationCode(taskWrk.getOriginTargetPoint()); //鐩爣搴撲綅
                         }else if (taskWrk.getIoType() ==2){
@@ -242,6 +242,10 @@
                             map1.put(405,"G-1101");
                             taskOverToWms.setEquipmentCode(map1.get(taskWrk.getTargetPoint())); //璁惧缂栫爜
                             taskOverToWms.setSourceLocationCode(taskWrk.getOriginStartPoint()); //婧愬簱浣�
+                        } else if (taskWrk.getIoType() == 3) {
+                            taskOverToWms.setEquipmentCode(String.valueOf(taskWrk.getCrnNo())); //璁惧缂栫爜
+                            taskOverToWms.setSourceLocationCode(taskWrk.getOriginStartPoint());
+                            taskOverToWms.setTargetLocationCode(taskWrk.getOriginTargetPoint()); //鐩爣搴撲綅
                         }
                         taskOverToWms.setTaskStatus("location_done"); //浠诲姟鐘舵��
                         String response = null;
@@ -316,51 +320,41 @@
             log.error("鍫嗗灈鏈哄啓鍏ュ懡浠や负绌�");
             return false;
         }
+        //杞崲鍓嶅厠闅嗕竴浠�
+        CrnCommand crnCommand = new CrnCommand();
+        crnCommand.setCrnNo(command.getCrnNo());
+        crnCommand.setAckFinish(command.getAckFinish());
+        crnCommand.setTaskNo(command.getTaskNo());
+        crnCommand.setTaskMode(command.getTaskMode());
+        crnCommand.setSourcePosX(command.getSourcePosX());
+        crnCommand.setSourcePosY(command.getSourcePosY());
+        crnCommand.setSourcePosZ(command.getSourcePosZ());
+        crnCommand.setDestinationPosX(command.getDestinationPosX());
+        crnCommand.setDestinationPosY(command.getDestinationPosY());
+        crnCommand.setDestinationPosZ(command.getDestinationPosZ());
+//        crnCommand.setSourceStaNo(command.getSourceStaNo());
+//        crnCommand.setDestinationStaNo(command.getDestinationStaNo());
+        crnCommand.setCommand(command.getCommand());
         if (command.getTaskNo() == 0 && command.getAckFinish() == 0 && command.getAuto() != 1) {
-            command.setTaskNo((short) 9999);
+            command.setTaskNo((short) 999);
         }
 
         command.setCrnNo(slave.getId());
         short[] array = new short[10];
-        if (Cools.isEmpty(command.getAckFinish())) {
-            array[0] = 5;
-        } else {
-            array[0] = command.getAckFinish();
-        }
+        array[0] = command.getAckFinish();
         array[1] = command.getTaskNo();
         array[2] = command.getTaskMode();
-        array[3] = command.getSourcePosZ();
-        array[4] = command.getSourcePosX();
-        array[5] = command.getSourcePosY();
-        array[6] = command.getDestinationPosZ();
-        array[7] = command.getDestinationPosX();
-        array[8] = command.getDestinationPosY();
+        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.getCommand();
 
         // 浣滀笟淇℃伅
         OperateResult result = siemensNet.Write("DB100.0", array);
 
-        if (command.getAckFinish() != 1) {
-            BasCrnOptService bean = SpringUtils.getBean(BasCrnOptService.class);
-            BasCrnOpt basCrnOpt = new BasCrnOpt(
-                    command.getTaskNo().intValue(),    // 浠诲姟鍙�
-                    command.getCrnNo(),    // 鍫嗗灈鏈篬闈炵┖]
-                    new Date(),    // 涓嬪彂鏃堕棿
-                    command.getTaskModeType() == null ? null : 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);
-        }
 
         if (result.IsSuccess) {
             log.warn("鍫嗗灈鏈哄懡浠や笅鍙慬id:{},鏃堕棿锛歿}] >>>>> {}", slave.getId(), DateUtils.convert(new Date(), DateUtils.yyyyMMddHHmmsssss_F), JSON.toJSON(command));
@@ -376,13 +370,128 @@
     /**
      * 鍐欏叆鏁版嵁
      */
+    private synchronized boolean write5(CrnCommand command) throws InterruptedException {
+        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
+
+        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 result = siemensNet.Write("DB100.0", array);
+
+        int i=0;
+//        if(result.IsSuccess){
+//            log.info("鍫嗗灈鏈哄懡浠ゅ湴鍧�鍐欏叆鎴愬姛[id:{}]>>>>鍐欏叆[{}]", slave.getId(),JSON.toJSON(command));
+//        }else{
+//            log.error("鍫嗗灈鏈哄懡浠ゅ湴鍧�鍐欏叆澶辫触锛侊紒锛乕id:{}]>>>>鍐欏叆[{}]", slave.getId(),JSON.toJSON(command));
+//        }
+        do{
+            //鍫嗗灈鏈轰换鍔″啓鍏ュ悗锛屽洖璇讳竴娆★紝鐪嬫槸鍚︽垚鍔�
+            Thread.sleep(300);
+            OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 20);
+            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())
+                ){
+                    i++;
+                    log.error("鍫嗗灈鏈哄懡浠ゅ湴鍧�鍐欏叆鍚庡洖璇诲け璐id:{}]>>>>閲嶅啓[{}] >>>>> 鍐欏叆[{}],===>>鍥炶[{}]", slave.getId(), i,JSON.toJSON(command),JSON.toJSON(one));
+                    result = siemensNet.Write("DB100.0", array);
+                }else{
+                    result = siemensNet.Write("DB100.18", (short) 1);
+                    i=5;
+                }
+            }
+        }while(i<5);
+
+
+        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(300);
+            this.readStatus();
+            log.info(methodName + ":鍫嗗灈鏈哄懡浠や笅鍙慬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 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(methodName + ":鍐欏叆鍫嗗灈鏈簆lc鏁版嵁澶辫触 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+            return false;
+        }
+    }
+
+    /**
+     * 鍐欏叆鏁版嵁
+     */
     private boolean write2(CrnCommand command){
         if (null == command) {
             log.error("鍫嗗灈鏈哄啓鍏ュ懡浠や负绌�");
             return false;
         }
+        //杞崲鍓嶅厠闅嗕竴浠�
+        CrnCommand crnCommand = new CrnCommand();
+        crnCommand.setCrnNo(command.getCrnNo());
+        crnCommand.setAckFinish(command.getAckFinish());
+        crnCommand.setTaskNo(command.getTaskNo());
+        crnCommand.setTaskMode(command.getTaskMode());
+        crnCommand.setSourcePosX(command.getSourcePosX());
+        crnCommand.setSourcePosY(command.getSourcePosY());
+        crnCommand.setSourcePosZ(command.getSourcePosZ());
+        crnCommand.setDestinationPosX(command.getDestinationPosX());
+        crnCommand.setDestinationPosY(command.getDestinationPosY());
+        crnCommand.setDestinationPosZ(command.getDestinationPosZ());
+//        crnCommand.setSourceStaNo(command.getSourceStaNo());
+//        crnCommand.setDestinationStaNo(command.getDestinationStaNo());
+        crnCommand.setCommand(command.getCommand());
         if (command.getTaskNo() == 0 && command.getAckFinish() == 0 && command.getAuto() != 1) {
-            command.setTaskNo((short) 9999);
+            command.setTaskNo((short) 999);
         }
 
         command.setCrnNo(slave.getId());
@@ -394,17 +503,16 @@
         }
         array[1] = command.getTaskNo();
         array[2] = command.getTaskMode();
-        array[3] = command.getSourcePosZ();
-        array[4] = command.getSourcePosX();
-        array[5] = command.getSourcePosY();
-        array[6] = command.getDestinationPosZ();
-        array[7] = command.getDestinationPosX();
-        array[8] = command.getDestinationPosY();
+        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.getCommand();
         // 浣滀笟淇℃伅
         OperateResult result = siemensNet.Write("DB100.0", array);
         // 鏃ュ織璁板綍
-//        try {
             if (command.getAckFinish() != 1) {
                 BasCrnOptService bean = SpringUtils.getBean(BasCrnOptService.class);
                 BasCrnOpt basCrnOpt = new BasCrnOpt(
@@ -439,30 +547,8 @@
         }
         CommandInfoLog commandInfoLog = JSON.parseObject(JSON.toJSONString(commandInfo), CommandInfoLog.class);
         commandInfoLog.setId(null);
-//        CommandInfoLog commandInfoLog = new CommandInfoLog();
-//        if (commandInfo != null) {
-//            commandInfoLog = JSON.parseObject(JSON.toJSONString(commandInfo), CommandInfoLog.class);
-//            commandInfoLog.setId(null);
-//        }else {
-//            Date now = new Date();
-//            commandInfoLog.setCommand(JSON.toJSONString(command));
-//            commandInfoLog.setCommandStatus(3);
-//            commandInfoLog.setStartTime(now);
-//            commandInfoLog.setExecuteTime(now);
-//            commandInfoLog.setCompleteTime(now);
-//            commandInfoLog.setDevice(SlaveType.Devp.toString());
-//            commandInfoLog.setWrkNo(9999);
-//            commandInfoLog.setTaskNo("9999");
-//            commandInfoLog.setCommandDesc("鎵嬪姩鍛戒护");
-//        }
-
-
         if (result.IsSuccess) {
-            log.warn("鍫嗗灈鏈哄懡浠や笅鍙慬id:{},鏃堕棿锛歿}] >>>>> {}", slave.getId(), DateUtils.convert(new Date(), DateUtils.yyyyMMddHHmmsssss_F), JSON.toJSON(command));
-            OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
-
             Date now = new Date();
-
             //鏇存柊鎸囦护鏃ュ織
             commandInfoLog.setDeviceLog("鎸囦护涓嬪彂鎴愬姛");
             commandInfoLogService.insert(commandInfoLog);
@@ -483,16 +569,12 @@
                 taskWrkService.updateById(taskWrk);
             }
 
-            return true;
+
         } else {
             OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆戝啓鍏ュ爢鍨涙満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("鍐欏叆鍫嗗灈鏈簆lc鏁版嵁澶辫触 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
-
-            //鏇存柊鎸囦护鏃ュ織
-            commandInfoLog.setDeviceLog("鎸囦护涓嬪彂澶辫触");
-            commandInfoLogService.insert(commandInfoLog);
-            return false;
         }
+        return true;
     }
 
     private boolean writeAuto(CrnCommand command){

--
Gitblit v1.9.1