use of org.openhab.core.automation.handler.ConditionHandler in project openhab-core by openhab.
the class RuleEngineImpl method register.
/**
* This method register the Rule to start working. This is the final step of initialization process where
* triggers received {@link TriggerHandlerCallback}s object and starts to notify the rule engine when they are
* triggered. After activating all triggers the rule goes into IDLE state.
*
* @param rule an initialized rule which has to starts tracking the triggers.
*/
private void register(WrappedRule rule) {
final String ruleUID = rule.getUID();
TriggerHandlerCallback thCallback = getTriggerHandlerCallback(ruleUID);
rule.getTriggers().forEach(trigger -> {
TriggerHandler triggerHandler = trigger.getModuleHandler();
if (triggerHandler != null) {
triggerHandler.setCallback(thCallback);
}
});
rule.getConditions().forEach(condition -> {
ConditionHandler conditionHandler = condition.getModuleHandler();
if (conditionHandler != null) {
conditionHandler.setCallback(moduleHandlerCallback);
}
});
rule.getActions().forEach(action -> {
ActionHandler actionHandler = action.getModuleHandler();
if (actionHandler != null) {
actionHandler.setCallback(moduleHandlerCallback);
}
});
}
use of org.openhab.core.automation.handler.ConditionHandler in project openhab-core by openhab.
the class RuleEngineImpl method setModuleHandlers.
/**
* This method links modules to corresponding module handlers.
*
* @param rUID id of rule containing these modules
* @param modules list of modules
* @return null when all modules are connected or list of RuleErrors for missing handlers.
*/
@Nullable
private <T extends WrappedModule<?, ?>> String setModuleHandlers(String rUID, List<T> modules) {
StringBuilder sb = null;
for (T mm : modules) {
final Module m = mm.unwrap();
try {
ModuleHandler moduleHandler = getModuleHandler(m, rUID);
if (moduleHandler != null) {
if (mm instanceof WrappedAction) {
((WrappedAction) mm).setModuleHandler((ActionHandler) moduleHandler);
} else if (mm instanceof WrappedCondition) {
((WrappedCondition) mm).setModuleHandler((ConditionHandler) moduleHandler);
} else if (mm instanceof WrappedTrigger) {
((WrappedTrigger) mm).setModuleHandler((TriggerHandler) moduleHandler);
}
} else {
if (sb == null) {
sb = new StringBuilder();
}
String message = "Missing handler '" + m.getTypeUID() + "' for module '" + m.getId() + "'";
sb.append(message).append("\n");
logger.trace(message);
}
} catch (Throwable t) {
if (sb == null) {
sb = new StringBuilder();
}
String message = "Getting handler '" + m.getTypeUID() + "' for module '" + m.getId() + "' failed: " + t.getMessage();
sb.append(message).append("\n");
logger.trace(message);
}
}
return sb != null ? sb.toString() : null;
}
use of org.openhab.core.automation.handler.ConditionHandler in project openhab-core by openhab.
the class RuleEngineImpl 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(WrappedRule rule) {
List<WrappedCondition> conditions = rule.getConditions();
if (conditions.isEmpty()) {
return true;
}
final String ruleUID = rule.getUID();
RuleStatus ruleStatus = null;
for (WrappedCondition wrappedCondition : conditions) {
ruleStatus = getRuleStatus(ruleUID);
if (ruleStatus != RuleStatus.RUNNING) {
return false;
}
final Condition condition = wrappedCondition.unwrap();
ConditionHandler tHandler = wrappedCondition.getModuleHandler();
Map<String, Object> context = getContext(ruleUID, wrappedCondition.getConnections());
if (tHandler != null && !tHandler.isSatisfied(Collections.unmodifiableMap(context))) {
logger.debug("The condition '{}' of rule '{}' is unsatisfied.", condition.getId(), ruleUID);
return false;
}
}
return true;
}
use of org.openhab.core.automation.handler.ConditionHandler in project openhab-core by openhab.
the class CompositeConditionHandler method isSatisfied.
/**
* The method calls handlers of child modules and return true only when they all are satisfied.
*
* @see org.openhab.core.automation.handler.ConditionHandler#isSatisfied(java.util.Map)
*/
@Override
public boolean isSatisfied(Map<String, Object> context) {
List<Condition> children = getChildren();
Map<String, Object> compositeContext = getCompositeContext(context);
for (Condition child : children) {
Map<String, Object> childContext = Collections.unmodifiableMap(getChildContext(child, compositeContext));
ConditionHandler childHandler = moduleHandlerMap.get(child);
boolean isSatisfied = childHandler.isSatisfied(childContext);
if (!isSatisfied) {
return false;
}
}
return true;
}
Aggregations