Search in sources :

Example 1 with Condition

use of org.openhab.core.automation.Condition in project openhab-addons by openhab.

the class RuleConditionHandlerTests method ltOperator.

@Test
public void ltOperator() {
    Map<String, Object> context = new TreeMap<>();
    HueRuleConditionHandler subject;
    Condition c;
    Configuration configuration = new Configuration();
    configuration.put("operator", "lt");
    context.put("newState", new DecimalType(12));
    context.put("oldState", new DecimalType(0));
    configuration.put("value", "15");
    configuration.put("address", "/groups/10/action");
    c = ConditionBuilder.create().withId("a").withTypeUID(HueRuleConditionHandler.MODULE_TYPE_ID).withConfiguration(configuration).build();
    subject = new HueRuleConditionHandler(c, ds);
    assertThat(subject.isSatisfied(context), is(true));
}
Also used : Condition(org.openhab.core.automation.Condition) Configuration(org.openhab.core.config.core.Configuration) DecimalType(org.openhab.core.library.types.DecimalType) TreeMap(java.util.TreeMap) Test(org.junit.jupiter.api.Test)

Example 2 with Condition

use of org.openhab.core.automation.Condition in project openhab-addons by openhab.

the class RuleConditionHandlerTests method equalOperator.

@Test
public void equalOperator() {
    Map<String, Object> context = new TreeMap<>();
    HueRuleConditionHandler subject;
    Condition c;
    Configuration configuration = new Configuration();
    configuration.put("operator", "eq");
    context.put("newState", OnOffType.ON);
    context.put("oldState", OnOffType.OFF);
    configuration.put("value", "true");
    configuration.put("address", "/lights/1/state");
    c = ConditionBuilder.create().withId("a").withTypeUID(HueRuleConditionHandler.MODULE_TYPE_ID).withConfiguration(configuration).build();
    subject = new HueRuleConditionHandler(c, ds);
    assertThat(subject.isSatisfied(context), is(true));
    context.put("newState", OpenClosedType.OPEN);
    context.put("oldState", OpenClosedType.CLOSED);
    configuration.put("value", "true");
    configuration.put("address", "/sensors/2/state");
    c = ConditionBuilder.create().withId("a").withTypeUID(HueRuleConditionHandler.MODULE_TYPE_ID).withConfiguration(configuration).build();
    subject = new HueRuleConditionHandler(c, ds);
    assertThat(subject.isSatisfied(context), is(true));
    context.put("newState", new DecimalType(12));
    context.put("oldState", new DecimalType(0));
    configuration.put("value", "12");
    configuration.put("address", "/groups/10/action");
    c = ConditionBuilder.create().withId("a").withTypeUID(HueRuleConditionHandler.MODULE_TYPE_ID).withConfiguration(configuration).build();
    subject = new HueRuleConditionHandler(c, ds);
    assertThat(subject.isSatisfied(context), is(true));
}
Also used : Condition(org.openhab.core.automation.Condition) Configuration(org.openhab.core.config.core.Configuration) DecimalType(org.openhab.core.library.types.DecimalType) TreeMap(java.util.TreeMap) Test(org.junit.jupiter.api.Test)

Example 3 with Condition

use of org.openhab.core.automation.Condition in project openhab-core by openhab.

the class ScriptModuleHandlerFactory method internalCreate.

@Override
@Nullable
protected ModuleHandler internalCreate(Module module, String ruleUID) {
    logger.trace("create {} -> {}", module.getId(), module.getTypeUID());
    String moduleTypeUID = module.getTypeUID();
    if (ScriptConditionHandler.TYPE_ID.equals(moduleTypeUID) && module instanceof Condition) {
        ScriptConditionHandler handler = new ScriptConditionHandler((Condition) module, ruleUID, scriptEngineManager);
        return handler;
    } else if (ScriptActionHandler.TYPE_ID.equals(moduleTypeUID) && module instanceof Action) {
        ScriptActionHandler handler = new ScriptActionHandler((Action) module, ruleUID, scriptEngineManager);
        return handler;
    } else {
        logger.error("The ModuleHandler is not supported: {}", moduleTypeUID);
        return null;
    }
}
Also used : Condition(org.openhab.core.automation.Condition) ScriptConditionHandler(org.openhab.core.automation.module.script.internal.handler.ScriptConditionHandler) ScriptActionHandler(org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler) Action(org.openhab.core.automation.Action) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 4 with Condition

use of org.openhab.core.automation.Condition in project openhab-core by openhab.

the class ScriptRuleOSGiTest method testPredefinedRule.

