Search in sources :

Example 6 with EventPublisher

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

the class PersistentInboxTest method testEmittedUpdatedResultIsReadFromStorage.

@Test
public void testEmittedUpdatedResultIsReadFromStorage() {
    DiscoveryResult result = DiscoveryResultBuilder.create(THING_UID).withProperty("foo", 3).build();
    EventPublisher eventPublisher = mock(EventPublisher.class);
    inbox.setEventPublisher(eventPublisher);
    // 
    when(storage.get(THING_UID.toString())).thenReturn(// 
    result).thenReturn(DiscoveryResultBuilder.create(THING_UID).withProperty("foo", "bar").build());
    inbox.add(result);
    // 1st call checks existence of the result in the storage (returns the original result)
    // 2nd call retrieves the stored instance before the event gets emitted
    // (modified due to storage mock configuration)
    verify(storage, times(2)).get(THING_UID.toString());
    ArgumentCaptor<InboxUpdatedEvent> eventCaptor = ArgumentCaptor.forClass(InboxUpdatedEvent.class);
    verify(eventPublisher).post(eventCaptor.capture());
    assertThat(eventCaptor.getValue().getDiscoveryResult().properties, hasEntry("foo", "bar"));
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) InboxUpdatedEvent(org.eclipse.smarthome.config.discovery.inbox.events.InboxUpdatedEvent) Test(org.junit.Test)

Example 7 with EventPublisher

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

the class PersistentInboxTest method testEmittedAddedResultIsReadFromStorage.

@Test
public void testEmittedAddedResultIsReadFromStorage() {
    DiscoveryResult result = DiscoveryResultBuilder.create(THING_UID).withProperty("foo", 3).build();
    EventPublisher eventPublisher = mock(EventPublisher.class);
    inbox.setEventPublisher(eventPublisher);
    // 
    when(storage.get(THING_UID.toString())).thenReturn(// 
    null).thenReturn(DiscoveryResultBuilder.create(THING_UID).withProperty("foo", "bar").build());
    inbox.add(result);
    // 1st call checks existence of the result in the storage (returns null)
    // 2nd call retrieves the stored instance before the event gets emitted
    // (modified due to storage mock configuration)
    verify(storage, times(2)).get(THING_UID.toString());
    ArgumentCaptor<InboxAddedEvent> eventCaptor = ArgumentCaptor.forClass(InboxAddedEvent.class);
    verify(eventPublisher).post(eventCaptor.capture());
    assertThat(eventCaptor.getValue().getDiscoveryResult().properties, hasEntry("foo", "bar"));
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) InboxAddedEvent(org.eclipse.smarthome.config.discovery.inbox.events.InboxAddedEvent) Test(org.junit.Test)

Example 8 with EventPublisher

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

the class BusEvent method postUpdate.

/**
 * Posts a status update for a specified item to the event bus.
 *
 * @param itemName the name of the item to send the status update for
 * @param stateAsString the new state of the item
 */
public static Object postUpdate(String itemName, String stateString) {
    ItemRegistry registry = ScriptServiceUtil.getItemRegistry();
    EventPublisher publisher = ScriptServiceUtil.getEventPublisher();
    if (publisher != null && registry != null) {
        try {
            Item item = registry.getItem(itemName);
            State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateString);
            if (state != null) {
                publisher.post(ItemEventFactory.createStateEvent(itemName, state));
            } else {
                LoggerFactory.getLogger(BusEvent.class).warn("Cannot convert '{}' to a state type which item '{}' accepts: {}.", stateString, itemName, getAcceptedDataTypeNames(item));
            }
        } catch (ItemNotFoundException e) {
            LoggerFactory.getLogger(BusEvent.class).warn("Item '{}' does not exist.", itemName);
        }
    }
    return null;
}
Also used : Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) State(org.eclipse.smarthome.core.types.State) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 9 with EventPublisher

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

use of org.eclipse.smarthome.core.events.EventPublisher 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)

Aggregations

EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)10 Test (org.junit.Test)8 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)6 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)5 LinkedList (java.util.LinkedList)4 Set (java.util.Set)4 Rule (org.eclipse.smarthome.automation.Rule)4 RuleRegistry (org.eclipse.smarthome.automation.RuleRegistry)4 Event (org.eclipse.smarthome.core.events.Event)4 EventFilter (org.eclipse.smarthome.core.events.EventFilter)4 EventSubscriber (org.eclipse.smarthome.core.events.EventSubscriber)4 Item (org.eclipse.smarthome.core.items.Item)4 ItemCommandEvent (org.eclipse.smarthome.core.items.events.ItemCommandEvent)4 Action (org.eclipse.smarthome.automation.Action)3 Trigger (org.eclipse.smarthome.automation.Trigger)3 RuleStatusInfoEvent (org.eclipse.smarthome.automation.events.RuleStatusInfoEvent)3 Configuration (org.eclipse.smarthome.config.core.Configuration)3 Random (java.util.Random)2 DiscoveryResult (org.eclipse.smarthome.config.discovery.DiscoveryResult)2 GroupItem (org.eclipse.smarthome.core.items.GroupItem)2