Search in sources :

Example 1 with EHealthException

use of org.openhab.binding.ehealth.internal.EHealthException in project openhab1-addons by openhab.

the class SerialConnector method connect.

public void connect() throws EHealthException {
    logger.debug("Going to open Serial connection on port '{}'", portName);
    // parse ports and if the default port is found, initialized the reader
    Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
        CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
        if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            if (id.getName().equals(portName)) {
                logger.debug("Serial port '{}' has been found.", portName);
                portId = id;
            }
        }
    }
    if (portId != null) {
        // initialize serial port
        try {
            serialPort = portId.open("openHAB", 2000);
            inputStream = serialPort.getInputStream();
            outputStream = serialPort.getOutputStream();
            bReader = new BufferedReader(new InputStreamReader(inputStream));
            bWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
            serialPort.setSerialPortParams(BAUD, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } catch (IOException e) {
            throw new EHealthException(e);
        } catch (PortInUseException e) {
            throw new EHealthException(e);
        } catch (UnsupportedCommOperationException e) {
            throw new EHealthException(e);
        } catch (TooManyListenersException e) {
            throw new EHealthException(e);
        }
    } else {
        StringBuilder sb = new StringBuilder();
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
            if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                sb.append(id.getName() + "\n");
            }
        }
        throw new EHealthException("Serial port '" + portName + "' could not be found. Available ports are:\n" + sb.toString());
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) TooManyListenersException(java.util.TooManyListenersException) PortInUseException(gnu.io.PortInUseException) InputStreamReader(java.io.InputStreamReader) CommPortIdentifier(gnu.io.CommPortIdentifier) BufferedReader(java.io.BufferedReader) EHealthException(org.openhab.binding.ehealth.internal.EHealthException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Aggregations

CommPortIdentifier (gnu.io.CommPortIdentifier)1 PortInUseException (gnu.io.PortInUseException)1 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 TooManyListenersException (java.util.TooManyListenersException)1 EHealthException (org.openhab.binding.ehealth.internal.EHealthException)1