Search in sources :

Example 1 with BaseThingHandler

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

the class ThingManagerOSGiJavaTest method testInitializeOnlyIfInitializable.

@Test
public void testInitializeOnlyIfInitializable() throws Exception {
    registerThingTypeProvider();
    registerChannelTypeProvider();
    registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {

        @Override
        public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
        }
    });
    ConfigDescriptionProvider mockConfigDescriptionProvider = mock(ConfigDescriptionProvider.class);
    List<ConfigDescriptionParameter> parameters = // 
    Collections.singletonList(// 
    ConfigDescriptionParameterBuilder.create(CONFIG_PARAM_NAME, Type.TEXT).withRequired(true).build());
    registerService(mockConfigDescriptionProvider, ConfigDescriptionProvider.class.getName());
    // verify a missing mandatory thing config prevents it from getting initialized
    when(mockConfigDescriptionProvider.getConfigDescription(eq(CONFIG_DESCRIPTION_THING), any())).thenReturn(new ConfigDescription(CONFIG_DESCRIPTION_THING, parameters));
    assertThingStatus(Collections.emptyMap(), Collections.emptyMap(), ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_CONFIGURATION_PENDING);
    // verify a missing mandatory channel config prevents it from getting initialized
    when(mockConfigDescriptionProvider.getConfigDescription(eq(CONFIG_DESCRIPTION_CHANNEL), any())).thenReturn(new ConfigDescription(CONFIG_DESCRIPTION_CHANNEL, parameters));
    assertThingStatus(Collections.singletonMap(CONFIG_PARAM_NAME, "value"), Collections.emptyMap(), ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_CONFIGURATION_PENDING);
    // verify a satisfied config does not prevent it from getting initialized anymore
    assertThingStatus(Collections.singletonMap(CONFIG_PARAM_NAME, "value"), Collections.singletonMap(CONFIG_PARAM_NAME, "value"), ThingStatus.ONLINE, ThingStatusDetail.NONE);
}
Also used : BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) Command(org.eclipse.smarthome.core.types.Command) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) ConfigDescriptionProvider(org.eclipse.smarthome.config.core.ConfigDescriptionProvider) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 2 with BaseThingHandler

use of org.eclipse.smarthome.core.thing.binding.BaseThingHandler 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)

Example 3 with BaseThingHandler

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

the class ThingRegistryOSGiTest method assertThatThingRegistryDelegatesConfigUpdateToThingHandler.

@Test
public void assertThatThingRegistryDelegatesConfigUpdateToThingHandler() {
    ThingUID thingUID = new ThingUID("binding:type:thing");
    Thing thing = ThingBuilder.create(THING_TYPE_UID, thingUID).build();
    ThingHandler thingHandler = new BaseThingHandler(thing) {

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

        @Override
        public void handleConfigurationUpdate(@NonNull Map<@NonNull String, @NonNull Object> configurationParameters) {
            changedParameters = configurationParameters;
        }
    };
    thing.setHandler(thingHandler);
    ThingProvider thingProvider = new ThingProvider() {

        @Override
        public void addProviderChangeListener(ProviderChangeListener<@NonNull Thing> listener) {
        }

        @Override
        public Collection<@NonNull Thing> getAll() {
            return Collections.singleton(thing);
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<@NonNull Thing> listener) {
        }
    };
    registerService(thingProvider);
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("param1", "value1");
    parameters.put("param2", 1);
    thingRegistry.updateConfiguration(thingUID, parameters);
    assertThat(changedParameters.entrySet(), is(equalTo(parameters.entrySet())));
}
Also used : BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) HashMap(java.util.HashMap) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) ThingProvider(org.eclipse.smarthome.core.thing.ThingProvider) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) Command(org.eclipse.smarthome.core.types.Command) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) NonNull(org.eclipse.jdt.annotation.NonNull) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) HashMap(java.util.HashMap) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 4 with BaseThingHandler

use of org.eclipse.smarthome.core.thing.binding.BaseThingHandler 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 5 with BaseThingHandler

use of org.eclipse.smarthome.core.thing.binding.BaseThingHandler 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)

Aggregations

ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)5 BaseThingHandler (org.eclipse.smarthome.core.thing.binding.BaseThingHandler)5 Command (org.eclipse.smarthome.core.types.Command)5 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)5 Test (org.junit.Test)5 Thing (org.eclipse.smarthome.core.thing.Thing)4 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)4 Semaphore (java.util.concurrent.Semaphore)3 Bridge (org.eclipse.smarthome.core.thing.Bridge)3 BaseBridgeHandler (org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler)3 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 ConfigDescription (org.eclipse.smarthome.config.core.ConfigDescription)1 ConfigDescriptionParameter (org.eclipse.smarthome.config.core.ConfigDescriptionParameter)1 ConfigDescriptionProvider (org.eclipse.smarthome.config.core.ConfigDescriptionProvider)1 ProviderChangeListener (org.eclipse.smarthome.core.common.registry.ProviderChangeListener)1 ManagedThingProvider (org.eclipse.smarthome.core.thing.ManagedThingProvider)1 ThingProvider (org.eclipse.smarthome.core.thing.ThingProvider)1 ThingRegistry (org.eclipse.smarthome.core.thing.ThingRegistry)1