Search in sources :

Example 1 with ItemRegistry

use of org.eclipse.smarthome.core.items.ItemRegistry 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 ItemRegistry

use of org.eclipse.smarthome.core.items.ItemRegistry in project smarthome by eclipse.

the class RuntimeRuleTest method ruleEnableHandlerWorks.

@Test
@Ignore
public void ruleEnableHandlerWorks() throws ItemNotFoundException {
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    final String firstRuleUID = "FirstTestRule";
    final String secondRuleUID = "SecondTestRule";
    final String thirdRuleUID = "ThirdTestRule";
    final String[] firstConfig = new String[] { "FirstTestRule", "SecondTestRule" };
    final String[] secondConfig = new String[] { "FirstTestRule" };
    final String firstRuleAction = "firstRuleAction";
    final String secondRuleAction = "secondRuleAction";
    try {
        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<>("enable", false), new SimpleEntry<>("ruleUIDs", firstConfig)).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
        final Rule rule = new Rule(firstRuleAction);
        rule.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger3", "core.ItemStateChangeTrigger", triggerConfig) }));
        rule.setActions(Arrays.asList(new Action[] { new Action("RuleAction", "core.RuleEnablementAction", actionConfig, null) }));
        ruleRegistry.add(new Rule(firstRuleUID));
        ruleRegistry.add(new Rule(secondRuleUID));
        ruleRegistry.add(new Rule(thirdRuleUID));
        ruleRegistry.add(rule);
        final ItemRegistry itemRegistry = getService(ItemRegistry.class);
        final EventPublisher eventPublisher = getService(EventPublisher.class);
        final Item myMotionItem = itemRegistry.getItem("myMotionItem3");
        eventPublisher.post(ItemEventFactory.createCommandEvent("myMotionItem3", TypeParser.parseCommand(myMotionItem.getAcceptedCommandTypes(), "ON")));
        waitForAssert(() -> {
            Assert.assertEquals(RuleStatus.DISABLED, ruleRegistry.getStatus(firstRuleUID));
            Assert.assertEquals(RuleStatus.DISABLED, ruleRegistry.getStatus(secondRuleUID));
            Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatus(thirdRuleUID));
        });
        final Configuration triggerConfig2 = new Configuration(Stream.of(new SimpleEntry<>("itemName", "myMotionItem3")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
        final Configuration actionConfig2 = new Configuration(Stream.of(new SimpleEntry<>("enable", true), new SimpleEntry<>("ruleUIDs", secondConfig)).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
        final Rule rule2 = new Rule(secondRuleAction);
        rule2.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger3", "core.ItemStateChangeTrigger", triggerConfig2) }));
        rule2.setActions(Arrays.asList(new Action[] { new Action("RuleAction", "core.RuleEnablementAction", actionConfig2, null) }));
        ruleRegistry.add(rule2);
        eventPublisher.post(ItemEventFactory.createCommandEvent("myMotionItem3", TypeParser.parseCommand(myMotionItem.getAcceptedCommandTypes(), "OFF")));
        waitForAssert(() -> {
            Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatus(firstRuleUID));
            Assert.assertEquals(RuleStatus.DISABLED, ruleRegistry.getStatus(secondRuleUID));
            Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatus(thirdRuleUID));
        });
    } finally {
        ruleRegistry.remove(firstRuleUID);
        ruleRegistry.remove(secondRuleUID);
        ruleRegistry.remove(thirdRuleUID);
        ruleRegistry.remove(firstRuleAction);
        ruleRegistry.remove(secondRuleAction);
    }
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) Action(org.eclipse.smarthome.automation.Action) Trigger(org.eclipse.smarthome.automation.Trigger) Configuration(org.eclipse.smarthome.config.core.Configuration) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) Rule(org.eclipse.smarthome.automation.Rule) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) Ignore(org.junit.Ignore) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 3 with ItemRegistry

use of org.eclipse.smarthome.core.items.ItemRegistry in project smarthome by eclipse.

the class ScriptEngineOSGiTest method setup.

