Search in sources :

Example 1 with ItemCommandEvent

use of org.eclipse.smarthome.core.items.events.ItemCommandEvent in project smarthome by eclipse.

the class RunRuleModuleTest method sceneActivatedByRule.

@Test
public void sceneActivatedByRule() throws ItemNotFoundException, InterruptedException {
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    Assert.assertNotNull(ruleRegistry);
    // Scene rule
    final Rule sceneRule = createSceneRule();
    logger.info("SceneRule created: {}", sceneRule.getUID());
    ruleRegistry.add(sceneRule);
    ruleRegistry.setEnabled(sceneRule.getUID(), true);
    waitForAssert(() -> {
        Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatusInfo(sceneRule.getUID()).getStatus());
    });
    // Outer rule
    final Rule outerRule = createOuterRule();
    logger.info("SceneActivationRule created: {}", outerRule.getUID());
    ruleRegistry.add(outerRule);
    ruleRegistry.setEnabled(outerRule.getUID(), true);
    waitForAssert(() -> {
        Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatusInfo(outerRule.getUID()).getStatus());
    });
    // Test rule
    final EventPublisher eventPublisher = getService(EventPublisher.class);
    Assert.assertNotNull(eventPublisher);
    final ItemRegistry itemRegistry = getService(ItemRegistry.class);
    Assert.assertNotNull(itemRegistry);
    final Queue<Event> events = new LinkedList<>();
    registerService(new EventSubscriber() {

        @Override
        public void receive(final Event event) {
            logger.info("Event: {}", event.getTopic());
            events.add(event);
        }

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Collections.singleton(ItemCommandEvent.TYPE);
        }

        @Override
        public EventFilter getEventFilter() {
            return null;
        }
    });
    // trigger rule by switching triggerItem ON
    eventPublisher.post(ItemEventFactory.createStateEvent("ruleTrigger", OnOffType.ON));
    waitForAssert(() -> {
        assertFalse(events.isEmpty());
        ItemCommandEvent event = (ItemCommandEvent) events.remove();
        assertEquals("smarthome/items/switch3/command", event.getTopic());
        assertEquals(OnOffType.ON, event.getItemCommand());
    });
}
Also used : EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) Event(org.eclipse.smarthome.core.events.Event) Rule(org.eclipse.smarthome.automation.Rule) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) EventFilter(org.eclipse.smarthome.core.events.EventFilter) LinkedList(java.util.LinkedList) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 2 with ItemCommandEvent

use of org.eclipse.smarthome.core.items.events.ItemCommandEvent in project smarthome by eclipse.

the class RuntimeRuleTest method testPredefinedRule.

@Test
@Ignore
public void testPredefinedRule() throws ItemNotFoundException, InterruptedException {
    final EventPublisher eventPublisher = getService(EventPublisher.class);
    // final ItemRegistry itemRegistry = getService(ItemRegistry.class);
    // final SwitchItem myMotionItem = (SwitchItem) itemRegistry.getItem("myMotionItem");
    // eventPublisher.post(ItemEventFactory.createStateEvent("myPresenceItem", OnOffType.ON));
    final Queue<Event> events = new LinkedList<>();
    registerService(new EventSubscriber() {

        @Override
        public void receive(final Event event) {
            logger.info("Event: {}", event.getTopic());
            events.add(event);
        }

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Collections.singleton(ItemCommandEvent.TYPE);
        }

        @Override
        public EventFilter getEventFilter() {
            return null;
        }
    });
    eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem", OnOffType.ON));
    waitForAssert(() -> {
        assertFalse(events.isEmpty());
        ItemCommandEvent event = (ItemCommandEvent) events.remove();
        assertEquals("smarthome/items/myLampItem/command", event.getTopic());
        assertEquals(OnOffType.ON, event.getItemCommand());
    });
}
Also used : EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Event(org.eclipse.smarthome.core.events.Event) EventFilter(org.eclipse.smarthome.core.events.EventFilter) LinkedList(java.util.LinkedList) Ignore(org.junit.Ignore) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 3 with ItemCommandEvent

use of org.eclipse.smarthome.core.items.events.ItemCommandEvent in project smarthome by eclipse.

the class ItemCommandActionHandler method execute.

@Override
public Map<String, Object> execute(Map<String, Object> inputs) {
    String itemName = (String) module.getConfiguration().get(ITEM_NAME);
    String command = (String) module.getConfiguration().get(COMMAND);
    if (itemName != null && command != null && eventPublisher != null && itemRegistry != null) {
        try {
            Item item = itemRegistry.getItem(itemName);
            Command commandObj = TypeParser.parseCommand(item.getAcceptedCommandTypes(), command);
            ItemCommandEvent itemCommandEvent = ItemEventFactory.createCommandEvent(itemName, commandObj);
            logger.debug("Executing ItemCommandAction on Item {} with Command {}", itemCommandEvent.getItemName(), itemCommandEvent.getItemCommand());
            eventPublisher.post(itemCommandEvent);
        } catch (ItemNotFoundException e) {
            logger.error("Item with name {} not found in ItemRegistry.", itemName);
        }
    } else {
        logger.error("Command was not posted because either the configuration was not correct or a service was missing: ItemName: {}, Command: {}, eventPublisher: {}, ItemRegistry: {}", itemName, command, eventPublisher, itemRegistry);
    }
    return null;
}
Also used : Item(org.eclipse.smarthome.core.items.Item) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) Command(org.eclipse.smarthome.core.types.Command) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 4 with ItemCommandEvent

