From fc4e5c458094c6bf5238d7d21291325f19a57adb Mon Sep 17 00:00:00 2001
From: huang <1532065656@qq.com>
Date: 星期一, 22 十二月 2025 16:57:21 +0800
Subject: [PATCH] 修改:逻辑交互写入使用plc通讯工厂统一接口

---
 mes-processes/mes-plcSend/src/main/java/com/mes/interaction/workstation/transfer/handler/HorizontalTransferLogicHandler.java |   52 +++++++++++++++++++++++-----------------------------
 1 files changed, 23 insertions(+), 29 deletions(-)

diff --git a/mes-processes/mes-plcSend/src/main/java/com/mes/interaction/workstation/transfer/handler/HorizontalTransferLogicHandler.java b/mes-processes/mes-plcSend/src/main/java/com/mes/interaction/workstation/transfer/handler/HorizontalTransferLogicHandler.java
index b5d7d50..aead560 100644
--- a/mes-processes/mes-plcSend/src/main/java/com/mes/interaction/workstation/transfer/handler/HorizontalTransferLogicHandler.java
+++ b/mes-processes/mes-plcSend/src/main/java/com/mes/interaction/workstation/transfer/handler/HorizontalTransferLogicHandler.java
@@ -9,9 +9,8 @@
 import com.mes.device.vo.DevicePlcVO;
 import com.mes.interaction.workstation.base.WorkstationBaseHandler;
 import com.mes.interaction.workstation.config.WorkstationLogicConfig;
