Search in sources :

Example 1 with ThingHandlerFactory

use of org.openhab.core.thing.binding.ThingHandlerFactory in project openhab-core by openhab.

the class DynamicThingUpdateOSGiTest method setUp.

@BeforeEach
public void setUp() {
    registerVolatileStorageService();
    ThingTypeProvider thingTypeProvider = mock(ThingTypeProvider.class);
    when(thingTypeProvider.getThingType(eq(THING_TYPE_UID), any())).thenReturn(THING_TYPE);
    registerService(thingTypeProvider);
    inbox = getService(Inbox.class);
    managedThingProvider = getService(ManagedThingProvider.class);
    assertEquals(0, inbox.getAll().size());
    ThingHandlerFactory thingHandlerFactory = createThingHandlerFactory();
    registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
}
Also used : ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ThingTypeProvider(org.openhab.core.thing.binding.ThingTypeProvider) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) BaseThingHandlerFactory(org.openhab.core.thing.binding.BaseThingHandlerFactory) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with ThingHandlerFactory

use of org.openhab.core.thing.binding.ThingHandlerFactory in project openhab-core by openhab.

the class ThingRegistryOSGiTest method assertThatCreateThingDelegatesToRegisteredThingHandlerFactory.

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

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

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

        @Override
        @Nullable
        public Thing createThing(ThingTypeUID thingTypeUID, 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.get() != null);
    });
    assertThat(thing, is(notNullValue()));
    if (thing != null) {
        assertThat(thing, is(thingResultWrapper.get()));
    }
}
Also used : Configuration(org.openhab.core.config.core.Configuration) BaseThingHandlerFactory(org.openhab.core.thing.binding.BaseThingHandlerFactory) ThingUID(org.openhab.core.thing.ThingUID) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) AtomicReference(java.util.concurrent.atomic.AtomicReference) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) BaseThingHandlerFactory(org.openhab.core.thing.binding.BaseThingHandlerFactory) Thing(org.openhab.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) ThingRegistry(org.openhab.core.thing.ThingRegistry) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 3 with ThingHandlerFactory

use of org.openhab.core.thing.binding.ThingHandlerFactory in project openhab-core by openhab.

the class ChannelCommandDescriptionProviderOSGiTest method beforeEach.

@BeforeEach
public void beforeEach() throws Exception {
    Mockito.when(componentContextMock.getBundleContext()).thenReturn(bundleContext);
    registerVolatileStorageService();
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    final TestThingHandlerFactory thingHandlerFactory = new TestThingHandlerFactory();
    thingHandlerFactory.activate(componentContextMock);
    registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
    final StateDescriptionFragment stateDescriptionFragment = StateDescriptionFragmentBuilder.create().withMinimum(BigDecimal.ZERO).withMaximum(BigDecimal.valueOf(100)).withStep(BigDecimal.TEN).withPattern("%d Peek").withReadOnly(true).withOption(new StateOption("SOUND", "My great sound.")).build();
    final CommandDescription command = CommandDescriptionBuilder.create().withCommandOption(new CommandOption("COMMAND", "My command.")).build();
    final ChannelType channelType1 = ChannelTypeBuilder.state(new ChannelTypeUID("hue:state-as-command"), " ", CoreItemFactory.NUMBER).withStateDescriptionFragment(stateDescriptionFragment).build();
    final ChannelType channelType2 = ChannelTypeBuilder.state(new ChannelTypeUID("hue:static"), " ", CoreItemFactory.STRING).withTag("Light").withCommandDescription(command).build();
    final ChannelType channelType3 = ChannelTypeBuilder.state(CHANNEL_TYPE_UID, " ", CoreItemFactory.STRING).withTag("Light").build();
    List<ChannelType> channelTypes = new ArrayList<>();
    channelTypes.add(channelType1);
    channelTypes.add(channelType2);
    channelTypes.add(channelType3);
    registerService(new ChannelTypeProvider() {

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

        @Override
        @Nullable
        public ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
            for (final ChannelType channelType : channelTypes) {
                if (channelType.getUID().equals(channelTypeUID)) {
                    return channelType;
                }
            }
            return null;
        }
    });
    testBundle = SyntheticBundleInstaller.install(bundleContext, TEST_BUNDLE_NAME);
    assertThat(testBundle, is(notNullValue()));
    thingStatusInfoI18nLocalizationService = getService(ThingStatusInfoI18nLocalizationService.class);
    assertThat(thingStatusInfoI18nLocalizationService, is(notNullValue()));
    thingStatusInfoI18nLocalizationService.setBundleResolver(new BundleResolverImpl());
    List<ChannelDefinition> channelDefinitions = new ArrayList<>();
    channelDefinitions.add(new ChannelDefinitionBuilder("1", channelType1.getUID()).build());
    channelDefinitions.add(new ChannelDefinitionBuilder("7_1", channelType2.getUID()).build());
    channelDefinitions.add(new ChannelDefinitionBuilder("7_2", channelType3.getUID()).build());
    registerService(new SimpleThingTypeProvider(Set.of(ThingTypeBuilder.instance(new ThingTypeUID("hue:lamp"), "label").withChannelDefinitions(channelDefinitions).build())));
    List<Item> items = new ArrayList<>();
    items.add(new NumberItem("TestItem1"));
    items.add(new NumberItem("TestItem7_1"));
    items.add(new NumberItem("TestItem7_2"));
    registerService(new TestItemProvider(items));
    linkRegistry = getService(ItemChannelLinkRegistry.class);
}
Also used : Locale(java.util.Locale) ChannelDefinitionBuilder(org.openhab.core.thing.type.ChannelDefinitionBuilder) CommandOption(org.openhab.core.types.CommandOption) ArrayList(java.util.ArrayList) ItemRegistry(org.openhab.core.items.ItemRegistry) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ItemChannelLinkRegistry(org.openhab.core.thing.link.ItemChannelLinkRegistry) StateDescriptionFragment(org.openhab.core.types.StateDescriptionFragment) ThingStatusInfoI18nLocalizationService(org.openhab.core.thing.i18n.ThingStatusInfoI18nLocalizationService) CommandDescription(org.openhab.core.types.CommandDescription) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) BaseThingHandlerFactory(org.openhab.core.thing.binding.BaseThingHandlerFactory) StateOption(org.openhab.core.types.StateOption) NumberItem(org.openhab.core.library.items.NumberItem) ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) ChannelTypeProvider(org.openhab.core.thing.type.ChannelTypeProvider) Collection(java.util.Collection) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) ChannelType(org.openhab.core.thing.type.ChannelType) Nullable(org.eclipse.jdt.annotation.Nullable) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with ThingHandlerFactory

