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