-import com.mes.s7.enhanced.EnhancedS7Serializer;
-import com.mes.s7.provider.S7SerializerProvider;
-import com.mes.service.PlcDynamicDataService;
+import com.mes.plc.client.PlcClient;
+import com.mes.plc.factory.PlcClientFactory;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
@@ -32,9 +31,8 @@
 @Component
 public class HorizontalTransferLogicHandler extends WorkstationBaseHandler {
 
-    private final PlcDynamicDataService plcDynamicDataService;
     private final GlassInfoService glassInfoService;
-    private final S7SerializerProvider s7SerializerProvider;
+    private final PlcClientFactory plcClientFactory;
     
     @Autowired(required = false)
     private DeviceGlassInfoMapper glassInfoMapper;
@@ -57,13 +55,13 @@
 
     @Autowired
     public HorizontalTransferLogicHandler(DevicePlcOperationService devicePlcOperationService,
-                                         PlcDynamicDataService plcDynamicDataService,
                                          @Qualifier("deviceGlassInfoService") GlassInfoService glassInfoService,
-                                         S7SerializerProvider s7SerializerProvider) {
+                                         PlcClientFactory plcClientFactory) {
         super(devicePlcOperationService);
-        this.plcDynamicDataService = plcDynamicDataService;
         this.glassInfoService = glassInfoService;
-        this.s7SerializerProvider = s7SerializerProvider;
+        this.plcClientFactory = plcClientFactory;
+        // 璁剧疆 PlcClientFactory 鍒板熀绫�
+        this.setPlcClientFactory(plcClientFactory);
     }
 
     @Override
@@ -116,10 +114,10 @@
             Map<String, Object> params) {
         
         String deviceId = deviceConfig.getDeviceId();
-        EnhancedS7Serializer serializer = s7SerializerProvider.getSerializer(deviceConfig);
-        if (serializer == null) {
+        PlcClient plcClient = getPlcClient(deviceConfig);
+        if (plcClient == null) {
             return buildResult(deviceConfig, "checkAndProcess", false, 
-                    "鑾峰彇PLC搴忓垪鍖栧櫒澶辫触");
+                    "鑾峰彇PLC瀹㈡埛绔け璐�");
         }
 
         try {
@@ -184,7 +182,7 @@
 
             // 6. 鍐欏叆PLC锛堝皾璇曚粠浠诲姟鍙傛暟涓幏鍙栧崸杞珛缂栧彿锛�
             DevicePlcVO.OperationResult writeResult = writeBatchToPlc(
-                    deviceConfig, batch, serializer, logicParams, params);
+                    deviceConfig, batch, plcClient, logicParams, params);
             
             if (!Boolean.TRUE.equals(writeResult.getSuccess())) {
                 return writeResult;
@@ -261,12 +259,10 @@
             // 浠庨厤缃腑鑾峰彇workLine锛岀敤浜庤繃婊わ紙閰嶇疆涓槸Integer绫诲瀷锛�
             Integer workLine = getLogicParam(logicParams, "workLine", null);
             
-            // 鏌ヨ鏈�杩�2鍒嗛挓鍐呯殑鐜荤拑璁板綍锛堟墿澶ф椂闂寸獥鍙o紝纭繚涓嶉仐婕忥級
-            Date twoMinutesAgo = new Date(System.currentTimeMillis() - 120000);
-            
+            // 鏌ヨstate=1鐨勭幓鐠冭褰曪紙宸叉壂鐮佷氦浜掑畬鎴愶紝绛夊緟鍗ц浆绔嬪鐞嗭級
             LambdaQueryWrapper<GlassInfo> wrapper = new LambdaQueryWrapper<>();
             wrapper.in(GlassInfo::getStatus, GlassInfo.Status.PENDING, GlassInfo.Status.ACTIVE)
-                   .ge(GlassInfo::getCreatedTime, twoMinutesAgo)
+                   .eq(GlassInfo::getState, 1) // 鍙煡璇tate=1鐨勭幓鐠冿紙宸叉壂鐮佸畬鎴愶級
                    .orderByDesc(GlassInfo::getCreatedTime)
                    .last("LIMIT 20"); // 闄愬埗鏌ヨ鏁伴噺锛岄伩鍏嶈繃澶�
             
@@ -403,7 +399,7 @@
     private DevicePlcVO.OperationResult writeBatchToPlc(
             DeviceConfig deviceConfig,
             List<GlassInfo> batch,
-            EnhancedS7Serializer serializer,
+            PlcClient plcClient,
             Map<String, Object> logicParams,
             Map<String, Object> params) {
         
@@ -419,19 +415,16 @@
         // 鍐欏叆鐜荤拑鏁伴噺
         payload.put("plcGlassCount", count);
         
-        // 鍐欏叆鍗ц浆绔嬬紪鍙凤紙浼樺厛浠庝换鍔″弬鏁拌幏鍙栵紝鍏舵浠庤澶囬厤缃幏鍙栵級
-        Integer inPosition = null;
+        // 鍐欏叆鍗ц浆绔嬬紪鍙凤紙浼樺厛浠庝换鍔″弬鏁拌幏鍙栵紝鍏舵浠庤澶囬厤缃幏鍙栵紝鐩存帴鍐欏叆缂栧彿锛屼笉杩涜浣嶇疆鏄犲皠锛�
+        Object inPosition = null;
         if (params != null) {
             try {
                 Object ctxObj = params.get("_taskContext");
                 if (ctxObj instanceof com.mes.task.model.TaskExecutionContext) {
                     com.mes.task.model.TaskExecutionContext ctx =
                             (com.mes.task.model.TaskExecutionContext) ctxObj;
-                    Object positionObj = ctx.getParameters().getExtra() != null 
+                    inPosition = ctx.getParameters().getExtra() != null 
                             ? ctx.getParameters().getExtra().get("inPosition") : null;
-                    if (positionObj instanceof Number) {
-                        inPosition = ((Number) positionObj).intValue();
-                    }
                 }
             } catch (Exception e) {
                 log.debug("浠庝换鍔″弬鏁拌幏鍙栧崸杞珛缂栧彿澶辫触: deviceId={}", deviceConfig.getId(), e);
@@ -442,6 +435,7 @@
             inPosition = getLogicParam(logicParams, "inPosition", null);
         }
         if (inPosition != null) {
+            // 鐩存帴鍐欏叆缂栧彿鏈韩锛屼笉杩涜浣嶇疆鏄犲皠杞崲
             payload.put("inPosition", inPosition);
             log.info("鍐欏叆鍗ц浆绔嬬紪鍙�: deviceId={}, inPosition={}", deviceConfig.getId(), inPosition);
         } else {
@@ -452,7 +446,7 @@
         payload.put("plcRequest", 1);
         
         try {
-            plcDynamicDataService.writePlcData(deviceConfig, payload, serializer);
+            plcClient.writeData(payload);
             log.info("鎵规宸插啓鍏LC: deviceId={}, glassCount={}, inPosition={}", 
                     deviceConfig.getId(), count, inPosition);
             return buildResult(deviceConfig, "writeBatchToPlc", true, 
@@ -544,9 +538,9 @@
      */
     private DevicePlcVO.OperationResult handleClearPlc(DeviceConfig deviceConfig) {
         try {
-            EnhancedS7Serializer serializer = s7SerializerProvider.getSerializer(deviceConfig);
-            if (serializer == null) {
-                return buildResult(deviceConfig, "clearPlc", false, "鑾峰彇PLC搴忓垪鍖栧櫒澶辫触");
+            PlcClient plcClient = getPlcClient(deviceConfig);
+            if (plcClient == null) {
+                return buildResult(deviceConfig, "clearPlc", false, "鑾峰彇PLC瀹㈡埛绔け璐�");
             }
             
             Map<String, Object> payload = new HashMap<>();
@@ -558,7 +552,7 @@
             payload.put("plcRequest", 0);
             payload.put("inPosition", 0);
             
-            plcDynamicDataService.writePlcData(deviceConfig, payload, serializer);
+            plcClient.writeData(payload);
             log.info("鍗ц浆绔嬩富浣撴竻绌篜LC瀛楁瀹屾垚: deviceId={}", deviceConfig.getId());
             return buildResult(deviceConfig, "clearPlc", true, "宸叉竻绌哄崸杞珛涓讳綋PLC瀛楁");
         } catch (Exception e) {

--
Gitblit v1.8.0