]> git.basschouten.com Git - openhab-addons.git/blob
a06953906d9fde37a9a5bcb105c3a92f26ad61e4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.pioneeravr.internal.protocol.serial;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.util.stream.Collectors;
19
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;
30
31 /**
32  * A class that wraps the communication to a Pioneer AVR devices through a serial port
33  *
34  * @author Antoine Besnard - Initial contribution
35  */
36 @NonNullByDefault
37 public class SerialAvrConnection extends StreamAvrConnection {
38
39     private final Logger logger = LoggerFactory.getLogger(SerialAvrConnection.class);
40
41     private static final Integer LINK_SPEED = 9600;
42
43     private final String portName;
44
45     private @Nullable SerialPort serialPort;
46
47     private final SerialPortManager serialPortManager;
48
49     public SerialAvrConnection(String portName, SerialPortManager serialPortManager) {
50         this.portName = portName;
51         this.serialPortManager = serialPortManager;
52     }
53
54     @Override
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);
62
63         }
64
65         try {
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);
72         }
73
74         logger.debug("Connected to {}", getConnectionName());
75     }
76
77     @Override
78     public boolean isConnected() {
79         return serialPort != null;
80     }
81
82     @Override
83     public void close() {
84         super.close();
85         SerialPort localSerialPort = serialPort;
86         if (localSerialPort != null) {
87             localSerialPort.close();
88             serialPort = null;
89             logger.debug("Closed port {}", portName);
90         }
91     }
92
93     @Override
94     public String getConnectionName() {
95         return portName;
96     }
97
98     @Override
99     protected @Nullable InputStream getInputStream() throws IOException {
100         SerialPort localSerialPort = serialPort;
101         return localSerialPort != null ? localSerialPort.getInputStream() : null;
102     }
103
104     @Override
105     protected @Nullable OutputStream getOutputStream() throws IOException {
106         SerialPort localSerialPort = serialPort;
107         return localSerialPort != null ? localSerialPort.getOutputStream() : null;
108     }
109 }