use of org.eclipse.smarthome.core.thing.profiles.Profile in project smarthome by eclipse.
the class CommunicationManager method handleEvent.
private <T extends Type> void handleEvent(String itemName, T type, String source, Function<@Nullable String, @Nullable List<Class<? extends T>>> acceptedTypesFunction, ProfileAction<T> action) {
final Item item = getItem(itemName);
if (item == null) {
logger.debug("Received an event for item {} which does not exist", itemName);
return;
}
itemChannelLinkRegistry.stream().filter(link -> {
// all links for the item
return link.getItemName().equals(itemName);
}).filter(link -> {
// make sure the command event is not sent back to its source
return !link.getLinkedUID().toString().equals(source);
}).forEach(link -> {
ChannelUID channelUID = link.getLinkedUID();
Thing thing = getThing(channelUID.getThingUID());
if (thing != null) {
Channel channel = thing.getChannel(channelUID.getId());
if (channel != null) {
@Nullable T convertedType = toAcceptedType(type, channel, acceptedTypesFunction);
if (convertedType != null) {
if (thing.getHandler() != null) {
Profile profile = getProfile(link, item, thing);
action.handle(profile, thing, convertedType);
}
} else {
logger.debug("Received event '{}' ({}) could not be converted to any type accepted by item '{}' ({})", type, type.getClass().getSimpleName(), itemName, item.getType());
}
} else {
logger.debug("Received event '{}' for non-existing channel '{}', not forwarding it to the handler", type, channelUID);
}
} else {
logger.debug("Received event '{}' for non-existing thing '{}', not forwarding it to the handler", type, channelUID.getThingUID());
}
});
}
use of org.eclipse.smarthome.core.thing.profiles.Profile in project smarthome by eclipse.
the class CommunicationManager method getProfileFromFactories.
@Nullable
private Profile getProfileFromFactories(ProfileTypeUID profileTypeUID, ItemChannelLink link, ProfileCallback callback) {
ProfileContextImpl context = new ProfileContextImpl(link.getConfiguration());
if (supportsProfileTypeUID(defaultProfileFactory, profileTypeUID)) {
logger.trace("using the default ProfileFactory to create profile '{}'", profileTypeUID);
return defaultProfileFactory.createProfile(profileTypeUID, callback, context);
}
for (Entry<ProfileFactory, Set<String>> entry : profileFactories.entrySet()) {
ProfileFactory factory = entry.getKey();
if (supportsProfileTypeUID(factory, profileTypeUID)) {
logger.trace("using ProfileFactory '{}' to create profile '{}'", factory, profileTypeUID);
Profile profile = factory.createProfile(profileTypeUID, callback, context);
if (profile == null) {
logger.error("ProfileFactory '{}' returned 'null' although it claimed it supports item type '{}'", factory, profileTypeUID);
} else {
entry.getValue().add(link.getUID());
return profile;
}
}
}
logger.debug("no ProfileFactory found which supports '{}'", profileTypeUID);
return null;
}
use of org.eclipse.smarthome.core.thing.profiles.Profile in project smarthome by eclipse.
the class CommunicationManager method getProfile.
private Profile getProfile(ItemChannelLink link, Item item, @Nullable Thing thing) {
synchronized (profiles) {
Profile profile = profiles.get(link.getUID());
if (profile != null) {
return profile;
}
ProfileTypeUID profileTypeUID = determineProfileTypeUID(link, item, thing);
if (profileTypeUID != null) {
profile = getProfileFromFactories(profileTypeUID, link, createCallback(link));
if (profile != null) {
profiles.put(link.getUID(), profile);
return profile;
}
}
return new NoOpProfile();
}
}
use of org.eclipse.smarthome.core.thing.profiles.Profile in project smarthome by eclipse.
the class CommunicationManager method receiveUpdate.
private void receiveUpdate(ItemStateEvent updateEvent) {
final String itemName = updateEvent.getItemName();
final State newState = updateEvent.getItemState();
handleEvent(itemName, newState, updateEvent.getSource(), s -> acceptedStateTypeMap.get(s), (profile, thing, convertedState) -> {
//
safeCaller.create(profile, Profile.class).withAsync().withIdentifier(//
thing).withTimeout(//
THINGHANDLER_EVENT_TIMEOUT).build().onStateUpdateFromItem(convertedState);
});
}
use of org.eclipse.smarthome.core.thing.profiles.Profile in project smarthome by eclipse.
the class CommunicationManager method handleCallFromHandler.
void handleCallFromHandler(ChannelUID channelUID, @Nullable Thing thing, Consumer<Profile> action) {
itemChannelLinkRegistry.stream().filter(link -> {
// all links for the channel
return link.getLinkedUID().equals(channelUID);
}).forEach(link -> {
Item item = getItem(link.getItemName());
if (item != null) {
Profile profile = getProfile(link, item, thing);
action.accept(profile);
}
});
}
Aggregations