Search in sources :

Example 16 with ThingHandler

use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.

the class ThingManagerOSGiJavaTest method testChildHandlerInitialized_modifiedUninitializedThing.

@Test
public void testChildHandlerInitialized_modifiedUninitializedThing() {
    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() {
            if (getBridge() == null) {
                throw new RuntimeException("Fail because of missing bridge");
            }
            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.UNINITIALIZED, thing.getStatus());
        assertEquals(ThingStatusDetail.HANDLER_INITIALIZING_ERROR, thing.getStatusInfo().getStatusDetail());
    });
    assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
    thing.setBridgeUID(bridge.getUID());
    managedThingProvider.update(thing);
    waitForAssert(() -> {
        assertEquals(ThingStatus.ONLINE, thing.getStatus());
    });
    // childHandlerInitialized(...) must be called
    waitForAssert(() -> assertEquals(0, childHandlerInitializedSemaphore.availablePermits()));
    // thingUpdated(...) is not called
    assertEquals(1, 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)

Example 17 with ThingHandler

use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.

the class ThingManagerOSGiJavaTest method testChildHandlerInitialized_replacedUnitializedThing.

@Test
public void testChildHandlerInitialized_replacedUnitializedThing() {
    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() {
            if (getBridge() == null) {
                throw new RuntimeException("Fail because of missing bridge");
            }
            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.UNINITIALIZED, thing.getStatus());
        assertEquals(ThingStatusDetail.HANDLER_INITIALIZING_ERROR, thing.getStatusInfo().getStatusDetail());
    });
    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(...) must be called
    waitForAssert(() -> assertEquals(0, childHandlerInitializedSemaphore.availablePermits()));
    // thingUpdated(...) is not called
    assertEquals(1, 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)

Example 18 with ThingHandler

use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.

the class ZoneTemperatureControlHandler method getDssBridgeHandler.

private synchronized BridgeHandler getDssBridgeHandler() {
    if (this.dssBridgeHandler == null) {
        Bridge bridge = getBridge();
        if (bridge == null) {
            logger.debug("Bride cannot be found");
            return null;
        }
        ThingHandler handler = bridge.getHandler();
        if (handler instanceof BridgeHandler) {
            dssBridgeHandler = (BridgeHandler) handler;
        } else {
            return null;
        }
    }
    return dssBridgeHandler;
}
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 19 with ThingHandler

use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.

the class BridgeHandler method setStatus.

private void setStatus(ThingStatus status) {
    logger.debug("set status to: {}", status);
    updateStatus(status);
    for (Thing thing : getThing().getThings()) {
        ThingHandler handler = thing.getHandler();
        if (handler != null) {
            handler.bridgeStatusChanged(getThing().getStatusInfo());
        }
    }
}
Also used : ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Thing(org.eclipse.smarthome.core.thing.Thing)

Example 20 with ThingHandler

use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.

the class Job method scheduleEvent.

/**
 * Schedules an {@link EventJob} instance
 *
 * @param thingUID the Thing UID
 * @param astroHandler the {@link ThingHandler} instance
 * @param eventAt the {@link Calendar} instance denoting scheduled instant
 * @param events the event IDs to schedule
 * @param channelId the channel ID
 */
public static void scheduleEvent(String thingUID, AstroThingHandler astroHandler, Calendar eventAt, List<String> events, String channelId, boolean configAlreadyApplied) {
    boolean thingNull = checkNull(thingUID, "Thing UID is null");
    boolean astroHandlerNull = checkNull(astroHandler, "AstroThingHandler is null");
    boolean eventAtNull = checkNull(eventAt, "Scheduled Instant is null");
    boolean eventsNull = checkNull(events, "Events list is null");
    boolean channelIdNull = checkNull(channelId, "Channel ID is null");
    if (thingNull || astroHandlerNull || eventAtNull || eventsNull || channelIdNull || events.isEmpty()) {
        return;
    }
    final Calendar instant;
    if (!configAlreadyApplied) {
        final Channel channel = astroHandler.getThing().getChannel(channelId);
        if (channel == null) {
            LOGGER.warn("Cannot find channel '{}' for thing '{}'.", channelId, astroHandler.getThing().getUID());
            return;
        }
        AstroChannelConfig config = channel.getConfiguration().as(AstroChannelConfig.class);
        instant = applyConfig(eventAt, config);
    } else {
        instant = eventAt;
    }
    List<Job> jobs = events.stream().map(e -> new EventJob(thingUID, channelId, e)).collect(toList());
    schedule(thingUID, astroHandler, new CompositeJob(thingUID, jobs), instant);
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) SECOND(java.util.Calendar.SECOND) AstroChannelConfig(org.eclipse.smarthome.binding.astro.internal.config.AstroChannelConfig) Logger(org.slf4j.Logger) DateTimeUtils(org.eclipse.smarthome.binding.astro.internal.util.DateTimeUtils) MethodHandles(java.lang.invoke.MethodHandles) Planet(org.eclipse.smarthome.binding.astro.internal.model.Planet) LoggerFactory(org.slf4j.LoggerFactory) AstroThingHandler(org.eclipse.smarthome.binding.astro.handler.AstroThingHandler) Collections.singletonList(java.util.Collections.singletonList) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Calendar(java.util.Calendar) Range(org.eclipse.smarthome.binding.astro.internal.model.Range) Arrays.asList(java.util.Arrays.asList) Thing(org.eclipse.smarthome.core.thing.Thing) Objects.isNull(java.util.Objects.isNull) DateUtils.truncatedEquals(org.apache.commons.lang.time.DateUtils.truncatedEquals) AstroBindingConstants(org.eclipse.smarthome.binding.astro.AstroBindingConstants) SunPhaseName(org.eclipse.smarthome.binding.astro.internal.model.SunPhaseName) Calendar(java.util.Calendar) Channel(org.eclipse.smarthome.core.thing.Channel) AstroChannelConfig(org.eclipse.smarthome.binding.astro.internal.config.AstroChannelConfig)

Aggregations

ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)30 Thing (org.eclipse.smarthome.core.thing.Thing)14 BaseThingHandler (org.eclipse.smarthome.core.thing.binding.BaseThingHandler)12 Bridge (org.eclipse.smarthome.core.thing.Bridge)9 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)6 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)6 Test (org.junit.Test)6 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)4 ThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory)4 Command (org.eclipse.smarthome.core.types.Command)4 Semaphore (java.util.concurrent.Semaphore)3 Lock (java.util.concurrent.locks.Lock)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 Channel (org.eclipse.smarthome.core.thing.Channel)3 BaseBridgeHandler (org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler)3 List (java.util.List)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 NonNull (org.eclipse.jdt.annotation.NonNull)2 ThingStatusInfo (org.eclipse.smarthome.core.thing.ThingStatusInfo)2 MethodHandles (java.lang.invoke.MethodHandles)1