use of org.eclipse.smarthome.automation.RuleStatusInfo in project smarthome by eclipse.
the class RuleEngine method updated.
@Override
public void updated(ModuleType oldElement, ModuleType moduleType) {
if (moduleType.equals(oldElement)) {
return;
}
String moduleTypeName = moduleType.getUID();
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) {
if (getRuleStatus(rUID).equals(RuleStatus.IDLE) || getRuleStatus(rUID).equals(RuleStatus.RUNNING)) {
setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.UNINITIALIZED), true);
unregister(getRuntimeRule(rUID));
}
if (!getRuleStatus(rUID).equals(RuleStatus.DISABLED)) {
scheduleRuleInitialization(rUID);
}
}
}
}
use of org.eclipse.smarthome.automation.RuleStatusInfo in project smarthome by eclipse.
the class RuleEngine method removeMissingModuleTypes.
private void removeMissingModuleTypes(Collection<String> moduleTypes) {
Map<String, List<String>> mapMissingHandlers = null;
for (Iterator<String> it = moduleTypes.iterator(); it.hasNext(); ) {
String moduleTypeName = it.next();
Set<String> rules = null;
synchronized (this) {
rules = mapModuleTypeToRules.get(moduleTypeName);
}
if (rules != null) {
for (String rUID : rules) {
RuleStatus ruleStatus = getRuleStatus(rUID);
switch(ruleStatus) {
case RUNNING:
case IDLE:
mapMissingHandlers = mapMissingHandlers != null ? mapMissingHandlers : new HashMap<String, List<String>>(20);
List<String> list = mapMissingHandlers.get(rUID);
if (list == null) {
list = new ArrayList<String>(5);
}
list.add(moduleTypeName);
mapMissingHandlers.put(rUID, list);
break;
default:
break;
}
}
}
}
// for
if (mapMissingHandlers != null) {
for (Entry<String, List<String>> e : mapMissingHandlers.entrySet()) {
String rUID = e.getKey();
List<String> missingTypes = e.getValue();
StringBuffer sb = new StringBuffer();
sb.append("Missing handlers: ");
for (String typeUID : missingTypes) {
sb.append(typeUID).append(", ");
}
setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.UNINITIALIZED, RuleStatusDetail.HANDLER_MISSING_ERROR, sb.substring(0, sb.length() - 2)), true);
unregister(getRuntimeRule(rUID));
}
}
}
use of org.eclipse.smarthome.automation.RuleStatusInfo in project smarthome by eclipse.
the class RuleEngine method getRuleStatus.
/**
* This method gets rule's status object.
*
* @param rUID rule uid
* @return status of the rule or null when such rule does not exists.
*/
protected RuleStatus getRuleStatus(String rUID) {
RuleStatusInfo info = getRuleStatusInfo(rUID);
RuleStatus status = null;
if (info != null) {
status = info.getStatus();
}
return status;
}
use of org.eclipse.smarthome.automation.RuleStatusInfo in project smarthome by eclipse.
the class RuleEngine method runRule.
/**
* This method runs a {@link Rule}. It is called by the {@link RuleEngineCallback}'s thread when a new
* {@link TriggerData} is available. This method switches
*
* @param rule 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(RuntimeRule rule, RuleEngineCallbackImpl.TriggerData td) {
String rUID = rule.getUID();
if (reCallbacks.get(rUID) == null) {
// the rule was unregistered
return;
}
synchronized (this) {
final RuleStatus ruleStatus = getRuleStatus(rUID);
if (ruleStatus != RuleStatus.IDLE) {
logger.error("Failed to execute rule ‘{}' with status '{}'", rUID, ruleStatus.name());
return;
}
// change state to RUNNING
setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.RUNNING), true);
}
try {
clearContext(rule);
setTriggerOutputs(rUID, td);
boolean isSatisfied = calculateConditions(rule);
if (isSatisfied) {
executeActions(rule, true);
logger.debug("The rule '{}' is executed.", rUID);
} else {
logger.debug("The rule '{}' is NOT executed, since it has unsatisfied conditions.", rUID);
}
} catch (Throwable t) {
logger.error("Failed to execute rule '{}': {}", rUID, t.getMessage());
logger.debug("", t);
}
// change state to IDLE only if the rule has not been DISABLED.
synchronized (this) {
if (getRuleStatus(rUID) == RuleStatus.RUNNING) {
setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.IDLE), true);
}
}
}
use of org.eclipse.smarthome.automation.RuleStatusInfo in project smarthome by eclipse.
the class RuleEngine method runNow.
protected void runNow(String ruleUID, boolean considerConditions, Map<String, Object> context) {
RuntimeRule rule = getRuntimeRule(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
setRuleStatusInfo(ruleUID, new RuleStatusInfo(RuleStatus.RUNNING), true);
}
try {
clearContext(rule);
if (context != null && !context.isEmpty()) {
getContext(ruleUID).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) {
setRuleStatusInfo(ruleUID, new RuleStatusInfo(RuleStatus.IDLE), true);
}
}
}
Aggregations