/** * Copyright (c) 2014-2017 by the respective copyright holders. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html */ package org.eclipse.smarthome.core.thing.binding; import java.util.List; import org.eclipse.smarthome.core.thing.Bridge; import org.eclipse.smarthome.core.thing.Thing; import org.eclipse.smarthome.core.thing.ThingUID; import org.eclipse.smarthome.core.thing.binding.builder.BridgeBuilder; /** * The {@link BaseBridgeHandler} implements the {@link BridgeHandler} interface and adds some convenience methods for * bridges to the {@link BaseThingHandler}. * <p> * It is recommended to extend this abstract base class. * <p> * * @author Dennis Nobel - Initial contribution * @author Stefan Bußweiler - Added implementation of BridgeHandler interface */ public abstract class BaseBridgeHandler extends BaseThingHandler implements BridgeHandler { /** * @see BaseThingHandler */ public BaseBridgeHandler(Bridge bridge) { super(bridge); } /** * Finds and returns a child thing for a given UID of this bridge. * * @param uid uid of the child thing * @return child thing with the given uid or null if thing was not found */ public Thing getThingByUID(ThingUID uid) { Bridge bridge = getThing(); List<Thing> things = bridge.getThings(); for (Thing thing : things) { if (thing.getUID().equals(uid)) { return thing; } } return null; } @Override public Bridge getThing() { return (Bridge) super.getThing(); } /** * Creates a bridge builder, which allows to modify the bridge. The method * {@link BaseThingHandler#updateThing(Thing)} must be called to persist the changes. * * @return {@link BridgeBuilder} which builds an exact copy of the bridge (not null) */ @Override protected BridgeBuilder editThing() { return BridgeBuilder.create(this.thing.getThingTypeUID(), this.thing.getUID()) .withBridge(this.thing.getBridgeUID()).withChannels(this.thing.getChannels()) .withConfiguration(this.thing.getConfiguration()).withLabel(this.thing.getLabel()) .withLocation(this.thing.getLocation()).withProperties(this.thing.getProperties()); } @Override public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) { // do nothing by default, can be overridden by subclasses } @Override public void childHandlerDisposed(ThingHandler childHandler, Thing childThing) { // do nothing by default, can be overridden by subclasses } }