Search in sources :

Example 16 with Rule

use of org.eclipse.smarthome.model.rule.rules.Rule in project smarthome by eclipse.

the class RuleTriggerManager method removeRules.

private void removeRules(TriggerTypes type, Collection<? extends Collection<Rule>> ruleSets, RuleModel model) {
    for (Collection<Rule> ruleSet : ruleSets) {
        Set<Rule> clonedSet = new HashSet<Rule>(ruleSet);
        // first remove all rules of the model, if not null (=non-existent)
        if (model != null) {
            for (Rule newRule : model.getRules()) {
                for (Rule oldRule : clonedSet) {
                    if (newRule.getName().equals(oldRule.getName())) {
                        ruleSet.remove(oldRule);
                        if (type == TIMER) {
                            removeTimerRule(oldRule);
                        }
                    }
                }
            }
        }
        // now also remove all proxified rules from the set
        clonedSet = new HashSet<Rule>(ruleSet);
        for (Rule rule : clonedSet) {
            if (rule.eResource() == null) {
                ruleSet.remove(rule);
                if (type == TIMER) {
                    removeTimerRule(rule);
                }
            }
        }
    }
}
Also used : Rule(org.eclipse.smarthome.model.rule.rules.Rule) HashSet(java.util.HashSet)

Example 17 with Rule

use of org.eclipse.smarthome.model.rule.rules.Rule in project smarthome by eclipse.

the class RuleTriggerManager method internalGetChangeRules.

private void internalGetChangeRules(String name, Boolean isGroup, List<Class<? extends State>> acceptedDataTypes, State newState, State oldState, List<Rule> result) {
    final String mapName = (isGroup) ? GROUP_NAME_PREFIX + name : name;
    for (Rule rule : getAllRules(CHANGE, mapName)) {
        for (EventTrigger t : rule.getEventtrigger()) {
            String triggerOldStateString = null;
            String triggerNewStateString = null;
            if ((!isGroup) && (t instanceof ChangedEventTrigger)) {
                final ChangedEventTrigger ct = (ChangedEventTrigger) t;
                if (ct.getItem().equals(name)) {
                    triggerOldStateString = ct.getOldState();
                    triggerNewStateString = ct.getNewState();
                } else {
                    continue;
                }
            } else if ((isGroup) && (t instanceof GroupMemberChangedEventTrigger)) {
                final GroupMemberChangedEventTrigger gmct = (GroupMemberChangedEventTrigger) t;
                if (gmct.getGroup().equals(name)) {
                    triggerOldStateString = gmct.getOldState();
                    triggerNewStateString = gmct.getNewState();
                } else {
                    continue;
                }
            } else {
                continue;
            }
            if (triggerOldStateString != null) {
                final State triggerOldState = TypeParser.parseState(acceptedDataTypes, triggerOldStateString);
                if (!oldState.equals(triggerOldState)) {
                    continue;
                }
            }
            if (triggerNewStateString != null) {
                final State triggerNewState = TypeParser.parseState(acceptedDataTypes, triggerNewStateString);
                if (!newState.equals(triggerNewState)) {
                    continue;
                }
            }
            result.add(rule);
        }
    }
}
Also used : State(org.eclipse.smarthome.core.types.State) Rule(org.eclipse.smarthome.model.rule.rules.Rule) ThingStateChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateChangedEventTrigger) ChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ChangedEventTrigger) GroupMemberChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger) GroupMemberChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger) UpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.UpdateEventTrigger) ThingStateChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateChangedEventTrigger) ThingStateUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateUpdateEventTrigger) EventTrigger(org.eclipse.smarthome.model.rule.rules.EventTrigger) ChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ChangedEventTrigger) GroupMemberUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberUpdateEventTrigger) GroupMemberChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger) GroupMemberCommandEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberCommandEventTrigger) CommandEventTrigger(org.eclipse.smarthome.model.rule.rules.CommandEventTrigger)

Example 18 with Rule

use of org.eclipse.smarthome.model.rule.rules.Rule in project smarthome by eclipse.

the class ExecuteRuleJob method execute.

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    String modelName = (String) context.getJobDetail().getJobDataMap().get(JOB_DATA_RULEMODEL);
    String ruleName = (String) context.getJobDetail().getJobDataMap().get(JOB_DATA_RULENAME);
    if (modelRepository != null && scriptEngine != null) {
        EObject model = modelRepository.getModel(modelName);
        if (model instanceof RuleModel) {
            RuleModel ruleModel = (RuleModel) model;
            Rule rule = getRule(ruleModel, ruleName);
            if (rule != null) {
                Script script = scriptEngine.newScriptFromXExpression(rule.getScript());
                logger.debug("Executing scheduled rule '{}'", rule.getName());
                try {
                    script.execute(RuleContextHelper.getContext(rule, injector));
                } catch (ScriptExecutionException e) {
                    logger.error("Error during the execution of rule '{}': {}", rule.getName(), e.getMessage());
                }
            } else {
                logger.debug("Scheduled rule '{}' does not exist", ruleName);
            }
        } else {
            logger.debug("Rule file '{}' does not exist", modelName);
        }
    }
}
Also used : Script(org.eclipse.smarthome.model.script.engine.Script) ScriptExecutionException(org.eclipse.smarthome.model.script.engine.ScriptExecutionException) EObject(org.eclipse.emf.ecore.EObject) Rule(org.eclipse.smarthome.model.rule.rules.Rule) RuleModel(org.eclipse.smarthome.model.rule.rules.RuleModel)

Aggregations

Rule (org.eclipse.smarthome.model.rule.rules.Rule)18 ChangedEventTrigger (org.eclipse.smarthome.model.rule.rules.ChangedEventTrigger)5 CommandEventTrigger (org.eclipse.smarthome.model.rule.rules.CommandEventTrigger)5 EventTrigger (org.eclipse.smarthome.model.rule.rules.EventTrigger)5 GroupMemberChangedEventTrigger (org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger)5 GroupMemberCommandEventTrigger (org.eclipse.smarthome.model.rule.rules.GroupMemberCommandEventTrigger)5 GroupMemberUpdateEventTrigger (org.eclipse.smarthome.model.rule.rules.GroupMemberUpdateEventTrigger)5 ThingStateChangedEventTrigger (org.eclipse.smarthome.model.rule.rules.ThingStateChangedEventTrigger)5 ThingStateUpdateEventTrigger (org.eclipse.smarthome.model.rule.rules.ThingStateUpdateEventTrigger)5 UpdateEventTrigger (org.eclipse.smarthome.model.rule.rules.UpdateEventTrigger)5 ArrayList (java.util.ArrayList)3 Command (org.eclipse.smarthome.core.types.Command)3 State (org.eclipse.smarthome.core.types.State)3 HashSet (java.util.HashSet)2 ThingStatus (org.eclipse.smarthome.core.thing.ThingStatus)2 Script (org.eclipse.smarthome.model.script.engine.Script)2 ScriptExecutionException (org.eclipse.smarthome.model.script.engine.ScriptExecutionException)2 Collection (java.util.Collection)1 Set (java.util.Set)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1