Search in sources :

Example 11 with Condition

use of org.eclipse.smarthome.automation.Condition in project smarthome by eclipse.

the class ReferenceResolverUtilTest method testModuleConfigurationResolving.

@Test
public void testModuleConfigurationResolving() {
    // test trigger configuration..
    Module trigger = new Trigger(null, null, new Configuration(moduleConfiguration));
    ReferenceResolverUtil.updateModuleConfiguration(trigger, context);
    Assert.assertEquals(trigger.getConfiguration(), new Configuration(expectedModuleConfiguration));
    // test condition configuration..
    Module condition = new Condition(null, null, new Configuration(moduleConfiguration), null);
    ReferenceResolverUtil.updateModuleConfiguration(condition, context);
    Assert.assertEquals(condition.getConfiguration(), new Configuration(expectedModuleConfiguration));
    // test action configuration..
    Module action = new Action(null, null, new Configuration(moduleConfiguration), null);
    ReferenceResolverUtil.updateModuleConfiguration(action, context);
    Assert.assertEquals(action.getConfiguration(), new Configuration(expectedModuleConfiguration));
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) Action(org.eclipse.smarthome.automation.Action) Trigger(org.eclipse.smarthome.automation.Trigger) Configuration(org.eclipse.smarthome.config.core.Configuration) Module(org.eclipse.smarthome.automation.Module) Test(org.junit.Test)

Example 12 with Condition

use of org.eclipse.smarthome.automation.Condition in project smarthome by eclipse.

the class WelcomeHomeRulesProvider method createLightsRule.

/**
 * This method creates a rule from scratch by using trigger, condition, action, configDescriptions and
 * configuration, tags.
 *
 * @return the created rule
 */
private Rule createLightsRule() {
    // initialize the trigger
    String triggerId = "LightsSwitchOnRuleTrigger";
    List<Trigger> triggers = new ArrayList<Trigger>();
    triggers.add(new Trigger(triggerId, LightsTriggerType.UID, null));
    // initialize the condition - here the tricky part is the referring into the condition input - trigger output.
    // The syntax is a similar to the JUEL syntax.
    Configuration config = new Configuration();
    config.put(StateConditionType.CONFIG_STATE, "on");
    List<Condition> conditions = new ArrayList<Condition>();
    Map<String, String> inputs = new HashMap<String, String>();
    inputs.put(StateConditionType.INPUT_CURRENT_STATE, triggerId + "." + StateConditionType.INPUT_CURRENT_STATE);
    conditions.add(new Condition("LightsStateCondition", StateConditionType.UID, config, inputs));
    // initialize the action - here the tricky part is the referring into the action configuration parameter - the
    // template configuration parameter. The syntax is a similar to the JUEL syntax.
    config = new Configuration();
    config.put(WelcomeHomeActionType.CONFIG_DEVICE, "Lights");
    config.put(WelcomeHomeActionType.CONFIG_RESULT, "Lights are switched on");
    List<Action> actions = new ArrayList<Action>();
    actions.add(new Action("LightsSwitchOnAction", WelcomeHomeActionType.UID, config, null));
    // initialize the configDescriptions
    List<ConfigDescriptionParameter> configDescriptions = new ArrayList<ConfigDescriptionParameter>();
    final ConfigDescriptionParameter device = ConfigDescriptionParameterBuilder.create(WelcomeHomeRulesProvider.CONFIG_UNIT, Type.TEXT).withRequired(true).withReadOnly(true).withMultiple(false).withLabel("Device").withDescription("Device description").build();
    final ConfigDescriptionParameter result = ConfigDescriptionParameterBuilder.create(WelcomeHomeRulesProvider.CONFIG_EXPECTED_RESULT, Type.TEXT).withRequired(true).withReadOnly(true).withMultiple(false).withLabel("Result").withDescription("Result description").build();
    configDescriptions.add(device);
    configDescriptions.add(result);
    // initialize the configuration
    config = new Configuration();
    config.put(CONFIG_UNIT, "Lights");
    config.put(CONFIG_EXPECTED_RESULT, "The lights are switched on.");
    // create the rule
    Rule lightsSwitchOn = new Rule(L_UID);
    lightsSwitchOn.setTriggers(triggers);
    lightsSwitchOn.setConfigurationDescriptions(configDescriptions);
    lightsSwitchOn.setConditions(conditions);
    lightsSwitchOn.setActions(actions);
    // initialize the tags
    Set<String> tags = new HashSet<String>();
    tags.add("lights");
    // set the tags
    lightsSwitchOn.setTags(tags);
    return lightsSwitchOn;
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) Action(org.eclipse.smarthome.automation.Action) Configuration(org.eclipse.smarthome.config.core.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Trigger(org.eclipse.smarthome.automation.Trigger) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) Rule(org.eclipse.smarthome.automation.Rule) HashSet(java.util.HashSet)

