From d56b8093dc9e3e75f8efe1a0f1aa6d821c9c3dfb Mon Sep 17 00:00:00 2001
From: Junjie <xjj@123>
Date: 星期二, 15 四月 2025 13:30:35 +0800
Subject: [PATCH] #

---
 zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/NyLiftThread.java |  244 +++++++++++++++++++++++++++++++++---------------
 1 files changed, 168 insertions(+), 76 deletions(-)

diff --git a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/NyLiftThread.java b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/NyLiftThread.java
index 1f91941..0be1c20 100644
--- a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/NyLiftThread.java
+++ b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/NyLiftThread.java
@@ -10,18 +10,21 @@
 import com.zy.asrs.framework.common.SpringUtils;
 import com.zy.asrs.framework.exception.CoolException;
 import com.zy.asrs.wcs.common.ExecuteSupport;
+import com.zy.asrs.wcs.core.domain.dto.BasLiftStaDto;
 import com.zy.asrs.wcs.core.entity.BasLift;
 import com.zy.asrs.wcs.core.model.command.LiftCommand;
 import com.zy.asrs.wcs.core.model.enums.LiftCommandModeType;
+import com.zy.asrs.wcs.core.model.enums.MotionCtgType;
 import com.zy.asrs.wcs.core.service.BasLiftService;
+import com.zy.asrs.wcs.core.utils.LiftDispatcher;
 import com.zy.asrs.wcs.core.utils.RedisUtil;
 import com.zy.asrs.wcs.core.utils.Utils;
 import com.zy.asrs.wcs.rcs.News;
 import com.zy.asrs.wcs.rcs.cache.OutputQueue;
 import com.zy.asrs.wcs.rcs.entity.Device;
 import com.zy.asrs.wcs.rcs.entity.DeviceDataLog;
+import com.zy.asrs.wcs.rcs.model.CommandResponse;
 import com.zy.asrs.wcs.rcs.model.enums.LiftProtocolStatusType;
-import com.zy.asrs.wcs.rcs.model.enums.SlaveType;
 import com.zy.asrs.wcs.rcs.model.protocol.LiftProtocol;
 import com.zy.asrs.wcs.rcs.service.DeviceDataLogService;
 import com.zy.asrs.wcs.rcs.thread.LiftThread;
@@ -29,7 +32,6 @@
 import lombok.extern.slf4j.Slf4j;
 
 import java.text.MessageFormat;
-import java.text.SimpleDateFormat;
 import java.util.*;
 
 @Slf4j
@@ -41,27 +43,11 @@
     private LiftProtocol liftProtocol;
     private SiemensS7Net siemensS7Net;
     private List<LiftStaProtocol> liftStaProtocols = new ArrayList<>();
-    private List<Sta> staList = new ArrayList<>();
+    private List<BasLiftStaDto> staList = new ArrayList<>();
 
     public NyLiftThread(Device device, RedisUtil redisUtil) {
         this.device = device;
         this.redisUtil = redisUtil;
-
-        //鍒濆鍖栫珯鐐�
-        BasLiftService basLiftService = SpringUtils.getBean(BasLiftService.class);
-        BasLift basLift = basLiftService.getOne(new LambdaQueryWrapper<BasLift>()
-                .eq(BasLift::getDeviceId, device.getId()));
-        List<Sta> staList = JSON.parseArray(basLift.getSta(), Sta.class);
-        this.staList = staList;
-        for (Sta sta : staList) {
-            LiftStaProtocol liftStaProtocol = new LiftStaProtocol();
-            liftStaProtocol.setStaNo(sta.getStaNo());//绔欑偣鍙�
-            liftStaProtocol.setLev(sta.getLev());//绔欑偣妤煎眰
-            String locNo = Utils.getLocNo(sta.getRow(), sta.getBay(), sta.getLev());
-            liftStaProtocol.setLocNo(locNo);//绔欑偣搴撲綅鍙�
-            liftStaProtocol.setLiftNo(basLift.getLiftNo());//鎻愬崌鏈哄彿
-            liftStaProtocols.add(liftStaProtocol);
-        }
     }
 
     @Override
