Search in sources :

Example 1 with InitializationException

use of org.openhab.binding.comfoair.internal.InitializationException in project openhab1-addons by openhab.

the class ComfoAirConnector method open.

/**
     * Open and initialize a serial port.
     *
     * @param portName
     *            e.g. /dev/ttyS0
     * @param listener
     *            the listener which is informed after a successful response
     *            read
     * @throws InitializationException
     */
public void open(String portName) throws InitializationException {
    logger.debug("Open ComfoAir connection");
    port = portName;
    CommPortIdentifier portIdentifier;
    try {
        portIdentifier = CommPortIdentifier.getPortIdentifier(port);
        try {
            serialPort = portIdentifier.open("openhab", 3000);
            serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            serialPort.enableReceiveThreshold(1);
            serialPort.enableReceiveTimeout(1000);
            // RXTX serial port library causes high CPU load
            // Start event listener, which will just sleep and slow down event loop
            serialPort.addEventListener(new CPUWorkaroundThread());
            serialPort.notifyOnDataAvailable(true);
            inputStream = new DataInputStream(new BufferedInputStream(serialPort.getInputStream()));
            outputStream = serialPort.getOutputStream();
            ComfoAirCommand command = ComfoAirCommandType.getChangeCommand(ComfoAirCommandType.ACTIVATE.key, new DecimalType(1));
            sendCommand(command);
        } catch (PortInUseException e) {
            throw new InitializationException(e);
        } catch (UnsupportedCommOperationException e) {
            throw new InitializationException(e);
        } catch (IOException e) {
            throw new InitializationException(e);
        } catch (TooManyListenersException e) {
            throw new InitializationException(e);
        }
    } catch (NoSuchPortException e) {
        StringBuilder sb = new StringBuilder();
        @SuppressWarnings("rawtypes") Enumeration portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
            if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                sb.append(id.getName() + "\n");
            }
        }
        throw new InitializationException("Serial port '" + port + "' could not be found. Available ports are:\n" + sb.toString());
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) Enumeration(java.util.Enumeration) CommPortIdentifier(gnu.io.CommPortIdentifier) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) InitializationException(org.openhab.binding.comfoair.internal.InitializationException) TooManyListenersException(java.util.TooManyListenersException) PortInUseException(gnu.io.PortInUseException) NoSuchPortException(gnu.io.NoSuchPortException) BufferedInputStream(java.io.BufferedInputStream) DecimalType(org.openhab.core.library.types.DecimalType)

Aggregations

CommPortIdentifier (gnu.io.CommPortIdentifier)1 NoSuchPortException (gnu.io.NoSuchPortException)1 PortInUseException (gnu.io.PortInUseException)1 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)1 BufferedInputStream (java.io.BufferedInputStream)1 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 Enumeration (java.util.Enumeration)1 TooManyListenersException (java.util.TooManyListenersException)1 InitializationException (org.openhab.binding.comfoair.internal.InitializationException)1 DecimalType (org.openhab.core.library.types.DecimalType)1