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

---
 src/main/java/com/zy/core/thread/impl/ZyRgvThread.java |  101 ++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 73 insertions(+), 28 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/impl/ZyRgvThread.java b/src/main/java/com/zy/core/thread/impl/ZyRgvThread.java
index 6ce7e22..41c78fe 100644
--- a/src/main/java/com/zy/core/thread/impl/ZyRgvThread.java
+++ b/src/main/java/com/zy/core/thread/impl/ZyRgvThread.java
@@ -1,15 +1,16 @@
 package com.zy.core.thread.impl;
 
-import HslCommunication.Profinet.Siemens.SiemensPLCS;
-import HslCommunication.Profinet.Siemens.SiemensS7Net;
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.core.common.DateUtils;
 import com.core.common.SpringUtils;
 import com.zy.asrs.entity.BasRgv;
+import com.zy.asrs.entity.BasRgvOpt;
 import com.zy.asrs.entity.DeviceConfig;
 import com.zy.asrs.entity.DeviceDataLog;
 import com.zy.asrs.service.BasRgvService;
+import com.zy.asrs.service.BasRgvOptService;
+import com.zy.asrs.utils.Utils;
 import com.zy.common.utils.RedisUtil;
 import com.zy.core.cache.MessageQueue;
 import com.zy.core.cache.OutputQueue;
@@ -38,6 +39,7 @@
     private RedisUtil redisUtil;
     private ZyRgvConnectDriver zyRgvConnectDriver;
     private RgvProtocol rgvProtocol;
+    private int deviceLogCollectTime = 200;
 
     public ZyRgvThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
         this.deviceConfig = deviceConfig;
@@ -49,28 +51,39 @@
     public void run() {
         connect();
         initRgv();
-        while (true) {
-            try {
-                int step = 1;
-                Task task = MessageQueue.poll(SlaveType.Rgv, deviceConfig.getDeviceNo());
-                if (task != null) {
-                    step = task.getStep();
+
+        Thread readThread = new Thread(() -> {
+            while (true) {
+                try {
+                    deviceLogCollectTime = Utils.getDeviceLogCollectTime();
+                    readStatus();
+                    Thread.sleep(100);
+                } catch (Exception e) {
+                    log.error("RgvThread Fail", e);
                 }
-                switch (step) {
-                    case 1:
-                        readStatus();
-                        break;
-                    case 2:
-                        sendCommand((RgvCommand) task.getData());
-                        break;
-                    default:
-                        break;
-                }
-                Thread.sleep(200);
-            } catch (Exception e) {
-                e.printStackTrace();
             }
-        }
+        });
+        readThread.start();
+
+        Thread processThread = new Thread(() -> {
+            while (true) {
+                try {
+                    int step = 1;
+                    Task task = MessageQueue.poll(SlaveType.Rgv, deviceConfig.getDeviceNo());
+                    if (task != null) {
+                        step = task.getStep();
+                    }
+                    if (step == 2) {
+                        sendCommand((RgvCommand) task.getData());
+                    }
+
+                    Thread.sleep(100);
+                } catch (Exception e) {
+                    log.error("RgvProcess Fail", e);
+                }
+            }
+        });
+        processThread.start();
     }
 
     private void initRgv() {
@@ -89,7 +102,7 @@
     @Override
     public boolean connect() {
         zyRgvConnectDriver = new ZyRgvConnectDriver(deviceConfig);
-        new Thread(zyRgvConnectDriver).start();
+        zyRgvConnectDriver.start();
         DeviceConnectPool.put(SlaveType.Rgv, deviceConfig.getDeviceNo(), zyRgvConnectDriver);
         return true;
     }
@@ -117,7 +130,7 @@
             rgvProtocol.setLastCommandTime(System.currentTimeMillis());
         }
 
-        if (System.currentTimeMillis() - rgvProtocol.getDeviceDataLog() > 200) {
+        if (System.currentTimeMillis() - rgvProtocol.getDeviceDataLog() > deviceLogCollectTime) {
             DeviceDataLog deviceDataLog = new DeviceDataLog();
             deviceDataLog.setOriginData(JSON.toJSONString(s));
             deviceDataLog.setWcsData(JSON.toJSONString(rgvProtocol));
@@ -128,7 +141,12 @@
             rgvProtocol.setDeviceDataLog(System.currentTimeMillis());
         }
 
-        BasRgvService basRgvService = SpringUtils.getBean(BasRgvService.class);
+        BasRgvService basRgvService = null;
+        try {
+            basRgvService = SpringUtils.getBean(BasRgvService.class);
+        } catch (Exception e) {
+
+        }
         if (basRgvService != null) {
             BasRgv basRgv = basRgvService.selectOne(new EntityWrapper<BasRgv>().eq("rgv_no", deviceConfig.getDeviceNo()));
             if(basRgv == null) {
@@ -143,7 +161,9 @@
 
     @Override
     public void close() {
-        zyRgvConnectDriver.close();
+        if (zyRgvConnectDriver != null) {
+            zyRgvConnectDriver.close();
+        }
     }
 
     @Override
@@ -188,6 +208,31 @@
     @Override
     public synchronized CommandResponse sendCommand(RgvCommand command) {
         rgvProtocol.setLastCommandTime(System.currentTimeMillis());
-        return zyRgvConnectDriver.sendCommand(command);
+        CommandResponse response = null;
+        try {
+            response = zyRgvConnectDriver.sendCommand(command);
+            return response;
+        } finally {
+            BasRgvOptService bean = SpringUtils.getBean(BasRgvOptService.class);
+            ZyRgvStatusEntity statusEntity = zyRgvConnectDriver.getStatus();
+            BasRgvOpt basRgvOpt = new BasRgvOpt(
+                    command.getTaskNo(),
+                    command.getRgvNo(),
+                    new Date(),
+                    String.valueOf(command.getTaskMode()),
+                    String.valueOf(command.getSourcePos()),
+                    String.valueOf(command.getTargetPos()),
+                    null,
+                    null,
+                    null,
+                    JSON.toJSONString(command),
+                    JSON.toJSONString(statusEntity),
+                    1,
+                    JSON.toJSONString(response)
+            );
+            if (bean != null) {
+                bean.insert(basRgvOpt);
+            }
+        }
     }
-}
\ No newline at end of file
+}

--
Gitblit v1.9.1