use of org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule in project smarthome by eclipse.
the class RuleEngineImpl method setEnabled.
@Override
public synchronized void setEnabled(String uid, boolean enable) {
final WrappedRule rule = managedRules.get(uid);
if (rule == null) {
throw new IllegalArgumentException(String.format("No rule with id=%s was found!", uid));
}
if (enable) {
if (disabledRulesStorage != null) {
disabledRulesStorage.remove(uid);
}
if (getStatus(rule.getUID()) == RuleStatus.UNINITIALIZED) {
register(rule);
// change status to IDLE
setStatus(rule.getUID(), new RuleStatusInfo(RuleStatus.IDLE));
}
} else {
if (disabledRulesStorage != null) {
disabledRulesStorage.put(uid, true);
}
unregister(rule, RuleStatusDetail.DISABLED, null);
}
}
use of org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule in project smarthome by eclipse.
the class RuleEngineImpl 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.
*/
protected void addRule(Rule newRule) {
synchronized (this) {
if (isDisposed) {
throw new IllegalStateException("RuleEngineImpl is disposed!");
}
}
final String rUID = newRule.getUID();
final WrappedRule rule = new WrappedRule(newRule);
managedRules.put(rUID, rule);
RuleStatusInfo initStatusInfo = disabledRulesStorage == null || disabledRulesStorage.get(rUID) == null ? new RuleStatusInfo(RuleStatus.INITIALIZING) : new RuleStatusInfo(RuleStatus.UNINITIALIZED, RuleStatusDetail.DISABLED);
rule.setStatusInfo(initStatusInfo);
WrappedRule oldRule = getManagedRule(rUID);
if (oldRule != null) {
unregister(oldRule);
}
if (isEnabled(rUID)) {
setRule(rule);
}
}
use of org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule in project smarthome by eclipse.
the class RuleEngineImpl method runNow.
@Override
public void runNow(String ruleUID, boolean considerConditions, @Nullable Map<String, Object> context) {
final WrappedRule rule = getManagedRule(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
setStatus(ruleUID, new RuleStatusInfo(RuleStatus.RUNNING));
}
try {
clearContext(ruleUID);
if (context != null && !context.isEmpty()) {
getContext(ruleUID, null).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) {
setStatus(ruleUID, new RuleStatusInfo(RuleStatus.IDLE));
}
}
}
use of org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule in project smarthome by eclipse.
the class RuleEngineImpl method setStatus.
/**
* This method updates the status of the {@link Rule}
*
* @param ruleUID unique id of the rule
* @param newStatusInfo the new status of the rule
*/
private void setStatus(String ruleUID, RuleStatusInfo newStatusInfo) {
final WrappedRule rule = managedRules.get(ruleUID);
if (rule == null) {
return;
}
rule.setStatusInfo(newStatusInfo);
postRuleStatusInfoEvent(ruleUID, newStatusInfo);
}
use of org.eclipse.smarthome.automation.core.internal.ruleengine.WrappedRule in project smarthome by eclipse.
the class RuleEngineImpl method runRule.
/**
* This method runs a {@link Rule}. It is called by the {@link TriggerHandlerCallback}'s thread when a new
* {@link TriggerData} is available. This method switches
*
* @param ruleUID 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(String ruleUID, TriggerHandlerCallbackImpl.TriggerData td) {
if (thCallbacks.get(ruleUID) == null) {
// the rule was unregistered
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
setStatus(ruleUID, new RuleStatusInfo(RuleStatus.RUNNING));
}
try {
clearContext(ruleUID);
setTriggerOutputs(ruleUID, td);
final WrappedRule rule = managedRules.get(ruleUID);
boolean isSatisfied = calculateConditions(rule);
if (isSatisfied) {
executeActions(rule, true);
logger.debug("The rule '{}' is executed.", ruleUID);
} else {
logger.debug("The rule '{}' is NOT executed, since it has unsatisfied conditions.", ruleUID);
}
} catch (Throwable t) {
logger.error("Failed to execute rule '{}': {}", ruleUID, t.getMessage());
logger.debug("", t);
}
// change state to IDLE only if the rule has not been DISABLED.
synchronized (this) {
if (getRuleStatus(ruleUID) == RuleStatus.RUNNING) {
setStatus(ruleUID, new RuleStatusInfo(RuleStatus.IDLE));
}
}
}
Aggregations