From e76f0739e647fe8a7e0e2618914e2faff554b1b7 Mon Sep 17 00:00:00 2001
From: huang <1532065656@qq.com>
Date: 星期一, 17 十一月 2025 17:33:23 +0800
Subject: [PATCH] 解决冲突

---
 mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DevicePlcOperationServiceImpl.java |  290 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 290 insertions(+), 0 deletions(-)

diff --git a/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DevicePlcOperationServiceImpl.java b/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DevicePlcOperationServiceImpl.java
new file mode 100644
index 0000000..c3c50d1
--- /dev/null
+++ b/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DevicePlcOperationServiceImpl.java
@@ -0,0 +1,290 @@
+package com.mes.device.service.impl;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.mes.device.entity.DeviceConfig;
+import com.mes.device.service.DeviceConfigService;
+import com.mes.device.service.DeviceGroupRelationService;
+import com.mes.device.service.DevicePlcOperationService;
+import com.mes.device.vo.DeviceGroupVO;
+import com.mes.device.vo.DevicePlcVO;
+import com.mes.service.PlcTestWriteService;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+
+import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.stream.Collectors;
+
+/**
+ * 璁惧 PLC 鎿嶄綔鏈嶅姟瀹炵幇
+ *
+ * @author mes
+ * @since 2025-11-17
+ */
+@Slf4j
+@Service
+@RequiredArgsConstructor
+public class DevicePlcOperationServiceImpl implements DevicePlcOperationService {
+
+    private static final String PLC_PROJECT_ID_KEY = "plcProjectId";
+
+    private final DeviceConfigService deviceConfigService;
+    private final DeviceGroupRelationService deviceGroupRelationService;
+    private final PlcTestWriteService plcTestWriteService;
+    private final ObjectMapper objectMapper;
+
+    @Override
+    public DevicePlcVO.OperationResult triggerRequest(Long deviceId) {
+        return executeOperation(deviceId, PlcOperationType.REQUEST);
+    }
+
+    @Override
+    public List<DevicePlcVO.OperationResult> triggerRequest(List<Long> deviceIds) {
+        return executeBatch(deviceIds, PlcOperationType.REQUEST);
+    }
+
+    @Override
+    public List<DevicePlcVO.OperationResult> triggerRequestByGroup(Long groupId) {
+        return executeBatch(getDeviceIdsByGroup(groupId), PlcOperationType.REQUEST);
+    }
+
+    @Override
+    public DevicePlcVO.OperationResult triggerReport(Long deviceId) {
+        return executeOperation(deviceId, PlcOperationType.REPORT);
+    }
+
+    @Override
+    public List<DevicePlcVO.OperationResult> triggerReport(List<Long> deviceIds) {
+        return executeBatch(deviceIds, PlcOperationType.REPORT);
+    }
+
+    @Override
+    public List<DevicePlcVO.OperationResult> triggerReportByGroup(Long groupId) {
+        return executeBatch(getDeviceIdsByGroup(groupId), PlcOperationType.REPORT);
+    }
+
+    @Override
+    public DevicePlcVO.OperationResult resetDevice(Long deviceId) {
+        return executeOperation(deviceId, PlcOperationType.RESET);
+    }
+
+    @Override
+    public List<DevicePlcVO.OperationResult> resetDevices(List<Long> deviceIds) {
+        return executeBatch(deviceIds, PlcOperationType.RESET);
+    }
+
+    @Override
+    public DevicePlcVO.StatusInfo readStatus(Long deviceId) {
+        DeviceConfig device = deviceConfigService.getDeviceById(deviceId);
+        if (device == null) {
+            return DevicePlcVO.StatusInfo.builder()
+                    .deviceId(deviceId)
+                    .deviceName("鏈煡璁惧")
+                    .data(Collections.emptyMap())
+                    .timestamp(LocalDateTime.now())
+                    .build();
+        }
+
+        try {
+            Map<String, Object> data = plcTestWriteService.readPlcStatusByDevice(deviceId);
+            return DevicePlcVO.StatusInfo.builder()
+                    .deviceId(device.getId())
+                    .deviceName(device.getDeviceName())
+                    .deviceCode(device.getDeviceCode())
+                    .projectId(String.valueOf(device.getProjectId()))
+                    .data(data)
+                    .timestamp(LocalDateTime.now())
+                    .build();
+        } catch (Exception e) {
+            log.error("璇诲彇璁惧 PLC 鐘舵�佸け璐�, deviceId={}", deviceId, e);
+            return DevicePlcVO.StatusInfo.builder()
+                    .deviceId(device.getId())
+                    .deviceName(device.getDeviceName())
+                    .deviceCode(device.getDeviceCode())
+                    .projectId(null)
+                    .data(Collections.emptyMap())
+                    .timestamp(LocalDateTime.now())
+                    .build();
+        }
+    }
+
+    @Override
+    public List<DevicePlcVO.StatusInfo> readStatusByGroup(Long groupId) {
+        List<Long> deviceIds = getDeviceIdsByGroup(groupId);
+        if (CollectionUtils.isEmpty(deviceIds)) {
+            return Collections.emptyList();
+        }
+        return deviceIds.stream()
+                .map(this::readStatus)
+                .collect(Collectors.toList());
+    }
+
+    private List<DevicePlcVO.OperationResult> executeBatch(List<Long> deviceIds, PlcOperationType type) {
+        if (CollectionUtils.isEmpty(deviceIds)) {
+            return Collections.emptyList();
+        }
+        return deviceIds.stream()
+                .filter(Objects::nonNull)
+                .distinct()
+                .map(id -> executeOperation(id, type))
+                .collect(Collectors.toList());
+    }
+
+    private DevicePlcVO.OperationResult executeOperation(Long deviceId, PlcOperationType type) {
+        DeviceConfig device = deviceConfigService.getDeviceById(deviceId);
+        if (device == null) {
+            return buildResult(deviceId, null, null, type, false, "璁惧涓嶅瓨鍦�");
+        }
+
+        try {
+            boolean success = invokeOperation(type, deviceId);
+            String message = success ? type.successMsg : type.failedMsg;
+            return buildResult(device.getId(), device, String.valueOf(device.getProjectId()), type, success, message);
+        } catch (Exception e) {
+            log.error("鎵ц PLC 鎿嶄綔澶辫触, deviceId={}, operation={}", deviceId, type, e);
+            return buildResult(device.getId(), device, null, type, false, e.getMessage());
+        }
+    }
+
+    private boolean invokeOperation(PlcOperationType type, Long deviceId) {
+        switch (type) {
+            case REQUEST:
+                return plcTestWriteService.simulatePlcRequestByDevice(deviceId);
+            case REPORT:
+                return plcTestWriteService.simulatePlcReportByDevice(deviceId);
+            case RESET:
+                return plcTestWriteService.resetPlcByDevice(deviceId);
+            default:
+                return false;
+        }
+    }
+
+    private DevicePlcVO.OperationResult buildResult(Long deviceId, DeviceConfig device, String projectId,
+                                                    PlcOperationType type, boolean success, String message) {
+        return DevicePlcVO.OperationResult.builder()
+                .deviceId(deviceId)
+                .deviceName(device != null ? device.getDeviceName() : "鏈煡璁惧")
+                .deviceCode(device != null ? device.getDeviceCode() : null)
+                .projectId(projectId)
+                .operation(type.display)
+                .success(success)
+                .message(message)
+                .timestamp(LocalDateTime.now())
+                .build();
+    }
+
+    private List<Long> getDeviceIdsByGroup(Long groupId) {
+        if (groupId == null) {
+            return Collections.emptyList();
+        }
+        try {
+            List<DeviceGroupVO.DeviceInfo> devices = deviceGroupRelationService.getGroupDevices(groupId);
+            if (CollectionUtils.isEmpty(devices)) {
+                return Collections.emptyList();
+            }
+            return devices.stream()
+                    .map(DeviceGroupVO.DeviceInfo::getId)
+                    .filter(Objects::nonNull)
+                    .collect(Collectors.toList());
+        } catch (Exception e) {
+            log.error("鑾峰彇璁惧缁勮澶囧け璐�, groupId={}", groupId, e);
+            return Collections.emptyList();
+        }
+    }
+
+    @Override
+    public DevicePlcVO.OperationResult writeFields(Long deviceId, Map<String, Object> fieldValues, String operationName) {
+        DeviceConfig device = deviceConfigService.getDeviceById(deviceId);
+        if (device == null) {
+            return buildResult(deviceId, null, null, PlcOperationType.REQUEST, false, "璁惧涓嶅瓨鍦�");
+        }
+        try {
+            boolean success = plcTestWriteService.writeFieldsByDevice(deviceId, fieldValues);
+            String opName = operationName != null ? operationName : "PLC鍐欏叆";
+            return DevicePlcVO.OperationResult.builder()
+                    .deviceId(device.getId())
+                    .deviceName(device.getDeviceName())
+                    .deviceCode(device.getDeviceCode())
+                    .projectId(String.valueOf(device.getProjectId()))
+                    .operation(opName)
+                    .success(success)
+                    .message(success ? opName + "鎴愬姛" : opName + "澶辫触")
+                    .timestamp(LocalDateTime.now())
+                    .build();
+        } catch (Exception e) {
+            log.error("鍐欏叆PLC瀛楁澶辫触, deviceId={}", deviceId, e);
+            return DevicePlcVO.OperationResult.builder()
+                    .deviceId(device.getId())
+                    .deviceName(device.getDeviceName())
+                    .deviceCode(device.getDeviceCode())
+                    .projectId(null)
+                    .operation(operationName)
+                    .success(false)
+                    .message(e.getMessage())
+                    .timestamp(LocalDateTime.now())
+                    .build();
+        }
+    }
+
+    @Override
+    public String resolveProjectId(Long deviceId) {
+        DeviceConfig device = deviceConfigService.getDeviceById(deviceId);
+        if (device == null) {
+            throw new IllegalArgumentException("璁惧涓嶅瓨鍦�: " + deviceId);
+        }
+        return resolveProjectId(device);
+    }
+
+    private String resolveProjectId(DeviceConfig device) {
+        if (device == null) {
+            throw new IllegalArgumentException("璁惧淇℃伅涓虹┖");
+        }
+
+        String extra = device.getExtraParams();
+        if (extra != null && !extra.isEmpty()) {
+            try {
+                Map<String, Object> extraParams = objectMapper.readValue(extra, new TypeReference<Map<String, Object>>() {});
+                Object plcProjectId = extraParams.get(PLC_PROJECT_ID_KEY);
+                if (plcProjectId != null) {
+                    return String.valueOf(plcProjectId);
+                }
+            } catch (Exception e) {
+                log.warn("瑙f瀽璁惧鎵╁睍鍙傛暟澶辫触, deviceId={}", device.getId(), e);
+            }
+        }
+
+        if (device.getProjectId() != null) {
+            return String.valueOf(device.getProjectId());
+        }
+
+        if (device.getDeviceCode() != null && !device.getDeviceCode().isEmpty()) {
+            return device.getDeviceCode();
+        }
+
+        throw new IllegalStateException("鏃犳硶瑙f瀽璁惧鐨� PLC 椤圭洰鏍囪瘑, deviceId=" + device.getId());
+    }
+
+    private enum PlcOperationType {
+        REQUEST("PLC璇锋眰", "PLC 璇锋眰鍙戦�佹垚鍔�", "PLC 璇锋眰鍙戦�佸け璐�"),
+        REPORT("PLC姹囨姤", "PLC 姹囨姤妯℃嫙鎴愬姛", "PLC 姹囨姤妯℃嫙澶辫触"),
+        RESET("PLC閲嶇疆", "PLC 鐘舵�佸凡閲嶇疆", "PLC 鐘舵�侀噸缃け璐�");
+
+        private final String display;
+        private final String successMsg;
+        private final String failedMsg;
+
+        PlcOperationType(String display, String successMsg, String failedMsg) {
+            this.display = display;
+            this.successMsg = successMsg;
+            this.failedMsg = failedMsg;
+        }
+    }
+}
+

--
Gitblit v1.8.0