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/scanner/handler/HorizontalScannerLogicHandler.java |  119 +++++++++++++++++++++++++++++++----------------------------
 1 files changed, 63 insertions(+), 56 deletions(-)

diff --git a/mes-processes/mes-plcSend/src/main/java/com/mes/interaction/workstation/scanner/handler/HorizontalScannerLogicHandler.java b/mes-processes/mes-plcSend/src/main/java/com/mes/interaction/workstation/scanner/handler/HorizontalScannerLogicHandler.java
index fd1c2a1..4b9338f 100644
--- a/mes-processes/mes-plcSend/src/main/java/com/mes/interaction/workstation/scanner/handler/HorizontalScannerLogicHandler.java
+++ b/mes-processes/mes-plcSend/src/main/java/com/mes/interaction/workstation/scanner/handler/HorizontalScannerLogicHandler.java
@@ -7,9 +7,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 com.mes.task.model.TaskExecutionContext;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
@@ -18,6 +17,7 @@
 import java.time.LocalDateTime;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -32,18 +32,16 @@
 
     private static final List<String> MES_FIELDS = Arrays.asList("mesSend", "mesGlassId", "mesWidth", "mesHeight", "workLine");
 
-    private final PlcDynamicDataService plcDynamicDataService;
     private final GlassInfoService glassInfoService;
-    private final S7SerializerProvider s7SerializerProvider;
+    private final PlcClientFactory plcClientFactory;
 
     public HorizontalScannerLogicHandler(DevicePlcOperationService devicePlcOperationService,
-                                         PlcDynamicDataService plcDynamicDataService,
                                          GlassInfoService glassInfoService,
-                                         S7SerializerProvider s7SerializerProvider) {
+                                         PlcClientFactory plcClientFactory) {
         super(devicePlcOperationService);
-        this.plcDynamicDataService = plcDynamicDataService;
         this.glassInfoService = glassInfoService;
-        this.s7SerializerProvider = s7SerializerProvider;
+        this.plcClientFactory = plcClientFactory;
+        this.setPlcClientFactory(plcClientFactory);
     }
 
     @Override
