2 * Copyright (c) 2010-2022 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.monopriceaudio.internal.communication;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.io.UnsupportedEncodingException;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.monopriceaudio.internal.MonopriceAudioException;
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;
32 * Class for communicating with the MonopriceAudio device through a serial connection
34 * @author Laurent Garnier - Initial contribution
35 * @author Michael Lobstein - Adapted for the MonopriceAudio binding
38 public class MonopriceAudioSerialConnector extends MonopriceAudioConnector {
40 private final Logger logger = LoggerFactory.getLogger(MonopriceAudioSerialConnector.class);
42 private final String serialPortName;
43 private final SerialPortManager serialPortManager;
44 private final String uid;
46 private @Nullable SerialPort serialPort;
51 * @param serialPortManager the serial port manager
52 * @param serialPortName the serial port name to be used
53 * @param uid the thing uid string
55 public MonopriceAudioSerialConnector(SerialPortManager serialPortManager, String serialPortName, String uid) {
56 this.serialPortManager = serialPortManager;
57 this.serialPortName = serialPortName;
62 public synchronized void open() throws MonopriceAudioException {
63 logger.debug("Opening serial connection on port {}", serialPortName);
65 SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
66 if (portIdentifier == null) {
68 throw new MonopriceAudioException("Opening serial connection failed: No Such Port");
71 SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
73 commPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
74 commPort.enableReceiveThreshold(1);
75 commPort.enableReceiveTimeout(100);
76 commPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
78 InputStream dataIn = commPort.getInputStream();
79 OutputStream dataOut = commPort.getOutputStream();
81 if (dataOut != null) {
84 if (dataIn != null && dataIn.markSupported()) {
87 } catch (IOException e) {
91 Thread thread = new MonopriceAudioReaderThread(this, this.uid, this.serialPortName);
92 setReaderThread(thread);
95 this.serialPort = commPort;
97 this.dataOut = dataOut;
101 logger.debug("Serial connection opened");
102 } catch (PortInUseException e) {
104 throw new MonopriceAudioException("Opening serial connection failed: Port in Use Exception", e);
105 } catch (UnsupportedCommOperationException e) {
107 throw new MonopriceAudioException("Opening serial connection failed: Unsupported Comm Operation Exception",
109 } catch (UnsupportedEncodingException e) {
111 throw new MonopriceAudioException("Opening serial connection failed: Unsupported Encoding Exception", e);
112 } catch (IOException e) {
114 throw new MonopriceAudioException("Opening serial connection failed: IO Exception", e);
119 public synchronized void close() {
120 logger.debug("Closing serial connection");
121 SerialPort serialPort = this.serialPort;
122 if (serialPort != null) {
123 serialPort.removeEventListener();
126 if (serialPort != null) {
128 this.serialPort = null;
131 logger.debug("Serial connection closed");