]> git.basschouten.com Git - openhab-addons.git/blob
187f51d6546ad0b0b6261229e23618cc909b866e
[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.net.Socket;
17
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;
25
26 /**
27  * {@link VelbusNetworkBridgeHandler} is the handler for a Velbus network interface and connects it to
28  * the framework.
29  *
30  * @author Cedric Boon - Initial contribution
31  */
32 @NonNullByDefault
33 public class VelbusNetworkBridgeHandler extends VelbusBridgeHandler {
34     private final Logger logger = LoggerFactory.getLogger(VelbusNetworkBridgeHandler.class);
35
36     private @Nullable Socket socket;
37     private @NonNullByDefault({}) VelbusNetworkBridgeConfig networkBridgeConfig;
38
39     public VelbusNetworkBridgeHandler(Bridge velbusBridge) {
40         super(velbusBridge);
41     }
42
43     @Override
44     public void initialize() {
45         this.networkBridgeConfig = getConfigAs(VelbusNetworkBridgeConfig.class);
46
47         super.initialize();
48     }
49
50     /**
51      * Makes a connection to the Velbus system.
52      *
53      * @return True if the connection succeeded, false if the connection did not succeed.
54      */
55     @Override
56     protected boolean connect() {
57         try {
58             Socket socket = new Socket(networkBridgeConfig.address, networkBridgeConfig.port);
59             this.socket = socket;
60
61             initializeStreams(socket.getOutputStream(), socket.getInputStream());
62
63             updateStatus(ThingStatus.ONLINE);
64             logger.debug("Bridge online on network address {}:{}", networkBridgeConfig.address,
65                     networkBridgeConfig.port);
66
67             // Start Velbus packet listener. This listener will act on all packets coming from
68             // IP-interface.
69             Thread thread = new Thread(this::readPackets, "OH-binding-" + this.thing.getUID());
70             thread.setDaemon(true);
71             thread.start();
72
73             return true;
74         } catch (IOException ex) {
75             onConnectionLost();
76             logger.debug("Failed to connect to network address {}:{}", networkBridgeConfig.address,
77                     networkBridgeConfig.port);
78         }
79
80         return false;
81     }
82
83     @Override
84     protected void disconnect() {
85         final Socket socket = this.socket;
86         if (socket != null) {
87             try {
88                 socket.close();
89             } catch (IOException e) {
90                 logger.debug("Error while closing socket", e);
91             }
92             this.socket = null;
93         }
94
95         super.disconnect();
96     }
97 }