@@ -56,22 +54,22 @@
                                                     String operation,
                                                     Map<String, Object> params,
                                                     Map<String, Object> logicParams) {
-        EnhancedS7Serializer serializer = s7SerializerProvider.getSerializer(deviceConfig);
-        if (serializer == null) {
-            return buildResult(deviceConfig, operation, false, "鑾峰彇PLC搴忓垪鍖栧櫒澶辫触", null);
+        PlcClient plcClient = getPlcClient(deviceConfig);
+        if (plcClient == null) {
+            return buildResult(deviceConfig, operation, false, "鑾峰彇PLC瀹㈡埛绔け璐�", null);
         }
 
         if ("clearPlc".equalsIgnoreCase(operation) || "reset".equalsIgnoreCase(operation)) {
-            return clearPlc(deviceConfig, serializer);
+            return clearPlc(deviceConfig, plcClient);
         }
 
         WorkstationLogicConfig config = parseWorkstationConfig(logicParams);
-        return executeScan(deviceConfig, config, serializer, params);
+        return executeScan(deviceConfig, config, plcClient, params);
     }
 
     private DevicePlcVO.OperationResult executeScan(DeviceConfig deviceConfig,
                                                     WorkstationLogicConfig config,
-                                                    EnhancedS7Serializer serializer,
+                                                    PlcClient plcClient,
                                                     Map<String, Object> params) {
         try {
             // 浠庡弬鏁颁腑鑾峰彇鐜荤拑ID锛堝畾鏃跺櫒姣忔鍙鐞嗕竴涓級
@@ -84,7 +82,7 @@
             }
             
             // 鎵ц鍗曟鎵弿锛堝畾鏃跺櫒浼氬惊鐜皟鐢ㄦ鏂规硶锛�
-            return executeSingleScan(deviceConfig, config, serializer, inputGlassId, params);
+            return executeSingleScan(deviceConfig, config, plcClient, inputGlassId, params);
         } catch (InterruptedException e) {
             Thread.currentThread().interrupt();
             log.warn("鍗ц浆绔嬫壂鐮佺瓑寰匨ES鏁版嵁琚腑鏂�, deviceId={}", deviceConfig.getId(), e);
@@ -100,17 +98,19 @@
      */
     private DevicePlcVO.OperationResult executeSingleScan(DeviceConfig deviceConfig,
                                                           WorkstationLogicConfig config,
-                                                          EnhancedS7Serializer serializer,
+                                                          PlcClient plcClient,
                                                           String inputGlassId,
                                                           Map<String, Object> params) throws InterruptedException {
         // 1. 鍐欏叆plcRequest=1鍜宲lcGlassId锛堝鏋滄彁渚涗簡鐜荤拑ID锛�
-        triggerScanRequest(deviceConfig, serializer, inputGlassId);
+        triggerScanRequest(deviceConfig, plcClient, inputGlassId);
         
         // 2. 绛夊緟MES鍥炲啓mesSend=1浠ュ強鐜荤拑淇℃伅
-        Map<String, Object> mesData = waitForMesData(deviceConfig, serializer, config);
+        Map<String, Object> mesData = waitForMesData(deviceConfig, plcClient, config);
         if (mesData == null || mesData.isEmpty()) {
             log.error("绛夊緟MES鍐欏叆鐜荤拑淇℃伅瓒呮椂: deviceId={}, timeout={}ms", 
                     deviceConfig.getId(), config.getScanIntervalMs());
+            // 瓒呮椂涔熻娓呯┖plcRequest
+            clearPlcRequestFields(deviceConfig, plcClient);
             return buildResult(deviceConfig, "scanOnce", false,
                     String.format("绛夊緟MES鍐欏叆鐜荤拑淇℃伅瓒呮椂(%dms)", config.getScanIntervalMs()), null);
         }
@@ -118,38 +118,40 @@
         // 3. 璇诲彇MES鍥炲啓鐨勭幓鐠冧俊鎭�
         String glassId = parseString(mesData.get("mesGlassId"));
         if (!StringUtils.hasText(glassId)) {
+            // MES鏈彁渚涚幓鐠僆D涔熻娓呯┖plcRequest
+            clearPlcRequestFields(deviceConfig, plcClient);
             return buildResult(deviceConfig, "scanOnce", false, "MES鍐欏尯鏈彁渚涚幓鐠僆D", null);
         }
-        // 璇诲彇MES灏哄鏁版嵁锛歮esWidth=琛ㄥ锛宮esHeight=闀�
+        // 璇诲彇MES灏哄鏁版嵁锛歮esWidth=瀹斤紝mesHeight=闀�
         Integer rawWidth = parseInteger(mesData.get("mesWidth"));
         Integer rawHeight = parseInteger(mesData.get("mesHeight"));
         Integer workLine = parseInteger(mesData.get("workLine"));
 
         // 4. 娓呯┖plcRequest鍜宲lcGlassId锛堝彧娓呴櫎PLC瀛楁锛�
-        clearPlcRequestFields(deviceConfig, serializer);
+        clearPlcRequestFields(deviceConfig, plcClient);
 
+        // 5. 鏇存柊鐜荤拑淇℃伅鐘舵�侊細灏唖tate浠�0鏀逛负1锛堝凡鎵爜浜や簰锛�
+        boolean updated = glassInfoService.updateGlassStateAfterScan(glassId, rawWidth, rawHeight, workLine);
+        if (!updated) {
+            log.warn("鏇存柊鐜荤拑淇℃伅鐘舵�佸け璐ワ紝鐜荤拑鍙兘涓嶅瓨鍦�: glassId={}", glassId);
+            // 涓嶈繑鍥為敊璇紝缁х画鎵ц锛屽洜涓哄彲鑳芥槸鏂扮幓鐠冭繕鏈鍏�
+        }
+        
+        // 6. 灏嗘壂鎻忓埌鐨勭幓鐠僆D淇濆瓨鍒板叡浜暟鎹腑锛堜緵澶ц溅璁惧瀹氭椂鍣ㄨ鍙栵級
+        saveScannedGlassId(params, glassId);
 
-            // 5. 淇濆瓨鐜荤拑淇℃伅鍒版暟鎹簱
-            GlassInfo glassInfo = buildGlassInfo(glassId, rawWidth, rawHeight, workLine);
-            boolean saved = glassInfoService.saveOrUpdateGlassInfo(glassInfo);
-            if (!saved) {
-                return buildResult(deviceConfig, "scanOnce", false, "淇濆瓨鐜荤拑淇℃伅澶辫触: " + glassId, null);
-            }
-            
-            // 6. 灏嗘壂鎻忓埌鐨勭幓鐠僆D淇濆瓨鍒板叡浜暟鎹腑锛堜緵澶ц溅璁惧瀹氭椂鍣ㄨ鍙栵級
-            saveScannedGlassId(params, glassId);
-
-            String msg = String.format("鐜荤拑[%s] 灏哄[琛ㄥ:%s x 闀�:%s] 宸叉帴鏀跺苟鍏ュ簱锛寃orkLine=%s",
-                    glassId,
-                    rawWidth != null ? rawWidth + "mm" : "-",
-                    rawHeight != null ? rawHeight + "mm" : "-",
-                    workLine != null ? workLine : "-");
-            Map<String, Object> resultData = new HashMap<>();
-            resultData.put("glassIds", Collections.singletonList(glassId));
-            if (workLine != null) {
-                resultData.put("workLine", workLine);
-            }
-            return buildResult(deviceConfig, "scanOnce", true, msg, resultData);
+        Integer intervalMs = config != null ? config.getScanIntervalMs() : null;
+        String msg = String.format("鐜荤拑[%s] 灏哄[瀹�:%s x 闀�:%s] 宸叉帴鏀讹紝workLine=%s",
+                glassId,
+                rawWidth != null ? rawWidth + "mm" : "-",
+                rawHeight != null ? rawHeight + "mm" : "-",
+                workLine != null ? workLine : "-");
+        Map<String, Object> resultData = new HashMap<>();
+        resultData.put("glassIds", Collections.singletonList(glassId));
+        if (workLine != null) {
+            resultData.put("workLine", workLine);
+        }
+        return buildResult(deviceConfig, "scanOnce", true, msg, resultData);
     }
     
     /**
@@ -183,13 +185,13 @@
     }
 
     private DevicePlcVO.OperationResult clearPlc(DeviceConfig deviceConfig,
-                                                 EnhancedS7Serializer serializer) {
+                                                 PlcClient plcClient) {
         try {
             // 鍙竻绌篜LC鎿嶄綔鍖哄瓧娈碉紙plcRequest銆乸lcGlassId锛夛紝涓嶆竻绌篗ES鍐欏尯瀛楁
             Map<String, Object> resetFields = new HashMap<>();
             resetFields.put("plcRequest", 0);
             resetFields.put("plcGlassId", "");
-            plcDynamicDataService.writePlcData(deviceConfig, resetFields, serializer);
+            plcClient.writeData(resetFields);
             return buildResult(deviceConfig, "clearPlc", true, "宸叉竻绌篜LC鎿嶄綔鍖哄瓧娈碉紙淇濈暀MES鍐欏尯瀛楁锛�", null);
         } catch (Exception e) {
             log.error("鍗ц浆绔嬫壂鐮佹竻绌篜LC澶辫触, deviceId={}", deviceConfig.getId(), e);
@@ -200,7 +202,7 @@
     /**
      * 瑙﹀彂MES璇锋眰锛氬啓鍏lcRequest=1鍜宲lcGlassId锛堝鏋滄彁渚涗簡鐜荤拑ID锛�
      */
-    private void triggerScanRequest(DeviceConfig deviceConfig, EnhancedS7Serializer serializer, String glassId) {
+    private void triggerScanRequest(DeviceConfig deviceConfig, PlcClient plcClient, String glassId) {
         Map<String, Object> writeFields = new HashMap<>();
         writeFields.put("plcRequest", 1);
         
@@ -208,18 +210,18 @@
             writeFields.put("plcGlassId", glassId);
         }
         
-        plcDynamicDataService.writePlcData(deviceConfig, writeFields, serializer);
+        plcClient.writeData(writeFields);
     }
     
     /**
      * 娓呯┖PLC璇锋眰瀛楁锛氬彧娓呴櫎plcRequest鍜宲lcGlassId锛堜笉娓呴櫎MES鍐欏尯瀛楁锛�
      */
-    private void clearPlcRequestFields(DeviceConfig deviceConfig, EnhancedS7Serializer serializer) {
+    private void clearPlcRequestFields(DeviceConfig deviceConfig, PlcClient plcClient) {
         try {
             Map<String, Object> clearFields = new HashMap<>();
             clearFields.put("plcRequest", 0);
             clearFields.put("plcGlassId", "");
-            plcDynamicDataService.writePlcData(deviceConfig, clearFields, serializer);
+            plcClient.writeData(clearFields);
         } catch (Exception e) {
             log.error("娓呯┖PLC璇锋眰瀛楁澶辫触: deviceId={}", deviceConfig.getId(), e);
             // 涓嶆竻绌轰笉褰卞搷涓绘祦绋嬶紝鍙褰曢敊璇�
@@ -227,14 +229,14 @@
     }
 
     private Map<String, Object> waitForMesData(DeviceConfig deviceConfig,
-                                               EnhancedS7Serializer serializer,
+                                               PlcClient plcClient,
                                                WorkstationLogicConfig config) throws InterruptedException {
         long timeoutMs = Math.max(config.getScanIntervalMs(), 3_000);
         long deadline = System.currentTimeMillis() + timeoutMs;
         int pollInterval = Math.max(200, Math.min(config.getScanIntervalMs() / 5, 1_000));
         
         while (System.currentTimeMillis() < deadline) {
-            Map<String, Object> mesData = plcDynamicDataService.readPlcData(deviceConfig, MES_FIELDS, serializer);
+            Map<String, Object> mesData = plcClient.readData(MES_FIELDS.toArray(new String[0]));
             
             if (mesData != null && !mesData.isEmpty()) {
                 Integer mesSend = parseInteger(mesData.get("mesSend"));
@@ -254,7 +256,7 @@
         
         // 瓒呮椂鍓嶆渶鍚庝竴娆″皾璇曡鍙�
         log.warn("绛夊緟MES鏁版嵁瓒呮椂: deviceId={}, timeout={}ms", deviceConfig.getId(), timeoutMs);
-        Map<String, Object> lastMesData = plcDynamicDataService.readPlcData(deviceConfig, MES_FIELDS, serializer);
+        Map<String, Object> lastMesData = plcClient.readData(MES_FIELDS.toArray(new String[0]));
         if (lastMesData != null && !lastMesData.isEmpty()) {
             log.warn("瓒呮椂鍓嶆渶鍚庝竴娆¤鍙栧埌鐨勬暟鎹�: deviceId={}, mesData={}", 
                     deviceConfig.getId(), lastMesData);
@@ -266,17 +268,22 @@
     private GlassInfo buildGlassInfo(String glassId, Integer width, Integer height, Integer workLine) {
         GlassInfo glassInfo = new GlassInfo();
         glassInfo.setGlassId(glassId.trim());
-        // mesWidth=琛ㄥ -> glassWidth, mesHeight=闀� -> glassLength
+        // mesWidth=瀹� -> glassWidth, mesHeight=闀� -> glassLength
         if (width != null) {
-            glassInfo.setGlassWidth(width);  // 琛ㄥ
+            glassInfo.setGlassWidth(width);
         }
         if (height != null) {
-            glassInfo.setGlassLength(height); // 闀�
+            glassInfo.setGlassLength(height);
         }
-        glassInfo.setStatus(GlassInfo.Status.ACTIVE);
+        glassInfo.setStatus(GlassInfo.Status.PENDING);
         if (workLine != null) {
-            glassInfo.setDescription("workLine=" + workLine);
+            glassInfo.setWorkLine(workLine);
         }
+        Date now = new Date();
+        glassInfo.setCreatedTime(now);
+        glassInfo.setUpdatedTime(now);
+        glassInfo.setCreatedBy("system");
+        glassInfo.setUpdatedBy("system");
         return glassInfo;
     }
 

--
Gitblit v1.8.0