From 09fcc4ec56a1ce6c0fcb308348d8dd5e2c08d336 Mon Sep 17 00:00:00 2001
From: fyxc <fyxc@qq.com>
Date: 星期四, 22 五月 2025 14:33:06 +0800
Subject: [PATCH] server commit files

---
 src/main/java/com/zy/core/thread/impl/ZyForkLiftThread.java |  190 +++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 164 insertions(+), 26 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/impl/ZyForkLiftThread.java b/src/main/java/com/zy/core/thread/impl/ZyForkLiftThread.java
index 82bc397..03bc4eb 100644
--- a/src/main/java/com/zy/core/thread/impl/ZyForkLiftThread.java
+++ b/src/main/java/com/zy/core/thread/impl/ZyForkLiftThread.java
@@ -1,6 +1,8 @@
 package com.zy.core.thread.impl;
 
+import HslCommunication.Core.Types.OperateResult;
 import HslCommunication.Core.Types.OperateResultExOne;
+import HslCommunication.Profinet.Siemens.SiemensPLCS;
 import HslCommunication.Profinet.Siemens.SiemensS7Net;
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
@@ -14,9 +16,12 @@
 import com.zy.common.ExecuteSupport;
 import com.zy.common.utils.RedisUtil;
 import com.zy.core.News;
+import com.zy.core.action.ForkLiftAction;
 import com.zy.core.cache.OutputQueue;
+import com.zy.core.enums.ForkLiftIoModeType;
 import com.zy.core.enums.ForkLiftProtocolStatusType;
 import com.zy.core.enums.ForkLiftTaskModeType;
+import com.zy.core.enums.RedisKeyType;
 import com.zy.core.model.CommandResponse;
 import com.zy.core.model.ForkLiftSlave;
 import com.zy.core.model.command.ForkLiftCommand;
@@ -27,7 +32,10 @@
 import lombok.extern.slf4j.Slf4j;
 
 import java.text.MessageFormat;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.Date;
+import java.util.List;
 
 @Slf4j
 @SuppressWarnings("all")
