]> git.basschouten.com Git - openhab-addons.git/blob
164165faa63ee40402723ff7b3e5b183e9e13e4a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.velbus.internal.handler;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.util.TooManyListenersException;
19
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;
34
35 /**
36  * {@link VelbusSerialBridgeHandler} is the handler for a Velbus Serial-interface and connects it to
37  * the framework.
38  *
39  * @author Cedric Boon - Initial contribution
40  */
41 @NonNullByDefault
42 public class VelbusSerialBridgeHandler extends VelbusBridgeHandler implements SerialPortEventListener {
43     private final Logger logger = LoggerFactory.getLogger(VelbusSerialBridgeHandler.class);
44
45     private SerialPortManager serialPortManager;
46
47     private @Nullable SerialPort serialPort;
48     private @NonNullByDefault({}) VelbusSerialBridgeConfig serialBridgeConfig;
49
50     public VelbusSerialBridgeHandler(Bridge velbusBridge, SerialPortManager serialPortManager) {
51         super(velbusBridge);
52         this.serialPortManager = serialPortManager;
53     }
54
55     @Override
56     public void initialize() {
57         this.serialBridgeConfig = getConfigAs(VelbusSerialBridgeConfig.class);
58
59         super.initialize();
60     }
61
62     @Override
63     public void serialEvent(SerialPortEvent event) {
64         logger.debug("Serial port event triggered");
65
66         if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
67             readPackets();
68         }
69     }
70
71     /**
72      * Makes a connection to the Velbus system.
73      *
74      * @return True if the connection succeeded, false if the connection did not succeed.
75      */
76     @Override
77     protected boolean connect() {
78         // parse ports and if the port is found, initialize the reader
79         SerialPortIdentifier portId = serialPortManager.getIdentifier(serialBridgeConfig.port);
80         if (portId == null) {
81             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, "Port is not known!");
82             return false;
83         }
84
85         // initialize serial port
86         try {
87             SerialPort serialPort = portId.open(getThing().getUID().toString(), 2000);
88             this.serialPort = serialPort;
89
90             OutputStream outputStream = serialPort.getOutputStream();
91             InputStream inputStream = serialPort.getInputStream();
92
93             if (outputStream != null && inputStream != null) {
94                 initializeStreams(outputStream, inputStream);
95
96                 serialPort.addEventListener(this);
97                 serialPort.notifyOnDataAvailable(true);
98
99                 updateStatus(ThingStatus.ONLINE);
100                 logger.debug("Bridge online on serial port {}", serialBridgeConfig.port);
101                 return true;
102             }
103         } catch (final IOException ex) {
104             onConnectionLost();
105             logger.debug("I/O error on serial port {}", serialBridgeConfig.port);
106         } catch (PortInUseException e) {
107             onConnectionLost();
108             logger.debug("Port {} is in use", serialBridgeConfig.port);
109         } catch (TooManyListenersException e) {
110             onConnectionLost();
111             logger.debug("Cannot attach listener to port {}", serialBridgeConfig.port);
112         }
113
114         return false;
115     }
116
117     @Override
118     protected void disconnect() {
119         final SerialPort serialPort = this.serialPort;
120         if (serialPort != null) {
121             serialPort.removeEventListener();
122             serialPort.close();
123             this.serialPort = null;
124         }
125
126         super.disconnect();
127     }
128 }