Search in sources :

Example 1 with RuleStatus

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

the class RuleEngine method removeMissingModuleTypes.

private void removeMissingModuleTypes(Collection<String> moduleTypes) {
    Map<String, List<String>> mapMissingHandlers = null;
    for (Iterator<String> it = moduleTypes.iterator(); it.hasNext(); ) {
        String moduleTypeName = it.next();
        Set<String> rules = null;
        synchronized (this) {
            rules = mapModuleTypeToRules.get(moduleTypeName);
        }
        if (rules != null) {
            for (String rUID : rules) {
                RuleStatus ruleStatus = getRuleStatus(rUID);
                switch(ruleStatus) {
                    case RUNNING:
                    case IDLE:
                        mapMissingHandlers = mapMissingHandlers != null ? mapMissingHandlers : new HashMap<String, List<String>>(20);
                        List<String> list = mapMissingHandlers.get(rUID);
                        if (list == null) {
                            list = new ArrayList<String>(5);
                        }
                        list.add(moduleTypeName);
                        mapMissingHandlers.put(rUID, list);
                        break;
                    default:
                        break;
                }
            }
        }
    }
    // for
    if (mapMissingHandlers != null) {
        for (Entry<String, List<String>> e : mapMissingHandlers.entrySet()) {
            String rUID = e.getKey();
            List<String> missingTypes = e.getValue();
            StringBuffer sb = new StringBuffer();
            sb.append("Missing handlers: ");
            for (String typeUID : missingTypes) {
                sb.append(typeUID).append(", ");
            }
            setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.UNINITIALIZED, RuleStatusDetail.HANDLER_MISSING_ERROR, sb.substring(0, sb.length() - 2)), true);
            unregister(getRuntimeRule(rUID));
        }
    }
}
Also used : RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo) HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) RuleStatus(org.eclipse.smarthome.automation.RuleStatus)

Example 2 with RuleStatus

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

the class RuleEngine method getRuleStatus.

/**
 * This method gets rule's status object.
 *
 * @param rUID rule uid
 * @return status of the rule or null when such rule does not exists.
 */
protected RuleStatus getRuleStatus(String rUID) {
    RuleStatusInfo info = getRuleStatusInfo(rUID);
    RuleStatus status = null;
    if (info != null) {
        status = info.getStatus();
    }
    return status;
}
Also used : RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo) RuleStatus(org.eclipse.smarthome.automation.RuleStatus)

Example 3 with RuleStatus

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

the class RuleEngine method calculateConditions.

/**
 * This method checks if all rule's condition are satisfied or not.
 *
 * @param rule the checked rule
 * @return true when all conditions of the rule are satisfied, false otherwise.
 */
private boolean calculateConditions(Rule rule) {
    List<Condition> conditions = ((RuntimeRule) rule).getConditions();
    if (conditions.size() == 0) {
        return true;
    }
    RuleStatus ruleStatus = null;
    for (Iterator<Condition> it = conditions.iterator(); it.hasNext(); ) {
        ruleStatus = getRuleStatus(rule.getUID());
        if (ruleStatus != RuleStatus.RUNNING) {
            return false;
        }
        RuntimeCondition c = (RuntimeCondition) it.next();
        ConditionHandler tHandler = c.getModuleHandler();
        Map<String, Object> context = getContext(rule.getUID(), c.getConnections());
        if (!tHandler.isSatisfied(Collections.unmodifiableMap(context))) {
            logger.debug("The condition '{}' of rule '{}' is unsatisfied.", new Object[] { c.getId(), rule.getUID() });
            return false;
        }
    }
    return true;
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) ConditionHandler(org.eclipse.smarthome.automation.handler.ConditionHandler) RuleStatus(org.eclipse.smarthome.automation.RuleStatus)

Example 4 with RuleStatus

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

the class RuleEngine method runRule.

/**
 * This method runs a {@link Rule}. It is called by the {@link RuleEngineCallback}'s thread when a new
 * {@link TriggerData} is available. This method switches
 *
 * @param rule the {@link Rule} which has to evaluate new {@link TriggerData}.
 * @param td {@link TriggerData} object containing new values for {@link Trigger}'s {@link Output}s
 */