Example 13 with Condition

use of org.eclipse.smarthome.automation.Condition in project smarthome by eclipse.

the class ModuleTypeRegistryImpl method copyConditions.

private static List<Condition> copyConditions(List<Condition> conditions) {
    List<Condition> res = new ArrayList<Condition>(11);
    if (conditions != null) {
        for (Condition c : conditions) {
            Configuration conf = new Configuration();
            conf.setProperties(c.getConfiguration().getProperties());
            Condition condition = new Condition(c.getId(), c.getTypeUID(), conf, new HashMap<String, String>(c.getInputs()));
            condition.setLabel(condition.getLabel());
            condition.setDescription(condition.getDescription());
            res.add(condition);
        }
    }
    return res;
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) Configuration(org.eclipse.smarthome.config.core.Configuration) ArrayList(java.util.ArrayList)

Example 14 with Condition

use of org.eclipse.smarthome.automation.Condition in project smarthome by eclipse.

the class RuntimeRuleTest method compareConditionWorks.

@Test
public void compareConditionWorks() {
    final Configuration conditionConfig = newRightOperatorConfig("ON", "=");
    final Map<String, String> inputs = Stream.of(new SimpleEntry<>("input", "someTrigger.someoutput")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
    final Condition condition = new Condition("id", "core.GenericCompareCondition", conditionConfig, inputs);
    final CompareConditionHandler handler = new CompareConditionHandler(condition);
    assertSatisfiedHandlerInput(handler, true, OnOffType.ON);
    assertSatisfiedHandlerInput(handler, true, "ON");
    assertSatisfiedHandlerInput(handler, false, OnOffType.OFF);
    assertSatisfiedHandlerInput(handler, false, "OFF");
    condition.setConfiguration(newRightOperatorConfig("21", "="));
    assertSatisfiedHandlerInput(handler, true, 21);
    assertSatisfiedHandlerInput(handler, false, 22);
    condition.setConfiguration(newRightOperatorConfig("21", "<"));
    assertSatisfiedHandlerInput(handler, true, 20);
    assertSatisfiedHandlerInput(handler, false, 22);
    assertSatisfiedHandlerInput(handler, true, 20l);
    assertSatisfiedHandlerInput(handler, false, 22l);
    assertSatisfiedHandlerInput(handler, true, 20.9d);
    assertSatisfiedHandlerInput(handler, false, 21.1d);
    condition.setConfiguration(newRightOperatorConfig("21", "<="));
    assertSatisfiedHandlerInput(handler, true, 20);
    assertSatisfiedHandlerInput(handler, true, 21);
    assertSatisfiedHandlerInput(handler, false, 22);
    assertSatisfiedHandlerInput(handler, true, 20l);
    assertSatisfiedHandlerInput(handler, true, 21l);
    assertSatisfiedHandlerInput(handler, false, 22l);
    assertSatisfiedHandlerInput(handler, true, 20.9d);
    assertSatisfiedHandlerInput(handler, true, 21.0d);
    assertSatisfiedHandlerInput(handler, false, 21.1d);
    condition.setConfiguration(newRightOperatorConfig("21", "<"));
    assertSatisfiedHandlerInput(handler, true, 20);
    assertSatisfiedHandlerInput(handler, false, 22);
    assertSatisfiedHandlerInput(handler, true, 20l);
    assertSatisfiedHandlerInput(handler, false, 22l);
    assertSatisfiedHandlerInput(handler, true, 20.9d);
    assertSatisfiedHandlerInput(handler, false, 21.1d);
    condition.setConfiguration(newRightOperatorConfig("21", "<="));
    assertSatisfiedHandlerInput(handler, true, 20);
    assertSatisfiedHandlerInput(handler, true, 21);
    assertSatisfiedHandlerInput(handler, false, 22);
    assertSatisfiedHandlerInput(handler, true, 20l);
    assertSatisfiedHandlerInput(handler, true, 21l);
    assertSatisfiedHandlerInput(handler, false, 22l);
    assertSatisfiedHandlerInput(handler, true, 20.9d);
    assertSatisfiedHandlerInput(handler, true, 21.0d);
    assertSatisfiedHandlerInput(handler, false, 21.1d);
    condition.setConfiguration(newRightOperatorConfig(".*anything.*", "matches"));
    assertSatisfiedHandlerInput(handler, false, "something matches?");
    assertSatisfiedHandlerInput(handler, true, "anything matches?");
    Assert.assertFalse(handler.isSatisfied(Stream.of(new SimpleEntry<>("nothing", "nothing")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()))));
    condition.setConfiguration(newRightOperatorConfig("ONOFF", "matches"));
    assertSatisfiedHandlerInput(handler, false, OnOffType.ON);
    final Event event = ItemEventFactory.createStateEvent("itemName", OnOffType.OFF, "source");
    condition.setConfiguration(newRightOperatorInputPropertyConfig(".*ON.*", "matches", "itemName"));
    assertSatisfiedHandlerInput(handler, false, event);
    condition.setConfiguration(newRightOperatorInputPropertyConfig("itemName", "matches", "itemName"));
    assertSatisfiedHandlerInput(handler, true, event);
    condition.setConfiguration(newRightOperatorConfig("null", "="));
    assertSatisfiedHandlerInput(handler, true, null);
    condition.setConfiguration(newRightOperatorConfig("notnull", "="));
    assertSatisfiedHandlerInput(handler, false, null);
    condition.setConfiguration(newRightOperatorConfig("ON", "<"));
    assertSatisfiedHandlerInput(handler, false, OnOffType.ON);
    condition.setConfiguration(newRightOperatorInputPropertyConfig("ON", "<", "nothing"));
    assertSatisfiedHandlerInput(handler, false, event);
    condition.setConfiguration(newRightOperatorInputPropertyConfig("ON", "=", "nothing"));
    assertSatisfiedHandlerInput(handler, true, "ON");
}
Also used : Trigger(org.eclipse.smarthome.automation.Trigger) ModuleTypeRegistry(org.eclipse.smarthome.automation.type.ModuleTypeRegistry) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Arrays(java.util.Arrays) Condition(org.eclipse.smarthome.automation.Condition) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) TypeParser(org.eclipse.smarthome.core.types.TypeParser) LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) Random(java.util.Random) ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) Map(java.util.Map) LinkedList(java.util.LinkedList) SimpleEntry(java.util.AbstractMap.SimpleEntry) Configuration(org.eclipse.smarthome.config.core.Configuration) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) CompareConditionHandler(org.eclipse.smarthome.automation.module.core.handler.CompareConditionHandler) Before(org.junit.Before) RuleStatus(org.eclipse.smarthome.automation.RuleStatus) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) VolatileStorageService(org.eclipse.smarthome.test.storage.VolatileStorageService) Logger(org.slf4j.Logger) ItemEventFactory(org.eclipse.smarthome.core.items.events.ItemEventFactory) Collection(java.util.Collection) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) Test(org.junit.Test) Rule(org.eclipse.smarthome.automation.Rule) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Stream(java.util.stream.Stream) Ignore(org.junit.Ignore) Action(org.eclipse.smarthome.automation.Action) Queue(java.util.Queue) Assert(org.junit.Assert) Event(org.eclipse.smarthome.core.events.Event) Collections(java.util.Collections) Condition(org.eclipse.smarthome.automation.Condition) Configuration(org.eclipse.smarthome.config.core.Configuration) SimpleEntry(java.util.AbstractMap.SimpleEntry) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Event(org.eclipse.smarthome.core.events.Event) CompareConditionHandler(org.eclipse.smarthome.automation.module.core.handler.CompareConditionHandler) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 15 with Condition

