2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.oppo.internal.communication;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
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;
31 * Class for communicating with the Oppo player through a serial connection
33 * @author Laurent Garnier - Initial contribution
34 * @author Michael Lobstein - Adapted for the Oppo binding
37 public class OppoSerialConnector extends OppoConnector {
38 private final Logger logger = LoggerFactory.getLogger(OppoSerialConnector.class);
40 private final String serialPortName;
41 private final SerialPortManager serialPortManager;
42 private final String uid;
44 private @Nullable SerialPort serialPort;
49 * @param serialPortManager the serial port manager
50 * @param serialPortName the serial port name to be used
51 * @param uid the thing uid string
53 public OppoSerialConnector(SerialPortManager serialPortManager, String serialPortName, String uid) {
54 this.serialPortManager = serialPortManager;
55 this.serialPortName = serialPortName;
60 public synchronized void open() throws OppoException {
61 logger.debug("Opening serial connection on port {}", serialPortName);
63 SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
64 if (portIdentifier == null) {
66 throw new OppoException("Opening serial connection failed: No Such Port");
69 SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
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);
76 InputStream dataIn = commPort.getInputStream();
77 OutputStream dataOut = commPort.getOutputStream();
79 if (dataOut != null) {
82 if (dataIn != null && dataIn.markSupported()) {
85 } catch (IOException e) {
89 Thread thread = new OppoReaderThread(this, this.uid, this.serialPortName);
90 setReaderThread(thread);
93 this.serialPort = commPort;
95 this.dataOut = dataOut;
99 logger.debug("Serial connection opened");
100 } catch (PortInUseException e) {
102 throw new OppoException("Opening serial connection failed: Port in Use Exception", e);
103 } catch (UnsupportedCommOperationException e) {
105 throw new OppoException("Opening serial connection failed: Unsupported Comm Operation Exception", e);
106 } catch (IOException e) {
108 throw new OppoException("Opening serial connection failed: IO Exception", e);
113 public synchronized void close() {
114 logger.debug("Closing serial connection");
115 SerialPort serialPort = this.serialPort;
116 if (serialPort != null) {
117 serialPort.removeEventListener();
120 if (serialPort != null) {
122 this.serialPort = null;
125 logger.debug("Serial connection closed");