]> git.basschouten.com Git - openhab-addons.git/blob
a1d40d04b4c26c7807aa2d40dd7b8a9464e375ed
[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.jeelink.internal.connection;
14
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStreamReader;
18 import java.io.OutputStream;
19 import java.util.TooManyListenersException;
20
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.SerialPortEvent;
24 import org.openhab.core.io.transport.serial.SerialPortEventListener;
25 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
26 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Reads lines from serial port and propagates them to registered InputListeners.
32  *
33  * @author Volker Bier - Initial contribution
34  */
35 public class JeeLinkSerialConnection extends AbstractJeeLinkConnection {
36     private final Logger logger = LoggerFactory.getLogger(JeeLinkSerialConnection.class);
37
38     private final int baudRate;
39     private SerialPort serialPort;
40     private final SerialPortIdentifier serialPortIdentifier;
41     private boolean open;
42
43     public JeeLinkSerialConnection(SerialPortIdentifier serialPortIdentifier, int baudRate,
44             ConnectionListener listener) {
45         super(serialPortIdentifier.getName(), listener);
46
47         logger.debug("Creating serial connection for port {} with baud rate {}...", port, baudRate);
48         this.baudRate = baudRate;
49         this.serialPortIdentifier = serialPortIdentifier;
50     }
51
52     @Override
53     public synchronized void closeConnection() {
54         if (open) {
55             logger.debug("Closing serial connection to port {}...", port);
56
57             serialPort.notifyOnDataAvailable(false);
58             serialPort.removeEventListener();
59
60             serialPort.close();
61             notifyClosed();
62             open = false;
63         }
64     }
65
66     @Override
67     public synchronized void openConnection() {
68         try {
69             if (!open) {
70                 logger.debug("Opening serial connection to port {} with baud rate {}...", port, baudRate);
71                 serialPort = serialPortIdentifier.open("openhab", 3000);
72                 open = true;
73
74                 serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
75                         SerialPort.PARITY_NONE);
76
77                 final BufferedReader input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
78
79                 serialPort.addEventListener(new SerialPortEventListener() {
80                     @Override
81                     public void serialEvent(SerialPortEvent event) {
82                         try {
83                             if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
84                                 propagateLine(input.readLine());
85                             }
86                         } catch (IOException ex) {
87                             logger.debug("Error reading from serial port!", ex);
88                             closeConnection();
89                             notifyAbort("propagate: " + ex.getMessage());
90                         }
91                     }
92                 });
93
94                 serialPort.notifyOnDataAvailable(true);
95                 notifyOpen();
96             }
97         } catch (UnsupportedCommOperationException | IOException | TooManyListenersException ex) {
98             closeConnection();
99             notifyAbort(ex.getMessage());
100         } catch (PortInUseException ex) {
101             notifyAbort("Port in use: " + port);
102         }
103     }
104
105     @Override
106     public OutputStream getInitStream() throws IOException {
107         return open ? serialPort.getOutputStream() : null;
108     }
109 }