@@ -82,9 +68,9 @@
         try {
             readStatus();
 
-            //鎻愬崌鏈哄浜庤繍琛岀姸鎬侊紝灏嗘爣璁扮疆涓篺alse
+            //鎻愬崌鏈哄浜庤繍琛岀姸鎬侊紝灏嗘爣璁扮疆涓簍rue
             if (liftProtocol.getRun()) {
-                liftProtocol.setPakMk(false);
+                liftProtocol.setPakMk(true);
             }
         } catch (Exception e) {
             OutputQueue.LIFT.offer(MessageFormat.format("銆恵0}銆戣鍙栨彁鍗囨満鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort()));
@@ -93,6 +79,24 @@
 
     private void readStatus() {
         try {
+            if (liftStaProtocols.isEmpty()) {
+                //鍒濆鍖栫珯鐐�
+                BasLiftService basLiftService = SpringUtils.getBean(BasLiftService.class);
+                BasLift basLift = basLiftService.getOne(new LambdaQueryWrapper<BasLift>()
+                        .eq(BasLift::getDeviceId, device.getId()));
+                List<BasLiftStaDto> staList = JSON.parseArray(basLift.getSta(), BasLiftStaDto.class);
+                this.staList = staList;
+                for (BasLiftStaDto sta : staList) {
+                    LiftStaProtocol liftStaProtocol = new LiftStaProtocol();
+                    liftStaProtocol.setStaNo(sta.getStaNo());//绔欑偣鍙�
+                    liftStaProtocol.setLev(sta.getLev());//绔欑偣妤煎眰
+                    String locNo = Utils.getLocNo(sta.getRow(), sta.getBay(), sta.getLev());
+                    liftStaProtocol.setLocNo(locNo);//绔欑偣搴撲綅鍙�
+                    liftStaProtocol.setLiftNo(basLift.getLiftNo());//鎻愬崌鏈哄彿
+                    liftStaProtocols.add(liftStaProtocol);
+                }
+            }
+
             //鑾峰彇鎻愬崌鏈烘暟鎹�
             OperateResultExOne<byte[]> result1 = siemensS7Net.Read("DB82.4.0", (short) 10);
             if (result1.IsSuccess) {
@@ -132,7 +136,7 @@
                 //鏈夊皬杞�
                 liftProtocol.setHasCar(status2[6]);
                 //璁惧鏁呴殰
-                liftProtocol.setErrorCode(String.valueOf(status2[7]));
+                liftProtocol.setErrorCode(status2[7]?"1":"0");
                 //鐩殑鍦板潃
                 liftProtocol.setDistAddress(siemensS7Net.getByteTransform().TransInt16(result1.Content, 4));
                 //宸插畬鎴愪换鍔″彿
@@ -142,10 +146,16 @@
                 liftProtocol.setLev((int) lev);
 
                 //************琛ュ厖鎵╁睍瀛楁*************
-                InnerLiftExtend liftExtend = JSON.parseObject(JSON.toJSONString(liftProtocol.getExtend()), InnerLiftExtend.class);
+                InnerLiftExtend liftExtend = (InnerLiftExtend) liftProtocol.getExtend();
                 //浠诲姟鍙�
                 liftExtend.setLiftTaskNo(String.valueOf(siemensS7Net.getByteTransform().TransInt16(result1.Content, 2)));
                 liftProtocol.setExtend(liftExtend);
+
+                boolean ready = true;
+                if (!liftProtocol.getModel() || liftProtocol.getRun()) {
+                    ready = false;
+                }
+                liftProtocol.setReady(ready);//灏辩华鐘舵��
 
 
             }else {
@@ -211,6 +221,31 @@
                 liftProtocol.setDeviceDataLog(System.currentTimeMillis());
             }
 
+            //灏嗘彁鍗囨満鐘舵�佷繚瀛樿嚦鏁版嵁搴�
+            BasLiftService basLiftService = SpringUtils.getBean(BasLiftService.class);
+            BasLift basLift = basLiftService.getOne(new LambdaQueryWrapper<BasLift>()
+                    .eq(BasLift::getLiftNo, device.getDeviceNo())
+                    .eq(BasLift::getHostId, device.getHostId()));
+            if (basLift == null) {
+                basLift = new BasLift();
+                //鎻愬崌鏈哄彿
+                basLift.setLiftNo(Integer.valueOf(device.getDeviceNo()));
+                basLift.setStatus(1);
+                basLift.setDeleted(0);
+                basLift.setHostId(device.getHostId());
+                basLift.setDeviceId(device.getId());
+                basLiftService.save(basLift);
+            }
+            //浠诲姟鍙�
+            basLift.setTaskNo(liftProtocol.getTaskNo().intValue());
+            //淇敼鏃堕棿
+            basLift.setUpdateTime(new Date());
+            //璁惧鐘舵��
+            basLift.setProtocol(JSON.toJSONString(liftProtocol));
+            if (basLiftService.updateById(basLift)) {
+                OutputQueue.LIFT.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), device.getDeviceNo()));
+            }
+
         } catch (Exception e) {
             OutputQueue.LIFT.offer(MessageFormat.format("銆恵0}銆戣鍙栨彁鍗囨満鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort()));
         }
@@ -227,19 +262,21 @@
     }
 
     @Override
-    public boolean move(LiftCommand command) {
+    public CommandResponse move(LiftCommand command) {
         return write(command);
     }
 
     @Override
-    public boolean palletInOut(LiftCommand command) {
+    public CommandResponse palletInOut(LiftCommand command) {
         return write(command);
     }
 
-    private boolean write(LiftCommand command) {
+    private CommandResponse write(LiftCommand command) {
+        CommandResponse response = new CommandResponse(false);
         if (null == command) {
             News.error("鎻愬崌鏈哄啓鍏ュ懡浠や负绌�");
-            return false;
+            response.setMessage("鎻愬崌鏈哄啓鍏ュ懡浠や负绌�");
+            return response;
         }
 
         List<Short> shorts = JSON.parseArray(command.getBody(), Short.class);
@@ -248,32 +285,47 @@
             array[i] = shorts.get(i);
         }
 
+        LiftDispatcher liftDispatcher = SpringUtils.getBean(LiftDispatcher.class);
+
+        Integer origin = command.getOriginLev();
+        Integer target = command.getTargetLev();
+        if (command.getMode() == 2) {
+            origin = liftDispatcher.getLiftLevOffset(this.device.getId().intValue(), liftProtocol.getLev());
+            target = liftDispatcher.getLiftLevOffset(this.device.getId().intValue(), command.getTargetLev());
+        }
+
+        array[1] = origin.shortValue();
+        array[2] = target.shortValue();
+
         OperateResult result = siemensS7Net.Write("DB83.0", array);
         if (result != null && result.IsSuccess) {
             liftProtocol.setSendTime(System.currentTimeMillis());//鎸囦护涓嬪彂鏃堕棿
             News.info("鎻愬崌鏈哄懡浠や笅鍙慬id:{}] >>>>> {}", device.getId(), JSON.toJSON(command));
             OutputQueue.LIFT.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), device.getId(), JSON.toJSON(command)));
-            return true;
+            response.setMessage(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), device.getId(), JSON.toJSON(command)));
+            response.setResult(true);
+            return response;
         } else {
             OutputQueue.LIFT.offer(MessageFormat.format("銆恵0}銆戝啓鍏ユ彁鍗囨満plc鏁版嵁澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}],娆℃暟锛歿}", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort()));
             News.error("鍐欏叆鎻愬崌鏈簆lc鏁版嵁澶辫触 ===>> [id:{}] [ip:{}] [port:{}]", device.getId(), device.getIp(), device.getPort());
-            return false;
+            response.setMessage(MessageFormat.format("銆恵0}銆戝啓鍏ユ彁鍗囨満plc鏁版嵁澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}],娆℃暟锛歿}", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort()));
+            return response;
         }
     }
 
     @Override
