use of org.openhab.core.automation.handler.ActionHandler 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.ActionHandler in project openhab-core by openhab.
the class CompositeActionHandler method execute.
/**
* The method calls handlers of child action, collect their outputs and sets the output of the parent action.
*
* @see org.openhab.core.automation.handler.ActionHandler#execute(java.util.Map)
*/
@Override
@Nullable
public Map<String, Object> execute(Map<String, Object> context) {
final Map<String, Object> result = new HashMap<>();
final List<Action> children = getChildren();
final Map<String, Object> compositeContext = getCompositeContext(context);
for (Action child : children) {
ActionHandler childHandler = moduleHandlerMap.get(child);
Map<String, Object> childContext = Collections.unmodifiableMap(getChildContext(child, compositeContext));
Map<String, Object> childResults = childHandler == null ? null : childHandler.execute(childContext);
if (childResults != null) {
for (Entry<String, Object> childResult : childResults.entrySet()) {
String childOuputName = child.getId() + "." + childResult.getKey();
Output output = compositeOutputs.get(childOuputName);
if (output != null) {
String childOuputRef = output.getReference();
if (childOuputRef != null && childOuputRef.length() > childOuputName.length()) {
childOuputRef = childOuputRef.substring(childOuputName.length());
result.put(output.getName(), ReferenceResolver.resolveComplexDataReference(childResult.getValue(), childOuputRef));
} else {
result.put(output.getName(), childResult.getValue());
}
}
}
}
}
return !result.isEmpty() ? result : null;
}
use of org.openhab.core.automation.handler.ActionHandler in project openhab-core by openhab.
the class CompositeModuleHandlerFactory method internalCreate.
@Override
@Nullable
public ModuleHandler internalCreate(Module module, String ruleUID) {
ModuleHandler handler = null;
String moduleType = module.getTypeUID();
ModuleType mt = mtRegistry.get(moduleType);
if (mt instanceof CompositeTriggerType) {
List<Trigger> childModules = ((CompositeTriggerType) mt).getChildren();
LinkedHashMap<Trigger, @Nullable TriggerHandler> mapModuleToHandler = getChildHandlers(module.getId(), module.getConfiguration(), childModules, ruleUID);
if (mapModuleToHandler != null) {
handler = new CompositeTriggerHandler((Trigger) module, (CompositeTriggerType) mt, mapModuleToHandler, ruleUID);
}
} else if (mt instanceof CompositeConditionType) {
List<Condition> childModules = ((CompositeConditionType) mt).getChildren();
LinkedHashMap<Condition, @Nullable ConditionHandler> mapModuleToHandler = getChildHandlers(module.getId(), module.getConfiguration(), childModules, ruleUID);
if (mapModuleToHandler != null) {
handler = new CompositeConditionHandler((Condition) module, (CompositeConditionType) mt, mapModuleToHandler, ruleUID);
}
} else if (mt instanceof CompositeActionType) {
List<Action> childModules = ((CompositeActionType) mt).getChildren();
LinkedHashMap<Action, @Nullable ActionHandler> mapModuleToHandler = getChildHandlers(module.getId(), module.getConfiguration(), childModules, ruleUID);
if (mapModuleToHandler != null) {
handler = new CompositeActionHandler((Action) module, (CompositeActionType) mt, mapModuleToHandler, ruleUID);
}
}
if (handler != null) {
logger.debug("Set module handler: {} -> {} of rule {}.", module.getId(), handler.getClass().getSimpleName() + "(" + moduleType + ")", ruleUID);
} else {
logger.debug("Not found module handler {} for moduleType {} of rule {}.", module.getId(), moduleType, ruleUID);
}
return handler;
}
use of org.openhab.core.automation.handler.ActionHandler in project openhab-core by openhab.
the class RuleEngineImpl method executeActions.
/**
* This method evaluates actions of the {@link Rule} and set their {@link Output}s when they exists.
*
* @param rule executed rule.
*/
private void executeActions(WrappedRule rule, boolean stopOnFirstFail) {
final String ruleUID = rule.getUID();
final Collection<WrappedAction> actions = rule.getActions();
if (actions.isEmpty()) {
return;
}
RuleStatus ruleStatus = null;
for (WrappedAction wrappedAction : actions) {
ruleStatus = getRuleStatus(ruleUID);
if (ruleStatus != RuleStatus.RUNNING) {
return;
}
final Action action = wrappedAction.unwrap();
ActionHandler aHandler = wrappedAction.getModuleHandler();
if (aHandler != null) {
Map<String, Object> context = getContext(ruleUID, wrappedAction.getConnections());
try {
Map<String, ?> outputs = aHandler.execute(Collections.unmodifiableMap(context));
if (outputs != null) {
context = getContext(ruleUID, null);
updateContext(ruleUID, action.getId(), outputs);
}
} catch (Throwable t) {
String errMessage = "Fail to execute action: " + action.getId();
if (stopOnFirstFail) {
RuntimeException re = new RuntimeException(errMessage, t);
throw re;
} else {
logger.warn(errMessage, t);
}
}
}
}
}
Aggregations