From 9ea4fda8bd24942bedd72de72c2983cbaed6af65 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期二, 31 三月 2026 17:08:51 +0800
Subject: [PATCH] #出入库模式

---
 src/main/java/com/zy/core/network/real/ZyStationV4RealConnect.java |  294 +++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 258 insertions(+), 36 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 d65f7e4..482eea7 100644
--- a/src/main/java/com/zy/core/network/real/ZyStationV4RealConnect.java
+++ b/src/main/java/com/zy/core/network/real/ZyStationV4RealConnect.java
@@ -6,7 +6,7 @@
 import HslCommunication.Profinet.Siemens.SiemensS7Net;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
-import com.baomidou.mybatisplus.mapper.EntityWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.core.common.DateUtils;
 import com.core.common.SpringUtils;
 import com.zy.asrs.entity.BasDevp;
@@ -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;
@@ -34,8 +36,40 @@
 @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 static final String[] CONVEYOR_COMMON_ALARM_MESSAGES = {
+            "鎬ュ仠",
+            "浣庝綅鏂矾鍣ㄦ柇寮�",
+            "浣庝綅鍙橀鍣ㄦ晠闅�",
+            "杈撻�佽繍琛岃秴鏃�",
+            "椤跺崌杩愯瓒呮椂",
+            "鐢宠浠诲姟瓒呮椂",
+            "鎵樼洏绐佸嚭鎶ヨ",
+            "浠诲姟杩愯瓒呮椂",
+            "FWD鏃嬭浆闄愪綅鎶ヨ",
+            "REV鏃嬭浆闄愪綅鎶ヨ",
+            "鎵爜妫�楠屽紓甯�",
+            "瀹夊叏闂ㄦ墦寮�鎶ヨ",
+            "浠诲姟閲嶅鎶ヨ",
+            "鍏ョ珯杩囩▼涓墠缃潯浠跺紓甯�",
+            "鍏ョ珯鍓嶇疆鏉′欢绛夊緟瓒呮椂",
+            "鍑虹珯杩囩▼涓墠缃潯浠跺紓甯�",
+            "鍑虹珯鍓嶇疆鏉′欢绛夊緟瓒呮椂",
+            "楂樹綅鏂矾鍣ㄦ柇寮�",
+            "楂樹綅鍙橀鍣ㄦ晠闅�",
+            "椤跺崌闄愪綅淇″彿寮傚父",
+            "鍏ョ珯杩愯涓紝涓婄珯鍚屾杩愯璇锋眰娑堝け",
+            "鍑虹珯瀹屾垚鍚庯紝浠嶇劧妫�娴嬪埌鎵樼洏",
+            "鍏ョ珯瀹屾垚鍚庯紝娌℃湁妫�娴嬪埌鎵樼洏",
+            "涓嬬珯绔欏彿寮傚父",
+            "璺緞鏌ヨ寮傚父"
+    };
+
     private List<ZyStationStatusEntity> statusList;
     private List<StationObjModel> barcodeOriginList;
+    private List<StationObjModel> inStationOriginList;
     private SiemensS7Net siemensNet;
     private DeviceConfig deviceConfig;
     private RedisUtil redisUtil;
@@ -83,7 +117,7 @@
             }
 
             BasDevp basDevp = basDevpService
-                    .selectOne(new EntityWrapper<BasDevp>().eq("devp_no", deviceConfig.getDeviceNo()));
+                    .getOne(new QueryWrapper<BasDevp>().eq("devp_no", deviceConfig.getDeviceNo()));
             if (basDevp == null) {
                 return Collections.emptyList();
             }
@@ -93,6 +127,7 @@
                 statusList.sort(Comparator.comparing(ZyStationStatusEntity::getStationId));
             }
             barcodeOriginList = basDevp.getBarcodeStationList$();
+            inStationOriginList = sortStationObjModelsByStationId(basDevp.getInStationList$());
         }
 
         if (siemensNet == null) {
@@ -101,6 +136,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)); // 宸ヤ綔鍙�
@@ -131,8 +167,10 @@
                 statusEntity.setPalletHeight(palletHeight);//楂樹綆淇″彿
 
                 statusEntity.setError(0);//榛樿鏃犳姤璀�
+                statusEntity.setErrorMsg("");
 
                 statusEntity.setTaskWriteIdx((int) siemensNet.getByteTransform().TransInt16(result.Content, i * 10 + 8));//浠诲姟鍙啓鍖�
