]> git.basschouten.com Git - openhab-addons.git/blob
2335ef0f99895c6d0a4bf077e3b11da727c21836
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.lutron.internal.radiora;
14
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStreamReader;
18 import java.util.TooManyListenersException;
19
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;
30
31 /**
32  * RS232 connection to the RadioRA Classic system.
33  *
34  * @author Jeff Lauterbach - Initial Contribution
35  *
36  */
37 public class RS232Connection implements RadioRAConnection, SerialPortEventListener {
38
39     private final Logger logger = LoggerFactory.getLogger(RS232Connection.class);
40
41     protected SerialPortManager serialPortManager;
42     protected SerialPort serialPort;
43
44     protected BufferedReader inputReader;
45
46     protected RadioRAFeedbackListener listener;
47     protected RS232MessageParser parser = new RS232MessageParser();
48
49     public RS232Connection(SerialPortManager serialPortManager) {
50         super();
51         this.serialPortManager = serialPortManager;
52     }
53
54     @Override
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));
59         }
60
61         try {
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");
75         }
76     }
77
78     @Override
79     public void write(String command) {
80         logger.debug("Writing to serial port: {}", command.toString());
81         try {
82             serialPort.getOutputStream().write(command.getBytes());
83         } catch (IOException e) {
84             logger.debug("An error occurred writing to serial port", e);
85         }
86     }
87
88     @Override
89     public void disconnect() {
90         serialPort.close();
91     }
92
93     @Override
94     public void serialEvent(SerialPortEvent ev) {
95         switch (ev.getEventType()) {
96             case SerialPortEvent.DATA_AVAILABLE:
97                 try {
98                     if (!inputReader.ready()) {
99                         logger.debug("Serial Data Available but input reader not ready");
100                         return;
101                     }
102
103                     String message = inputReader.readLine();
104                     logger.debug("Msg Received: {}", message);
105                     RadioRAFeedback feedback = parser.parse(message);
106
107                     if (feedback != null) {
108                         logger.debug("Msg Parsed as {}", feedback.getClass().getName());
109                         listener.handleRadioRAFeedback(feedback);
110                     }
111                     logger.debug("Finished handling feedback");
112                 } catch (IOException e) {
113                     logger.debug("IOException occurred", e);
114                 }
115                 break;
116             default:
117                 logger.debug("Unhandled SerialPortEvent raised [{}]", ev.getEventType());
118                 break;
119         }
120     }
121
122     @Override
123     public void setListener(RadioRAFeedbackListener listener) {
124         this.listener = listener;
125     }
126 }