package org.onetwo.boot.module.rxtx;

import java.util.Enumeration;
import java.util.Map;

import org.onetwo.common.exception.BaseException;
import org.onetwo.common.log.JFishLoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import com.google.common.collect.Maps;

import gnu.io.CommPortIdentifier;

/**
 * @author weishao zeng
 * <br/>
 */

public class SerialPortManager implements InitializingBean {

	private Map<String, CommPortIdentifier> portMap;
	
	private SerialPortEventPublisher serialPortEventPublisher;
	
	public SerialPortManager(SerialPortEventPublisher serialPortEventPublisher) {
		super();
		this.serialPortEventPublisher = serialPortEventPublisher;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		this.init();
	}

	public void init() {
		this.portMap = scanPorts();
	}
	
	public JSerialPort getSerialPort(String portName) {
		portName = portName.toUpperCase();
		CommPortIdentifier portIdentifier = getCommPortIdentifier(portName);
		if (portIdentifier==null) {
			throw new BaseException("serial port not found: " + portName);
		}
		JSerialPort serialPort = createJSerialPort(portIdentifier);
		return serialPort;
	}
	
	protected JSerialPort createJSerialPort(CommPortIdentifier portIdentifier) {
		return new JSerialPort(portIdentifier, serialPortEventPublisher);
	}
	
	public CommPortIdentifier getCommPortIdentifier(String portName) {
		return portMap.get(portName);
	}

	
    @SuppressWarnings("unchecked")
	public static final Map<String, CommPortIdentifier> scanPorts() {
        //获得当前可用串口
        Enumeration<CommPortIdentifier> identifiers = CommPortIdentifier.getPortIdentifiers();//获得所有串口

        Map<String, CommPortIdentifier> portMap = Maps.newHashMap();
        //串口名字添加到List并返回    
        while (identifiers.hasMoreElements()) {
        	CommPortIdentifier port = identifiers.nextElement();
        	portMap.put(port.getName().toUpperCase(), port);
        	JFishLoggerFactory.getCommonLogger().info("scaned port: {}", port.getName());
        }
        return portMap;
    }

	public SerialPortEventPublisher getSerialPortEventPublisher() {
		return serialPortEventPublisher;
	}
    
}

@@ -536,16 +536,63 @@
 sher;%0A%09%0A
+%09public SerialPortManager() %7B%0A%09%09this(null);%0A%09%7D%0A
 %09public 

fix seria