2 * Copyright (c) 2010-2024 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.openwebnet.internal.serial;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.util.TooManyListenersException;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.io.transport.serial.PortInUseException;
23 import org.openhab.core.io.transport.serial.SerialPort;
24 import org.openhab.core.io.transport.serial.SerialPortEvent;
25 import org.openhab.core.io.transport.serial.SerialPortEventListener;
26 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
27 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * openwebnet4j SerialPort implementation based on OH serial transport
34 * @author M. Valla - Initial contribution
38 public class SerialPortAdapter implements org.openwebnet4j.communication.serial.spi.SerialPort {
40 private static final Logger logger = LoggerFactory.getLogger(SerialPortAdapter.class);
42 private static final int OPEN_TIMEOUT_MS = 200;
44 private final SerialPortIdentifier spid;
46 private @Nullable SerialPort sp = null;
48 public SerialPortAdapter(final SerialPortIdentifier spid) {
53 public boolean setSerialPortParams(int baudrate, int dataBits, int stopBits, int parity) {
58 lsp.setSerialPortParams(baudrate, dataBits, stopBits, parity);
60 } catch (UnsupportedCommOperationException e) {
61 logger.error("UnsupportedCommOperationException while setting port params in setSerialPortParams: {}",
70 public boolean addEventListener(org.openwebnet4j.communication.serial.spi.SerialPortEventListener listener) {
75 lsp.addEventListener(new SerialPortEventListener() {
78 public void serialEvent(SerialPortEvent event) {
80 listener.serialEvent(new SerialPortEventAdapter(event));
84 lsp.notifyOnDataAvailable(true);
86 } catch (TooManyListenersException e) {
87 logger.error("TooManyListenersException while adding event listener: {}", e.getMessage());
94 public boolean open() {
96 sp = spid.open(this.getClass().getName(), OPEN_TIMEOUT_MS);
97 } catch (PortInUseException e) {
98 logger.error("PortInUseException while opening serial port {}: {}", spid.getName(), e.getMessage());
105 public @Nullable String getName() {
109 return lsp.getName();
116 public @Nullable InputStream getInputStream() throws IOException {
120 return lsp.getInputStream();
127 public @Nullable OutputStream getOutputStream() throws IOException {
131 return lsp.getOutputStream();
138 public void close() {