2 * Copyright (c) 2010-2021 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.dscalarm.internal.handler;
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStreamReader;
18 import java.io.OutputStreamWriter;
19 import java.io.UnsupportedEncodingException;
20 import java.util.TooManyListenersException;
22 import org.openhab.binding.dscalarm.internal.config.IT100BridgeConfiguration;
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.SerialPortEvent;
26 import org.openhab.core.io.transport.serial.SerialPortEventListener;
27 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
28 import org.openhab.core.io.transport.serial.SerialPortManager;
29 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The bridge handler for the DSC IT100 RS232 Serial interface.
39 * @author Russell Stephens - Initial Contribution
42 public class IT100BridgeHandler extends DSCAlarmBaseBridgeHandler implements SerialPortEventListener {
44 private final Logger logger = LoggerFactory.getLogger(IT100BridgeHandler.class);
45 private final SerialPortManager serialPortManager;
47 private String serialPortName = "";
49 private SerialPort serialPort = null;
50 private OutputStreamWriter serialOutput = null;
51 private BufferedReader serialInput = null;
53 public IT100BridgeHandler(Bridge bridge, SerialPortManager serialPortManager) {
54 super(bridge, DSCAlarmBridgeType.IT100, DSCAlarmProtocol.IT100_API);
55 this.serialPortManager = serialPortManager;
59 public void initialize() {
60 logger.debug("Initializing the DSC IT100 Bridge handler.");
62 IT100BridgeConfiguration configuration = getConfigAs(IT100BridgeConfiguration.class);
64 if (configuration.serialPort == null || configuration.serialPort.trim().isEmpty()) {
65 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
66 "Set a serial port in the thing configuration.");
68 serialPortName = configuration.serialPort.trim();
69 baudRate = configuration.baud.intValue();
70 pollPeriod = configuration.pollPeriod.intValue();
74 logger.debug("IT100 Bridge Handler Initialized.");
75 logger.debug(" Serial Port: {},", serialPortName);
76 logger.debug(" Baud: {},", baudRate);
77 logger.debug(" PollPeriod: {},", pollPeriod);
82 public void openConnection() {
83 logger.debug("openConnection(): Connecting to IT-100");
85 SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
86 if (portIdentifier == null) {
87 logger.error("openConnection(): No Such Port: {}", serialPort);
93 SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
95 serialPort = commPort;
96 serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
97 SerialPort.PARITY_NONE);
98 serialPort.enableReceiveThreshold(1);
99 serialPort.disableReceiveTimeout();
101 serialOutput = new OutputStreamWriter(serialPort.getOutputStream(), "US-ASCII");
102 serialInput = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
104 setSerialEventHandler(this);
107 } catch (PortInUseException portInUseException) {
108 logger.error("openConnection(): Port in Use Exception: {}", portInUseException.getMessage());
110 } catch (UnsupportedCommOperationException unsupportedCommOperationException) {
111 logger.error("openConnection(): Unsupported Comm Operation Exception: {}",
112 unsupportedCommOperationException.getMessage());
114 } catch (UnsupportedEncodingException unsupportedEncodingException) {
115 logger.error("openConnection(): Unsupported Encoding Exception: {}",
116 unsupportedEncodingException.getMessage());
118 } catch (IOException ioException) {
119 logger.error("openConnection(): IO Exception: {}", ioException.getMessage());
125 public void write(String writeString, boolean doNotLog) {
127 serialOutput.write(writeString);
128 serialOutput.flush();
129 logger.debug("write(): Message Sent: {}", doNotLog ? "***" : writeString);
130 } catch (IOException ioException) {
131 logger.error("write(): {}", ioException.getMessage());
133 } catch (Exception exception) {
134 logger.error("write(): Unable to write to serial port: {} ", exception.getMessage(), exception);
140 public String read() {
144 message = readLine();
145 logger.debug("read(): Message Received: {}", message);
146 } catch (IOException ioException) {
147 logger.error("read(): IO Exception: {} ", ioException.getMessage());
149 } catch (Exception exception) {
150 logger.error("read(): Exception: {} ", exception.getMessage(), exception);
158 * Read a line from the Input Stream.
161 * @throws IOException
163 private String readLine() throws IOException {
164 return serialInput.readLine();
168 public void closeConnection() {
169 logger.debug("closeConnection(): Closing Serial Connection!");
171 if (serialPort == null) {
176 serialPort.removeEventListener();
178 if (serialInput != null) {
181 } catch (IOException e) {
182 logger.debug("Error while closing the input stream: {}", e.getMessage());
187 if (serialOutput != null) {
189 serialOutput.close();
190 } catch (IOException e) {
191 logger.debug("Error while closing the output stream: {}", e.getMessage());
200 logger.debug("close(): Serial Connection Closed!");
204 * Gets the Serial Port Name of the IT-100
206 * @return serialPortName
208 public String getSerialPortName() {
209 return serialPortName;
213 * Receives Serial Port Events and reads Serial Port Data.
215 * @param serialPortEvent
218 public synchronized void serialEvent(SerialPortEvent serialPortEvent) {
219 if (serialPortEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
221 String messageLine = serialInput.readLine();
222 handleIncomingMessage(messageLine);
223 } catch (IOException ioException) {
224 logger.error("serialEvent(): IO Exception: {}", ioException.getMessage());
230 * Set the serial event handler.
232 * @param serialPortEventListenser
234 private void setSerialEventHandler(SerialPortEventListener serialPortEventListenser) {
236 // Add the serial port event listener
237 serialPort.addEventListener(serialPortEventListenser);
238 serialPort.notifyOnDataAvailable(true);
239 } catch (TooManyListenersException tooManyListenersException) {
240 logger.error("setSerialEventHandler(): Too Many Listeners Exception: {}",
241 tooManyListenersException.getMessage());