]> git.basschouten.com Git - openhab-addons.git/blob
8bfc9697086108ce3991d7afbaee289c4e6dca2b
[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.milight.internal.handler;
14
15 import java.net.DatagramSocket;
16 import java.net.InetAddress;
17 import java.net.UnknownHostException;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.thing.Bridge;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingStatusDetail;
26 import org.openhab.core.thing.binding.BaseBridgeHandler;
27 import org.openhab.core.types.Command;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The {@link AbstractBridgeHandler} is responsible for handling commands, which are
33  * sent to one of the channels.
34  *
35  * @author David Graeff - Initial contribution
36  */
37 @NonNullByDefault
38 public abstract class AbstractBridgeHandler extends BaseBridgeHandler {
39     protected final Logger logger = LoggerFactory.getLogger(AbstractBridgeHandler.class);
40     protected volatile boolean preventReinit = false;
41     protected BridgeHandlerConfig config = new BridgeHandlerConfig();
42     protected @Nullable InetAddress address;
43     protected @NonNullByDefault({}) DatagramSocket socket;
44     protected int bridgeOffset;
45
46     public AbstractBridgeHandler(Bridge bridge, int bridgeOffset) {
47         super(bridge);
48         this.bridgeOffset = bridgeOffset;
49     }
50
51     @Override
52     public void handleCommand(ChannelUID channelUID, Command command) {
53         // There is nothing to handle in the bridge handler
54     }
55
56     @Override
57     public void handleConfigurationUpdate(Map<String, Object> configurationParameters) {
58         if (preventReinit) {
59             return;
60         }
61         super.handleConfigurationUpdate(configurationParameters);
62     }
63
64     /**
65      * Creates a connection and other supportive objects.
66      */
67     protected abstract void startConnectAndKeepAlive();
68
69     /**
70      * You need a CONFIG_HOST_NAME and CONFIG_ID for a milight bridge handler to initialize correctly.
71      * The ID is a unique 12 character long ASCII based on the bridge MAC address (for example ACCF23A20164)
72      * and is send as response for a discovery message.
73      */
74     @Override
75     public void initialize() {
76         config = getConfigAs(BridgeHandlerConfig.class);
77
78         if (!config.host.isEmpty()) {
79             try {
80                 address = InetAddress.getByName(config.host);
81             } catch (UnknownHostException ignored) {
82                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
83                         "Address set, but is invalid!");
84                 return;
85             }
86         }
87
88         startConnectAndKeepAlive();
89     }
90 }