-    public boolean lock(LiftCommand command) {
-        return true;
+    public CommandResponse lock(LiftCommand command) {
+        return new CommandResponse(true);
     }
 
     @Override
-    public boolean unlock(LiftCommand command) {
-        return true;
+    public CommandResponse unlock(LiftCommand command) {
+        return new CommandResponse(true);
     }
 
     @Override
-    public boolean reset(LiftCommand command) {
-        return false;
+    public CommandResponse reset(LiftCommand command) {
+        return new CommandResponse(false);
     }
 
     @Override
@@ -282,19 +334,15 @@
     }
 
     @Override
-    public boolean isIdle(ExecuteSupport support) {
-        if (null != support) {
-            if (!support.judgement()) {
-                return false;
-            }
-        }
+    public boolean isIdle(MotionCtgType flag) {
         // 鍒ゆ柇鎻愬崌鏈烘槸鍚﹁嚜鍔ㄣ�佸氨缁�佺┖闂�
         if (this.liftProtocol.getModel()
                 && !this.liftProtocol.getRun()
                 && this.liftProtocol.getReady()
                 && this.liftProtocol.getPakMk()
                 && this.liftProtocol.getErrorCode().equals("0")
-                && this.liftProtocol.getProtocolStatus().equals(LiftProtocolStatusType.IDLE)
+                && (this.liftProtocol.getProtocolStatusType().equals(LiftProtocolStatusType.IDLE)
+                || this.liftProtocol.getProtocolStatusType().equals(LiftProtocolStatusType.WAITING))
         ) {
             return true;
         }
@@ -302,15 +350,75 @@
     }
 
     @Override
-    public LiftCommand getMoveCommand(Integer taskNo, Integer sourceLev, Integer targetLev, Integer mode) {
+    public boolean isDeviceIdle() {
+        return isDeviceIdle(null);
+    }
+
+    @Override
+    public boolean isDeviceIdle(ExecuteSupport support) {
+        if (null != support) {
+            Boolean judgement = support.judgement();
+            if (judgement != null && !judgement) {
+                return true;
+            }
+        }
+
+        // 鍒ゆ柇鎻愬崌鏈烘槸鍚﹁嚜鍔ㄣ�佸氨缁�佺┖闂�
+        if (this.liftProtocol.getModel()
+                && !this.liftProtocol.getRun()
+                && this.liftProtocol.getReady()
+                && this.liftProtocol.getErrorCode().equals("0")
+        ) {
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public synchronized boolean setProtocolStatus(LiftProtocolStatusType status) {
+        this.liftProtocol.setProtocolStatus(status);
+        return true;
+    }
+
+    @Override
+    public synchronized boolean setSyncTaskNo(Integer taskNo) {
+        this.liftProtocol.setTaskNo(taskNo);
+        return true;
+    }
+
+    @Override
+    public boolean isLock(ExecuteSupport support) {
+        if (support != null) {
+            return support.judgement();
+        }
+        return true;
+    }
+
+    @Override
+    public int generateDeviceTaskNo(int taskNo, MotionCtgType motionCtgType) {
+        int deviceTaskNo = taskNo;
+        try {
+            deviceTaskNo = Utils.getTaskNo("LIFT_TASK_NO");
+        } catch (Exception e) {
+            return taskNo;
+        }
+        return deviceTaskNo;
+    }
+
+    @Override
+    public List<LiftCommand> getMoveCommand(Integer taskNo, Integer sourceLev, Integer targetLev, LiftCommandModeType mode) {
         /**
          * 浠诲姟绫诲瀷
-         * 1=绉绘墭鐩橈紱鍗囬檷鏈哄皢婧愮珯鍙版墭鐩樼Щ鍒扮洰鏍囩珯鍙�
-         * 2=绉诲皬杞︼紝鍗囬檷鏈虹Щ鍒扮洰鏍囧眰锛岀瓑寰�
+         1=绉绘墭鐩橈紱鍗囬檷鏈哄皢婧愮珯鍙版墭鐩樼Щ鍒扮洰鏍囩珯鍙�
+         2=鎹㈠眰锛屽崌闄嶆満绉诲埌鐩爣灞傦紝绛夊緟
+         3=鍏ュ簱鎵樼洏,鍗囬檷鏈哄皢婧愮珯鍙版墭鐩樼Щ鍒扮洰鏍囧眰鏁帮紱
+         4=鍑哄簱鎵樼洏锛屽崌闄嶆満灏嗘簮绔欏彴鎵樼洏绉诲埌鐩爣绔欏彴
          */
         short taskMode = 2;
-        if (mode == null) {
-            taskMode = mode.shortValue();
+        if (mode.equals(LiftCommandModeType.PALLET_IN)) {
+            taskMode = 3;
+        } else if (mode.equals(LiftCommandModeType.PALLET_OUT)) {
+            taskMode = 4;
         }
 
         // 寮�濮嬩换鍔�
@@ -327,32 +435,33 @@
         LiftCommand command = new LiftCommand();
         command.setLiftNo(Integer.valueOf(this.device.getDeviceNo()));
         command.setBody(JSON.toJSONString(array));
-        command.setMode(LiftCommandModeType.MOVE.id);
+        command.setMode(mode.id);
         command.setOriginLev(sourceLev);
         command.setTargetLev(targetLev);
-        return command;
+
+        ArrayList<LiftCommand> list = new ArrayList<>();
+        list.add(command);
+        return list;
     }
 
     @Override
-    public LiftCommand getMoveWithShuttleCommand(Integer taskNo, Integer sourceLev, Integer targetLev, Integer mode) {
-        return getMoveCommand(taskNo, sourceLev, targetLev, 2);
+    public List<LiftCommand> getMoveWithShuttleCommand(Integer taskNo, Integer sourceLev, Integer targetLev, LiftCommandModeType mode) {
+        return getMoveCommand(taskNo, sourceLev, targetLev, mode);
     }
 
     @Override
-    public LiftCommand getPalletInOutCommand(Integer taskNo, Integer sourceLev, Integer targetLev, Integer originSta, Integer targetSta, Integer mode) {
-        return getMoveCommand(taskNo, sourceLev, targetLev, 1);
+    public List<LiftCommand> getPalletInOutCommand(Integer taskNo, Integer sourceLev, Integer targetLev, Integer originSta, Integer targetSta, LiftCommandModeType mode) {
+        return getMoveCommand(taskNo, sourceLev, targetLev, mode);
     }
 
     @Override
-    public LiftCommand getLockCommand(Integer taskNo, Boolean lock) {
-        LiftCommand command = new LiftCommand();
-        return command;
+    public List<LiftCommand> getLockCommand(Integer taskNo, Boolean lock) {
+        return null;
     }
 
     @Override
-    public LiftCommand getShuttleSignalCommand(Integer taskNo, Boolean signal) {
-        LiftCommand command = new LiftCommand();
-        return command;
+    public List<LiftCommand> getShuttleSignalCommand(Integer taskNo, Boolean signal) {
+        return null;
     }
 
     @Override
@@ -478,23 +587,6 @@
          * 鎻愬崌鏈哄彿
          */
         private Integer liftNo;
-
-    }
-
-    @Data
-    public static class Sta {
-
-        // 鎻愬崌鏈虹珯鐐瑰彿
-        private Integer staNo;
-
-        //杈撻�佺珯鐐规帓
-        private Integer row;
-
-        //杈撻�佺珯鐐瑰垪
-        private Integer bay;
-
-        // 灞�
-        private Integer lev;
 
     }
 

--
Gitblit v1.9.1