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.milight.internal.handler;
15 import java.net.DatagramSocket;
16 import java.net.InetAddress;
17 import java.net.UnknownHostException;
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;
32 * The {@link AbstractBridgeHandler} is responsible for handling commands, which are
33 * sent to one of the channels.
35 * @author David Graeff - Initial contribution
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;
46 public AbstractBridgeHandler(Bridge bridge, int bridgeOffset) {
48 this.bridgeOffset = bridgeOffset;
52 public void handleCommand(ChannelUID channelUID, Command command) {
53 // There is nothing to handle in the bridge handler
57 public void handleConfigurationUpdate(Map<String, Object> configurationParameters) {
61 super.handleConfigurationUpdate(configurationParameters);
65 * Creates a connection and other supportive objects.
69 protected abstract void startConnectAndKeepAlive();
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.
77 public void initialize() {
78 config = getConfigAs(BridgeHandlerConfig.class);
80 if (!config.host.isEmpty()) {
82 address = InetAddress.getByName(config.host);
83 } catch (UnknownHostException ignored) {
84 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
85 "Address set, but is invalid!");
90 startConnectAndKeepAlive();