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.benqprojector.internal.connector;
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.benqprojector.internal.BenqProjectorException;
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.SerialPortManager;
28 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Connector for serial port communication.
35 * @author Michael Lobstein - Initial contribution
38 public class BenqProjectorSerialConnector implements BenqProjectorConnector, SerialPortEventListener {
40 private final Logger logger = LoggerFactory.getLogger(BenqProjectorSerialConnector.class);
41 private final String serialPortName;
42 private final SerialPortManager serialPortManager;
44 private @Nullable InputStream in = null;
45 private @Nullable OutputStream out = null;
46 private @Nullable SerialPort serialPort = null;
48 public BenqProjectorSerialConnector(SerialPortManager serialPortManager, String serialPort) {
49 this.serialPortManager = serialPortManager;
50 this.serialPortName = serialPort;
54 public void connect() throws BenqProjectorException {
56 logger.debug("Open connection to serial port '{}'", serialPortName);
58 SerialPortIdentifier serialPortIdentifier = serialPortManager.getIdentifier(serialPortName);
60 if (serialPortIdentifier == null) {
61 throw new IOException("Unknown serial port");
63 SerialPort serialPort = serialPortIdentifier.open(this.getClass().getName(), 2000);
64 serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
65 serialPort.enableReceiveThreshold(1);
66 serialPort.disableReceiveTimeout();
68 InputStream in = serialPort.getInputStream();
69 OutputStream out = serialPort.getOutputStream();
71 if (in != null && out != null) {
73 if (in.markSupported()) {
77 serialPort.notifyOnDataAvailable(true);
79 this.serialPort = serialPort;
83 } catch (PortInUseException | UnsupportedCommOperationException | IOException e) {
84 throw new BenqProjectorException(e);
89 public void disconnect() throws BenqProjectorException {
90 InputStream in = this.in;
91 OutputStream out = this.out;
92 SerialPort serialPort = this.serialPort;
95 logger.debug("Close serial out stream");
98 } catch (IOException e) {
99 logger.debug("Error occurred when closing serial out stream: {}", e.getMessage());
104 logger.debug("Close serial in stream");
107 } catch (IOException e) {
108 logger.debug("Error occurred when closing serial in stream: {}", e.getMessage());
112 if (serialPort != null) {
113 logger.debug("Close serial port");
115 serialPort.removeEventListener();
116 this.serialPort = null;
119 logger.debug("Closed");
123 public String sendMessage(String data) throws BenqProjectorException {
124 InputStream in = this.in;
125 OutputStream out = this.out;
127 if (in == null || out == null) {
134 if (in != null && out != null) {
135 // flush input stream
136 if (in.markSupported()) {
139 while (in.available() > 0) {
140 int availableBytes = in.available();
142 if (availableBytes > 0) {
143 byte[] tmpData = new byte[availableBytes];
144 in.read(tmpData, 0, availableBytes);
148 return sendMsgReadResp(data, in, out);
152 } catch (IOException e) {
153 logger.debug("IO error occurred...reconnect and resend once: {}", e.getMessage());
158 return sendMsgReadResp(data, in, out);
159 } catch (IOException e1) {
160 throw new BenqProjectorException(e);
166 public void serialEvent(SerialPortEvent arg0) {