package nl.nikhef.sfp;

import java.io.IOException;
import java.util.List;

import com.ftdi.DeviceType;
import com.ftdi.FTDevice;

public class MultiSFPProvider extends SFPProviderBase
{
	
	@Override
	public String getName() {
		return "MultiSFP";
	}

	public SFPDevice getDeviceBySerial(String serial) {
		for (SFPDevice dev : getDevices()) {
			if (dev.getSerial().equals(serial)) return dev;
		}
		return null;
	}
	

	public void scanForNewDevices() {
		try {
			List<FTDevice> devs;
			synchronized(this)
			{
				
				devs = FTDevice.getDevices(true);
				System.out.printf("FTDevice.getDevices() Found %d devices\n", devs.size());
			}
			
			for (FTDevice devRaw : devs)
			{
				if (devRaw.getDevType() == DeviceType.DEVICE_2232H && devRaw.getDevDescription().equals("MultiSFP A")) {
					
					// Only get the primary 'A' device, we know the second.
					String serial = devRaw.getDevSerialNumber();
					
					// chop of the last part from the serial:
					serial = serial.substring(0, serial.length() - 1);
					
					SFPDevice dev = getDeviceBySerial(serial);
					
					
					if (dev == null) 
					{
						dev = new MultiSFPDevice(serial, this);
					}
					updAdd(dev);
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		updProcess();		
	}
	
	

}