@Before
public void setup() {
    registerVolatileStorageService();
    EventPublisher eventPublisher = event -> {
    };
    registerService(eventPublisher);
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    itemProvider = new ItemProvider() {

        @Override
        public void addProviderChangeListener(ProviderChangeListener<Item> listener) {
        }

        @Override
        public Collection<Item> getAll() {
            return Lists.newArrayList(new SwitchItem(ITEM_NAME), createNumberItem(NUMBER_ITEM_TEMPERATURE, Temperature.class), createNumberItem(NUMBER_ITEM_LENGTH, Length.class), new NumberItem(NUMBER_ITEM_DECIMAL));
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<Item> listener) {
        }
    };
    registerService(itemProvider);
    ScriptServiceUtil scriptServiceUtil = getService(ScriptServiceUtil.class);
    assertNotNull(scriptServiceUtil);
    scriptEngine = ScriptServiceUtil.getScriptEngine();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Collection(java.util.Collection) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) Test(org.junit.Test) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Item(org.eclipse.smarthome.core.items.Item) ScriptServiceUtil(org.eclipse.smarthome.model.script.ScriptServiceUtil) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) Lists(com.google.common.collect.Lists) Temperature(javax.measure.quantity.Temperature) Length(javax.measure.quantity.Length) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) After(org.junit.After) Assert(org.junit.Assert) State(org.eclipse.smarthome.core.types.State) NonNull(org.eclipse.jdt.annotation.NonNull) Before(org.junit.Before) ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ScriptServiceUtil(org.eclipse.smarthome.model.script.ScriptServiceUtil) Collection(java.util.Collection) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Before(org.junit.Before)

Example 4 with ItemRegistry

use of org.eclipse.smarthome.core.items.ItemRegistry in project smarthome by eclipse.

the class BusEvent method sendCommand.

/**
 * Sends a command for a specified item to the event bus.
 *
 * @param itemName the name of the item to send the command to
 * @param commandString the command to send
 */
public static Object sendCommand(String itemName, String commandString) {
    ItemRegistry registry = ScriptServiceUtil.getItemRegistry();
    EventPublisher publisher = ScriptServiceUtil.getEventPublisher();
    if (publisher != null && registry != null) {
        try {
            Item item = registry.getItem(itemName);
            Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandString);
            if (command != null) {
                publisher.post(ItemEventFactory.createCommandEvent(itemName, command));
            } else {
                LoggerFactory.getLogger(BusEvent.class).warn("Cannot convert '{}' to a command type which item '{}' accepts: {}.", commandString, itemName, getAcceptedCommandNames(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) Command(org.eclipse.smarthome.core.types.Command) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 5 with ItemRegistry

use of org.eclipse.smarthome.core.items.ItemRegistry in project smarthome by eclipse.

the class NtpOSGiTest method setUp.

@Before
public void setUp() {
    VolatileStorageService volatileStorageService = new VolatileStorageService();
    registerService(volatileStorageService);
    managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
    assertNotNull(managedThingProvider);
    thingRegistry = getService(ThingRegistry.class);
    assertNotNull(thingRegistry);
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    channelTypeUID = new ChannelTypeUID(NtpBindingConstants.BINDING_ID + ":channelType");
    channelTypeProvider = mock(ChannelTypeProvider.class);
    when(channelTypeProvider.getChannelType(any(ChannelTypeUID.class), any(Locale.class))).thenReturn(new ChannelType(channelTypeUID, false, "Switch", ChannelKind.STATE, "label", null, null, null, null, null, null));
    registerService(channelTypeProvider);
}
Also used : Locale(java.util.Locale) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) ThingProvider(org.eclipse.smarthome.core.thing.ThingProvider) ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) ChannelTypeProvider(org.eclipse.smarthome.core.thing.type.ChannelTypeProvider) VolatileStorageService(org.eclipse.smarthome.test.storage.VolatileStorageService) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) Before(org.junit.Before)

Aggregations

ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)9 Item (org.eclipse.smarthome.core.items.Item)6 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)5 Before (org.junit.Before)4 Test (org.junit.Test)4 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)3 ManagedThingProvider (org.eclipse.smarthome.core.thing.ManagedThingProvider)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 Collection (java.util.Collection)2 Locale (java.util.Locale)2 Rule (org.eclipse.smarthome.automation.Rule)2 RuleRegistry (org.eclipse.smarthome.automation.RuleRegistry)2 GroupItem (org.eclipse.smarthome.core.items.GroupItem)2 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)2 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)2 StringItem (org.eclipse.smarthome.core.library.items.StringItem)2 ItemChannelLinkRegistry (org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry)2 ChannelType (org.eclipse.smarthome.core.thing.type.ChannelType)2 ChannelTypeProvider (org.eclipse.smarthome.core.thing.type.ChannelTypeProvider)2 VolatileStorageService (org.eclipse.smarthome.test.storage.VolatileStorageService)2