+                fillTaskBufferStatus(taskBufferRaw, i, statusEntity);
             }
         }
 
@@ -163,7 +201,7 @@
             }
         }
 
-        // 鎶ヨ淇℃伅
+        // 鏉$爜绔欐姤璀︿俊鎭�
         OperateResultExOne<byte[]> result4 = siemensNet.Read("DB103.2", (short) (barcodeOriginList.size() * 2));
         if (result4.IsSuccess) {
             for (int i = 0; i < barcodeOriginList.size(); i++) {
@@ -201,12 +239,35 @@
                     sb.append("鎵爜寮傚父;");
                 }
 
-                if(sb.length() > 0) {
-                    barcodeEntity.setError(1);
-                }else {
-                    barcodeEntity.setError(0);
+                applyAlarmMessage(barcodeEntity, sb.toString());
+            }
+        }
+
+        // 杈撻�佺珯鐐归�氱敤鎶ヨ淇℃伅
+        OperateResultExOne<byte[]> result5 = siemensNet.Read("DB25.0", (short) (statusList.size() * 4));
+        if (result5.IsSuccess) {
+            for (int i = 0; i < statusList.size(); i++) {
+                ZyStationStatusEntity statusEntity = statusList.get(i); // 绔欑偣缂栧彿
+                boolean[] status1 = siemensNet.getByteTransform().TransBool(result5.Content, i * 4, 1);
+                boolean[] status2 = siemensNet.getByteTransform().TransBool(result5.Content, i * 4 + 1, 1);
+                boolean[] status3 = siemensNet.getByteTransform().TransBool(result5.Content, i * 4 + 2, 1);
+                boolean[] status4 = siemensNet.getByteTransform().TransBool(result5.Content, i * 4 + 3, 1);
+
+                String alarmMsg = buildConveyorCommonAlarmMessage(status1, status2, status3, status4);
+                applyAlarmMessage(statusEntity, mergeAlarmMessages(statusEntity.getErrorMsg(), alarmMsg));
+            }
+        }
+
+        // 鍑哄叆搴撴ā寮�
+        OperateResultExOne<byte[]> result6 = siemensNet.Read("DB167.2", (short) (inStationOriginList.size() * 2));
+        if (result6.IsSuccess) {
+            for (int i = 0; i < inStationOriginList.size(); i++) {
+                ZyStationStatusEntity inStationEntity = findStatusEntityByInStationIdx(i);
+                if (inStationEntity == null) {
+                    continue;
                 }
-                barcodeEntity.setErrorMsg(sb.toString());
+                Integer ioMode = normalizeIoMode((int) siemensNet.getByteTransform().TransInt16(result6.Content, i * 2));
+                inStationEntity.setIoMode(ioMode);
             }
         }
 
@@ -221,19 +282,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,16 +313,54 @@
             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;
         }
 
         log.info("鍐欏叆杈撻�佺嚎鍛戒护鎴愬姛銆備换鍔″彿={}锛岀珯鐐规暟鎹�={}", command.getTaskNo(), JSON.toJSON(command));
         commandResponse.setResult(true);
+        return commandResponse;
+    }
+
+    @Override
+    public CommandResponse clearTaskBufferSlot(Integer deviceNo, Integer stationId, Integer slotIdx) {
+        CommandResponse commandResponse = new CommandResponse(false);
+        if (stationId == null) {
+            commandResponse.setMessage("绔欑偣鍙蜂负绌�");
+            return commandResponse;
+        }
+        if (slotIdx == null || slotIdx <= 0 || slotIdx > TASK_AREA_SLOT_COUNT) {
+            commandResponse.setMessage("缂撳瓨鍖烘Ы浣嶆棤鏁�");
+            return commandResponse;
+        }
+
+        getStatus(deviceNo);
+
+        int stationIdx = findIndex(stationId);
+        if (stationIdx < 0) {
+            commandResponse.setMessage("鏈壘鍒扮珯鐐圭姸鎬�");
+            return commandResponse;
+        }
+
+        int slotBaseOffset = stationIdx * TASK_AREA_LENGTH + slotIdx * TASK_AREA_SLOT_SIZE;
+        CommandResponse clearTaskNo = sendOriginCommand("DB13." + slotBaseOffset, new short[]{0, 0});
+        if (clearTaskNo == null || !Boolean.TRUE.equals(clearTaskNo.getResult())) {
+            commandResponse.setMessage(clearTaskNo == null ? "娓呯┖浠诲姟鍙峰け璐�" : clearTaskNo.getMessage());
+            return commandResponse;
+        }
+
+        CommandResponse clearTarget = sendOriginCommand("DB13." + (slotBaseOffset + 6), new short[]{0});
+        if (clearTarget == null || !Boolean.TRUE.equals(clearTarget.getResult())) {
+            commandResponse.setMessage(clearTarget == null ? "娓呯┖鐩爣绔欏け璐�" : clearTarget.getMessage());
+            return commandResponse;
+        }
+
+        commandResponse.setResult(true);
+        commandResponse.setMessage("缂撳瓨鍖烘Ы浣嶆竻鐞嗘垚鍔�");
         return commandResponse;
     }
 
