Search in sources :

Example 1 with ThingHandlerFactory

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

the class ThingManager method getThingHandlerFactory.

private ThingHandlerFactory getThingHandlerFactory(Thing thing) {
    ThingHandlerFactory thingHandlerFactory = findThingHandlerFactory(thing.getThingTypeUID());
    if (thingHandlerFactory != null) {
        return thingHandlerFactory;
    }
    logger.debug("Not registering a handler at this point since no handler factory for thing '{}' found.", thing.getUID());
    return null;
}
Also used : ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory)

Example 2 with ThingHandlerFactory

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

the class ThingManagerOSGiJavaTest method testCreateChannelBuilder.

@Test
public void testCreateChannelBuilder() throws Exception {
    registerThingTypeProvider();
    registerChannelTypeProvider();
    AtomicReference<ThingHandlerCallback> thc = new AtomicReference<>();
    ThingHandlerFactory thingHandlerFactory = new BaseThingHandlerFactory() {

        @Override
        public boolean supportsThingType(@NonNull ThingTypeUID thingTypeUID) {
            return true;
        }

        @Override
        @Nullable
        protected ThingHandler createHandler(@NonNull Thing thing) {
            ThingHandler mockHandler = mock(ThingHandler.class);
            doAnswer(a -> {
                thc.set((ThingHandlerCallback) a.getArguments()[0]);
                return null;
            }).when(mockHandler).setCallback(any(ThingHandlerCallback.class));
            when(mockHandler.getThing()).thenReturn(THING);
            return mockHandler;
        }
    };
    registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
    new Thread((Runnable) () -> managedThingProvider.add(THING)).start();
    waitForAssert(() -> {
        assertNotNull(thc.get());
    });
    ChannelBuilder channelBuilder = thc.get().createChannelBuilder(new ChannelUID(THING_UID, "test"), CHANNEL_TYPE_UID);
    Channel channel = channelBuilder.build();
    assertThat(channel.getLabel(), is("Test Label"));
    assertThat(channel.getDescription(), is("Test Description"));
    assertThat(channel.getAcceptedItemType(), is("Switch"));
    assertThat(channel.getDefaultTags().size(), is(1));
    assertThat(channel.getDefaultTags().iterator().next(), is("Test Tag"));
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) ThingHandlerCallback(org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback) AtomicReference(java.util.concurrent.atomic.AtomicReference) BaseThingHandler(org.eclipse.smarthome.core.thing.binding.BaseThingHandler) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) NonNull(org.eclipse.jdt.annotation.NonNull) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ChannelBuilder(org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 3 with ThingHandlerFactory

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

the class ChannelStateDescriptionProviderOSGiTest method setup.

@Before
public void setup() {
    initMocks(this);
    Mockito.when(componentContext.getBundleContext()).thenReturn(bundleContext);
    registerVolatileStorageService();
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    final TestThingHandlerFactory thingHandlerFactory = new TestThingHandlerFactory();
    thingHandlerFactory.activate(componentContext);
    registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
    final StateDescription state = new StateDescription(BigDecimal.ZERO, BigDecimal.valueOf(100), BigDecimal.TEN, "%d Peek", true, Collections.singletonList(new StateOption("SOUND", "My great sound.")));
    final StateDescription state2 = new StateDescription(BigDecimal.ZERO, BigDecimal.valueOf(256), BigDecimal.valueOf(8), null, false, null);
    final ChannelType channelType = new ChannelType(new ChannelTypeUID("hue:alarm"), false, "Number", " ", "", null, null, state, null);
    final ChannelType channelType2 = new ChannelType(new ChannelTypeUID("hue:num"), false, "Number", " ", "", null, null, state2, null);
    final ChannelType channelType3 = new ChannelType(new ChannelTypeUID("hue:info"), true, "String", " ", "", null, null, null, null);
    final ChannelType channelType4 = new ChannelType(new ChannelTypeUID("hue:color"), false, "Color", "Color", "", "ColorLight", null, null, null);
    final ChannelType channelType5 = new ChannelType(new ChannelTypeUID("hue:brightness"), false, "Dimmer", "Brightness", "", "DimmableLight", null, null, null);
    final ChannelType channelType6 = new ChannelType(new ChannelTypeUID("hue:switch"), false, "Switch", "Switch", "", "Light", null, null, null);
    final ChannelType channelType7 = new ChannelType(new ChannelTypeUID("hue:num-dynamic"), false, "Number", " ", "", "Light", null, state, null);
    List<ChannelType> channelTypes = new ArrayList<>();
    channelTypes.add(channelType);
    channelTypes.add(channelType2);
    channelTypes.add(channelType3);
    channelTypes.add(channelType4);
    channelTypes.add(channelType5);
    channelTypes.add(channelType6);
    channelTypes.add(channelType7);
    registerService(new ChannelTypeProvider() {

        @Override
        public Collection<ChannelType> getChannelTypes(Locale locale) {
            return channelTypes;
        }

        @Override
        public ChannelType getChannelType(ChannelTypeUID channelTypeUID, Locale locale) {
            for (final ChannelType channelType : channelTypes) {
                if (channelType.getUID().equals(channelTypeUID)) {
                    return channelType;
                }
            }
            return null;
        }

        @Override
        public ChannelGroupType getChannelGroupType(ChannelGroupTypeUID channelGroupTypeUID, Locale locale) {
            return null;
        }

        @Override
        public Collection<ChannelGroupType> getChannelGroupTypes(Locale locale) {
            return Collections.emptySet();
        }
    });
    registerService(new DynamicStateDescriptionProvider() {

        final StateDescription newState = new StateDescription(BigDecimal.valueOf(10), BigDecimal.valueOf(100), BigDecimal.valueOf(5), "VALUE %d", false, Arrays.asList(new StateOption("value0", "label0"), new StateOption("value1", "label1")));

        @Override
        @Nullable
        public StateDescription getStateDescription(@NonNull Channel channel, @Nullable StateDescription original, @Nullable Locale locale) {
            String id = channel.getUID().getIdWithoutGroup();
            if ("7_1".equals(id)) {
                assertEquals(channel.getChannelTypeUID(), channelType7.getUID());
                return newState;
            } else if ("7_2".equals(id)) {
                assertEquals(channel.getChannelTypeUID(), channelType7.getUID());
                StateDescription newState2 = new StateDescription(original.getMinimum().add(BigDecimal.ONE), original.getMaximum().add(BigDecimal.ONE), original.getStep().add(BigDecimal.TEN), "NEW " + original.getPattern(), true, original.getOptions());
                return newState2;
            }
            return null;
        }
    });
    List<ChannelDefinition> channelDefinitions = new ArrayList<>();
    channelDefinitions.add(new ChannelDefinition("1", channelType.getUID()));
    channelDefinitions.add(new ChannelDefinition("2", channelType2.getUID()));
    channelDefinitions.add(new ChannelDefinition("3", channelType3.getUID()));
    channelDefinitions.add(new ChannelDefinition("4", channelType4.getUID()));
    channelDefinitions.add(new ChannelDefinition("5", channelType5.getUID()));
    channelDefinitions.add(new ChannelDefinition("6", channelType6.getUID()));
    channelDefinitions.add(new ChannelDefinition("7_1", channelType7.getUID()));
    channelDefinitions.add(new ChannelDefinition("7_2", channelType7.getUID()));
    registerService(new SimpleThingTypeProvider(Collections.singleton(ThingTypeBuilder.instance(new ThingTypeUID("hue:lamp"), "label").withChannelDefinitions(channelDefinitions).build())));
    List<Item> items = new ArrayList<>();
    items.add(new NumberItem("TestItem"));
    items.add(new NumberItem("TestItem2"));
    items.add(new StringItem("TestItem3"));
    items.add(new ColorItem("TestItem4"));
    items.add(new DimmerItem("TestItem5"));
    items.add(new SwitchItem("TestItem6"));
    items.add(new NumberItem("TestItem7_1"));
    items.add(new NumberItem("TestItem7_2"));
    registerService(new TestItemProvider(items));
    linkRegistry = getService(ItemChannelLinkRegistry.class);
    stateDescriptionProvider = getService(StateDescriptionProvider.class);
    assertNotNull(stateDescriptionProvider);
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) ChannelGroupType(org.eclipse.smarthome.core.thing.type.ChannelGroupType) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) DynamicStateDescriptionProvider(org.eclipse.smarthome.core.thing.type.DynamicStateDescriptionProvider) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) ItemChannelLinkRegistry(org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) StateDescription(org.eclipse.smarthome.core.types.StateDescription) Channel(org.eclipse.smarthome.core.thing.Channel) ChannelGroupTypeUID(org.eclipse.smarthome.core.thing.type.ChannelGroupTypeUID) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) StringItem(org.eclipse.smarthome.core.library.items.StringItem) StateOption(org.eclipse.smarthome.core.types.StateOption) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) ChannelDefinition(org.eclipse.smarthome.core.thing.type.ChannelDefinition) ChannelTypeProvider(org.eclipse.smarthome.core.thing.type.ChannelTypeProvider) Collection(java.util.Collection) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) StateDescriptionProvider(org.eclipse.smarthome.core.types.StateDescriptionProvider) DynamicStateDescriptionProvider(org.eclipse.smarthome.core.thing.type.DynamicStateDescriptionProvider) Nullable(org.eclipse.jdt.annotation.Nullable) Before(org.junit.Before)