use of org.eclipse.smarthome.core.items.events.ItemCommandEvent in project smarthome by eclipse.

the class RuntimeRuleTest method itemStateUpdatedBySimpleRule.

@Test
public void itemStateUpdatedBySimpleRule() throws ItemNotFoundException, InterruptedException {
    final Configuration triggerConfig = new Configuration(Stream.of(new SimpleEntry<>("eventSource", "myMotionItem2"), new SimpleEntry<>("eventTopic", "smarthome/*"), new SimpleEntry<>("eventTypes", "ItemStateEvent")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
    final Configuration actionConfig = new Configuration(Stream.of(new SimpleEntry<>("itemName", "myLampItem2"), new SimpleEntry<>("command", "ON")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
    final Rule rule = new Rule("myRule21" + new Random().nextInt());
    rule.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger2", "core.GenericEventTrigger", triggerConfig) }));
    rule.setActions(Arrays.asList(new Action[] { new Action("ItemPostCommandAction2", "core.ItemCommandAction", actionConfig, null) }));
    // I would expect the factory to create the UID of the rule and the name to be in the list of parameters.
    rule.setName("RuleByJAVA_API");
    logger.info("Rule created: {}", rule.getUID());
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    ruleRegistry.add(rule);
    ruleRegistry.setEnabled(rule.getUID(), true);
    waitForAssert(() -> {
        Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatusInfo(rule.getUID()).getStatus());
    });
    // Test rule
    final EventPublisher eventPublisher = getService(EventPublisher.class);
    Assert.assertNotNull(eventPublisher);
    eventPublisher.post(ItemEventFactory.createStateEvent("myPresenceItem2", OnOffType.ON));
    final Queue<Event> events = new LinkedList<>();
    registerService(new EventSubscriber() {

        @Override
        public void receive(final Event event) {
            logger.info("Event: {}", event.getTopic());
            events.add(event);
        }

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Collections.singleton(ItemCommandEvent.TYPE);
        }

        @Override
        public EventFilter getEventFilter() {
            return null;
        }
    });
    eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem2", OnOffType.ON));
    waitForAssert(() -> {
        assertFalse(events.isEmpty());
        ItemCommandEvent event = (ItemCommandEvent) events.remove();
        assertEquals("smarthome/items/myLampItem2/command", event.getTopic());
        assertEquals(OnOffType.ON, event.getItemCommand());
    });
}
Also used : EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) Action(org.eclipse.smarthome.automation.Action) Set(java.util.Set) Configuration(org.eclipse.smarthome.config.core.Configuration) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) EventFilter(org.eclipse.smarthome.core.events.EventFilter) LinkedList(java.util.LinkedList) Trigger(org.eclipse.smarthome.automation.Trigger) Random(java.util.Random) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Event(org.eclipse.smarthome.core.events.Event) Rule(org.eclipse.smarthome.automation.Rule) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 5 with ItemCommandEvent

use of org.eclipse.smarthome.core.items.events.ItemCommandEvent in project smarthome by eclipse.

the class ItemCommandTriggerHandler method receive.

@Override
public void receive(Event event) {
    if (ruleEngineCallback != null) {
        logger.trace("Received Event: Source: {} Topic: {} Type: {}  Payload: {}", event.getSource(), event.getTopic(), event.getType(), event.getPayload());
        Map<String, Object> values = new HashMap<>();
        if (event instanceof ItemCommandEvent) {
            Command command = ((ItemCommandEvent) event).getItemCommand();
            if (this.command == null || this.command.equals(command.toFullString())) {
                values.put("command", command);
                values.put("event", event);
                ruleEngineCallback.triggered(this.module, values);
            }
        }
    }
}
Also used : ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) HashMap(java.util.HashMap) Command(org.eclipse.smarthome.core.types.Command)

Aggregations

ItemCommandEvent (org.eclipse.smarthome.core.items.events.ItemCommandEvent)5 LinkedList (java.util.LinkedList)3 Set (java.util.Set)3 Event (org.eclipse.smarthome.core.events.Event)3 EventFilter (org.eclipse.smarthome.core.events.EventFilter)3 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)3 EventSubscriber (org.eclipse.smarthome.core.events.EventSubscriber)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 Test (org.junit.Test)3 Rule (org.eclipse.smarthome.automation.Rule)2 RuleRegistry (org.eclipse.smarthome.automation.RuleRegistry)2 RuleStatusInfoEvent (org.eclipse.smarthome.automation.events.RuleStatusInfoEvent)2 Command (org.eclipse.smarthome.core.types.Command)2 HashMap (java.util.HashMap)1 Random (java.util.Random)1 Action (org.eclipse.smarthome.automation.Action)1 Trigger (org.eclipse.smarthome.automation.Trigger)1 Configuration (org.eclipse.smarthome.config.core.Configuration)1 Item (org.eclipse.smarthome.core.items.Item)1 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)1