Search in sources :

Example 1 with Profile

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());
        }
    });
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) Arrays(java.util.Arrays) ItemFactory(org.eclipse.smarthome.core.items.ItemFactory) ProfileFactory(org.eclipse.smarthome.core.thing.profiles.ProfileFactory) LoggerFactory(org.slf4j.LoggerFactory) Command(org.eclipse.smarthome.core.types.Command) ItemChannelLinkConfigDescriptionProvider(org.eclipse.smarthome.core.thing.internal.link.ItemChannelLinkConfigDescriptionProvider) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) UID(org.eclipse.smarthome.core.thing.UID) State(org.eclipse.smarthome.core.types.State) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemStateConverter(org.eclipse.smarthome.core.items.ItemStateConverter) ReferencePolicy(org.osgi.service.component.annotations.ReferencePolicy) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) ThingEventFactory(org.eclipse.smarthome.core.thing.events.ThingEventFactory) List(java.util.List) StateProfile(org.eclipse.smarthome.core.thing.profiles.StateProfile) Entry(java.util.Map.Entry) NonNull(org.eclipse.jdt.annotation.NonNull) SystemProfileFactory(org.eclipse.smarthome.core.thing.internal.profiles.SystemProfileFactory) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) Function(java.util.function.Function) RegistryChangeListener(org.eclipse.smarthome.core.common.registry.RegistryChangeListener) HashSet(java.util.HashSet) ProfileCallbackImpl(org.eclipse.smarthome.core.thing.internal.profiles.ProfileCallbackImpl) Component(org.osgi.service.component.annotations.Component) SafeCaller(org.eclipse.smarthome.core.common.SafeCaller) Type(org.eclipse.smarthome.core.types.Type) ProfileCallback(org.eclipse.smarthome.core.thing.profiles.ProfileCallback) TriggerProfile(org.eclipse.smarthome.core.thing.profiles.TriggerProfile) Logger(org.slf4j.Logger) Profile(org.eclipse.smarthome.core.thing.profiles.Profile) Item(org.eclipse.smarthome.core.items.Item) ItemStateEvent(org.eclipse.smarthome.core.items.events.ItemStateEvent) ItemChannelLink(org.eclipse.smarthome.core.thing.link.ItemChannelLink) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) ProfileAdvisor(org.eclipse.smarthome.core.thing.profiles.ProfileAdvisor) ChannelTriggeredEvent(org.eclipse.smarthome.core.thing.events.ChannelTriggeredEvent) ItemChannelLinkRegistry(org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry) Event(org.eclipse.smarthome.core.events.Event) ProfileTypeUID(org.eclipse.smarthome.core.thing.profiles.ProfileTypeUID) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) Item(org.eclipse.smarthome.core.items.Item) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Channel(org.eclipse.smarthome.core.thing.Channel) Thing(org.eclipse.smarthome.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) StateProfile(org.eclipse.smarthome.core.thing.profiles.StateProfile) TriggerProfile(org.eclipse.smarthome.core.thing.profiles.TriggerProfile) Profile(org.eclipse.smarthome.core.thing.profiles.Profile)

Example 2 with Profile

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;
}
Also used : Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) HashSet(java.util.HashSet) ProfileFactory(org.eclipse.smarthome.core.thing.profiles.ProfileFactory) SystemProfileFactory(org.eclipse.smarthome.core.thing.internal.profiles.SystemProfileFactory) StateProfile(org.eclipse.smarthome.core.thing.profiles.StateProfile) TriggerProfile(org.eclipse.smarthome.core.thing.profiles.TriggerProfile) Profile(org.eclipse.smarthome.core.thing.profiles.Profile) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 3 with Profile

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();
    }
}
Also used : StateProfile(org.eclipse.smarthome.core.thing.profiles.StateProfile) TriggerProfile(org.eclipse.smarthome.core.thing.profiles.TriggerProfile) Profile(org.eclipse.smarthome.core.thing.profiles.Profile) ProfileTypeUID(org.eclipse.smarthome.core.thing.profiles.ProfileTypeUID)

