huang
4 天以前 0dfdc8148cc266fd3e877183c5b162fb986d5c65
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.mes.interaction;
 
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;
import java.util.List;
import java.util.Map;
 
/**
 * 设备逻辑处理器工厂类
 * 根据设备类型获取对应的处理器
 * 
 * @author mes
 * @since 2025-01-XX
 */
@Slf4j
@Component
public class DeviceLogicHandlerFactory {
 
    @Autowired
    private List<DeviceLogicHandler> handlers;
 
    private final Map<String, DeviceLogicHandler> handlerMap = new HashMap<>();
 
    /**
     * 初始化处理器映射
     */
    @PostConstruct
    public void init() {
        if (handlers != null) {
            for (DeviceLogicHandler handler : handlers) {
                String deviceType = handler.getDeviceType();
                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());
                }
            }
        }
        log.info("设备逻辑处理器初始化完成,共注册 {} 个处理器", handlerMap.size());
    }
 
    /**
     * 根据设备类型获取对应的处理器
     * 
     * @param deviceType 设备类型
     * @return 设备逻辑处理器,如果未找到返回null
     */
    public DeviceLogicHandler getHandler(String deviceType) {
        if (!StringUtils.hasText(deviceType)) {
            return null;
        }
        DeviceLogicHandler handler = handlerMap.get(deviceType);
        if (handler != null) {
            return handler;
        }
        String normalized = normalizeDeviceType(deviceType);
        if (!normalized.equals(deviceType)) {
            handler = handlerMap.get(normalized);
        }
        return handler;
    }
 
    /**
     * 检查是否支持指定的设备类型
     * 
     * @param deviceType 设备类型
     * @return true表示支持,false表示不支持
     */
    public boolean supports(String deviceType) {
        return deviceType != null && handlerMap.containsKey(deviceType);
    }
 
    /**
     * 获取所有已注册的设备类型
     * 
     * @return 设备类型集合
     */
    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;
    }
}