use of org.eclipse.smarthome.automation.handler.ActionHandler in project smarthome by eclipse.
the class RuleEngine 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(Rule rule, boolean stopOnFirstFail) {
List<Action> actions = ((RuntimeRule) rule).getActions();
if (actions.size() == 0) {
return;
}
RuleStatus ruleStatus = null;
for (Iterator<Action> it = actions.iterator(); it.hasNext(); ) {
ruleStatus = getRuleStatus(rule.getUID());
if (ruleStatus != RuleStatus.RUNNING) {
return;
}
RuntimeAction action = (RuntimeAction) it.next();
ActionHandler aHandler = action.getModuleHandler();
String rUID = rule.getUID();
Map<String, Object> context = getContext(rUID, action.getConnections());
try {
Map<String, ?> outputs = aHandler.execute(Collections.unmodifiableMap(context));
if (outputs != null) {
context = getContext(rUID);
updateContext(rUID, action.getId(), outputs);
}
} catch (Throwable t) {
String errMessage = "Failed to execute action '" + action.getId() + "': " + t.getMessage();
if (stopOnFirstFail) {
RuntimeException re = new RuntimeException(errMessage, t);
throw re;
} else {
logger.warn("Error message: {}", errMessage);
}
}
}
}
use of org.eclipse.smarthome.automation.handler.ActionHandler in project smarthome by eclipse.
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.eclipse.smarthome.automation.handler.ActionHandler#execute(java.util.Map)
*/
@Override
public Map<String, Object> execute(Map<String, Object> context) {
final Map<String, Object> result = new HashMap<String, Object>();
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.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(), ReferenceResolverUtil.getValue(childResult.getValue(), childOuputRef));
} else {
result.put(output.getName(), childResult.getValue());
}
}
}
}
}
return result.size() > 0 ? result : null;
}
use of org.eclipse.smarthome.automation.handler.ActionHandler in project smarthome by eclipse.
the class CompositeModuleHandlerFactory method internalCreate.
@Override
public ModuleHandler internalCreate(Module module, String ruleUID) {
ModuleHandler handler = null;
if (module != null) {
String moduleType = module.getTypeUID();
ModuleType mt = mtRegistry.get(moduleType);
if (mt instanceof CompositeTriggerType) {
List<Trigger> childModules = ((CompositeTriggerType) mt).getChildren();
LinkedHashMap<Trigger, 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, 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, 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;
}
Aggregations