]> git.basschouten.com Git - openhab-addons.git/blob
ff8da3400c2c62cb3490fc87fe9d93b1a201e755
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.regoheatpump.internal.protocol;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
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.SerialPortIdentifier;
24 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
25
26 /**
27  * The {@link SerialRegoConnection} is responsible for creating serial connections to clients.
28  *
29  * @author Boris Krivonog - Initial contribution
30  */
31 @NonNullByDefault
32 public class SerialRegoConnection implements RegoConnection {
33     private final int baudRate;
34     private final String portName;
35     private @Nullable SerialPort serialPort;
36     private final SerialPortIdentifier serialPortIdentifier;
37
38     public SerialRegoConnection(SerialPortIdentifier serialPortIdentifier, int baudRate) {
39         this.serialPortIdentifier = serialPortIdentifier;
40         this.portName = serialPortIdentifier.getName();
41         this.baudRate = baudRate;
42     }
43
44     @Override
45     public void connect() throws IOException {
46         try {
47             SerialPort serialPort = serialPortIdentifier.open(SerialRegoConnection.class.getCanonicalName(), 2000);
48             serialPort.enableReceiveTimeout(100);
49             serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
50                     SerialPort.PARITY_NONE);
51             this.serialPort = serialPort;
52         } catch (PortInUseException e) {
53             throw new IOException("Serial port already used: " + portName, e);
54         } catch (UnsupportedCommOperationException e) {
55             throw new IOException("Unsupported operation on '" + portName + "': " + e.getMessage(), e);
56         }
57     }
58
59     @Override
60     public boolean isConnected() {
61         return serialPort != null;
62     }
63
64     @Override
65     public void close() {
66         SerialPort serialPort = this.serialPort;
67         this.serialPort = null;
68         if (serialPort != null) {
69             serialPort.close();
70         }
71     }
72
73     @Override
74     public OutputStream outputStream() throws IOException {
75         OutputStream outputStream = getSerialPort().getOutputStream();
76         if (outputStream == null) {
77             throw new IOException("Sending data is not supported");
78         }
79         return outputStream;
80     }
81
82     @Override
83     public InputStream inputStream() throws IOException {
84         InputStream inputStream = getSerialPort().getInputStream();
85         if (inputStream == null) {
86             throw new IOException("Receiving data is not supported");
87         }
88         return inputStream;
89     }
90
91     private SerialPort getSerialPort() throws IOException {
92         SerialPort serialPort = this.serialPort;
93         if (serialPort == null) {
94             throw new IOException("Connection closed");
95         }
96         return serialPort;
97     }
98 }