protected void runRule(RuntimeRule rule, RuleEngineCallbackImpl.TriggerData td) {
    String rUID = rule.getUID();
    if (reCallbacks.get(rUID) == null) {
        // the rule was unregistered
        return;
    }
    synchronized (this) {
        final RuleStatus ruleStatus = getRuleStatus(rUID);
        if (ruleStatus != RuleStatus.IDLE) {
            logger.error("Failed to execute rule ‘{}' with status '{}'", rUID, ruleStatus.name());
            return;
        }
        // change state to RUNNING
        setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.RUNNING), true);
    }
    try {
        clearContext(rule);
        setTriggerOutputs(rUID, td);
        boolean isSatisfied = calculateConditions(rule);
        if (isSatisfied) {
            executeActions(rule, true);
            logger.debug("The rule '{}' is executed.", rUID);
        } else {
            logger.debug("The rule '{}' is NOT executed, since it has unsatisfied conditions.", rUID);
        }
    } catch (Throwable t) {
        logger.error("Failed to execute rule '{}': {}", rUID, t.getMessage());
        logger.debug("", t);
    }
    // change state to IDLE only if the rule has not been DISABLED.
    synchronized (this) {
        if (getRuleStatus(rUID) == RuleStatus.RUNNING) {
            setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.IDLE), true);
        }
    }
}
Also used : RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo) RuleStatus(org.eclipse.smarthome.automation.RuleStatus)

Example 5 with RuleStatus

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

the class RuleEngine method runNow.

protected void runNow(String ruleUID, boolean considerConditions, Map<String, Object> context) {
    RuntimeRule rule = getRuntimeRule(ruleUID);
    if (rule == null) {
        logger.warn("Failed to execute rule '{}': Invalid Rule UID", ruleUID);
        return;
    }
    synchronized (this) {
        final RuleStatus ruleStatus = getRuleStatus(ruleUID);
        if (ruleStatus != RuleStatus.IDLE) {
            logger.error("Failed to execute rule ‘{}' with status '{}'", ruleUID, ruleStatus.name());
            return;
        }
        // change state to RUNNING
        setRuleStatusInfo(ruleUID, new RuleStatusInfo(RuleStatus.RUNNING), true);
    }
    try {
        clearContext(rule);
        if (context != null && !context.isEmpty()) {
            getContext(ruleUID).putAll(context);
        }
        if (considerConditions) {
            if (calculateConditions(rule)) {
                executeActions(rule, false);
            }
        } else {
            executeActions(rule, false);
        }
        logger.debug("The rule '{}' is executed.", ruleUID);
    } catch (Throwable t) {
        logger.error("Failed to execute rule '{}': ", ruleUID, t);
    }
    // change state to IDLE only if the rule has not been DISABLED.
    synchronized (this) {
        if (getRuleStatus(ruleUID) == RuleStatus.RUNNING) {
            setRuleStatusInfo(ruleUID, new RuleStatusInfo(RuleStatus.IDLE), true);
        }
    }
}
Also used : RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo) RuleStatus(org.eclipse.smarthome.automation.RuleStatus)

Aggregations

RuleStatus (org.eclipse.smarthome.automation.RuleStatus)10 RuleStatusInfo (org.eclipse.smarthome.automation.RuleStatusInfo)5 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Action (org.eclipse.smarthome.automation.Action)1 Condition (org.eclipse.smarthome.automation.Condition)1 Rule (org.eclipse.smarthome.automation.Rule)1 CompositeModuleHandlerFactory (org.eclipse.smarthome.automation.core.internal.composite.CompositeModuleHandlerFactory)1 ActionHandler (org.eclipse.smarthome.automation.handler.ActionHandler)1 ConditionHandler (org.eclipse.smarthome.automation.handler.ConditionHandler)1 ModuleHandlerFactory (org.eclipse.smarthome.automation.handler.ModuleHandlerFactory)1