]> git.basschouten.com Git - openhab-addons.git/blob
dad98ca73d0d20d32b941052bfd5810e1e77ae7b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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             return true;
67         } catch (IOException ex) {
68             onConnectionLost();
69             logger.debug("Failed to connect to network address {}:{}", networkBridgeConfig.address,
70                     networkBridgeConfig.port);
71         }
72
73         // Start Velbus packet listener. This listener will act on all packets coming from
74         // IP-interface.
75         Thread thread = new Thread(this::readPackets, "OH-binding-" + this.thing.getUID());
76         thread.setDaemon(true);
77         thread.start();
78
79         return false;
80     }
81
82     @Override
83     protected void disconnect() {
84         final Socket socket = this.socket;
85         if (socket != null) {
86             try {
87                 socket.close();
88             } catch (IOException e) {
89                 logger.debug("Error while closing socket", e);
90             }
91             this.socket = null;
92         }
93
94         super.disconnect();
95     }
96 }