Search in sources :

Example 16 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class RunRuleModuleTest method before.

@Before
public void before() {
    registerService(new ItemProvider() {

        @Override
        public void addProviderChangeListener(final ProviderChangeListener<Item> listener) {
        }

        @Override
        public void removeProviderChangeListener(final ProviderChangeListener<Item> listener) {
        }

        @Override
        public Collection<Item> getAll() {
            return Arrays.asList(new Item[] { new SwitchItem("switch1"), new SwitchItem("switch2"), new SwitchItem("switch3"), new SwitchItem("ruleTrigger") });
        }
    });
    registerService(volatileStorageService);
}
Also used : ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) Collection(java.util.Collection) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Before(org.junit.Before)

Example 17 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class RuntimeRuleTest method before.

@Before
public void before() {
    registerService(new ItemProvider() {

        @Override
        public void addProviderChangeListener(final ProviderChangeListener<Item> listener) {
        }

        @Override
        public void removeProviderChangeListener(final ProviderChangeListener<Item> listener) {
        }

        @Override
        public Collection<Item> getAll() {
            return Arrays.asList(new Item[] { new SwitchItem("myMotionItem"), new SwitchItem("myPresenceItem"), new SwitchItem("myLampItem"), new SwitchItem("myMotionItem2"), new SwitchItem("myPresenceItem2"), new SwitchItem("myLampItem2"), new SwitchItem("myMotionItem3"), new SwitchItem("myPresenceItem3"), new SwitchItem("myLampItem3"), new SwitchItem("myMotionItem4"), new SwitchItem("myPresenceItem4"), new SwitchItem("myLampItem4") });
        }
    });
    registerService(volatileStorageService);
}
Also used : ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) Collection(java.util.Collection) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Before(org.junit.Before)

Example 18 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class RuntimeRuleTest method ruleEnableHandlerWorks.

@Test
@Ignore
public void ruleEnableHandlerWorks() throws ItemNotFoundException {
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    final String firstRuleUID = "FirstTestRule";
    final String secondRuleUID = "SecondTestRule";
    final String thirdRuleUID = "ThirdTestRule";
    final String[] firstConfig = new String[] { "FirstTestRule", "SecondTestRule" };
    final String[] secondConfig = new String[] { "FirstTestRule" };
    final String firstRuleAction = "firstRuleAction";
    final String secondRuleAction = "secondRuleAction";
    try {
        final Configuration triggerConfig = new Configuration(Stream.of(new SimpleEntry<>("itemName", "myMotionItem3")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
        final Configuration actionConfig = new Configuration(Stream.of(new SimpleEntry<>("enable", false), new SimpleEntry<>("ruleUIDs", firstConfig)).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
        final Rule rule = new Rule(firstRuleAction);
        rule.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger3", "core.ItemStateChangeTrigger", triggerConfig) }));
        rule.setActions(Arrays.asList(new Action[] { new Action("RuleAction", "core.RuleEnablementAction", actionConfig, null) }));
        ruleRegistry.add(new Rule(firstRuleUID));
        ruleRegistry.add(new Rule(secondRuleUID));
        ruleRegistry.add(new Rule(thirdRuleUID));
        ruleRegistry.add(rule);
        final ItemRegistry itemRegistry = getService(ItemRegistry.class);
        final EventPublisher eventPublisher = getService(EventPublisher.class);
        final Item myMotionItem = itemRegistry.getItem("myMotionItem3");
        eventPublisher.post(ItemEventFactory.createCommandEvent("myMotionItem3", TypeParser.parseCommand(myMotionItem.getAcceptedCommandTypes(), "ON")));
        waitForAssert(() -> {
            Assert.assertEquals(RuleStatus.DISABLED, ruleRegistry.getStatus(firstRuleUID));
            Assert.assertEquals(RuleStatus.DISABLED, ruleRegistry.getStatus(secondRuleUID));
            Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatus(thirdRuleUID));
        });
        final Configuration triggerConfig2 = new Configuration(Stream.of(new SimpleEntry<>("itemName", "myMotionItem3")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
        final Configuration actionConfig2 = new Configuration(Stream.of(new SimpleEntry<>("enable", true), new SimpleEntry<>("ruleUIDs", secondConfig)).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
        final Rule rule2 = new Rule(secondRuleAction);
        rule2.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger3", "core.ItemStateChangeTrigger", triggerConfig2) }));
        rule2.setActions(Arrays.asList(new Action[] { new Action("RuleAction", "core.RuleEnablementAction", actionConfig2, null) }));
        ruleRegistry.add(rule2);
        eventPublisher.post(ItemEventFactory.createCommandEvent("myMotionItem3", TypeParser.parseCommand(myMotionItem.getAcceptedCommandTypes(), "OFF")));
        waitForAssert(() -> {
            Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatus(firstRuleUID));
            Assert.assertEquals(RuleStatus.DISABLED, ruleRegistry.getStatus(secondRuleUID));
            Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatus(thirdRuleUID));
        });
    } finally {
        ruleRegistry.remove(firstRuleUID);
        ruleRegistry.remove(secondRuleUID);
        ruleRegistry.remove(thirdRuleUID);
        ruleRegistry.remove(firstRuleAction);
        ruleRegistry.remove(secondRuleAction);
    }
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) Action(org.eclipse.smarthome.automation.Action) Trigger(org.eclipse.smarthome.automation.Trigger) Configuration(org.eclipse.smarthome.config.core.Configuration) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) Rule(org.eclipse.smarthome.automation.Rule) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) Ignore(org.junit.Ignore) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 19 with Item

use of org.eclipse.smarthome.core.items.Item 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 20 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class ItemChannelLinkRegistry method getLinkedItems.

public Set<Item> getLinkedItems(UID uid) {
    final Set<Item> linkedItems = new LinkedHashSet<>();
    for (final AbstractLink link : getAll()) {
        final String itemName = link.getItemName();
        Item item = itemRegistry.get(itemName);
        if (link.getLinkedUID().equals(uid) && item != null) {
            linkedItems.add(item);
        }
    }
    return linkedItems;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Item(org.eclipse.smarthome.core.items.Item)

Aggregations

Item (org.eclipse.smarthome.core.items.Item)111 GroupItem (org.eclipse.smarthome.core.items.GroupItem)42 GenericItem (org.eclipse.smarthome.core.items.GenericItem)39 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)37 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)35 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)31 State (org.eclipse.smarthome.core.types.State)26 Test (org.junit.Test)26 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)14 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)14 StringItem (org.eclipse.smarthome.core.library.items.StringItem)14 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)13 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)12 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)10 IntentInterpretation (org.openhab.ui.habot.nlp.IntentInterpretation)10 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)9 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)9 Thing (org.eclipse.smarthome.core.thing.Thing)9 Set (java.util.Set)8 Widget (org.eclipse.smarthome.model.sitemap.Widget)8