use of org.openhab.core.thing.binding.ThingHandlerFactory in project openhab-core by openhab.

the class ThingRegistryImpl method createThingOfType.

@Override
@Nullable
public Thing createThingOfType(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID, @Nullable String label, Configuration configuration) {
    logger.debug("Creating thing for type '{}'.", thingTypeUID);
    for (ThingHandlerFactory thingHandlerFactory : thingHandlerFactories) {
        if (thingHandlerFactory.supportsThingType(thingTypeUID)) {
            Thing thing = thingHandlerFactory.createThing(thingTypeUID, configuration, thingUID, bridgeUID);
            if (thing == null) {
                logger.warn("Cannot create thing of type '{}'. Binding '{}' says it supports it, but it could not be created.", thingTypeUID, thingHandlerFactory.getClass().getName());
            } else {
                thing.setLabel(label);
                return thing;
            }
        }
    }
    logger.warn("Cannot create thing. No binding found that supports creating a thing of type '{}'.", thingTypeUID);
    return null;
}
Also used : ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) Thing(org.openhab.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 5 with ThingHandlerFactory

use of org.openhab.core.thing.binding.ThingHandlerFactory in project openhab-core by openhab.

the class ThingManagerImpl method thingUpdated.

@Override
@SuppressWarnings("PMD.CompareObjectsWithEquals")
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);
                    if (isInitializable(thing, getThingType(thing))) {
                        safeCaller.create(thingHandler, ThingHandler.class).build().thingUpdated(thing);
                    } else {
                        final ThingHandlerFactory thingHandlerFactory = findThingHandlerFactory(thing.getThingTypeUID());
                        if (thingHandlerFactory != null) {
                            if (isBridge(thing)) {
                                unregisterAndDisposeChildHandlers((Bridge) thing, thingHandlerFactory);
                            }
                            disposeHandler(thing, thingHandler);
                            setThingStatus(thing, buildStatusInfo(ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_CONFIGURATION_PENDING, "@text/missing-or-invalid-configuration"));
                        }
                    }
                } 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);
                        if (thingHandlerFactory != null) {
                            unregisterHandler(thingHandler.getThing(), thingHandlerFactory);
                        } else {
                            logger.debug("No ThingHandlerFactory available that can handle {}", thing.getThingTypeUID());
                        }
                        registerAndInitializeHandler(thing, thingHandlerFactory);
                    }
                }
            } else {
                registerAndInitializeHandler(thing, getThingHandlerFactory(thing));
            }
        } finally {
            lock1.unlock();
        }
    }
}
Also used : ThingUID(org.openhab.core.thing.ThingUID) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) Thing(org.openhab.core.thing.Thing) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Aggregations

ThingHandlerFactory (org.openhab.core.thing.binding.ThingHandlerFactory)40 Thing (org.openhab.core.thing.Thing)32 ThingHandler (org.openhab.core.thing.binding.ThingHandler)31 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)30 Test (org.junit.jupiter.api.Test)27 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)26 Nullable (org.eclipse.jdt.annotation.Nullable)25 ThingHandlerCallback (org.openhab.core.thing.binding.ThingHandlerCallback)20 InvocationOnMock (org.mockito.invocation.InvocationOnMock)19 ThingStatusInfo (org.openhab.core.thing.ThingStatusInfo)15 ArrayList (java.util.ArrayList)8 ThingUID (org.openhab.core.thing.ThingUID)8 Configuration (org.openhab.core.config.core.Configuration)6 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)5 Item (org.openhab.core.items.Item)5 Bridge (org.openhab.core.thing.Bridge)5 ThingRegistry (org.openhab.core.thing.ThingRegistry)5 BaseThingHandlerFactory (org.openhab.core.thing.binding.BaseThingHandlerFactory)5 BeforeEach (org.junit.jupiter.api.BeforeEach)4 StringItem (org.openhab.core.library.items.StringItem)4