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.pioneeravr.internal.protocol.serial;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.util.stream.Collectors;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.pioneeravr.internal.protocol.StreamAvrConnection;
23 import org.openhab.core.io.transport.serial.PortInUseException;
24 import org.openhab.core.io.transport.serial.SerialPort;
25 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
26 import org.openhab.core.io.transport.serial.SerialPortManager;
27 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * A class that wraps the communication to a Pioneer AVR devices through a serial port
34 * @author Antoine Besnard - Initial contribution
37 public class SerialAvrConnection extends StreamAvrConnection {
39 private final Logger logger = LoggerFactory.getLogger(SerialAvrConnection.class);
41 private static final Integer LINK_SPEED = 9600;
43 private final String portName;
45 private @Nullable SerialPort serialPort;
47 private final SerialPortManager serialPortManager;
49 public SerialAvrConnection(String portName, SerialPortManager serialPortManager) {
50 this.portName = portName;
51 this.serialPortManager = serialPortManager;
55 protected void openConnection() throws IOException {
56 SerialPortIdentifier serialPortIdentifier = serialPortManager.getIdentifier(portName);
57 if (serialPortIdentifier == null) {
58 String availablePorts = serialPortManager.getIdentifiers().map(id -> id.getName())
59 .collect(Collectors.joining(", "));
60 throw new IOException(
61 "Serial port with name " + portName + " does not exist. Available port names: " + availablePorts);
66 SerialPort localSerialPort = serialPortIdentifier.open(SerialAvrConnection.class.getSimpleName(), 2000);
67 localSerialPort.setSerialPortParams(LINK_SPEED, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
68 SerialPort.PARITY_NONE);
69 serialPort = localSerialPort;
70 } catch (PortInUseException | UnsupportedCommOperationException e) {
71 throw new IOException("Failed to connect on port " + portName);
74 logger.debug("Connected to {}", getConnectionName());
78 public boolean isConnected() {
79 return serialPort != null;
85 SerialPort localSerialPort = serialPort;
86 if (localSerialPort != null) {
87 localSerialPort.close();
89 logger.debug("Closed port {}", portName);
94 public String getConnectionName() {
99 protected @Nullable InputStream getInputStream() throws IOException {
100 SerialPort localSerialPort = serialPort;
101 return localSerialPort != null ? localSerialPort.getInputStream() : null;
105 protected @Nullable OutputStream getOutputStream() throws IOException {
106 SerialPort localSerialPort = serialPort;
107 return localSerialPort != null ? localSerialPort.getOutputStream() : null;