// ignore - wip @Test
public void testPredefinedRule() throws ItemNotFoundException {
    EventPublisher eventPublisher = getService(EventPublisher.class);
    ItemRegistry itemRegistry = getService(ItemRegistry.class);
    RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    RuleManager ruleEngine = getService(RuleManager.class);
    // WAIT until Rule modules types are parsed and the rule becomes IDLE
    waitForAssert(() -> {
        assertThat(ruleRegistry.getAll().isEmpty(), is(false));
        Rule rule2 = ruleRegistry.get("javascript.rule1");
        assertThat(rule2, is(notNullValue()));
        RuleStatusInfo ruleStatus2 = ruleEngine.getStatusInfo(rule2.getUID());
        assertThat(ruleStatus2, is(notNullValue()));
        assertThat(ruleStatus2.getStatus(), is(RuleStatus.IDLE));
    }, 5000, 200);
    Rule rule = ruleRegistry.get("javascript.rule1");
    assertThat(rule, is(notNullValue()));
    assertThat(rule.getName(), is("DemoScriptRule"));
    Optional<? extends Trigger> trigger = rule.getTriggers().stream().filter(t -> "trigger".equals(t.getId())).findFirst();
    assertThat(trigger.isPresent(), is(true));
    assertThat(trigger.get().getTypeUID(), is("core.GenericEventTrigger"));
    assertThat(trigger.get().getConfiguration().get("eventSource"), is("MyTrigger"));
    assertThat(trigger.get().getConfiguration().get("eventTopic"), is("openhab/items/MyTrigger/state"));
    assertThat(trigger.get().getConfiguration().get("eventTypes"), is("ItemStateEvent"));
    Optional<? extends Condition> condition1 = rule.getConditions().stream().filter(c -> "condition".equals(c.getId())).findFirst();
    assertThat(condition1.isPresent(), is(true));
    assertThat(condition1.get().getTypeUID(), is("script.ScriptCondition"));
    assertThat(condition1.get().getConfiguration().get("type"), is("application/javascript"));
    assertThat(condition1.get().getConfiguration().get("script"), is("event.itemState==ON"));
    Optional<? extends Action> action = rule.getActions().stream().filter(a -> "action".equals(a.getId())).findFirst();
    assertThat(action.isPresent(), is(true));
    assertThat(action.get().getTypeUID(), is("script.ScriptAction"));
    assertThat(action.get().getConfiguration().get("type"), is("application/javascript"));
    assertThat(action.get().getConfiguration().get("script"), is("print(items.MyTrigger), print(things.getAll()), print(ctx.get('trigger.event')), events.sendCommand('ScriptItem', 'ON')"));
    RuleStatusInfo ruleStatus = ruleEngine.getStatusInfo(rule.getUID());
    assertThat(ruleStatus.getStatus(), is(RuleStatus.IDLE));
    SwitchItem myTriggerItem = (SwitchItem) itemRegistry.getItem("MyTrigger");
    logger.info("Triggering item: {}", myTriggerItem.getName());
    eventPublisher.post(ItemEventFactory.createStateEvent("MyTrigger", OnOffType.ON));
    waitForAssert(() -> {
        assertThat(receivedEvent, notNullValue());
    });
    assertThat(receivedEvent.getItemName(), is(equalTo("ScriptItem")));
    assertThat(receivedEvent.getItemCommand(), is(OnOffType.ON));
}
Also used : ItemEventFactory(org.openhab.core.items.events.ItemEventFactory) VolatileStorageService(org.openhab.core.test.storage.VolatileStorageService) CoreMatchers(org.hamcrest.CoreMatchers) BeforeEach(org.junit.jupiter.api.BeforeEach) EventSubscriber(org.openhab.core.events.EventSubscriber) RuleRegistry(org.openhab.core.automation.RuleRegistry) SwitchItem(org.openhab.core.library.items.SwitchItem) RuleManager(org.openhab.core.automation.RuleManager) LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.openhab.core.library.types.OnOffType) Trigger(org.openhab.core.automation.Trigger) Nullable(org.eclipse.jdt.annotation.Nullable) EventFilter(org.openhab.core.events.EventFilter) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) EventPublisher(org.openhab.core.events.EventPublisher) Condition(org.openhab.core.automation.Condition) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) RuleStatusInfo(org.openhab.core.automation.RuleStatusInfo) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Logger(org.slf4j.Logger) ItemProvider(org.openhab.core.items.ItemProvider) Collection(java.util.Collection) Action(org.openhab.core.automation.Action) Event(org.openhab.core.events.Event) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException) Set(java.util.Set) Rule(org.openhab.core.automation.Rule) Item(org.openhab.core.items.Item) ItemRegistry(org.openhab.core.items.ItemRegistry) RuleStatus(org.openhab.core.automation.RuleStatus) List(java.util.List) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest) Optional(java.util.Optional) ProviderChangeListener(org.openhab.core.common.registry.ProviderChangeListener) RuleStatusInfo(org.openhab.core.automation.RuleStatusInfo) EventPublisher(org.openhab.core.events.EventPublisher) RuleRegistry(org.openhab.core.automation.RuleRegistry) RuleManager(org.openhab.core.automation.RuleManager) Rule(org.openhab.core.automation.Rule) ItemRegistry(org.openhab.core.items.ItemRegistry) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 5 with Condition

use of org.openhab.core.automation.Condition in project openhab-core by openhab.

the class AutomationIntegrationTest method testModuleTypeProviderAndTemplateProvider.

