use of org.eclipse.smarthome.automation.RuleStatus in project smarthome by eclipse.
the class RuleEngine method added.
@Override
public void added(ModuleType moduleType) {
String moduleTypeName = moduleType.getUID();
for (ModuleHandlerFactory moduleHandlerFactory : allModuleHandlerFactories) {
Collection<String> moduleTypes = moduleHandlerFactory.getTypes();
if (moduleTypes.contains(moduleTypeName)) {
synchronized (this) {
this.moduleHandlerFactories.put(moduleTypeName, moduleHandlerFactory);
}
break;
}
}
Set<String> rules = null;
synchronized (this) {
Set<String> rulesPerModule = mapModuleTypeToRules.get(moduleTypeName);
if (rulesPerModule != null) {
rules = new HashSet<String>();
rules.addAll(rulesPerModule);
}
}
if (rules != null) {
for (String rUID : rules) {
RuleStatus ruleStatus = getRuleStatus(rUID);
if (ruleStatus == RuleStatus.UNINITIALIZED) {
scheduleRuleInitialization(rUID);
}
}
}
}
use of org.eclipse.smarthome.automation.RuleStatus 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.RuleStatus in project smarthome by eclipse.
the class AutomationCommandList method listRules.
/**
* This method is responsible for execution of command {@link AutomationCommands#LIST_RULES}.
*
* @return a string representing understandable for the user message containing information on the outcome of the
* command {@link AutomationCommands#LIST_RULES}.
*/
private String listRules() {
Collection<Rule> collection = autoCommands.getRules();
Map<String, Rule> rules = new Hashtable<String, Rule>();
Map<String, String> listRules = null;
if (collection != null && !collection.isEmpty()) {
addCollection(collection, rules);
String[] uids = new String[rules.size()];
Utils.quickSort(rules.keySet().toArray(uids), 0, rules.size());
listRules = Utils.putInHastable(uids);
}
if (listRules != null && !listRules.isEmpty()) {
if (id != null) {
collection = getRuleByFilter(listRules);
if (collection.size() == 1) {
Rule r = (Rule) collection.toArray()[0];
if (r != null) {
RuleStatus status = autoCommands.getRuleStatus(r.getUID());
return Printer.printRule(r, status);
} else {
return String.format("Nonexistent ID: %s", id);
}
} else if (collection.isEmpty()) {
return String.format("Nonexistent ID: %s", id);
} else {
if (!rules.isEmpty()) {
rules.clear();
}
addCollection(collection, rules);
listRules = Utils.filterList(rules, listRules);
}
}
return Printer.printRules(autoCommands, listRules);
}
return "There are no Rules available!";
}
use of org.eclipse.smarthome.automation.RuleStatus 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);
}
}
use of org.eclipse.smarthome.automation.RuleStatus in project smarthome by eclipse.
the class RuleEngine method addNewModuleTypes.
private void addNewModuleTypes(ModuleHandlerFactory mhf, Collection<String> moduleTypes) {
Set<String> notInitailizedRules = null;
for (Iterator<String> it = moduleTypes.iterator(); it.hasNext(); ) {
String moduleTypeName = it.next();
Set<String> rules = null;
synchronized (this) {
moduleHandlerFactories.put(moduleTypeName, mhf);
Set<String> rulesPerModule = mapModuleTypeToRules.get(moduleTypeName);
if (rulesPerModule != null) {
rules = new HashSet<String>();
rules.addAll(rulesPerModule);
}
}
if (rules != null) {
for (String rUID : rules) {
RuleStatus ruleStatus = getRuleStatus(rUID);
if (ruleStatus == RuleStatus.UNINITIALIZED) {
notInitailizedRules = notInitailizedRules != null ? notInitailizedRules : new HashSet<String>(20);
notInitailizedRules.add(rUID);
}
}
}
}
if (notInitailizedRules != null) {
for (final String rUID : notInitailizedRules) {
scheduleRuleInitialization(rUID);
}
}
}
Aggregations