Search in sources :

Example 1 with InitialiseRequestMessage

use of org.openhab.binding.plugwise.protocol.InitialiseRequestMessage in project openhab1-addons by openhab.

the class Stick method initialize.

/**
     * Initialize this device and open the serial port
     *
     * @throws PlugwiseInitializationException if port can not be opened
     */
@SuppressWarnings("rawtypes")
private void initialize() throws PlugwiseInitializationException {
    // Flush the deviceCache
    plugwiseDeviceCache.clear();
    // 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(port)) {
                logger.debug("Serial port '{}' has been found.", port);
                portId = id;
            }
        }
    }
    if (portId != null) {
        // initialize serial port
        try {
            serialPort = portId.open("openHAB", 2000);
        } catch (PortInUseException e) {
            throw new PlugwiseInitializationException(e);
        }
        try {
            serialPort.addEventListener(this);
        } catch (TooManyListenersException e) {
            throw new PlugwiseInitializationException(e);
        }
        // activate the DATA_AVAILABLE notifier
        serialPort.notifyOnDataAvailable(true);
        try {
            // set port parameters
            serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {
            throw new PlugwiseInitializationException(e);
        }
        try {
            // get the output stream
            outputChannel = Channels.newChannel(serialPort.getOutputStream());
        } catch (IOException e) {
            throw new PlugwiseInitializationException(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 PlugwiseInitializationException("Serial port '" + port + "' could not be found. Available ports are:\n" + sb);
    }
    initialised = true;
    // initialise the Stick
    sendMessage(new InitialiseRequestMessage());
}
Also used : TooManyListenersException(java.util.TooManyListenersException) UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) Enumeration(java.util.Enumeration) PortInUseException(gnu.io.PortInUseException) CommPortIdentifier(gnu.io.CommPortIdentifier) InitialiseRequestMessage(org.openhab.binding.plugwise.protocol.InitialiseRequestMessage) IOException(java.io.IOException)

Aggregations

CommPortIdentifier (gnu.io.CommPortIdentifier)1 PortInUseException (gnu.io.PortInUseException)1 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)1 IOException (java.io.IOException)1 Enumeration (java.util.Enumeration)1 TooManyListenersException (java.util.TooManyListenersException)1 InitialiseRequestMessage (org.openhab.binding.plugwise.protocol.InitialiseRequestMessage)1