huang
2025-12-01 dad0263459b30dbfa75f06dff062a0c85183517b
mes-processes/mes-plcSend/src/main/java/com/mes/interaction/DeviceLogicHandlerFactory.java
@@ -3,6 +3,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.util.HashMap;
@@ -33,8 +34,12 @@
        if (handlers != null) {
            for (DeviceLogicHandler handler : handlers) {
                String deviceType = handler.getDeviceType();
                if (deviceType != null && !deviceType.isEmpty()) {
                    handlerMap.put(deviceType, handler);
                if (StringUtils.hasText(deviceType)) {
                    String normalized = normalizeDeviceType(deviceType);
                    handlerMap.put(normalized, handler);
                    if (!normalized.equals(deviceType)) {
                        handlerMap.put(deviceType, handler);
                    }
                    log.info("注册设备逻辑处理器: {} -> {}", deviceType, handler.getClass().getSimpleName());
                }
            }
@@ -49,10 +54,18 @@
     * @return 设备逻辑处理器,如果未找到返回null
     */
    public DeviceLogicHandler getHandler(String deviceType) {
        if (deviceType == null || deviceType.isEmpty()) {
        if (!StringUtils.hasText(deviceType)) {
            return null;
        }
        return handlerMap.get(deviceType);
        DeviceLogicHandler handler = handlerMap.get(deviceType);
        if (handler != null) {
            return handler;
        }
        String normalized = normalizeDeviceType(deviceType);
        if (!normalized.equals(deviceType)) {
            handler = handlerMap.get(normalized);
        }
        return handler;
    }
    /**
@@ -73,5 +86,16 @@
    public java.util.Set<String> getSupportedDeviceTypes() {
        return handlerMap.keySet();
    }
    private String normalizeDeviceType(String deviceType) {
        if (!StringUtils.hasText(deviceType)) {
            return deviceType;
        }
        String trimmed = deviceType.trim();
        if (trimmed.endsWith("设备")) {
            return trimmed.substring(0, trimmed.length() - 2);
        }
        return trimmed;
    }
}