Search in sources :

Example 21 with Event

use of org.eclipse.smarthome.core.events.Event in project smarthome by eclipse.

the class SampleExtensionService method postInstalledEvent.

private void postInstalledEvent(String extensionId) {
    if (eventPublisher != null) {
        Event event = ExtensionEventFactory.createExtensionInstalledEvent(extensionId);
        eventPublisher.post(event);
    }
}
Also used : Event(org.eclipse.smarthome.core.events.Event)

Example 22 with Event

use of org.eclipse.smarthome.core.events.Event in project smarthome by eclipse.

the class RuntimeRuleTest method ruleTriggeredByCompositeTrigger.

@Test
public void ruleTriggeredByCompositeTrigger() throws ItemNotFoundException, InterruptedException {
    // //Test the creation of a rule out of
    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<>("itemName", "myLampItem3"), new SimpleEntry<>("command", "ON")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
    final Rule rule = new Rule("myRule21" + new Random().nextInt() + "_COMPOSITE");
    rule.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger3", "core.ItemStateChangeTrigger", triggerConfig) }));
    rule.setActions(Arrays.asList(new Action[] { new Action("ItemPostCommandAction3", "core.ItemCommandAction", actionConfig, null) }));
    rule.setName("RuleByJAVA_API_WithCompositeTrigger");
    logger.info("Rule created: {}", rule.getUID());
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    ruleRegistry.add(rule);
    // Test rule
    waitForAssert(() -> {
        Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatusInfo(rule.getUID()).getStatus());
    });
    final Queue<Event> events = new LinkedList<>();
    registerService(new EventSubscriber() {

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

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

        @Override
        public EventFilter getEventFilter() {
            return null;
        }
    });
    final EventPublisher eventPublisher = getService(EventPublisher.class);
    eventPublisher.post(ItemEventFactory.createStateEvent("myPresenceItem3", OnOffType.ON));
    eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem3", OnOffType.ON));
    waitForAssert(() -> {
        assertFalse(events.isEmpty());
        RuleStatusInfoEvent event = (RuleStatusInfoEvent) events.remove();
        assertEquals(RuleStatus.RUNNING, event.getStatusInfo().getStatus());
    });
    waitForAssert(() -> {
        assertFalse(events.isEmpty());
        RuleStatusInfoEvent event = (RuleStatusInfoEvent) events.remove();
        assertEquals(RuleStatus.IDLE, event.getStatusInfo().getStatus());
    });
}
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) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) EventFilter(org.eclipse.smarthome.core.events.EventFilter) LinkedList(java.util.LinkedList) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) 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 23 with Event

use of org.eclipse.smarthome.core.events.Event 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 24 with Event

use of org.eclipse.smarthome.core.events.Event in project smarthome by eclipse.

the class RuntimeRuleTest method compareConditionWorks.

