]> git.basschouten.com Git - openhab-addons.git/blob
46bd4904c49addd6b32c4200d0fde2df4ac50e36
[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.openwebnet.internal.serial;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.util.TooManyListenersException;
19
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;
30
31 /**
32  * openwebnet4j SerialPort implementation based on OH serial transport
33  *
34  * @author M. Valla - Initial contribution
35  */
36
37 @NonNullByDefault
38 public class SerialPortAdapter implements org.openwebnet4j.communication.serial.spi.SerialPort {
39
40     private static final Logger logger = LoggerFactory.getLogger(SerialPortAdapter.class);
41
42     private static final int OPEN_TIMEOUT_MS = 200;
43
44     private final SerialPortIdentifier spid;
45
46     private @Nullable SerialPort sp = null;
47
48     public SerialPortAdapter(final SerialPortIdentifier spid) {
49         this.spid = spid;
50     }
51
52     @Override
53     public boolean setSerialPortParams(int baudrate, int dataBits, int stopBits, int parity) {
54         @Nullable
55         SerialPort lsp = sp;
56         if (lsp != null) {
57             try {
58                 lsp.setSerialPortParams(baudrate, dataBits, stopBits, parity);
59                 return true;
60             } catch (UnsupportedCommOperationException e) {
61                 logger.error("UnsupportedCommOperationException while setting port params in setSerialPortParams: {}",
62                         e.getMessage());
63                 return false;
64             }
65         }
66         return false;
67     }
68
69     @Override
70     public boolean addEventListener(org.openwebnet4j.communication.serial.spi.SerialPortEventListener listener) {
71         @Nullable
72         SerialPort lsp = sp;
73         if (lsp != null) {
74             try {
75                 lsp.addEventListener(new SerialPortEventListener() {
76
77                     @Override
78                     public void serialEvent(SerialPortEvent event) {
79                         if (event != null) {
80                             listener.serialEvent(new SerialPortEventAdapter(event));
81                         }
82                     }
83                 });
84                 lsp.notifyOnDataAvailable(true);
85                 return true;
86             } catch (TooManyListenersException e) {
87                 logger.error("TooManyListenersException while adding event listener: {}", e.getMessage());
88             }
89         }
90         return false;
91     }
92
93     @Override
94     public boolean open() {
95         try {
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());
99             return false;
100         }
101         return true;
102     }
103
104     @Override
105     public @Nullable String getName() {
106         @Nullable
107         SerialPort lsp = sp;
108         if (lsp != null) {
109             return lsp.getName();
110         } else {
111             return null;
112         }
113     }
114
115     @Override
116     public @Nullable InputStream getInputStream() throws IOException {
117         @Nullable
118         SerialPort lsp = sp;
119         if (lsp != null) {
120             return lsp.getInputStream();
121         } else {
122             return null;
123         }
124     }
125
126     @Override
127     public @Nullable OutputStream getOutputStream() throws IOException {
128         @Nullable
129         SerialPort lsp = sp;
130         if (lsp != null) {
131             return lsp.getOutputStream();
132         } else {
133             return null;
134         }
135     }
136
137     @Override
138     public void close() {
139         @Nullable
140         SerialPort lsp = sp;
141         if (lsp != null) {
142             lsp.close();
143         }
144     }
145 }