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.velbus.internal.handler;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.util.TooManyListenersException;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.velbus.internal.config.VelbusSerialBridgeConfig;
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.thing.Bridge;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * {@link VelbusSerialBridgeHandler} is the handler for a Velbus Serial-interface and connects it to
39 * @author Cedric Boon - Initial contribution
42 public class VelbusSerialBridgeHandler extends VelbusBridgeHandler implements SerialPortEventListener {
43 private final Logger logger = LoggerFactory.getLogger(VelbusSerialBridgeHandler.class);
45 private SerialPortManager serialPortManager;
47 private @Nullable SerialPort serialPort;
48 private @NonNullByDefault({}) VelbusSerialBridgeConfig serialBridgeConfig;
50 public VelbusSerialBridgeHandler(Bridge velbusBridge, SerialPortManager serialPortManager) {
52 this.serialPortManager = serialPortManager;
56 public void initialize() {
57 this.serialBridgeConfig = getConfigAs(VelbusSerialBridgeConfig.class);
63 public void serialEvent(SerialPortEvent event) {
64 logger.debug("Serial port event triggered");
66 if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
72 * Makes a connection to the Velbus system.
74 * @return True if the connection succeeded, false if the connection did not succeed.
77 protected boolean connect() {
78 // parse ports and if the port is found, initialize the reader
79 SerialPortIdentifier portId = serialPortManager.getIdentifier(serialBridgeConfig.port);
81 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, "Port is not known!");
85 // initialize serial port
87 SerialPort serialPort = portId.open(getThing().getUID().toString(), 2000);
88 this.serialPort = serialPort;
90 OutputStream outputStream = serialPort.getOutputStream();
91 InputStream inputStream = serialPort.getInputStream();
93 if (outputStream != null && inputStream != null) {
94 initializeStreams(outputStream, inputStream);
96 serialPort.addEventListener(this);
97 serialPort.notifyOnDataAvailable(true);
99 updateStatus(ThingStatus.ONLINE);
100 logger.debug("Bridge online on serial port {}", serialBridgeConfig.port);
103 } catch (final IOException ex) {
105 logger.debug("I/O error on serial port {}", serialBridgeConfig.port);
106 } catch (PortInUseException e) {
108 logger.debug("Port {} is in use", serialBridgeConfig.port);
109 } catch (TooManyListenersException e) {
111 logger.debug("Cannot attach listener to port {}", serialBridgeConfig.port);
118 protected void disconnect() {
119 final SerialPort serialPort = this.serialPort;
120 if (serialPort != null) {
121 serialPort.removeEventListener();
123 this.serialPort = null;