From f6046d2fc1503dc76fc06c51604b480664f532a3 Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期二, 06 一月 2026 09:52:58 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/thread/impl/ZyStationThread.java |  106 +++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 82 insertions(+), 24 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/impl/ZyStationThread.java b/src/main/java/com/zy/core/thread/impl/ZyStationThread.java
index d54012a..df9af2a 100644
--- a/src/main/java/com/zy/core/thread/impl/ZyStationThread.java
+++ b/src/main/java/com/zy/core/thread/impl/ZyStationThread.java
@@ -4,6 +4,7 @@
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.zy.asrs.entity.BasDevp;
 import com.zy.asrs.service.BasDevpService;
+import com.zy.asrs.utils.Utils;
 import com.zy.core.network.DeviceConnectPool;
 import com.zy.core.thread.StationThread;
 import com.alibaba.fastjson.JSON;
@@ -11,6 +12,8 @@
 import com.core.common.SpringUtils;
 import com.zy.asrs.entity.DeviceConfig;
 import com.zy.asrs.entity.DeviceDataLog;
+import com.zy.asrs.entity.BasStationOpt;
+import com.zy.asrs.service.BasStationOptService;
 import com.zy.common.utils.RedisUtil;
 import com.zy.core.network.ZyStationConnectDriver;
 import com.zy.core.cache.MessageQueue;
@@ -25,6 +28,7 @@
 
 import java.text.MessageFormat;
 import java.util.*;
+import java.util.concurrent.ScheduledExecutorService;
 
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
@@ -40,6 +44,7 @@
     private DeviceConfig deviceConfig;
     private RedisUtil redisUtil;
     private ZyStationConnectDriver zyStationConnectDriver;
+    private int deviceLogCollectTime = 200;
     private long deviceDataLogTime = System.currentTimeMillis();
 
     public ZyStationThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
@@ -51,13 +56,14 @@
     @SuppressWarnings("InfiniteLoopStatement")
     public void run() {
         this.connect();
+        deviceLogCollectTime = Utils.getDeviceLogCollectTime();
 
-        //璁惧璇诲彇
         Thread readThread = new Thread(() -> {
             while (true) {
                 try {
+                    deviceLogCollectTime = Utils.getDeviceLogCollectTime();
                     readStatus();
-                    Thread.sleep(200);
+                    Thread.sleep(100);
                 } catch (Exception e) {
                     log.error("StationThread Fail", e);
                 }
@@ -65,25 +71,25 @@
         });
         readThread.start();
 
-        while (true) {
-            try {
-                int step = 1;
-                Task task = MessageQueue.poll(SlaveType.Devp, deviceConfig.getDeviceNo());
-                if (task != null) {
-                    step = task.getStep();
-                }
-                switch (step) {
-                    case 2:
+        Thread processThread = new Thread(() -> {
+            while (true) {
+                try {
+                    int step = 1;
+                    Task task = MessageQueue.poll(SlaveType.Devp, deviceConfig.getDeviceNo());
+                    if (task != null) {
+                        step = task.getStep();
+                    }
+                    if (step == 2) {
                         sendCommand((StationCommand) task.getData());
-                        break;
-                    default:
-                        break;
+                    }
+
+                    Thread.sleep(100);
+                } catch (Exception e) {
+                    log.error("StationProcess Fail", e);
                 }
-                Thread.sleep(200);
-            } catch (Exception e) {
-                e.printStackTrace();
             }
-        }
+        });
+        processThread.start();
     }
 
     private void readStatus() {
@@ -92,7 +98,12 @@
         }
 
         if(statusList.isEmpty()) {
-            BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
+            BasDevpService basDevpService = null;
+            try {
+                basDevpService = SpringUtils.getBean(BasDevpService.class);
+            } catch (Exception e) {
+
+            }
             if (basDevpService == null) {
                 return;
             }
@@ -126,13 +137,14 @@
                     stationProtocol.setPalletHeight(statusEntity.getPalletHeight());
                     stationProtocol.setError(statusEntity.getError());
                     stationProtocol.setBarcode(statusEntity.getBarcode());
+                    stationProtocol.setRunBlock(statusEntity.isRunBlock());
                 }
             }
         }
 
         OutputQueue.DEVP.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), deviceConfig.getDeviceNo()));
 
-        if (System.currentTimeMillis() - deviceDataLogTime > 1000 * 1) {
+        if (System.currentTimeMillis() - deviceDataLogTime > deviceLogCollectTime) {
             //淇濆瓨鏁版嵁璁板綍
             DeviceDataLog deviceDataLog = new DeviceDataLog();
             deviceDataLog.setOriginData(JSON.toJSONString(zyStationStatusEntities));
@@ -149,14 +161,17 @@
 
     @Override
     public boolean connect() {
-        zyStationConnectDriver = new ZyStationConnectDriver(deviceConfig);
-        new Thread(zyStationConnectDriver).start();
+        zyStationConnectDriver = new ZyStationConnectDriver(deviceConfig, redisUtil);
+        zyStationConnectDriver.start();
         DeviceConnectPool.put(SlaveType.Devp, deviceConfig.getDeviceNo(), zyStationConnectDriver);
         return true;
     }
 
     @Override
     public void close() {
+        if (zyStationConnectDriver != null) {
+            zyStationConnectDriver.close();
+        }
     }
 
     @Override
@@ -176,8 +191,41 @@
 
     @Override
     public CommandResponse sendCommand(StationCommand command) {
-       CommandResponse commandResponse = zyStationConnectDriver.sendCommand(command);
-       return commandResponse;
+       CommandResponse commandResponse = null;
+       try {
+           commandResponse = zyStationConnectDriver.sendCommand(command);
+           return commandResponse;
+       } finally {
+           BasStationOptService optService = SpringUtils.getBean(BasStationOptService.class);
+           List<ZyStationStatusEntity> statusListEntity = zyStationConnectDriver.getStatus();
+           ZyStationStatusEntity matched = null;
+           if (statusListEntity != null) {
+               for (ZyStationStatusEntity e : statusListEntity) {
+                   if (e.getStationId() != null && e.getStationId().equals(command.getStationId())) {
+                       matched = e;
+                       break;
+                   }
+               }
+           }
+           BasStationOpt basStationOpt = new BasStationOpt(
+                   command.getTaskNo(),
+                   command.getStationId(),
+                   new Date(),
+                   "MOVE",
+                   command.getStationId(),
+                   command.getTargetStaNo(),
+                   null,
+                   null,
+                   null,
+                   JSON.toJSONString(command),
+                   JSON.toJSONString(matched),
+                   1,
+                   JSON.toJSONString(commandResponse)
+           );
+           if (optService != null) {
+               optService.insert(basStationOpt);
+           }
+       }
     }
 
     @Override
@@ -188,4 +236,14 @@
         }
         return map;
     }
+
+    @Override
+    public CommandResponse sendOriginCommand(String address, short[] data) {
+        return zyStationConnectDriver.sendOriginCommand(address, data);
+    }
+
+    @Override
+    public byte[] readOriginCommand(String address, int length) {
+        return zyStationConnectDriver.readOriginCommand(address, length);
+    }
 }

--
Gitblit v1.9.1