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));
}
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;
}
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.");
}
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.");
}
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)));
}
Aggregations