@Test
public void compareConditionWorks() {
    final Configuration conditionConfig = newRightOperatorConfig("ON", "=");
    final Map<String, String> inputs = Stream.of(new SimpleEntry<>("input", "someTrigger.someoutput")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
    final Condition condition = new Condition("id", "core.GenericCompareCondition", conditionConfig, inputs);
    final CompareConditionHandler handler = new CompareConditionHandler(condition);
    assertSatisfiedHandlerInput(handler, true, OnOffType.ON);
    assertSatisfiedHandlerInput(handler, true, "ON");
    assertSatisfiedHandlerInput(handler, false, OnOffType.OFF);
    assertSatisfiedHandlerInput(handler, false, "OFF");
    condition.setConfiguration(newRightOperatorConfig("21", "="));
    assertSatisfiedHandlerInput(handler, true, 21);
    assertSatisfiedHandlerInput(handler, false, 22);
    condition.setConfiguration(newRightOperatorConfig("21", "<"));
    assertSatisfiedHandlerInput(handler, true, 20);
    assertSatisfiedHandlerInput(handler, false, 22);
    assertSatisfiedHandlerInput(handler, true, 20l);
    assertSatisfiedHandlerInput(handler, false, 22l);
    assertSatisfiedHandlerInput(handler, true, 20.9d);
    assertSatisfiedHandlerInput(handler, false, 21.1d);
    condition.setConfiguration(newRightOperatorConfig("21", "<="));
    assertSatisfiedHandlerInput(handler, true, 20);
    assertSatisfiedHandlerInput(handler, true, 21);
    assertSatisfiedHandlerInput(handler, false, 22);
    assertSatisfiedHandlerInput(handler, true, 20l);
    assertSatisfiedHandlerInput(handler, true, 21l);
    assertSatisfiedHandlerInput(handler, false, 22l);
    assertSatisfiedHandlerInput(handler, true, 20.9d);
    assertSatisfiedHandlerInput(handler, true, 21.0d);
    assertSatisfiedHandlerInput(handler, false, 21.1d);
    condition.setConfiguration(newRightOperatorConfig("21", "<"));
    assertSatisfiedHandlerInput(handler, true, 20);
    assertSatisfiedHandlerInput(handler, false, 22);
    assertSatisfiedHandlerInput(handler, true, 20l);
    assertSatisfiedHandlerInput(handler, false, 22l);
    assertSatisfiedHandlerInput(handler, true, 20.9d);
    assertSatisfiedHandlerInput(handler, false, 21.1d);
    condition.setConfiguration(newRightOperatorConfig("21", "<="));
    assertSatisfiedHandlerInput(handler, true, 20);
    assertSatisfiedHandlerInput(handler, true, 21);
    assertSatisfiedHandlerInput(handler, false, 22);
    assertSatisfiedHandlerInput(handler, true, 20l);
    assertSatisfiedHandlerInput(handler, true, 21l);
    assertSatisfiedHandlerInput(handler, false, 22l);
    assertSatisfiedHandlerInput(handler, true, 20.9d);
    assertSatisfiedHandlerInput(handler, true, 21.0d);
    assertSatisfiedHandlerInput(handler, false, 21.1d);
    condition.setConfiguration(newRightOperatorConfig(".*anything.*", "matches"));
    assertSatisfiedHandlerInput(handler, false, "something matches?");
    assertSatisfiedHandlerInput(handler, true, "anything matches?");
    Assert.assertFalse(handler.isSatisfied(Stream.of(new SimpleEntry<>("nothing", "nothing")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()))));
    condition.setConfiguration(newRightOperatorConfig("ONOFF", "matches"));
    assertSatisfiedHandlerInput(handler, false, OnOffType.ON);
    final Event event = ItemEventFactory.createStateEvent("itemName", OnOffType.OFF, "source");
    condition.setConfiguration(newRightOperatorInputPropertyConfig(".*ON.*", "matches", "itemName"));
    assertSatisfiedHandlerInput(handler, false, event);
    condition.setConfiguration(newRightOperatorInputPropertyConfig("itemName", "matches", "itemName"));
    assertSatisfiedHandlerInput(handler, true, event);
    condition.setConfiguration(newRightOperatorConfig("null", "="));
    assertSatisfiedHandlerInput(handler, true, null);
    condition.setConfiguration(newRightOperatorConfig("notnull", "="));
    assertSatisfiedHandlerInput(handler, false, null);
    condition.setConfiguration(newRightOperatorConfig("ON", "<"));
    assertSatisfiedHandlerInput(handler, false, OnOffType.ON);
    condition.setConfiguration(newRightOperatorInputPropertyConfig("ON", "<", "nothing"));
    assertSatisfiedHandlerInput(handler, false, event);
    condition.setConfiguration(newRightOperatorInputPropertyConfig("ON", "=", "nothing"));
    assertSatisfiedHandlerInput(handler, true, "ON");
}
Also used : Trigger(org.eclipse.smarthome.automation.Trigger) ModuleTypeRegistry(org.eclipse.smarthome.automation.type.ModuleTypeRegistry) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Arrays(java.util.Arrays) Condition(org.eclipse.smarthome.automation.Condition) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) TypeParser(org.eclipse.smarthome.core.types.TypeParser) LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) Random(java.util.Random) ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) Map(java.util.Map) LinkedList(java.util.LinkedList) SimpleEntry(java.util.AbstractMap.SimpleEntry) Configuration(org.eclipse.smarthome.config.core.Configuration) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) CompareConditionHandler(org.eclipse.smarthome.automation.module.core.handler.CompareConditionHandler) Before(org.junit.Before) RuleStatus(org.eclipse.smarthome.automation.RuleStatus) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) VolatileStorageService(org.eclipse.smarthome.test.storage.VolatileStorageService) Logger(org.slf4j.Logger) ItemEventFactory(org.eclipse.smarthome.core.items.events.ItemEventFactory) Collection(java.util.Collection) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) Test(org.junit.Test) Rule(org.eclipse.smarthome.automation.Rule) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Stream(java.util.stream.Stream) Ignore(org.junit.Ignore) Action(org.eclipse.smarthome.automation.Action) Queue(java.util.Queue) Assert(org.junit.Assert) Event(org.eclipse.smarthome.core.events.Event) Collections(java.util.Collections) Condition(org.eclipse.smarthome.automation.Condition) Configuration(org.eclipse.smarthome.config.core.Configuration) SimpleEntry(java.util.AbstractMap.SimpleEntry) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Event(org.eclipse.smarthome.core.events.Event) CompareConditionHandler(org.eclipse.smarthome.automation.module.core.handler.CompareConditionHandler) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 25 with Event

use of org.eclipse.smarthome.core.events.Event in project smarthome by eclipse.

the class EventHandler method handleEvent.

private void handleEvent(final String type, final String payload, final String topic, @Nullable final String source) {
    final EventFactory eventFactory = typedEventFactories.get(type);
    if (eventFactory == null) {
        logger.debug("Could not find an Event Factory for the event type '{}'.", type);
        return;
    }
    final Set<EventSubscriber> eventSubscribers = getEventSubscribers(type);
    if (eventSubscribers.isEmpty()) {
        return;
    }
    final Event eshEvent = createESHEvent(eventFactory, type, payload, topic, source);
    if (eshEvent == null) {
        return;
    }
    dispatchESHEvent(eventSubscribers, eshEvent);
}
Also used : EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) EventFactory(org.eclipse.smarthome.core.events.EventFactory) Event(org.eclipse.smarthome.core.events.Event)

Aggregations

Event (org.eclipse.smarthome.core.events.Event)30 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)19 Test (org.junit.Test)19 EventSubscriber (org.eclipse.smarthome.core.events.EventSubscriber)15 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)14 Set (java.util.Set)13 EventFilter (org.eclipse.smarthome.core.events.EventFilter)13 LinkedList (java.util.LinkedList)11 Collections (java.util.Collections)9 HashSet (java.util.HashSet)9 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 Ignore (org.junit.Ignore)8 GroupFunctionHelper (org.eclipse.smarthome.core.internal.items.GroupFunctionHelper)7 ItemStateConverterImpl (org.eclipse.smarthome.core.internal.items.ItemStateConverterImpl)7 GroupItemStateChangedEvent (org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent)7 ItemUpdatedEvent (org.eclipse.smarthome.core.items.events.ItemUpdatedEvent)7 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)7 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)7 StringType (org.eclipse.smarthome.core.library.types.StringType)7