Search in sources :

Example 21 with ItemChannelLink

use of org.openhab.core.thing.link.ItemChannelLink in project openhab-core by openhab.

the class GenericItemChannelLinkProvider method stopConfigurationUpdate.

@Override
public void stopConfigurationUpdate(String context) {
    final Set<String> previousItemNames = this.previousItemNames;
    this.previousItemNames = null;
    if (previousItemNames == null) {
        return;
    }
    for (String itemName : previousItemNames) {
        // we remove all binding configurations that were not processed
        Set<ItemChannelLink> links = itemChannelLinkMap.remove(itemName);
        if (links != null) {
            for (ItemChannelLink removedItemChannelLink : links) {
                notifyListenersAboutRemovedElement(removedItemChannelLink);
            }
        }
    }
    Optional.ofNullable(contextMap.get(context)).ifPresent(ctx -> ctx.removeAll(previousItemNames));
}
Also used : ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink)

Example 22 with ItemChannelLink

use of org.openhab.core.thing.link.ItemChannelLink in project openhab-core by openhab.

the class ItemChannelLinkConfigDescriptionProvider method getConfigDescription.

@Override
@Nullable
public ConfigDescription getConfigDescription(URI uri, @Nullable Locale locale) {
    if (SCHEME.equals(uri.getScheme())) {
        ItemChannelLink link = itemChannelLinkRegistry.get(uri.getSchemeSpecificPart());
        if (link == null) {
            return null;
        }
        Item item = itemRegistry.get(link.getItemName());
        if (item == null) {
            return null;
        }
        Thing thing = thingRegistry.get(link.getLinkedUID().getThingUID());
        if (thing == null) {
            return null;
        }
        Channel channel = thing.getChannel(link.getLinkedUID().getId());
        if (channel == null) {
            return null;
        }
        ConfigDescriptionParameter paramProfile = ConfigDescriptionParameterBuilder.create(PARAM_PROFILE, Type.TEXT).withLabel("Profile").withDescription("the profile to use").withRequired(false).withOptions(getOptions(link, item, channel, locale)).build();
        return ConfigDescriptionBuilder.create(uri).withParameter(paramProfile).build();
    }
    return null;
}
Also used : Item(org.openhab.core.items.Item) Channel(org.openhab.core.thing.Channel) ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink) ConfigDescriptionParameter(org.openhab.core.config.core.ConfigDescriptionParameter) Thing(org.openhab.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 23 with ItemChannelLink

use of org.openhab.core.thing.link.ItemChannelLink in project openhab-core by openhab.

the class LinkConsoleCommandExtension method clear.

private void clear(Console console) {
    Collection<ItemChannelLink> itemChannelLinks = itemChannelLinkRegistry.getAll();
    for (ItemChannelLink itemChannelLink : itemChannelLinks) {
        itemChannelLinkRegistry.remove(itemChannelLink.getUID());
    }
    console.println(itemChannelLinks.size() + " links successfully removed.");
}
Also used : ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink)

Example 24 with ItemChannelLink

use of org.openhab.core.thing.link.ItemChannelLink in project openhab-core by openhab.

the class LinkConsoleCommandExtension method addChannelLink.

private void addChannelLink(Console console, String itemName, ChannelUID channelUID) {
    ItemChannelLink itemChannelLink = new ItemChannelLink(itemName, channelUID);
    itemChannelLinkRegistry.add(itemChannelLink);
    console.println("Link " + itemChannelLink.toString() + " successfully added.");
}
Also used : ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink)

Example 25 with ItemChannelLink

use of org.openhab.core.thing.link.ItemChannelLink in project openhab-core by openhab.

the class ThingManagerOSGiTest method thingManagerHandlesStateUpdatesCorrectly.

