]> git.basschouten.com Git - openhab-addons.git/blob
82c1e5550a24670d48b667a1dac07dcdd606b93f
[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.kaleidescape.internal.communication;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.io.UnsupportedEncodingException;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.kaleidescape.internal.KaleidescapeException;
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  * Class for communicating with the Kaleidescape component through a serial connection
33  *
34  * @author Laurent Garnier - Initial contribution
35  * @author Michael Lobstein - Adapted for the Kaleidescape binding
36  */
37 @NonNullByDefault
38 public class KaleidescapeSerialConnector extends KaleidescapeConnector {
39     private final Logger logger = LoggerFactory.getLogger(KaleidescapeSerialConnector.class);
40
41     private final String serialPortName;
42     private final SerialPortManager serialPortManager;
43     private final String uid;
44
45     private @Nullable SerialPort serialPort;
46
47     /**
48      * Constructor
49      *
50      * @param serialPortManager the serial port manager
51      * @param serialPortName the serial port name to be used
52      * @param uid the thing uid string
53      */
54     public KaleidescapeSerialConnector(SerialPortManager serialPortManager, String serialPortName, String uid) {
55         this.serialPortManager = serialPortManager;
56         this.serialPortName = serialPortName;
57         this.uid = uid;
58     }
59
60     @Override
61     public synchronized void open() throws KaleidescapeException {
62         logger.debug("Opening serial connection on port {}", serialPortName);
63         try {
64             SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
65             if (portIdentifier == null) {
66                 setConnected(false);
67                 throw new KaleidescapeException("Opening serial connection failed: No Such Port");
68             }
69
70             SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
71
72             commPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
73             commPort.enableReceiveThreshold(1);
74             commPort.enableReceiveTimeout(100);
75             commPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
76
77             InputStream dataIn = commPort.getInputStream();
78             OutputStream dataOut = commPort.getOutputStream();
79
80             if (dataOut != null) {
81                 dataOut.flush();
82             }
83             if (dataIn != null && dataIn.markSupported()) {
84                 try {
85                     dataIn.reset();
86                 } catch (IOException e) {
87                     logger.debug("Caught IOException at dataIn.reset(): {}", e.getMessage());
88                 }
89             }
90
91             Thread thread = new KaleidescapeReaderThread(this, this.uid, this.serialPortName);
92             setReaderThread(thread);
93             thread.start();
94
95             this.serialPort = commPort;
96             this.dataIn = dataIn;
97             this.dataOut = dataOut;
98
99             setConnected(true);
100
101             logger.debug("Serial connection opened");
102         } catch (PortInUseException e) {
103             setConnected(false);
104             throw new KaleidescapeException("Opening serial connection failed: Port in Use Exception", e);
105         } catch (UnsupportedCommOperationException e) {
106             setConnected(false);
107             throw new KaleidescapeException("Opening serial connection failed: Unsupported Comm Operation Exception",
108                     e);
109         } catch (UnsupportedEncodingException e) {
110             setConnected(false);
111             throw new KaleidescapeException("Opening serial connection failed: Unsupported Encoding Exception", e);
112         } catch (IOException e) {
113             setConnected(false);
114             throw new KaleidescapeException("Opening serial connection failed: IO Exception", e);
115         }
116     }
117
118     @Override
119     public synchronized void close() {
120         logger.debug("Closing serial connection");
121         SerialPort serialPort = this.serialPort;
122         if (serialPort != null) {
123             serialPort.removeEventListener();
124         }
125         super.cleanup();
126         if (serialPort != null) {
127             serialPort.close();
128             this.serialPort = null;
129         }
130         setConnected(false);
131         logger.debug("Serial connection closed");
132     }
133 }