From cc720a16549bbb691344afb31f702530588c75fd Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期二, 24 三月 2026 00:03:06 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/network/real/ZyStationV4RealConnect.java |  105 ++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 76 insertions(+), 29 deletions(-)

diff --git a/src/main/java/com/zy/core/network/real/ZyStationV4RealConnect.java b/src/main/java/com/zy/core/network/real/ZyStationV4RealConnect.java
index 010a031..b5a45d8 100644
--- a/src/main/java/com/zy/core/network/real/ZyStationV4RealConnect.java
+++ b/src/main/java/com/zy/core/network/real/ZyStationV4RealConnect.java
@@ -18,11 +18,13 @@
 import com.zy.core.model.CommandResponse;
 import com.zy.core.model.StationObjModel;
 import com.zy.core.model.command.StationCommand;
+import com.zy.core.model.protocol.StationTaskBufferItem;
 import com.zy.core.network.api.ZyStationConnectApi;
 import com.zy.core.network.entity.ZyStationStatusEntity;
 import lombok.extern.slf4j.Slf4j;
 
 import java.text.MessageFormat;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Date;
@@ -33,6 +35,10 @@
  */
 @Slf4j
 public class ZyStationV4RealConnect implements ZyStationConnectApi {
+
+    private static final int TASK_AREA_LENGTH = 48;
+    private static final int TASK_AREA_SLOT_SIZE = 12;
+    private static final int TASK_AREA_SLOT_COUNT = 3;
 
     private List<ZyStationStatusEntity> statusList;
     private List<StationObjModel> barcodeOriginList;
@@ -101,6 +107,7 @@
 
         OperateResultExOne<byte[]> result = siemensNet.Read("DB100.0", (short) (statusList.size() * 10));
         if (result.IsSuccess) {
+            byte[] taskBufferRaw = readTaskBufferRaw();
             for (int i = 0; i < statusList.size(); i++) {
                 ZyStationStatusEntity statusEntity = statusList.get(i); // 绔欑偣缂栧彿
                 statusEntity.setTaskNo(siemensNet.getByteTransform().TransInt32(result.Content, i * 10)); // 宸ヤ綔鍙�
@@ -133,6 +140,7 @@
                 statusEntity.setError(0);//榛樿鏃犳姤璀�
 
                 statusEntity.setTaskWriteIdx((int) siemensNet.getByteTransform().TransInt16(result.Content, i * 10 + 8));//浠诲姟鍙啓鍖�
+                fillTaskBufferStatus(taskBufferRaw, i, statusEntity);
             }
         }
 
@@ -221,19 +229,30 @@
             return commandResponse;
         }
 