@@ -56,7 +64,21 @@
 
     @Override
     public boolean connect() {
-        return false;
+        boolean result = false;
+        siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, slave.getIp());
+        siemensS7Net.setRack(slave.getRack().byteValue());
+        siemensS7Net.setSlot(slave.getSlot().byteValue());
+        OperateResult connect = siemensS7Net.ConnectServer();
+        if (connect.IsSuccess) {
+            result = true;
+            OutputQueue.FORKLIFT.offer(MessageFormat.format("銆恵0}銆戣揣鍙夋彁鍗囨満杩炴帴鎴愬姛 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
+            News.info("璐у弶鎻愬崌鏈鸿繛鎺ユ垚鍔� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+        } else {
+            OutputQueue.FORKLIFT.offer(MessageFormat.format("銆恵0}銆戣揣鍙夋彁鍗囨満杩炴帴澶辫触锛侊紒锛� ===>> [id:{1}] [ip:{2}] [port:{3}]  [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
+            News.error("璐у弶鎻愬崌鏈鸿繛鎺ュけ璐ワ紒锛侊紒 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+        }
+        // siemensS7Net.ConnectClose();
+        return result;
     }
 
     @Override
@@ -72,9 +94,28 @@
             try {
                 read();
                 Thread.sleep(200);
+                execute();
             } catch (Exception e) {
                 e.printStackTrace();
             }
+        }
+    }
+
+    private void execute() {
+        ForkLiftAction forkLiftAction = SpringUtils.getBean(ForkLiftAction.class);
+        if (forkLiftAction == null) {
+            return;
+        }
+
+        Object object = redisUtil.get(RedisKeyType.FORK_LIFT_FLAG.key + slave.getId());
+        if (object == null) {
+            return;
+        }
+
+        Integer taskNo = Integer.valueOf(String.valueOf(object));
+        if (taskNo != 0) {
+            //瀛樺湪浠诲姟闇�瑕佹墽琛�
+            boolean result = forkLiftAction.executeWork(slave.getId(), taskNo);
         }
     }
 
@@ -96,7 +137,7 @@
     private void readStatus() {
         try {
             //鑾峰彇鎻愬崌鏈烘暟鎹�
-            OperateResultExOne<byte[]> result1 = siemensS7Net.Read("DB101.0", (short) 32);
+            OperateResultExOne<byte[]> result1 = siemensS7Net.Read("DB101.0", (short) 16);
             if (result1.IsSuccess) {
                 if (null == forkLiftProtocol) {
                     forkLiftProtocol = new ForkLiftProtocol();
@@ -109,27 +150,42 @@
                 //----------璇诲彇鎻愬崌鏈虹姸鎬�-----------
                 //妯″紡
                 forkLiftProtocol.setModel((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 0));
-                //浠诲姟鍙�
-                forkLiftProtocol.setTaskNo((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 4));
+                //PLC浠诲姟鍙�
+                forkLiftProtocol.setWrkNo((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 2));
                 //浠诲姟鐘舵��
-                forkLiftProtocol.setProtocolStatus((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 8));
+                forkLiftProtocol.setProtocolStatus((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 4));
                 //浠诲姟妯″紡
-                forkLiftProtocol.setModel((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 12));
+                forkLiftProtocol.setTaskMode((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 6));
                 //鍙栬揣鏁版嵁
-                forkLiftProtocol.setPick((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 16));
+                forkLiftProtocol.setPick((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 8));
                 //鏀捐揣鏁版嵁
-                forkLiftProtocol.setPut((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 20));
+                forkLiftProtocol.setPut((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 10));
                 //鍑哄叆搴撴ā寮�
-                forkLiftProtocol.setIOMode((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 24));
+                forkLiftProtocol.setIOMode((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 12));
                 //鏁呴殰鐮�
-                forkLiftProtocol.setErrorCode((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 28));
+                forkLiftProtocol.setErrorCode((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 14));
 
                 //************琛ュ厖鎵╁睍瀛楁*************
                 InnerForkLiftExtend forkLiftExtend = (InnerForkLiftExtend) forkLiftProtocol.getExtend();
                 forkLiftProtocol.setExtend(forkLiftExtend);
 
-            }else {
+            } else {
                 OutputQueue.FORKLIFT.offer(MessageFormat.format("銆恵0}銆憑1}璇诲彇璐у弶鎻愬崌鏈虹姸鎬佷俊鎭け璐�", DateUtils.convert(new Date()), slave.getId()));
+            }
+
+            OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB102.0", (short) 8);
+            if (result2.IsSuccess) {
+                for (int i = 0; i < this.slave.getSta().size(); i++) {
+                    ForkLiftSlave.Sta sta = this.slave.getSta().get(i);
+                    if (forkLiftStaProtocols.isEmpty()) {
+                        continue;
+                    }
+
+                    ForkLiftStaProtocol forkLiftStaProtocol = forkLiftStaProtocols.get(i);
+                    boolean[] status1 = siemensS7Net.getByteTransform().TransBool(result2.Content, i * 2, 2);
+                    forkLiftStaProtocol.setHasTray(status1[0]);
+                    forkLiftStaProtocol.setHasCar(status1[1]);
+                }
             }
 
             if (System.currentTimeMillis() - forkLiftProtocol.getDeviceDataLog() > 1000 * 5) {
@@ -166,7 +222,7 @@
             //璁惧鐘舵��
             basLift.setDeviceStatus(JSON.toJSONString(forkLiftProtocol));
             if (basLiftService.updateById(basLift)) {
-                OutputQueue.FORKLIFT.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), forkLiftProtocol.getLiftNo()));
+                OutputQueue.FORKLIFT.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛", DateUtils.convert(new Date()), forkLiftProtocol.getLiftNo()));
             }
 
         } catch (Exception e) {
@@ -194,32 +250,98 @@
 
     @Override
     public CommandResponse pickAndPut(ForkLiftCommand command) {
-        return null;
+        CommandResponse response = new CommandResponse(false);
+
+        short[] array = new short[4];
+        array[0] = command.getTaskNo();//浠诲姟鍙�
+        array[1] = command.getMode();//浠诲姟妯″紡
+        array[2] = command.getPick();//鍙栬揣鏁版嵁
+        array[3] = command.getPut();//鏀捐揣鏁版嵁
+        OperateResult result = siemensS7Net.Write("DB103.0", array);
+        if (result.IsSuccess) {
+            OperateResult result2 = siemensS7Net.Write("DB103.8", command.getConfirm());
+            if (result2.IsSuccess) {
+                response.setResult(true);
+            }
+        }
+        return response;
     }
 
     @Override
     public CommandResponse shuttleSwitch(ForkLiftCommand command) {
-        return null;
+        CommandResponse response = new CommandResponse(false);
+
+        short[] array = new short[4];
+        array[0] = command.getTaskNo();//浠诲姟鍙�
+        array[1] = command.getMode();//浠诲姟妯″紡
+        array[2] = command.getPick();//鍙栬揣鏁版嵁
+        array[3] = command.getPut();//鏀捐揣鏁版嵁
+        OperateResult result = siemensS7Net.Write("DB103.0", array);
+        if (result.IsSuccess) {
+            OperateResult result2 = siemensS7Net.Write("DB103.8", command.getConfirm());
+            if (result2.IsSuccess) {
+                response.setResult(true);
+            }
+        }
+        return response;
     }
 
     @Override
-    public CommandResponse reset(ForkLiftCommand command) {
-        return null;
+    public CommandResponse reset() {
+        CommandResponse response = new CommandResponse(false);
+        OperateResult result = siemensS7Net.Write("DB103.10", (short) 1);
+        if (result.IsSuccess) {
+            News.info("璐у弶鎻愬崌鏈虹‘璁ゅ懡浠や笅鍙戞垚鍔燂紝鎻愬崌鏈哄彿={}", forkLiftProtocol.getLiftNo());
+            response.setResult(true);
+        }
+        return response;
     }
 
     @Override
     public boolean isIdle() {
-        return false;
+        if (this.forkLiftProtocol.getTaskNo() == null
+                || this.forkLiftProtocol.getProtocolStatus() == null
+                || this.forkLiftProtocol.getModel() == null
+                || this.forkLiftProtocol.getErrorCode() == null
+        ) {
+            return false;
+        }
+
+        boolean res = this.forkLiftProtocol.getProtocolStatus() == ForkLiftProtocolStatusType.IDLE.id
+                && this.forkLiftProtocol.getWrkNo() == 0
+                && this.forkLiftProtocol.getTaskNo() == 0
+                && this.forkLiftProtocol.getModel() == 2
+                && this.forkLiftProtocol.getErrorCode() == 0;
+        return res;
     }
 
     @Override
     public boolean isDeviceIdle() {
-        return false;
+        return isDeviceIdle(null);
     }
 
     @Override
     public boolean isDeviceIdle(ExecuteSupport support) {
-        return false;
+        if (null != support) {
+            Boolean judgement = support.judgement();
+            if (judgement != null && !judgement) {
+                return true;
+            }
+        }
+
+        if (this.forkLiftProtocol.getTaskNo() == null
+                || this.forkLiftProtocol.getProtocolStatus() == null
+                || this.forkLiftProtocol.getModel() == null
+                || this.forkLiftProtocol.getErrorCode() == null
+        ) {
+            return false;
+        }
+
+        boolean res = this.forkLiftProtocol.getProtocolStatus() == ForkLiftProtocolStatusType.IDLE.id
+                && this.forkLiftProtocol.getWrkNo() == 0
+                && this.forkLiftProtocol.getModel() == 2
+                && this.forkLiftProtocol.getErrorCode() == 0;
+        return res;
     }
 
     @Override
@@ -229,23 +351,36 @@
 
     @Override
     public boolean setSyncTaskNo(Integer taskNo) {
+        this.forkLiftProtocol.setSyncTaskNo(taskNo);
+        return true;
+    }
+
+    @Override
+    public boolean switchIOMode(ForkLiftIoModeType type) {
+        OperateResult result = siemensS7Net.Write("DB103.12", type.id.shortValue());
+        if (result.IsSuccess) {
+            return true;
+        }
         return false;
     }
 
     @Override
     public int generateDeviceTaskNo(int taskNo, ForkLiftTaskModeType type) {
-        return 0;
+        return taskNo;
     }
 
     @Override
     public List<ForkLiftCommand> getPickAndPutCommand(Integer taskNo, Integer pick, Integer put) {
+        Integer realPick = pick % 1000;
+        Integer realPut = put % 1000;
+
         List<ForkLiftCommand> commands = new ArrayList<>();
         ForkLiftCommand command = new ForkLiftCommand();
         command.setLiftNo(slave.getId());
         command.setTaskNo(taskNo.shortValue());
         command.setMode(ForkLiftTaskModeType.PICK_PUT.id.shortValue());
-        command.setPick(pick.shortValue());
-        command.setPut(put.shortValue());
+        command.setPick(realPick.shortValue());
+        command.setPut(realPut.shortValue());
         command.setConfirm((short) 1);
 
         commands.add(command);
@@ -253,14 +388,17 @@
     }
 
     @Override
-    public List<ForkLiftCommand> getShuttleSwitchCommand(Integer taskNo, Integer originLev, Integer targetLev) {
+    public List<ForkLiftCommand> getShuttleSwitchCommand(Integer taskNo, Integer pick, Integer put) {
+        Integer realPick = pick % 1000;
+        Integer realPut = put % 1000;
+
         List<ForkLiftCommand> commands = new ArrayList<>();
         ForkLiftCommand command = new ForkLiftCommand();
         command.setLiftNo(slave.getId());
         command.setTaskNo(taskNo.shortValue());
         command.setMode(ForkLiftTaskModeType.SHUTTLE_SWITCH.id.shortValue());
-        command.setPick(originLev.shortValue());
-        command.setPut(targetLev.shortValue());
+        command.setPick(realPick.shortValue());
+        command.setPut(realPut.shortValue());
         command.setConfirm((short) 1);
 
         commands.add(command);

--
Gitblit v1.9.1