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());
}
Aggregations