package com.dcs.equipment.service;

import com.dcs.equipment.constants.ProtocolConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * 协议服务工厂
 * 根据协议类型获取对应的服务实现
 *
 * @author: wangpx
 * @date: 2025-07-24
 */
@Component
public class ProtocolServiceFactory {

    @Autowired
    @Qualifier("modbusTcpService")
    private ProtocolService modbusTcpService;

//    @Autowired
//    @Qualifier("opcUAService")
//    private ProtocolService opcUAService;

    @Autowired
    @Qualifier("mqttService")
    private ProtocolService mqttService;

    private final Map<Integer, ProtocolService> serviceMap = new HashMap<>();

    @PostConstruct
    public void init() {
        serviceMap.put(ProtocolConstants.MODBUS_TCP, modbusTcpService);
//        serviceMap.put(ProtocolConstants.OPC_UA, opcUAService);
        serviceMap.put(ProtocolConstants.MQTT, mqttService);
    }

    /**
     * 根据协议类型获取对应的服务
     */
    public ProtocolService getService(Integer protocolType) {
        ProtocolService service = serviceMap.get(protocolType);
        if (service == null) {
            throw new IllegalArgumentException("未找到协议类型 " + protocolType + " 的服务实现");
        }
        return service;
    }

    /**
     * 检查是否支持指定的协议类型
     */
    public boolean isSupported(Integer protocolType) {
        return serviceMap.containsKey(protocolType);
    }
}