use of org.eclipse.smarthome.automation.Condition in project smarthome by eclipse.

the class CoreModuleHandlerFactory method internalCreate.

@Override
protected synchronized ModuleHandler internalCreate(final Module module, final String ruleUID) {
    logger.trace("create {} -> {} : {}", module.getId(), module.getTypeUID(), ruleUID);
    final String moduleTypeUID = module.getTypeUID();
    if (module instanceof Trigger) {
        if (GenericEventTriggerHandler.MODULE_TYPE_ID.equals(moduleTypeUID)) {
            return new GenericEventTriggerHandler((Trigger) module, this.bundleContext);
        } else if (ChannelEventTriggerHandler.MODULE_TYPE_ID.equals(moduleTypeUID)) {
            return new ChannelEventTriggerHandler((Trigger) module, this.bundleContext);
        } else if (ItemCommandTriggerHandler.MODULE_TYPE_ID.equals(moduleTypeUID)) {
            return new ItemCommandTriggerHandler((Trigger) module, this.bundleContext);
        } else if (ItemStateTriggerHandler.CHANGE_MODULE_TYPE_ID.equals(moduleTypeUID) || ItemStateTriggerHandler.UPDATE_MODULE_TYPE_ID.equals(moduleTypeUID)) {
            return new ItemStateTriggerHandler((Trigger) module, this.bundleContext);
        }
    } else if (module instanceof Condition) {
        // Handle conditions
        if (ItemStateConditionHandler.ITEM_STATE_CONDITION.equals(moduleTypeUID)) {
            ItemStateConditionHandler handler = new ItemStateConditionHandler((Condition) module);
            handler.setItemRegistry(itemRegistry);
            return handler;
        } else if (GenericEventConditionHandler.MODULETYPE_ID.equals(moduleTypeUID)) {
            return new GenericEventConditionHandler((Condition) module);
        } else if (CompareConditionHandler.MODULE_TYPE.equals(moduleTypeUID)) {
            return new CompareConditionHandler((Condition) module);
        }
    } else if (module instanceof Action) {
        if (ItemCommandActionHandler.ITEM_COMMAND_ACTION.equals(moduleTypeUID)) {
            final ItemCommandActionHandler postCommandActionHandler = new ItemCommandActionHandler((Action) module);
            postCommandActionHandler.setEventPublisher(eventPublisher);
            postCommandActionHandler.setItemRegistry(itemRegistry);
            return postCommandActionHandler;
        } else if (RuleEnablementActionHandler.UID.equals(moduleTypeUID)) {
            return new RuleEnablementActionHandler((Action) module, ruleRegistry);
        } else if (RunRuleActionHandler.UID.equals(moduleTypeUID)) {
            return new RunRuleActionHandler((Action) module, ruleRegistry);
        }
    }
    logger.error("The ModuleHandler is not supported:{}", moduleTypeUID);
    return null;
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) GenericEventConditionHandler(org.eclipse.smarthome.automation.module.core.handler.GenericEventConditionHandler) Action(org.eclipse.smarthome.automation.Action) ItemStateTriggerHandler(org.eclipse.smarthome.automation.module.core.handler.ItemStateTriggerHandler) ItemCommandTriggerHandler(org.eclipse.smarthome.automation.module.core.handler.ItemCommandTriggerHandler) ItemCommandActionHandler(org.eclipse.smarthome.automation.module.core.handler.ItemCommandActionHandler) ChannelEventTriggerHandler(org.eclipse.smarthome.automation.module.core.handler.ChannelEventTriggerHandler) RuleEnablementActionHandler(org.eclipse.smarthome.automation.module.core.handler.RuleEnablementActionHandler) Trigger(org.eclipse.smarthome.automation.Trigger) GenericEventTriggerHandler(org.eclipse.smarthome.automation.module.core.handler.GenericEventTriggerHandler) ItemStateConditionHandler(org.eclipse.smarthome.automation.module.core.handler.ItemStateConditionHandler) CompareConditionHandler(org.eclipse.smarthome.automation.module.core.handler.CompareConditionHandler) RunRuleActionHandler(org.eclipse.smarthome.automation.module.core.handler.RunRuleActionHandler)

Aggregations

Condition (org.eclipse.smarthome.automation.Condition)21 Action (org.eclipse.smarthome.automation.Action)10 Configuration (org.eclipse.smarthome.config.core.Configuration)10 Trigger (org.eclipse.smarthome.automation.Trigger)9 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)4 Rule (org.eclipse.smarthome.automation.Rule)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 ConfigDescriptionParameter (org.eclipse.smarthome.config.core.ConfigDescriptionParameter)3 Set (java.util.Set)2 Module (org.eclipse.smarthome.automation.Module)2 RuleStatus (org.eclipse.smarthome.automation.RuleStatus)2 ConditionHandler (org.eclipse.smarthome.automation.handler.ConditionHandler)2 CompareConditionHandler (org.eclipse.smarthome.automation.module.core.handler.CompareConditionHandler)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1