huang
2025-11-20 366ba040d2447bacd3455299425e3166f1f992bb
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
package com.mes.interaction;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
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 (deviceType != null && !deviceType.isEmpty()) {
                    handlerMap.put(deviceType, handler);
                    log.info("注册设备逻辑处理器: {} -> {}", deviceType, handler.getClass().getSimpleName());
                }
            }
        }
        log.info("设备逻辑处理器初始化完成,共注册 {} 个处理器", handlerMap.size());
    }
 
    /**
     * 根据设备类型获取对应的处理器
     * 
     * @param deviceType 设备类型
     * @return 设备逻辑处理器,如果未找到返回null
     */
    public DeviceLogicHandler getHandler(String deviceType) {
        if (deviceType == null || deviceType.isEmpty()) {
            return null;
        }
        return handlerMap.get(deviceType);
    }
 
    /**
     * 检查是否支持指定的设备类型
     * 
     * @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();
    }
}