Search in sources :

Example 61 with Event

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

the class ItemEventFactoryTest method testCreateEventItemStateEventUnDefType.

@Test
public void testCreateEventItemStateEventUnDefType() throws Exception {
    Event event = factory.createEvent(ITEM_STATE_EVENT_TYPE, ITEM_STATE_EVENT_TOPIC, ITEM_UNDEF_STATE_EVENT_PAYLOAD, SOURCE);
    assertEquals(ItemStateEvent.class, event.getClass());
    ItemStateEvent itemStateEvent = (ItemStateEvent) event;
    assertEquals(ITEM_STATE_EVENT_TYPE, itemStateEvent.getType());
    assertEquals(ITEM_STATE_EVENT_TOPIC, itemStateEvent.getTopic());
    assertEquals(ITEM_UNDEF_STATE_EVENT_PAYLOAD, itemStateEvent.getPayload());
    assertEquals(ITEM_NAME, itemStateEvent.getItemName());
    assertEquals(SOURCE, itemStateEvent.getSource());
    assertEquals(UnDefType.UNDEF, itemStateEvent.getItemState());
}
Also used : Event(org.openhab.core.events.Event) Test(org.junit.jupiter.api.Test)

Example 62 with Event

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

the class ItemEventFactoryTest method testCreateEventItemAddedEvent.

@Test
public void testCreateEventItemAddedEvent() throws Exception {
    Event event = factory.createEvent(ITEM_ADDED_EVENT_TYPE, ITEM_ADDED_EVENT_TOPIC, ITEM_ADDED_EVENT_PAYLOAD, null);
    assertEquals(ItemAddedEvent.class, event.getClass());
    ItemAddedEvent itemAddedEvent = (ItemAddedEvent) event;
    assertEquals(ITEM_ADDED_EVENT_TYPE, itemAddedEvent.getType());
    assertEquals(ITEM_ADDED_EVENT_TOPIC, itemAddedEvent.getTopic());
    assertEquals(ITEM_ADDED_EVENT_PAYLOAD, itemAddedEvent.getPayload());
    assertNotNull(itemAddedEvent.getItem());
    assertEquals(ITEM_NAME, itemAddedEvent.getItem().name);
    assertEquals(CoreItemFactory.SWITCH, itemAddedEvent.getItem().type);
}
Also used : Event(org.openhab.core.events.Event) Test(org.junit.jupiter.api.Test)

Example 63 with Event

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

the class ItemEventFactoryTest method testCreateEventItemStatePredictedEventOnOffType.

@Test
public void testCreateEventItemStatePredictedEventOnOffType() throws Exception {
    Event event = factory.createEvent(ITEM_STATE_PREDICTED_EVENT_TYPE, ITEM_STATE_PREDICTED_EVENT_TOPIC, ITEM_STATE_PREDICTED_EVENT_PAYLOAD, SOURCE);
    assertEquals(ItemStatePredictedEvent.class, event.getClass());
    ItemStatePredictedEvent itemStatePredictedEvent = (ItemStatePredictedEvent) event;
    assertEquals(ITEM_STATE_PREDICTED_EVENT_TYPE, itemStatePredictedEvent.getType());
    assertEquals(ITEM_STATE_PREDICTED_EVENT_TOPIC, itemStatePredictedEvent.getTopic());
    assertEquals(ITEM_STATE_PREDICTED_EVENT_PAYLOAD, itemStatePredictedEvent.getPayload());
    assertEquals(ITEM_NAME, itemStatePredictedEvent.getItemName());
    assertEquals(OnOffType.class, itemStatePredictedEvent.getPredictedState().getClass());
    assertEquals(ITEM_STATE, itemStatePredictedEvent.getPredictedState());
}
Also used : Event(org.openhab.core.events.Event) Test(org.junit.jupiter.api.Test)

Example 64 with Event

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

the class NeeoBrainService method getEventFilter.

/**
 * Returns the {@link EventFilter} used by this service. The {@link EventFilter} will simply filter for those items
 * that have been bound
 *
 * @return a non-null {@link EventFilter}
 */
@NonNull
@Override
public EventFilter getEventFilter() {
    return new EventFilter() {

        @Override
        public boolean apply(@Nullable Event event) {
            Objects.requireNonNull(event, "event cannot be null");
            final ItemStateChangedEvent ise = (ItemStateChangedEvent) event;
            final String itemName = ise.getItemName();
            final NeeoDeviceKeys keys = api.getDeviceKeys();
            final boolean isBound = context.getDefinitions().isBound(keys, itemName);
            logger.trace("Apply Event: {} --- {} --- {} = {}", event, itemName, isBound, keys);
            return isBound;
        }
    };
}
Also used : ItemStateChangedEvent(org.openhab.core.items.events.ItemStateChangedEvent) ItemStateChangedEvent(org.openhab.core.items.events.ItemStateChangedEvent) ChannelTriggeredEvent(org.openhab.core.thing.events.ChannelTriggeredEvent) PropertyChangeEvent(java.beans.PropertyChangeEvent) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) Event(org.openhab.core.events.Event) NeeoDeviceKeys(org.openhab.io.neeo.internal.NeeoDeviceKeys) EventFilter(org.openhab.core.events.EventFilter) Nullable(org.eclipse.jdt.annotation.Nullable) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 65 with Event

use of org.openhab.core.events.Event in project jrule by seaside1.

the class JRuleHandler method propertyChange.

@Override
public void propertyChange(@Nullable PropertyChangeEvent evt) {
    if (evt == null) {
        return;
    }
    final String property = evt.getPropertyName();
    if (property.equals(JRuleEventSubscriber.PROPERTY_ITEM_REGISTRY_EVENT)) {
        Event event = (Event) evt.getNewValue();
        if (event == null) {
            logDebug("Event value null. ignoring: {}", evt);
            return;
        }
        String eventType = event.getType();
        String itemName = JRuleUtil.getItemNameFromTopic(event.getTopic());
        if (eventType.equals(ItemRemovedEvent.TYPE)) {
            logDebug("RemovedType: {}", evt);
            deleteClassFileForItem(itemName);
            deleteSourceFileForItem(itemName);
            recompileJar = true;
        } else if (eventType.equals(ItemAddedEvent.TYPE) || event.getType().equals(ItemUpdatedEvent.TYPE)) {
            try {
                logDebug("Added/updatedType: {}", evt);
                Item item = itemRegistry.getItem(itemName);
                generateItemSource(item);
            } catch (ItemNotFoundException e) {
                logDebug("Could not find new item", e);
            }
        } else {
            logDebug("Failed to do something with item event");
        }
        if (recompileJar) {
            compileItems();
            createItemsJar();
        }
    } else if (JRuleRulesWatcher.PROPERTY_ENTRY_CREATE.equals(property) || JRuleRulesWatcher.PROPERTY_ENTRY_MODIFY.equals(property) || JRuleRulesWatcher.PROPERTY_ENTRY_DELETE.equals(property)) {
        Path newValue = (Path) evt.getNewValue();
        logDebug("Directory watcher new value: {}", newValue);
        reloadRules();
        logDebug("All Rules reloaded");
    }
}
Also used : Path(java.nio.file.Path) Item(org.openhab.core.items.Item) ItemRemovedEvent(org.openhab.core.items.events.ItemRemovedEvent) ItemUpdatedEvent(org.openhab.core.items.events.ItemUpdatedEvent) PropertyChangeEvent(java.beans.PropertyChangeEvent) ItemAddedEvent(org.openhab.core.items.events.ItemAddedEvent) Event(org.openhab.core.events.Event) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

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