Search in sources :

Example 6 with RuleStatusInfo

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

the class RuleEngine method updateRule.

/**
 * This method is used to update existing rule. It creates an internal {@link RuntimeRule} object which is deep copy
 * of passed {@link Rule} object. If the rule exist in the rule engine it will be replaced by the new one.
 *
 * @param rule a rule which has to be updated.
 * @param enabled specifies the rule to be updated as disabled or not.
 */
protected void updateRule(Rule rule, boolean isEnabled) {
    String rUID = rule.getUID();
    if (getRuntimeRule(rUID) == null) {
        logger.debug("There is no rule with UID '{}' which could be updated", rUID);
        return;
    }
    RuntimeRule runtimeRule = new RuntimeRule(rule);
    synchronized (this) {
        RuntimeRule oldRule = rules.get(rUID);
        unregister(oldRule);
        rules.put(rUID, runtimeRule);
        if (isEnabled) {
            setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.UNINITIALIZED), false);
            setRule(runtimeRule);
        } else {
            setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.DISABLED), true);
        }
    }
    logger.debug("Rule with UID '{}' is updated.", rUID);
}
Also used : RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo)

Example 7 with RuleStatusInfo

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

the class RuleEngine method setRule.

/**
 * This method tries to initialize the rule. It uses available {@link ModuleHandlerFactory}s to create
 * {@link ModuleHandler}s for all {@link Module}s of the {@link Rule} and to link them. When all the modules have
 * associated module handlers then the {@link Rule} is initialized and it is ready to working. It goes into idle
 * state. Otherwise the Rule stays into not initialized and continue to wait missing handlers, module types or
 * templates.
 *
 * @param rUID a UID of rule which tries to be initialized.
 */
private void setRule(RuntimeRule runtimeRule) {
    if (isDisposed) {
        return;
    }
    String rUID = runtimeRule.getUID();
    setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.INITIALIZING), true);
    if (runtimeRule.getTemplateUID() != null) {
        setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.UNINITIALIZED, RuleStatusDetail.TEMPLATE_MISSING_ERROR), true);
        // Template is not available (when a template is resolved it removes tempalteUID configuration
        return;
    // property). The rule must stay NOT_INITIALISED.
    }
    List<Module> modules = runtimeRule.getModules(Module.class);
    for (Module m : modules) {
        updateMapModuleTypeToRule(rUID, m.getTypeUID());
    }
    String errMsgs;
    try {
        validateModuleIDs(modules);
        resolveConfiguration(runtimeRule);
        autoMapConnections(runtimeRule);
        ConnectionValidator.validateConnections(runtimeRule);
    } catch (RuntimeException e) {
        errMsgs = "\n Validation of rule " + rUID + " has failed! " + e.getLocalizedMessage();
        // change state to NOTINITIALIZED
        setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.UNINITIALIZED, RuleStatusDetail.CONFIGURATION_ERROR, errMsgs.trim()), true);
        return;
    }
    errMsgs = setModuleHandlers(rUID, modules);
    if (errMsgs == null) {
        register(runtimeRule);
        // change state to IDLE
        setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.IDLE), true);
        Future f = scheduleTasks.remove(rUID);
        if (f != null) {
            if (!f.isDone()) {
                f.cancel(true);
            }
        }
        if (scheduleTasks.isEmpty()) {
            if (executor != null) {
                executor.shutdown();
                executor = null;
            }
        }
    } else {
        // change state to NOTINITIALIZED
        setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.UNINITIALIZED, RuleStatusDetail.HANDLER_INITIALIZING_ERROR, errMsgs), true);
        unregister(runtimeRule);
    }
}
Also used : RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo) Future(java.util.concurrent.Future) Module(org.eclipse.smarthome.automation.Module)

Example 8 with RuleStatusInfo

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

the class RuleEngine method setRuleEnabled.

/**
 * This method can switch enabled/ disabled state of the {@link Rule}
 *
 * @param rUID unique id of the rule
 * @param isEnabled true to enable the rule, false to disable it
 */
protected void setRuleEnabled(Rule rule, boolean isEnabled) {
    String rUID = rule.getUID();
    RuleStatus status = getRuleStatus(rUID);
    String enabled = isEnabled ? "enabled" : "disabled";
    RuntimeRule runtimeRule = getRuntimeRule(rUID);
    if (runtimeRule == null) {
        logger.debug("There is no rule with UID '{}' which could be {}", rUID, enabled);
        return;
    }
    if (isEnabled) {
        if (status == RuleStatus.DISABLED) {
            setRule(runtimeRule);
        } else {
            logger.debug("The rule rId = {} is already enabled.", rUID);
        }
    } else {
        unregister(runtimeRule);
        setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.DISABLED), true);
    }
}
Also used : RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo) RuleStatus(org.eclipse.smarthome.automation.RuleStatus)

Example 9 with RuleStatusInfo

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

the class RuleEngine method addRule.

/**
 * This method add a new rule into rule engine. Scope identity of the Rule is the identity of the caller.
 *
 * @param rule a rule which has to be added.
 * @param isEnabled specifies the rule to be added as disabled or not.
 */
protected void addRule(Rule rule, boolean isEnabled) {
    String rUID = rule.getUID();
    RuntimeRule runtimeRule = new RuntimeRule(rule);
    synchronized (this) {
        rules.put(rUID, runtimeRule);
        if (isEnabled) {
            setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.UNINITIALIZED), false);
            setRule(runtimeRule);
        } else {
            setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.DISABLED), true);
        }
    }
}
Also used : RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo)

Aggregations

RuleStatusInfo (org.eclipse.smarthome.automation.RuleStatusInfo)9 RuleStatus (org.eclipse.smarthome.automation.RuleStatus)5 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Future (java.util.concurrent.Future)1 Module (org.eclipse.smarthome.automation.Module)1