Example 4 with Profile

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);
    });
}
Also used : State(org.eclipse.smarthome.core.types.State) StateProfile(org.eclipse.smarthome.core.thing.profiles.StateProfile) TriggerProfile(org.eclipse.smarthome.core.thing.profiles.TriggerProfile) Profile(org.eclipse.smarthome.core.thing.profiles.Profile)

Example 5 with Profile

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);
        }
    });
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) Arrays(java.util.Arrays) ItemFactory(org.eclipse.smarthome.core.items.ItemFactory) ProfileFactory(org.eclipse.smarthome.core.thing.profiles.ProfileFactory) LoggerFactory(org.slf4j.LoggerFactory) Command(org.eclipse.smarthome.core.types.Command) ItemChannelLinkConfigDescriptionProvider(org.eclipse.smarthome.core.thing.internal.link.ItemChannelLinkConfigDescriptionProvider) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) UID(org.eclipse.smarthome.core.thing.UID) State(org.eclipse.smarthome.core.types.State) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemStateConverter(org.eclipse.smarthome.core.items.ItemStateConverter) ReferencePolicy(org.osgi.service.component.annotations.ReferencePolicy) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) ThingEventFactory(org.eclipse.smarthome.core.thing.events.ThingEventFactory) List(java.util.List) StateProfile(org.eclipse.smarthome.core.thing.profiles.StateProfile) Entry(java.util.Map.Entry) NonNull(org.eclipse.jdt.annotation.NonNull) SystemProfileFactory(org.eclipse.smarthome.core.thing.internal.profiles.SystemProfileFactory) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) Function(java.util.function.Function) RegistryChangeListener(org.eclipse.smarthome.core.common.registry.RegistryChangeListener) HashSet(java.util.HashSet) ProfileCallbackImpl(org.eclipse.smarthome.core.thing.internal.profiles.ProfileCallbackImpl) Component(org.osgi.service.component.annotations.Component) SafeCaller(org.eclipse.smarthome.core.common.SafeCaller) Type(org.eclipse.smarthome.core.types.Type) ProfileCallback(org.eclipse.smarthome.core.thing.profiles.ProfileCallback) TriggerProfile(org.eclipse.smarthome.core.thing.profiles.TriggerProfile) Logger(org.slf4j.Logger) Profile(org.eclipse.smarthome.core.thing.profiles.Profile) Item(org.eclipse.smarthome.core.items.Item) ItemStateEvent(org.eclipse.smarthome.core.items.events.ItemStateEvent) ItemChannelLink(org.eclipse.smarthome.core.thing.link.ItemChannelLink) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) ProfileAdvisor(org.eclipse.smarthome.core.thing.profiles.ProfileAdvisor) ChannelTriggeredEvent(org.eclipse.smarthome.core.thing.events.ChannelTriggeredEvent) ItemChannelLinkRegistry(org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry) Event(org.eclipse.smarthome.core.events.Event) ProfileTypeUID(org.eclipse.smarthome.core.thing.profiles.ProfileTypeUID) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) Item(org.eclipse.smarthome.core.items.Item) StateProfile(org.eclipse.smarthome.core.thing.profiles.StateProfile) TriggerProfile(org.eclipse.smarthome.core.thing.profiles.TriggerProfile) Profile(org.eclipse.smarthome.core.thing.profiles.Profile)

Aggregations

Profile (org.eclipse.smarthome.core.thing.profiles.Profile)5 StateProfile (org.eclipse.smarthome.core.thing.profiles.StateProfile)5 TriggerProfile (org.eclipse.smarthome.core.thing.profiles.TriggerProfile)5 HashSet (java.util.HashSet)3 Set (java.util.Set)3 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)3 Nullable (org.eclipse.jdt.annotation.Nullable)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 TimeUnit (java.util.concurrent.TimeUnit)2 Consumer (java.util.function.Consumer)2 Function (java.util.function.Function)2 NonNull (org.eclipse.jdt.annotation.NonNull)2 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)2 SafeCaller (org.eclipse.smarthome.core.common.SafeCaller)2 RegistryChangeListener (org.eclipse.smarthome.core.common.registry.RegistryChangeListener)2