package watchdog;
import java.io.*;
import java.util.*;
import gnu.io.*;

public class PortMonitor implements SerialPortEventListener {
    static CommPortIdentifier portId;
    static Enumeration	      portList;
    SerialPort		          serialPort;

    public PortMonitor() {
      boolean		      portFound = false;
      String		      defaultPort = "COM1";

  	  portList = CommPortIdentifier.getPortIdentifiers();
	  while (portList.hasMoreElements() && (!portFound)) {
	    portId = (CommPortIdentifier) portList.nextElement();
	    System.out.println(portId.getName());
	    if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
		  if (portId.getName().equals(defaultPort)) {
		      System.out.println("Found port: "+defaultPort);
		      portFound = true;
		  }
	    }
 	  }
	  if (!portFound) {
	    System.out.println("port " + defaultPort + " not found.");
	  }
    }

    public boolean checkRinger() {
  	  boolean answer = false;
  	  try {
	    serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
	  } catch (PortInUseException e) {}
	  System.out.println("CD=" + serialPort.isCD() + " " +
	                     "CTS=" + serialPort.isCTS() + " " +
	                     "DSR=" + serialPort.isDSR() + " " +
	                     "DTR=" + serialPort.isDTR() + " " +
	                     "RI=" + serialPort.isRI() + " "
	  );
      if (serialPort.isRI()) answer = true;
      serialPort.close();
      return answer;
    }

    public void serialEvent(SerialPortEvent event) {
    }
}