-        int taskWriteIdx = getTaskWriteIdx(command.getStationId());
-        if (taskWriteIdx == -1) {
+        getStatus(deviceNo);
+
+        int stationIdx = findIndex(command.getStationId());
+        if (stationIdx == -1) {
+            commandResponse.setMessage("鍛戒护涓嬪彂澶辫触锛屾湭鎵惧埌绔欑偣鐘舵��");
+            return commandResponse;
+        }
+
+        ZyStationStatusEntity statusEntity = statusList.get(stationIdx);
+        Integer taskWriteIdx = statusEntity == null ? null : statusEntity.getTaskWriteIdx();
+        if (taskWriteIdx == null || taskWriteIdx <= 0) {
+            commandResponse.setMessage("鍛戒护涓嬪彂澶辫触锛屽綋鍓嶆棤鍙敤浠诲姟鍐欏叆鍖�");
+            return commandResponse;
+        }
+
+        int useTaskWriteIdx = getTaskWriteIdx(stationIdx, taskWriteIdx);
+        if (useTaskWriteIdx == -1) {
             commandResponse.setMessage("鍛戒护涓嬪彂瓒呮椂锛屾棤娉曟壘鍒板彲鐢ㄤ笅鍙戝尯鍩�");
             return commandResponse;
         }
 
-        int stationIdx = findIndex(command.getStationId());
+        int taskBaseOffset = stationIdx * TASK_AREA_LENGTH + (useTaskWriteIdx * TASK_AREA_SLOT_SIZE);
 
-        short[] data = new short[2];
-        data[0] = (short) 0;
-        data[1] = command.getTargetStaNo().shortValue();
-
-        OperateResult writeTaskNo = siemensNet.Write("DB13." + (stationIdx * 48 + (taskWriteIdx * 12)), command.getTaskNo());
+        OperateResult writeTaskNo = siemensNet.Write("DB13." + taskBaseOffset, command.getTaskNo());
         if (!writeTaskNo.IsSuccess) {
             log.error("鍐欏叆杈撻�佺嚎鍛戒护澶辫触銆傜珯鐐圭紪鍙�={}锛岀珯鐐规暟鎹�={}", command.getTaskNo(), JSON.toJSON(command));
             commandResponse.setResult(false);
@@ -241,11 +260,11 @@
             return commandResponse;
         }
 
-        OperateResult writeData = siemensNet.Write("DB13." + (stationIdx * 48 + (taskWriteIdx * 12 + 4)), data);
-        if (!writeData.IsSuccess) {
+        OperateResult writeTarget = siemensNet.Write("DB13." + (taskBaseOffset + 6), command.getTargetStaNo().shortValue());
+        if (!writeTarget.IsSuccess) {
             log.error("鍐欏叆杈撻�佺嚎鍛戒护澶辫触銆傜珯鐐圭紪鍙�={}锛岀珯鐐规暟鎹�={}", command.getTaskNo(), JSON.toJSON(command));
             commandResponse.setResult(false);
-            commandResponse.setMessage("鍛戒护涓嬪彂澶辫触锛屽啓鍏ユ暟鎹尯鍩熷け璐�");
+            commandResponse.setMessage("鍛戒护涓嬪彂澶辫触锛屽啓鍏ョ洰鏍囪澶囧彿澶辫触");
             return commandResponse;
         }
 
@@ -299,30 +318,58 @@
         return null;
     }
 
-    private int getTaskWriteIdx(int stationId) {
+    private int getTaskWriteIdx(int stationIdx, Integer taskWriteIdx) {
         int useIdx = -1;
-
-        int stationIdx = findIndex(stationId);
-        if (stationIdx != -1) {
-            ZyStationStatusEntity statusEntity = statusList.get(stationIdx);
-
-            Integer taskWriteIdx = statusEntity.getTaskWriteIdx();
-            if (taskWriteIdx > 0) {
-                OperateResultExOne<byte[]> resultTask = siemensNet.Read("DB13." + (stationId * 48), (short) 48);
-                if (resultTask.IsSuccess) {
-                    int taskNo = siemensNet.getByteTransform().TransInt32(resultTask.Content, taskWriteIdx * 12);
-                    int startPoint = siemensNet.getByteTransform().TransInt16(resultTask.Content, taskWriteIdx * 12 + 4);
-                    int targetPoint = siemensNet.getByteTransform().TransInt16(resultTask.Content, taskWriteIdx * 12 + 6);
-                    if (taskNo == 0 && startPoint == 0 && targetPoint == 0) {
-                        useIdx = taskWriteIdx;
-                    }
-                }
+        if (stationIdx < 0 || taskWriteIdx == null || taskWriteIdx <= 0) {
+            return useIdx;
+        }
+        OperateResultExOne<byte[]> resultTask = siemensNet.Read("DB13." + (stationIdx * TASK_AREA_LENGTH), (short) TASK_AREA_LENGTH);
+        if (resultTask.IsSuccess) {
+            int taskNo = siemensNet.getByteTransform().TransInt32(resultTask.Content, taskWriteIdx * TASK_AREA_SLOT_SIZE);
+            int targetPoint = siemensNet.getByteTransform().TransInt16(resultTask.Content, taskWriteIdx * TASK_AREA_SLOT_SIZE + 6);
+            if (taskNo == 0 && targetPoint == 0) {
+                useIdx = taskWriteIdx;
             }
         }
-
         return useIdx;
     }
 
+    private byte[] readTaskBufferRaw() {
+        int totalLength = statusList == null ? 0 : statusList.size() * TASK_AREA_LENGTH;
+        if (totalLength <= 0) {
+            return null;
+        }
+        OperateResultExOne<byte[]> resultTask = siemensNet.Read("DB13.0", (short) totalLength);
+        if (!resultTask.IsSuccess || resultTask.Content == null || resultTask.Content.length < totalLength) {
+            return null;
+        }
+        return resultTask.Content;
+    }
+
+    private void fillTaskBufferStatus(byte[] taskBufferRaw, int stationIdx, ZyStationStatusEntity statusEntity) {
+        if (statusEntity == null || stationIdx < 0 || taskBufferRaw == null) {
+            statusEntity.setTaskBufferItems(new ArrayList<>());
+            return;
+        }
+        List<StationTaskBufferItem> itemList = new ArrayList<>();
+        int stationOffset = stationIdx * TASK_AREA_LENGTH;
+        if (stationOffset + TASK_AREA_LENGTH > taskBufferRaw.length) {
+            statusEntity.setTaskBufferItems(itemList);
+            return;
+        }
+        for (int slotIdx = 1; slotIdx <= TASK_AREA_SLOT_COUNT; slotIdx++) {
+            int offset = stationOffset + (slotIdx * TASK_AREA_SLOT_SIZE);
+            int taskNo = siemensNet.getByteTransform().TransInt32(taskBufferRaw, offset);
+            int targetPoint = siemensNet.getByteTransform().TransInt16(taskBufferRaw, offset + 6);
+            StationTaskBufferItem item = new StationTaskBufferItem();
+            item.setSlotIdx(slotIdx);
+            item.setTaskNo(taskNo);
+            item.setTargetStaNo(targetPoint);
+            itemList.add(item);
+        }
+        statusEntity.setTaskBufferItems(itemList);
+    }
+
     private int findIndex(Integer stationId) {
         for (int i = 0; i < statusList.size(); i++) {
             ZyStationStatusEntity statusEntity = statusList.get(i);

--
Gitblit v1.9.1