2 * Copyright (c) 2010-2020 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.lutron.internal.radiora;
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStreamReader;
18 import java.util.TooManyListenersException;
20 import org.openhab.binding.lutron.internal.radiora.protocol.RadioRAFeedback;
21 import org.openhab.core.io.transport.serial.PortInUseException;
22 import org.openhab.core.io.transport.serial.SerialPort;
23 import org.openhab.core.io.transport.serial.SerialPortEvent;
24 import org.openhab.core.io.transport.serial.SerialPortEventListener;
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 * RS232 connection to the RadioRA Classic system.
34 * @author Jeff Lauterbach - Initial Contribution
37 public class RS232Connection implements RadioRAConnection, SerialPortEventListener {
39 private final Logger logger = LoggerFactory.getLogger(RS232Connection.class);
41 protected SerialPortManager serialPortManager;
42 protected SerialPort serialPort;
44 protected BufferedReader inputReader;
46 protected RadioRAFeedbackListener listener;
47 protected RS232MessageParser parser = new RS232MessageParser();
49 public RS232Connection(SerialPortManager serialPortManager) {
51 this.serialPortManager = serialPortManager;
55 public void open(String portName, int baud) throws RadioRAConnectionException {
56 SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(portName);
57 if (portIdentifier == null) {
58 throw new RadioRAConnectionException(String.format("Port not found", portName));
62 serialPort = portIdentifier.open("openhab", 5000);
63 serialPort.notifyOnDataAvailable(true);
64 serialPort.setSerialPortParams(baud, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
65 serialPort.addEventListener(this);
66 inputReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
67 } catch (PortInUseException e) {
68 throw new RadioRAConnectionException(String.format("Port %s already in use", portIdentifier.getName()));
69 } catch (UnsupportedCommOperationException e) {
70 throw new RadioRAConnectionException("Error initializing - Failed to set serial port params");
71 } catch (TooManyListenersException e) {
72 throw new RadioRAConnectionException("Error initializing - Failed to add event listener");
73 } catch (IOException e) {
74 throw new RadioRAConnectionException("Error initializing - Failed to get input stream");
79 public void write(String command) {
80 logger.debug("Writing to serial port: {}", command.toString());
82 serialPort.getOutputStream().write(command.getBytes());
83 } catch (IOException e) {
84 logger.debug("An error occurred writing to serial port", e);
89 public void disconnect() {
94 public void serialEvent(SerialPortEvent ev) {
95 switch (ev.getEventType()) {
96 case SerialPortEvent.DATA_AVAILABLE:
98 if (!inputReader.ready()) {
99 logger.debug("Serial Data Available but input reader not ready");
103 String message = inputReader.readLine();
104 logger.debug("Msg Received: {}", message);
105 RadioRAFeedback feedback = parser.parse(message);
107 if (feedback != null) {
108 logger.debug("Msg Parsed as {}", feedback.getClass().getName());
109 listener.handleRadioRAFeedback(feedback);
111 logger.debug("Finished handling feedback");
112 } catch (IOException e) {
113 logger.debug("IOException occurred", e);
117 logger.debug("Unhandled SerialPortEvent raised [{}]", ev.getEventType());
123 public void setListener(RadioRAFeedbackListener listener) {
124 this.listener = listener;