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();
|
}
|
}
|