Search in sources :

Example 1 with Event

use of org.openhab.core.events.Event in project openhab-addons by openhab.

the class LightsAndGroupsTests method changeGroupItemSwitchState.

@Test
public void changeGroupItemSwitchState() {
    assertThat(((HueStatePlug) cs.ds.groups.get("10").action).on, is(false));
    String body = "{'on':true}";
    Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/groups/10/action").request().put(Entity.json(body));
    assertEquals(200, response.getStatus());
    assertThat(response.readEntity(String.class), containsString("success"));
    assertThat(((HueStatePlug) cs.ds.groups.get("10").action).on, is(true));
    verify(commonSetup.eventPublisher).post(argThat((Event t) -> {
        assertThat(t.getPayload(), is("{\"type\":\"OnOff\",\"value\":\"ON\"}"));
        return true;
    }));
}
Also used : Response(javax.ws.rs.core.Response) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) Event(org.openhab.core.events.Event) Test(org.junit.jupiter.api.Test)

Example 2 with Event

use of org.openhab.core.events.Event in project openhab-addons by openhab.

the class LightsAndGroupsTests method changeSwitchState.

@Test
public void changeSwitchState() {
    assertThat(((HueStatePlug) cs.ds.lights.get("1").state).on, is(false));
    String body = "{'on':true}";
    Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/lights/1/state").request().put(Entity.json(body));
    assertEquals(200, response.getStatus());
    assertThat(response.readEntity(String.class), containsString("success"));
    assertThat(((HueStatePlug) cs.ds.lights.get("1").state).on, is(true));
    verify(commonSetup.eventPublisher).post(argThat((Event t) -> {
        assertThat(t.getPayload(), is("{\"type\":\"OnOff\",\"value\":\"ON\"}"));
        return true;
    }));
}
Also used : Response(javax.ws.rs.core.Response) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) Event(org.openhab.core.events.Event) Test(org.junit.jupiter.api.Test)

Example 3 with Event

use of org.openhab.core.events.Event in project openhab-core by openhab.

the class AddonResource method postFailureEvent.

private void postFailureEvent(String addonId, @Nullable String msg) {
    Event event = AddonEventFactory.createAddonFailureEvent(addonId, msg);
    eventPublisher.post(event);
}
Also used : Event(org.openhab.core.events.Event)

Example 4 with Event

use of org.openhab.core.events.Event in project openhab-core by openhab.

the class SitemapSubscriptionService method receive.

@Override
public void receive(Event event) {
    if (event instanceof ItemStatePredictedEvent) {
        ItemStatePredictedEvent prediction = (ItemStatePredictedEvent) event;
        Item item = itemUIRegistry.get(prediction.getItemName());
        if (item instanceof GroupItem) {
            // members and predictions aren't really possible in that case (or at least would be highly complex).
            return;
        }
        for (PageChangeListener pageChangeListener : pageChangeListeners.values()) {
            if (prediction.isConfirmation()) {
                pageChangeListener.keepCurrentState(item);
            } else {
                pageChangeListener.changeStateTo(item, prediction.getPredictedState());
            }
        }
    } else if (event instanceof ChannelDescriptionChangedEvent) {
        ChannelDescriptionChangedEvent channelDescriptionChangedEvent = (ChannelDescriptionChangedEvent) event;
        channelDescriptionChangedEvent.getLinkedItemNames().forEach(itemName -> {
            for (PageChangeListener pageChangeListener : pageChangeListeners.values()) {
                pageChangeListener.descriptionChanged(itemName);
            }
        });
    }
}
Also used : PageChangeListener(org.openhab.core.io.rest.sitemap.internal.PageChangeListener) LinkableWidget(org.openhab.core.model.sitemap.sitemap.LinkableWidget) BasicEList(org.eclipse.emf.common.util.BasicEList) EventType(org.openhab.core.model.core.EventType) EventSubscriber(org.openhab.core.events.EventSubscriber) ItemUIRegistry(org.openhab.core.ui.items.ItemUIRegistry) LoggerFactory(org.slf4j.LoggerFactory) PageChangeListener(org.openhab.core.io.rest.sitemap.internal.PageChangeListener) ArrayList(java.util.ArrayList) ItemStatePredictedEvent(org.openhab.core.items.events.ItemStatePredictedEvent) Component(org.osgi.service.component.annotations.Component) Nullable(org.eclipse.jdt.annotation.Nullable) Duration(java.time.Duration) Map(java.util.Map) ModelRepositoryChangeListener(org.openhab.core.model.core.ModelRepositoryChangeListener) EventFilter(org.openhab.core.events.EventFilter) Activate(org.osgi.service.component.annotations.Activate) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) GroupItem(org.openhab.core.items.GroupItem) Logger(org.slf4j.Logger) ChannelDescriptionChangedEvent(org.openhab.core.thing.events.ChannelDescriptionChangedEvent) Sitemap(org.openhab.core.model.sitemap.sitemap.Sitemap) Widget(org.openhab.core.model.sitemap.sitemap.Widget) Deactivate(org.osgi.service.component.annotations.Deactivate) Event(org.openhab.core.events.Event) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) ReferencePolicy(org.osgi.service.component.annotations.ReferencePolicy) Instant(java.time.Instant) EList(org.eclipse.emf.common.util.EList) Item(org.openhab.core.items.Item) Objects(java.util.Objects) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) List(java.util.List) SitemapProvider(org.openhab.core.model.sitemap.SitemapProvider) Entry(java.util.Map.Entry) Modified(org.osgi.service.component.annotations.Modified) SitemapEvent(org.openhab.core.io.rest.sitemap.internal.SitemapEvent) Reference(org.osgi.service.component.annotations.Reference) GroupItem(org.openhab.core.items.GroupItem) Item(org.openhab.core.items.Item) GroupItem(org.openhab.core.items.GroupItem) ItemStatePredictedEvent(org.openhab.core.items.events.ItemStatePredictedEvent) ChannelDescriptionChangedEvent(org.openhab.core.thing.events.ChannelDescriptionChangedEvent)

