]> git.basschouten.com Git - openhab-addons.git/blob
441f20c6a44e062f3505830da511b3510ea51ffb
[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      * @param addr
68      */
69     protected abstract void startConnectAndKeepAlive();
70
71     /**
72      * You need a CONFIG_HOST_NAME and CONFIG_ID for a milight bridge handler to initialize correctly.
73      * The ID is a unique 12 character long ASCII based on the bridge MAC address (for example ACCF23A20164)
74      * and is send as response for a discovery message.
75      */
76     @Override
77     public void initialize() {
78         config = getConfigAs(BridgeHandlerConfig.class);
79
80         if (!config.host.isEmpty()) {
81             try {
82                 address = InetAddress.getByName(config.host);
83             } catch (UnknownHostException ignored) {
84                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
85                         "Address set, but is invalid!");
86                 return;
87             }
88         }
89
90         startConnectAndKeepAlive();
91     }
92 }