Search in sources :

Example 6 with Trigger

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

the class CompositeTriggerHandler method setRuleEngineCallback.

/**
 * The {@link CompositeTriggerHandler} sets itself as callback to the child triggers and store the callback to the
 * rule engine. In this way the trigger of composite type will be notified always when some of the child triggers
 * are triggered and has an opportunity to set the outputs of parent trigger to the rule context.
 *
 * @see org.eclipse.smarthome.automation.handler.TriggerHandler#setRuleEngineCallback(org.eclipse.smarthome.automation.handler.RuleEngineCallback)
 */
@Override
public void setRuleEngineCallback(RuleEngineCallback ruleCallback) {
    this.ruleCallback = ruleCallback;
    if (ruleCallback != null) {
        // could be called with 'null' from dispose
        List<Trigger> children = getChildren();
        for (Trigger child : children) {
            TriggerHandler handler = moduleHandlerMap.get(child);
            handler.setRuleEngineCallback(this);
        }
    }
}
Also used : Trigger(org.eclipse.smarthome.automation.Trigger) TriggerHandler(org.eclipse.smarthome.automation.handler.TriggerHandler)

Example 7 with Trigger

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

the class RuleTemplateRegistry method copyTriggers.

private List<Trigger> copyTriggers(List<Trigger> triggers) {
    List<Trigger> res = new ArrayList<Trigger>(11);
    if (triggers != null) {
        for (Trigger t : triggers) {
            Configuration c = new Configuration();
            c.setProperties(t.getConfiguration().getProperties());
            Trigger trigger = new Trigger(t.getId(), t.getTypeUID(), c);
            trigger.setLabel(t.getLabel());
            trigger.setDescription(t.getDescription());
            res.add(trigger);
        }
    }
    return res;
}
Also used : Trigger(org.eclipse.smarthome.automation.Trigger) Configuration(org.eclipse.smarthome.config.core.Configuration) ArrayList(java.util.ArrayList)

Example 8 with Trigger

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

the class ModuleTypeRegistryImpl method copyTriggers.

private static List<Trigger> copyTriggers(List<Trigger> triggers) {
    List<Trigger> res = new ArrayList<Trigger>(11);
    if (triggers != null) {
        for (Trigger t : triggers) {
            Configuration c = new Configuration();
            c.setProperties(t.getConfiguration().getProperties());
            Trigger trigger = new Trigger(t.getId(), t.getTypeUID(), c);
            trigger.setLabel(trigger.getLabel());
            trigger.setDescription(trigger.getDescription());
            res.add(trigger);
        }
    }
    return res;
}
Also used : Trigger(org.eclipse.smarthome.automation.Trigger) Configuration(org.eclipse.smarthome.config.core.Configuration) ArrayList(java.util.ArrayList)

Example 9 with Trigger

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

the class ConnectionValidator method checkConnection.

/**
 * The method validates the connection between outputs of list of triggers to the action's or condition's input. It
 * checks if the input is unconnected and compatibility of data types of the input and connected output. Throws
 * exception if they are incompatible.
 *
 * @param connection that should be validated
 * @param input that should be validated
 * @param triggers is a list with triggers of the rule on which the action belongs
 * @throws IllegalArgumentException when validation fails.
 */
private static void checkConnection(Connection connection, Input input, List<Trigger> triggers) {
    Map<String, Trigger> triggersMap = new HashMap<String, Trigger>();
    for (Trigger trigger : triggers) {
        triggersMap.put(trigger.getId(), trigger);
    }
    String moduleId = connection.getOuputModuleId();
    String msg = " Invalid Connection \"" + connection.getInputName() + "\" : ";
    if (moduleId != null) {
        Trigger trigger = triggersMap.get(moduleId);
        if (trigger == null) {
            throw new IllegalArgumentException(msg + " Trigger with ID \"" + moduleId + "\" does not exist!");
        }
        String triggerTypeUID = trigger.getTypeUID();
        TriggerType triggerType = (TriggerType) mtRegistry.get(triggerTypeUID);
        if (triggerType == null) {
            throw new IllegalArgumentException(msg + " Trigger Type with UID \"" + triggerTypeUID + "\" does not exist!");
        }
        checkCompatibility(msg, connection, input, triggerType.getOutputs());
    }
}
Also used : TriggerType(org.eclipse.smarthome.automation.type.TriggerType) Trigger(org.eclipse.smarthome.automation.Trigger) HashMap(java.util.HashMap)

Example 10 with Trigger

use of org.eclipse.smarthome.automation.Trigger 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)

Aggregations

Trigger (org.eclipse.smarthome.automation.Trigger)27 Action (org.eclipse.smarthome.automation.Action)14 Configuration (org.eclipse.smarthome.config.core.Configuration)14 ArrayList (java.util.ArrayList)8 Condition (org.eclipse.smarthome.automation.Condition)8 Rule (org.eclipse.smarthome.automation.Rule)8 Test (org.junit.Test)6 HashMap (java.util.HashMap)4 Set (java.util.Set)4 RuleRegistry (org.eclipse.smarthome.automation.RuleRegistry)4 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)4 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)4 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)3 TriggerHandler (org.eclipse.smarthome.automation.handler.TriggerHandler)3 TriggerType (org.eclipse.smarthome.automation.type.TriggerType)3 ConfigDescriptionParameter (org.eclipse.smarthome.config.core.ConfigDescriptionParameter)3 Event (org.eclipse.smarthome.core.events.Event)3 EventFilter (org.eclipse.smarthome.core.events.EventFilter)3 EventSubscriber (org.eclipse.smarthome.core.events.EventSubscriber)3