Example 5 with Event

use of org.openhab.core.events.Event in project openhab-core by openhab.

the class InboxOSGiTest method assertThatInboxEventSubscribersReceiveEventsAboutDiscoveryResultChanges.

@Test
public void assertThatInboxEventSubscribersReceiveEventsAboutDiscoveryResultChanges() {
    ThingTypeUID thingTypeUID = new ThingTypeUID("some", "thing");
    ThingUID thingUID = new ThingUID(thingTypeUID, "uid");
    final AsyncResultWrapper<Event> receivedEvent = new AsyncResultWrapper<>();
    EventSubscriber inboxEventSubscriber = new EventSubscriber() {

        @Override
        public void receive(Event event) {
            receivedEvent.set(event);
        }

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Set.of(InboxAddedEvent.TYPE, InboxRemovedEvent.TYPE, InboxUpdatedEvent.TYPE);
        }

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }
    };
    registerService(inboxEventSubscriber);
    // add discovery result
    DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID).withTTL(DEFAULT_TTL).build();
    addDiscoveryResult(discoveryResult);
    waitForAssert(() -> assertThat(receivedEvent.getWrappedObject(), not(nullValue())));
    assertThat(receivedEvent.getWrappedObject(), is(instanceOf(InboxAddedEvent.class)));
    receivedEvent.reset();
    // update discovery result
    discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID).withTTL(DEFAULT_TTL).build();
    addDiscoveryResult(discoveryResult);
    waitForAssert(() -> assertThat(receivedEvent.getWrappedObject(), not(nullValue())));
    assertThat(receivedEvent.getWrappedObject(), is(instanceOf(InboxUpdatedEvent.class)));
    receivedEvent.reset();
    // remove discovery result
    removeDiscoveryResult(thingUID);
    waitForAssert(() -> assertThat(receivedEvent.getWrappedObject(), not(nullValue())));
    assertThat(receivedEvent.getWrappedObject(), is(instanceOf(InboxRemovedEvent.class)));
}
Also used : EventSubscriber(org.openhab.core.events.EventSubscriber) DiscoveryResult(org.openhab.core.config.discovery.DiscoveryResult) AsyncResultWrapper(org.openhab.core.test.AsyncResultWrapper) ThingUID(org.openhab.core.thing.ThingUID) InboxUpdatedEvent(org.openhab.core.config.discovery.inbox.events.InboxUpdatedEvent) InboxAddedEvent(org.openhab.core.config.discovery.inbox.events.InboxAddedEvent) Event(org.openhab.core.events.Event) InboxRemovedEvent(org.openhab.core.config.discovery.inbox.events.InboxRemovedEvent) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

Event (org.openhab.core.events.Event)120 Test (org.junit.jupiter.api.Test)93 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)58 EventSubscriber (org.openhab.core.events.EventSubscriber)46 ItemCommandEvent (org.openhab.core.items.events.ItemCommandEvent)38 Nullable (org.eclipse.jdt.annotation.Nullable)28 SwitchItem (org.openhab.core.library.items.SwitchItem)28 EventPublisher (org.openhab.core.events.EventPublisher)27 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)25 BeforeEach (org.junit.jupiter.api.BeforeEach)22 Item (org.openhab.core.items.Item)22 Set (java.util.Set)21 Rule (org.openhab.core.automation.Rule)20 RuleStatusInfoEvent (org.openhab.core.automation.events.RuleStatusInfoEvent)20 EventFilter (org.openhab.core.events.EventFilter)20 NumberItem (org.openhab.core.library.items.NumberItem)18 LinkedList (java.util.LinkedList)17 List (java.util.List)17 ArrayList (java.util.ArrayList)16 Configuration (org.openhab.core.config.core.Configuration)16