Example 4 with ThingHandlerFactory

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

the class ThingRegistryOSGiTest method assertThatCreateThingDelegatesToRegisteredThingHandlerFactory.

@Test
public void assertThatCreateThingDelegatesToRegisteredThingHandlerFactory() {
    ThingTypeUID expectedThingTypeUID = THING_TYPE_UID;
    ThingUID expectedThingUID = new ThingUID(THING_TYPE_UID, THING1_ID);
    Configuration expectedConfiguration = new Configuration();
    ThingUID expectedBridgeUID = new ThingUID(THING_TYPE_UID, THING2_ID);
    String expectedLabel = "Test Thing";
    AsyncResultWrapper<Thing> thingResultWrapper = new AsyncResultWrapper<Thing>();
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    ThingHandlerFactory thingHandlerFactory = new BaseThingHandlerFactory() {

        @Override
        public boolean supportsThingType(@NonNull ThingTypeUID thingTypeUID) {
            return true;
        }

        @Override
        @Nullable
        protected ThingHandler createHandler(@NonNull Thing thing) {
            return null;
        }

        @Override
        @Nullable
        public Thing createThing(@NonNull ThingTypeUID thingTypeUID, @NonNull Configuration configuration, @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
            assertThat(thingTypeUID, is(expectedThingTypeUID));
            assertThat(configuration, is(expectedConfiguration));
            assertThat(thingUID, is(expectedThingUID));
            assertThat(bridgeUID, is(expectedBridgeUID));
            Thing thing = ThingBuilder.create(thingTypeUID, thingUID.getId()).withBridge(bridgeUID).build();
            thingResultWrapper.set(thing);
            return thing;
        }
    };
    registerThingHandlerFactory(thingHandlerFactory);
    Thing thing = thingRegistry.createThingOfType(expectedThingTypeUID, expectedThingUID, expectedBridgeUID, expectedLabel, expectedConfiguration);
    waitForAssert(() -> {
        assertTrue(thingResultWrapper.isSet());
    });
    assertThat(thing, is(thingResultWrapper.getWrappedObject()));
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) AsyncResultWrapper(org.eclipse.smarthome.test.AsyncResultWrapper) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) NonNull(org.eclipse.jdt.annotation.NonNull) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) Thing(org.eclipse.smarthome.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 5 with ThingHandlerFactory

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

the class ThingManager method thingUpdated.

@Override
public void thingUpdated(final Thing thing, ThingTrackerEvent thingTrackerEvent) {
    ThingUID thingUID = thing.getUID();
    if (thingUpdatedLock.contains(thingUID)) {
        // called from the thing handler itself, therefore
        // it exists, is initializing/initialized and
        // must not be informed (in order to prevent infinite loops)
        replaceThing(getThing(thingUID), thing);
    } else {
        Lock lock1 = getLockForThing(thing.getUID());
        try {
            lock1.lock();
            Thing oldThing = getThing(thingUID);
            ThingHandler thingHandler = replaceThing(oldThing, thing);
            if (thingHandler != null) {
                if (ThingHandlerHelper.isHandlerInitialized(thing) || isInitializing(thing)) {
                    if (oldThing != null) {
                        oldThing.setHandler(null);
                    }
                    thing.setHandler(thingHandler);
                    safeCaller.create(thingHandler, ThingHandler.class).build().thingUpdated(thing);
                } else {
                    logger.debug("Cannot notify handler about updated thing '{}', because handler is not initialized (thing must be in status UNKNOWN, ONLINE or OFFLINE).", thing.getThingTypeUID());
                    if (thingHandler.getThing() == thing) {
                        logger.debug("Initializing handler of thing '{}'", thing.getThingTypeUID());
                        if (oldThing != null) {
                            oldThing.setHandler(null);
                        }
                        thing.setHandler(thingHandler);
                        initializeHandler(thing);
                    } else {
                        logger.debug("Replacing uninitialized handler for updated thing '{}'", thing.getThingTypeUID());
                        ThingHandlerFactory thingHandlerFactory = getThingHandlerFactory(thing);
                        unregisterHandler(thingHandler.getThing(), thingHandlerFactory);
                        registerAndInitializeHandler(thing, thingHandlerFactory);
                    }
                }
            } else {
                registerAndInitializeHandler(thing, getThingHandlerFactory(thing));
            }
        } finally {
            lock1.unlock();
        }
    }
}
Also used : ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) Thing(org.eclipse.smarthome.core.thing.Thing) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Aggregations

ThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory)8 Thing (org.eclipse.smarthome.core.thing.Thing)5 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)4 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)3 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)3 BaseThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory)3 ArrayList (java.util.ArrayList)2 Lock (java.util.concurrent.locks.Lock)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 NonNull (org.eclipse.jdt.annotation.NonNull)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 Channel (org.eclipse.smarthome.core.thing.Channel)2 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)2 Collection (java.util.Collection)1 List (java.util.List)1 Locale (java.util.Locale)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Configuration (org.eclipse.smarthome.config.core.Configuration)1 Item (org.eclipse.smarthome.core.items.Item)1