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.net.Socket;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.velbus.internal.config.VelbusNetworkBridgeConfig;
21 import org.openhab.core.thing.Bridge;
22 import org.openhab.core.thing.ThingStatus;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * {@link VelbusNetworkBridgeHandler} is the handler for a Velbus network interface and connects it to
30 * @author Cedric Boon - Initial contribution
33 public class VelbusNetworkBridgeHandler extends VelbusBridgeHandler {
34 private final Logger logger = LoggerFactory.getLogger(VelbusNetworkBridgeHandler.class);
36 private @Nullable Socket socket;
37 private @NonNullByDefault({}) VelbusNetworkBridgeConfig networkBridgeConfig;
39 public VelbusNetworkBridgeHandler(Bridge velbusBridge) {
44 public void initialize() {
45 this.networkBridgeConfig = getConfigAs(VelbusNetworkBridgeConfig.class);
51 * Makes a connection to the Velbus system.
53 * @return True if the connection succeeded, false if the connection did not succeed.
56 protected boolean connect() {
58 Socket socket = new Socket(networkBridgeConfig.address, networkBridgeConfig.port);
61 initializeStreams(socket.getOutputStream(), socket.getInputStream());
63 updateStatus(ThingStatus.ONLINE);
64 logger.debug("Bridge online on network address {}:{}", networkBridgeConfig.address,
65 networkBridgeConfig.port);
67 // Start Velbus packet listener. This listener will act on all packets coming from
69 Thread thread = new Thread(this::readPackets, "OH-binding-" + this.thing.getUID());
70 thread.setDaemon(true);
74 } catch (IOException ex) {
76 logger.debug("Failed to connect to network address {}:{}", networkBridgeConfig.address,
77 networkBridgeConfig.port);
84 protected void disconnect() {
85 final Socket socket = this.socket;
89 } catch (IOException e) {
90 logger.debug("Error while closing socket", e);