@@ -299,28 +409,67 @@
         return null;
     }
 
-    private int getTaskWriteIdx(int stationId) {
-        int useIdx = -1;
+    private ZyStationStatusEntity findStatusEntityByInStationIdx(Integer idx) {
+        Integer stationId = findInStationIdByOrder(inStationOriginList, idx);
 
-        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;
-                    }
-                }
+        for (ZyStationStatusEntity zyStationStatusEntity : statusList) {
+            if(zyStationStatusEntity.getStationId().equals(stationId)) {
+                return zyStationStatusEntity;
             }
         }
+        return null;
+    }
 
+    private int getTaskWriteIdx(int stationIdx, Integer taskWriteIdx) {
+        int useIdx = -1;
+        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) {
@@ -333,4 +482,77 @@
         return -1;
     }
 
+    static String buildConveyorCommonAlarmMessage(boolean[] status1, boolean[] status2, boolean[] status3, boolean[] status4) {
+        StringBuilder sb = new StringBuilder();
+        appendConveyorCommonAlarmMessages(sb, status1, 0);
+        appendConveyorCommonAlarmMessages(sb, status2, 8);
+        appendConveyorCommonAlarmMessages(sb, status3, 16);
+        appendConveyorCommonAlarmMessages(sb, status4, 24);
+        return sb.toString();
+    }
+
+    static String mergeAlarmMessages(String currentMsg, String appendMsg) {
+        String current = currentMsg == null ? "" : currentMsg;
+        String append = appendMsg == null ? "" : appendMsg;
+        if (append.isEmpty()) {
+            return current;
+        }
+        if (current.isEmpty()) {
+            return append;
+        }
+        if (!current.endsWith(";")) {
+            current += ";";
+        }
+        return current + append;
+    }
+
+    static Integer normalizeIoMode(Integer rawMode) {
+        if (rawMode == null) {
+            return null;
+        }
+        if (rawMode == 1 || rawMode == 2) {
+            return rawMode;
+        }
+        return null;
+    }
+
+    static Integer findInStationIdByOrder(List<StationObjModel> inStationOriginList, Integer orderIdx) {
+        if (inStationOriginList == null || orderIdx == null || orderIdx < 0 || orderIdx >= inStationOriginList.size()) {
+            return null;
+        }
+        StationObjModel stationObjModel = inStationOriginList.get(orderIdx);
+        return stationObjModel == null ? null : stationObjModel.getStationId();
+    }
+
+    static List<StationObjModel> sortStationObjModelsByStationId(List<StationObjModel> stationObjModels) {
+        if (stationObjModels == null) {
+            return Collections.emptyList();
+        }
+        stationObjModels.sort(Comparator.comparing(StationObjModel::getStationId, Comparator.nullsLast(Integer::compareTo)));
+        return stationObjModels;
+    }
+
+    private static void appendConveyorCommonAlarmMessages(StringBuilder sb, boolean[] status, int startIdx) {
+        if (status == null) {
+            return;
+        }
+        int limit = Math.min(status.length, 8);
+        for (int i = 0; i < limit; i++) {
+            int alarmIdx = startIdx + i;
+            if (!status[i] || alarmIdx >= CONVEYOR_COMMON_ALARM_MESSAGES.length) {
+                continue;
+            }
+            sb.append(CONVEYOR_COMMON_ALARM_MESSAGES[alarmIdx]).append(";");
+        }
+    }
+
+    private static void applyAlarmMessage(ZyStationStatusEntity statusEntity, String alarmMsg) {
+        if (statusEntity == null) {
+            return;
+        }
+        String message = alarmMsg == null ? "" : alarmMsg;
+        statusEntity.setError(message.isEmpty() ? 0 : 1);
+        statusEntity.setErrorMsg(message);
+    }
+
 }

--
Gitblit v1.9.1