@Test
public void testModuleTypeProviderAndTemplateProvider() {
    logger.info("test ModuleTypeProvider and TemplateProvider");
    TemplateRegistry<RuleTemplate> templateRegistry = getService(TemplateRegistry.class);
    ModuleTypeRegistry moduleTypeRegistry = getService(ModuleTypeRegistry.class);
    String templateUID = "testTemplate1";
    Set<String> tags = Stream.of("test", "testTag").collect(toSet());
    List<Trigger> templateTriggers = Collections.emptyList();
    List<Condition> templateConditions = Collections.emptyList();
    List<Action> templateActions = Collections.emptyList();
    List<ConfigDescriptionParameter> templateConfigDescriptionParameters = List.of(ConfigDescriptionParameterBuilder.create("param", ConfigDescriptionParameter.Type.TEXT).build());
    RuleTemplate template = new RuleTemplate(templateUID, "Test template Label", "Test template description", tags, templateTriggers, templateConditions, templateActions, templateConfigDescriptionParameters, Visibility.VISIBLE);
    String triggerTypeUID = "testTrigger1";
    TriggerType triggerType = new TriggerType(triggerTypeUID, templateConfigDescriptionParameters, null);
    String actionTypeUID = "testAction1";
    ActionType actionType = new ActionType(actionTypeUID, templateConfigDescriptionParameters, null);
    RuleTemplateProvider templateProvider = new RuleTemplateProvider() {

        @Override
        @Nullable
        public RuleTemplate getTemplate(String UID, @Nullable Locale locale) {
            if (UID.equals(templateUID)) {
                return template;
            } else {
                return null;
            }
        }

        @Override
        public Collection<RuleTemplate> getTemplates(@Nullable Locale locale) {
            return Set.of(template);
        }

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

        @Override
        public Collection<RuleTemplate> getAll() {
            return Set.of(template);
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<RuleTemplate> listener) {
        }
    };
    ModuleTypeProvider moduleTypeProvider = new ModuleTypeProvider() {

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

        @Override
        public Collection<ModuleType> getAll() {
            return Stream.of(triggerType, actionType).collect(toSet());
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<ModuleType> listener) {
        }

        @Override
        @Nullable
        public <T extends ModuleType> T getModuleType(String UID, @Nullable Locale locale) {
            if (UID.equals(triggerTypeUID)) {
                return (T) triggerType;
            } else if (UID.equals(actionTypeUID)) {
                return (T) actionType;
            } else {
                return null;
            }
        }

        @Override
        public <T extends ModuleType> Collection<T> getModuleTypes(@Nullable Locale locale) {
            return (Collection<T>) Stream.of(triggerType, actionType).collect(toSet());
        }
    };
    registerService(templateProvider);
    assertThat(templateRegistry.get(templateUID), is(notNullValue()));
    unregisterService(templateProvider);
    assertThat(templateRegistry.get(templateUID), is(nullValue()));
    registerService(moduleTypeProvider);
    assertThat(moduleTypeRegistry.get(actionTypeUID), is(notNullValue()));
    assertThat(moduleTypeRegistry.get(triggerTypeUID), is(notNullValue()));
    unregisterService(moduleTypeProvider);
    assertThat(moduleTypeRegistry.get(actionTypeUID), is(nullValue()));
    assertThat(moduleTypeRegistry.get(triggerTypeUID), is(nullValue()));
}
Also used : Condition(org.openhab.core.automation.Condition) Locale(java.util.Locale) TriggerType(org.openhab.core.automation.type.TriggerType) Action(org.openhab.core.automation.Action) ActionType(org.openhab.core.automation.type.ActionType) ModuleTypeRegistry(org.openhab.core.automation.type.ModuleTypeRegistry) RuleTemplate(org.openhab.core.automation.template.RuleTemplate) ModuleType(org.openhab.core.automation.type.ModuleType) Trigger(org.openhab.core.automation.Trigger) RuleTemplateProvider(org.openhab.core.automation.template.RuleTemplateProvider) ProviderChangeListener(org.openhab.core.common.registry.ProviderChangeListener) ModuleTypeProvider(org.openhab.core.automation.type.ModuleTypeProvider) Collection(java.util.Collection) ConfigDescriptionParameter(org.openhab.core.config.core.ConfigDescriptionParameter) Nullable(org.eclipse.jdt.annotation.Nullable) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

Condition (org.openhab.core.automation.Condition)34 Configuration (org.openhab.core.config.core.Configuration)21 Trigger (org.openhab.core.automation.Trigger)19 Test (org.junit.jupiter.api.Test)17 Action (org.openhab.core.automation.Action)16 Rule (org.openhab.core.automation.Rule)11 HashMap (java.util.HashMap)10 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)8 Random (java.util.Random)7 Nullable (org.eclipse.jdt.annotation.Nullable)7 ArrayList (java.util.ArrayList)5 TreeMap (java.util.TreeMap)5 Event (org.openhab.core.events.Event)5 ItemCommandEvent (org.openhab.core.items.events.ItemCommandEvent)5 Collection (java.util.Collection)4 List (java.util.List)4 Map (java.util.Map)4 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)4 RuleStatus (org.openhab.core.automation.RuleStatus)4 HueRuleEntry (org.openhab.io.hueemulation.internal.dto.HueRuleEntry)4