Search in sources :

Example 1 with Bridge

use of org.eclipse.smarthome.core.thing.Bridge in project smarthome by eclipse.

the class TradfriThingHandler method initialize.

@Override
public synchronized void initialize() {
    Bridge tradfriGateway = getBridge();
    this.id = getConfigAs(TradfriDeviceConfig.class).id;
    TradfriGatewayHandler handler = (TradfriGatewayHandler) tradfriGateway.getHandler();
    String uriString = handler.getGatewayURI() + "/" + id;
    try {
        URI uri = new URI(uriString);
        coapClient = new TradfriCoapClient(uri);
        coapClient.setEndpoint(handler.getEndpoint());
    } catch (URISyntaxException e) {
        logger.debug("Illegal device URI `{}`: {}", uriString, e.getMessage());
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
        return;
    }
    active = true;
    updateStatus(ThingStatus.UNKNOWN);
    switch(tradfriGateway.getStatus()) {
        case ONLINE:
            scheduler.schedule(() -> {
                coapClient.startObserve(this);
            }, 3, TimeUnit.SECONDS);
            break;
        case OFFLINE:
        default:
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, String.format("Gateway offline '%s'", tradfriGateway.getStatusInfo()));
            break;
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Bridge(org.eclipse.smarthome.core.thing.Bridge) TradfriCoapClient(org.eclipse.smarthome.binding.tradfri.internal.TradfriCoapClient)

Example 2 with Bridge

use of org.eclipse.smarthome.core.thing.Bridge in project smarthome by eclipse.

the class WemoLightHandler method getWemoBridgeHandler.

private synchronized WemoBridgeHandler getWemoBridgeHandler() {
    if (this.wemoBridgeHandler == null) {
        Bridge bridge = getBridge();
        if (bridge == null) {
            logger.error("Required bridge not defined for device {}.", wemoLightID);
            return null;
        }
        ThingHandler handler = bridge.getHandler();
        if (handler instanceof WemoBridgeHandler) {
            this.wemoBridgeHandler = (WemoBridgeHandler) handler;
        } else {
            logger.debug("No available bridge handler found for {} bridge {} .", wemoLightID, bridge.getUID());
            return null;
        }
    }
    return this.wemoBridgeHandler;
}
Also used : BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Bridge(org.eclipse.smarthome.core.thing.Bridge)

Example 3 with Bridge

use of org.eclipse.smarthome.core.thing.Bridge in project smarthome by eclipse.

the class ThingConsoleCommandExtension method printThings.

private void printThings(Console console, Collection<Thing> things) {
    if (things.isEmpty()) {
        console.println("No things found.");
    }
    for (Thing thing : things) {
        String id = thing.getUID().toString();
        String thingType = thing instanceof Bridge ? "Bridge" : "Thing";
        ThingStatusInfo status = thingStatusInfoI18nLocalizationService.getLocalizedThingStatusInfo(thing, null);
        ThingUID bridgeUID = thing.getBridgeUID();
        String label = thing.getLabel();
        console.println(String.format("%s (Type=%s, Status=%s, Label=%s, Bridge=%s)", id, thingType, status, label, bridgeUID));
    }
}
Also used : ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo) Thing(org.eclipse.smarthome.core.thing.Thing) Bridge(org.eclipse.smarthome.core.thing.Bridge)

Example 4 with Bridge

use of org.eclipse.smarthome.core.thing.Bridge in project smarthome by eclipse.

the class BaseBridgeHandler method getThingByUID.

/**
 * 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
 */
@Nullable
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;
}
Also used : Bridge(org.eclipse.smarthome.core.thing.Bridge) Thing(org.eclipse.smarthome.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 5 with Bridge

use of org.eclipse.smarthome.core.thing.Bridge in project smarthome by eclipse.

the class ThingManagerOSGiJavaTest method testChildHandlerInitialized_replacedInitializedThing.

@Test
public void testChildHandlerInitialized_replacedInitializedThing() {
    Semaphore childHandlerInitializedSemaphore = new Semaphore(1);
    Semaphore thingUpdatedSemapthore = new Semaphore(1);
    registerThingHandlerFactory(BRIDGE_TYPE_UID, bridge -> new BaseBridgeHandler((Bridge) bridge) {

        @Override
        public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
        }

        @Override
        public void initialize() {
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
            try {
                childHandlerInitializedSemaphore.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {

        @Override
        public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
        }

        @Override
        public void initialize() {
            updateStatus(ThingStatus.ONLINE);
        }

        @Override
        public void thingUpdated(Thing thing) {
            this.thing = thing;
            try {
                thingUpdatedSemapthore.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    Bridge bridge = BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_UID).build();
    managedThingProvider.add(bridge);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, bridge.getStatus());
    });
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
    managedThingProvider.add(thing);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, thing.getStatus());
    });
    assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
    Thing thing2 = ThingBuilder.create(THING_TYPE_UID, THING_UID).withBridge(BRIDGE_UID).build();
    managedThingProvider.update(thing2);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, thing2.getStatus());
    });
    // childHandlerInitialized(...) is not be called - framework calls ThingHandler.thingUpdated(...) instead.
    assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
    // ThingHandler.thingUpdated(...) must be called
    assertEquals(0, thingUpdatedSemapthore.availablePermits());
}
Also used : BaseBridgeHandler(org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler) Command(org.eclipse.smarthome.core.types.Command) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Semaphore(java.util.concurrent.Semaphore) Bridge(org.eclipse.smarthome.core.thing.Bridge) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

Bridge (org.eclipse.smarthome.core.thing.Bridge)26 Configuration (org.eclipse.smarthome.config.core.Configuration)9 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)9 BaseThingHandler (org.eclipse.smarthome.core.thing.binding.BaseThingHandler)9 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)9 DmxBridgeHandler (org.eclipse.smarthome.binding.dmx.internal.DmxBridgeHandler)7 Nullable (org.eclipse.jdt.annotation.Nullable)6 Thing (org.eclipse.smarthome.core.thing.Thing)6 BaseDmxChannel (org.eclipse.smarthome.binding.dmx.internal.multiverse.BaseDmxChannel)5 Semaphore (java.util.concurrent.Semaphore)3 ValueSet (org.eclipse.smarthome.binding.dmx.internal.ValueSet)3 BaseBridgeHandler (org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler)3 Command (org.eclipse.smarthome.core.types.Command)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 Before (org.junit.Before)3 Test (org.junit.Test)3 HueBridge (org.eclipse.smarthome.binding.hue.internal.HueBridge)2 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1