@Test
public void thingManagerHandlesStateUpdatesCorrectly() {
    registerThingTypeProvider();
    class ThingHandlerState {

        boolean thingUpdatedWasCalled;

        @Nullable
        ThingHandlerCallback callback;
    }
    final ThingHandlerState state = new ThingHandlerState();
    ThingHandler thingHandler = mock(ThingHandler.class);
    doAnswer(new Answer<Void>() {

        @Override
        @Nullable
        public Void answer(InvocationOnMock invocation) throws Throwable {
            state.callback = (ThingHandlerCallback) invocation.getArgument(0);
            return null;
        }
    }).when(thingHandler).setCallback(any(ThingHandlerCallback.class));
    doAnswer(new Answer<Void>() {

        @Override
        @Nullable
        public Void answer(InvocationOnMock invocation) throws Throwable {
            state.thingUpdatedWasCalled = true;
            return null;
        }
    }).when(thingHandler).thingUpdated(any(Thing.class));
    when(thingHandler.getThing()).thenReturn(thing);
    ThingHandlerFactory thingHandlerFactory = mock(ThingHandlerFactory.class);
    when(thingHandlerFactory.supportsThingType(any(ThingTypeUID.class))).thenReturn(true);
    when(thingHandlerFactory.registerHandler(any(Thing.class))).thenReturn(thingHandler);
    registerService(thingHandlerFactory);
    // Create item
    String itemName = "name";
    Item item = new StringItem(itemName);
    itemRegistry.add(item);
    managedThingProvider.add(thing);
    managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, CHANNEL_UID));
    state.callback.statusUpdated(thing, ThingStatusInfoBuilder.create(ThingStatus.ONLINE).build());
    final List<Event> receivedEvents = new ArrayList<>();
    @NonNullByDefault EventSubscriber itemUpdateEventSubscriber = new EventSubscriber() {

        @Override
        public void receive(Event event) {
            receivedEvents.add(event);
        }

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Set.of(ItemStateEvent.TYPE);
        }

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return new TopicEventFilter("openhab/items/.*/state");
        }
    };
    registerService(itemUpdateEventSubscriber);
    // thing manager posts the update to the event bus via EventPublisher
    state.callback.stateUpdated(CHANNEL_UID, new StringType("Value"));
    waitForAssert(() -> assertThat(receivedEvents.size(), is(1)));
    assertThat(receivedEvents.get(0), is(instanceOf(ItemStateEvent.class)));
    ItemStateEvent itemUpdateEvent = (ItemStateEvent) receivedEvents.get(0);
    assertThat(itemUpdateEvent.getTopic(), is("openhab/items/name/state"));
    assertThat(itemUpdateEvent.getItemName(), is(itemName));
    assertThat(itemUpdateEvent.getSource(), is(CHANNEL_UID.toString()));
    assertThat(itemUpdateEvent.getItemState(), is(instanceOf(StringType.class)));
    assertThat(itemUpdateEvent.getItemState(), is("Value"));
    receivedEvents.clear();
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).withChannel(ChannelBuilder.create(CHANNEL_UID, CoreItemFactory.SWITCH).build()).build();
    managedThingProvider.update(thing);
    state.callback.stateUpdated(CHANNEL_UID, new StringType("Value"));
    waitForAssert(() -> assertThat(receivedEvents.size(), is(1)));
    assertThat(receivedEvents.get(0), is(instanceOf(ItemStateEvent.class)));
    itemUpdateEvent = (ItemStateEvent) receivedEvents.get(0);
    assertThat(itemUpdateEvent.getTopic(), is("openhab/items/name/state"));
    assertThat(itemUpdateEvent.getItemName(), is(itemName));
    assertThat(itemUpdateEvent.getSource(), is(CHANNEL_UID.toString()));
    assertThat(itemUpdateEvent.getItemState(), is(instanceOf(StringType.class)));
    assertThat(itemUpdateEvent.getItemState(), is("Value"));
    waitForAssert(() -> assertThat(state.thingUpdatedWasCalled, is(true)));
}
Also used : EventSubscriber(org.openhab.core.events.EventSubscriber) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) StringType(org.openhab.core.library.types.StringType) ItemChannelLink(org.openhab.core.thing.link.ItemChannelLink) ArrayList(java.util.ArrayList) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) ThingHandler(org.openhab.core.thing.binding.ThingHandler) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) StringItem(org.openhab.core.library.items.StringItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ItemStateEvent(org.openhab.core.items.events.ItemStateEvent) ThingStatusInfoEvent(org.openhab.core.thing.events.ThingStatusInfoEvent) Event(org.openhab.core.events.Event) ThingStatusInfoChangedEvent(org.openhab.core.thing.events.ThingStatusInfoChangedEvent) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) TopicEventFilter(org.openhab.core.events.TopicEventFilter) ItemStateEvent(org.openhab.core.items.events.ItemStateEvent) Nullable(org.eclipse.jdt.annotation.Nullable) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

ItemChannelLink (org.openhab.core.thing.link.ItemChannelLink)49 Test (org.junit.jupiter.api.Test)30 Thing (org.openhab.core.thing.Thing)14 Item (org.openhab.core.items.Item)13 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)13 ChannelUID (org.openhab.core.thing.ChannelUID)11 StringItem (org.openhab.core.library.items.StringItem)8 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)8 Configuration (org.openhab.core.config.core.Configuration)6 NumberItem (org.openhab.core.library.items.NumberItem)6 ThingUID (org.openhab.core.thing.ThingUID)6 Nullable (org.eclipse.jdt.annotation.Nullable)5 Channel (org.openhab.core.thing.Channel)5 ThingRegistry (org.openhab.core.thing.ThingRegistry)5 ThingHandler (org.openhab.core.thing.binding.ThingHandler)5 ThingHandlerCallback (org.openhab.core.thing.binding.ThingHandlerCallback)5 ManagedItemChannelLinkProvider (org.openhab.core.thing.link.ManagedItemChannelLinkProvider)5 ArrayList (java.util.ArrayList)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 ManagedThingProvider (org.openhab.core.thing.ManagedThingProvider)4