Search in sources :

Example 1 with Type

use of org.eclipse.smarthome.core.types.Type 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 Type

use of org.eclipse.smarthome.core.types.Type in project smarthome by eclipse.

the class NumberExtensionsTest method testOperator_notEqualsTypeNumber.

/**
 * Test method for
 * {@link org.eclipse.smarthome.model.script.lib.NumberExtensions#operator_notEquals(org.eclipse.smarthome.core.types.Type, java.lang.Number)}
 * .
 */
@Test
public void testOperator_notEqualsTypeNumber() {
    DecimalType type = new DecimalType(10);
    Number x = 10;
    boolean result = NumberExtensions.operator_notEquals((Type) type, x);
    Assert.assertFalse(result);
    x = 1;
    result = NumberExtensions.operator_notEquals((Type) type, x);
    Assert.assertTrue(result);
}
Also used : Type(org.eclipse.smarthome.core.types.Type) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Test(org.junit.Test)

Example 3 with Type

use of org.eclipse.smarthome.core.types.Type in project smarthome by eclipse.

the class NumberExtensionsTest method testOperator_lessEqualsThanTypeNumber.

/**
 * Test method for
 * {@link org.eclipse.smarthome.model.script.lib.NumberExtensions#operator_lessEqualsThan(org.eclipse.smarthome.core.types.Type, java.lang.Number)}
 * .
 */
@Test
public void testOperator_lessEqualsThanTypeNumber() {
    DecimalType type = new DecimalType(10);
    Number x = 123;
    boolean result = NumberExtensions.operator_lessEqualsThan((Type) type, x);
    Assert.assertTrue(result);
    x = 2;
    result = NumberExtensions.operator_lessEqualsThan((Type) type, x);
    Assert.assertFalse(result);
    x = 10;
    result = NumberExtensions.operator_lessEqualsThan((Type) type, x);
    Assert.assertTrue(result);
}
Also used : Type(org.eclipse.smarthome.core.types.Type) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Test(org.junit.Test)

Example 4 with Type

use of org.eclipse.smarthome.core.types.Type in project smarthome by eclipse.

the class NumberExtensionsTest method testOperator_equalsTypeNumber.

/**
 * Test method for
 * {@link org.eclipse.smarthome.model.script.lib.NumberExtensions#operator_equals(org.eclipse.smarthome.core.types.Type, java.lang.Number)}
 * .
 */
@Test
public void testOperator_equalsTypeNumber() {
    DecimalType type = new DecimalType(10);
    Number x = 10;
    boolean result = NumberExtensions.operator_equals((Type) type, x);
    Assert.assertTrue(result);
    x = 1;
    result = NumberExtensions.operator_equals((Type) type, x);
    Assert.assertFalse(result);
}
Also used : Type(org.eclipse.smarthome.core.types.Type) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Test(org.junit.Test)

Example 5 with Type

use of org.eclipse.smarthome.core.types.Type in project smarthome by eclipse.

the class NumberExtensionsTest method testOperator_lessThanTypeNumber.

/**
 * Test method for
 * {@link org.eclipse.smarthome.model.script.lib.NumberExtensions#operator_lessThan(org.eclipse.smarthome.core.types.Type, java.lang.Number)}
 * .
 */
@Test
public void testOperator_lessThanTypeNumber() {
    DecimalType type = new DecimalType(10);
    Number x = 123;
    boolean result = NumberExtensions.operator_lessThan((Type) type, x);
    Assert.assertTrue(result);
    x = 2;
    result = NumberExtensions.operator_lessThan((Type) type, x);
    Assert.assertFalse(result);
}
Also used : Type(org.eclipse.smarthome.core.types.Type) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Test(org.junit.Test)

Aggregations

Type (org.eclipse.smarthome.core.types.Type)8 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)7 Test (org.junit.Test)6 Item (org.eclipse.smarthome.core.items.Item)2 ChronoUnit (java.time.temporal.ChronoUnit)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 TimeUnit (java.util.concurrent.TimeUnit)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Unit (javax.measure.Unit)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)1