2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.jeelink.internal.connection;
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStreamReader;
18 import java.io.OutputStream;
19 import java.util.TooManyListenersException;
21 import org.openhab.core.io.transport.serial.PortInUseException;
22 import org.openhab.core.io.transport.serial.SerialPort;
23 import org.openhab.core.io.transport.serial.SerialPortEvent;
24 import org.openhab.core.io.transport.serial.SerialPortEventListener;
25 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
26 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Reads lines from serial port and propagates them to registered InputListeners.
33 * @author Volker Bier - Initial contribution
35 public class JeeLinkSerialConnection extends AbstractJeeLinkConnection {
36 private final Logger logger = LoggerFactory.getLogger(JeeLinkSerialConnection.class);
38 private final int baudRate;
39 private SerialPort serialPort;
40 private final SerialPortIdentifier serialPortIdentifier;
43 public JeeLinkSerialConnection(SerialPortIdentifier serialPortIdentifier, int baudRate,
44 ConnectionListener listener) {
45 super(serialPortIdentifier.getName(), listener);
47 logger.debug("Creating serial connection for port {} with baud rate {}...", port, baudRate);
48 this.baudRate = baudRate;
49 this.serialPortIdentifier = serialPortIdentifier;
53 public synchronized void closeConnection() {
55 logger.debug("Closing serial connection to port {}...", port);
57 serialPort.notifyOnDataAvailable(false);
58 serialPort.removeEventListener();
67 public synchronized void openConnection() {
70 logger.debug("Opening serial connection to port {} with baud rate {}...", port, baudRate);
71 serialPort = serialPortIdentifier.open("openhab", 3000);
74 serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
75 SerialPort.PARITY_NONE);
77 final BufferedReader input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
79 serialPort.addEventListener(new SerialPortEventListener() {
81 public void serialEvent(SerialPortEvent event) {
83 if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
84 propagateLine(input.readLine());
86 } catch (IOException ex) {
87 logger.debug("Error reading from serial port!", ex);
89 notifyAbort("propagate: " + ex.getMessage());
94 serialPort.notifyOnDataAvailable(true);
97 } catch (UnsupportedCommOperationException | IOException | TooManyListenersException ex) {
99 notifyAbort(ex.getMessage());
100 } catch (PortInUseException ex) {
101 notifyAbort("Port in use: " + port);
106 public OutputStream getInitStream() throws IOException {
107 return open ? serialPort.getOutputStream() : null;