Search in sources :

Example 1 with EventSubscriber

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

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

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

the class ThingRegistryOSGiTest method assertThatThingRegistryEventSubscribersReceiveEventsAboutThingChanges.

@Test
public void assertThatThingRegistryEventSubscribersReceiveEventsAboutThingChanges() {
    EventSubscriber thingRegistryEventSubscriber = new EventSubscriber() {

        @Override
        public Set<String> getSubscribedEventTypes() {
            Set<String> types = new HashSet<>();
            types.add(ThingAddedEvent.TYPE);
            types.add(ThingRemovedEvent.TYPE);
            types.add(ThingUpdatedEvent.TYPE);
            return types;
        }

        @Override
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(Event event) {
            receivedEvent = event;
        }
    };
    registerService(thingRegistryEventSubscriber);
    // add new thing
    managedThingProvider.add(THING);
    waitForAssert(() -> {
        assertThat(receivedEvent, notNullValue());
    });
    assertThat(receivedEvent, is(instanceOf(ThingAddedEvent.class)));
    receivedEvent = null;
    // update thing
    Thing updatedThing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
    managedThingProvider.update(updatedThing);
    waitForAssert(() -> {
        assertThat(receivedEvent, notNullValue());
    });
    assertThat(receivedEvent, is(instanceOf(ThingUpdatedEvent.class)));
    receivedEvent = null;
    // remove thing
    managedThingProvider.remove(THING.getUID());
    waitForAssert(() -> {
        assertThat(receivedEvent, notNullValue());
    });
    assertThat(receivedEvent, is(instanceOf(ThingRemovedEvent.class)));
    receivedEvent = null;
}
Also used : EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) ThingUpdatedEvent(org.eclipse.smarthome.core.thing.events.ThingUpdatedEvent) ThingRemovedEvent(org.eclipse.smarthome.core.thing.events.ThingRemovedEvent) ThingAddedEvent(org.eclipse.smarthome.core.thing.events.ThingAddedEvent) Event(org.eclipse.smarthome.core.events.Event) Thing(org.eclipse.smarthome.core.thing.Thing) HashSet(java.util.HashSet) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 4 with EventSubscriber

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

the class NtpOSGiTest method assertEventIsReceived.

private void assertEventIsReceived(UpdateEventType updateEventType, String channelID, String acceptedItemType) {
    Configuration configuration = new Configuration();
    initialize(configuration, channelID, acceptedItemType, null, null);
    EventSubscriber eventSubscriberMock = mock(EventSubscriber.class);
    when(eventSubscriberMock.getSubscribedEventTypes()).thenReturn(Collections.singleton(ItemStateEvent.TYPE));
    registerService(eventSubscriberMock);
    if (updateEventType.equals(UpdateEventType.HANDLE_COMMAND)) {
        ntpHandler.handleCommand(new ChannelUID("ntp:test:chan:1"), new StringType("test"));
    } else if (updateEventType.equals(UpdateEventType.CHANNEL_LINKED)) {
        ntpHandler.channelLinked(new ChannelUID("ntp:test:chan:1"));
    }
    waitForAssert(() -> {
        verify(eventSubscriberMock, atLeastOnce()).receive(ArgumentMatchers.any(Event.class));
    });
}
Also used : EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) Configuration(org.eclipse.smarthome.config.core.Configuration) StringType(org.eclipse.smarthome.core.library.types.StringType) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) ItemStateEvent(org.eclipse.smarthome.core.items.events.ItemStateEvent) Event(org.eclipse.smarthome.core.events.Event)

Example 5 with EventSubscriber

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

the class GroupItemTest method setUp.

@Before
public void setUp() {
    initMocks(this);
    registerVolatileStorageService();
    publisher = event -> events.add(event);
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    registerService(new EventSubscriber() {

        @Override
        public void receive(Event event) {
            events.add(event);
        }

        @Override
        public Set<String> getSubscribedEventTypes() {
            HashSet<String> hs = new HashSet<>();
            hs.add(ItemUpdatedEvent.TYPE);
            return hs;
        }

        @Override
        public EventFilter getEventFilter() {
            return null;
        }
    });
    when(unitProvider.getUnit(Temperature.class)).thenReturn(Units.CELSIUS);
    groupFunctionHelper = new GroupFunctionHelper();
    itemStateConverter = new ItemStateConverterImpl();
    ((ItemStateConverterImpl) itemStateConverter).setUnitProvider(unitProvider);
}
Also used : EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) HashSet(java.util.HashSet) Set(java.util.Set) GroupItemStateChangedEvent(org.eclipse.smarthome.core.items.events.GroupItemStateChangedEvent) ItemUpdatedEvent(org.eclipse.smarthome.core.items.events.ItemUpdatedEvent) Event(org.eclipse.smarthome.core.events.Event) GroupFunctionHelper(org.eclipse.smarthome.core.internal.items.GroupFunctionHelper) ItemStateConverterImpl(org.eclipse.smarthome.core.internal.items.ItemStateConverterImpl) EventFilter(org.eclipse.smarthome.core.events.EventFilter) HashSet(java.util.HashSet) Before(org.junit.Before)

Aggregations

Event (org.eclipse.smarthome.core.events.Event)8 EventSubscriber (org.eclipse.smarthome.core.events.EventSubscriber)8 Set (java.util.Set)5 EventFilter (org.eclipse.smarthome.core.events.EventFilter)5 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)5 Test (org.junit.Test)5 LinkedList (java.util.LinkedList)4 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)4 ItemCommandEvent (org.eclipse.smarthome.core.items.events.ItemCommandEvent)4 Rule (org.eclipse.smarthome.automation.Rule)3 RuleRegistry (org.eclipse.smarthome.automation.RuleRegistry)3 RuleStatusInfoEvent (org.eclipse.smarthome.automation.events.RuleStatusInfoEvent)3 Configuration (org.eclipse.smarthome.config.core.Configuration)3 HashSet (java.util.HashSet)2 Random (java.util.Random)2 Action (org.eclipse.smarthome.automation.Action)2 Trigger (org.eclipse.smarthome.automation.Trigger)2 EventFactory (org.eclipse.smarthome.core.events.EventFactory)1 GroupFunctionHelper (org.eclipse.smarthome.core.internal.items.GroupFunctionHelper)1 ItemStateConverterImpl (org.eclipse.smarthome.core.internal.items.ItemStateConverterImpl)1