use of org.openhab.core.automation.handler.TriggerHandler 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.TriggerHandler 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.TriggerHandler in project openhab-core by openhab.
the class RuleExecutionSimulator method simulateExecutionsForRule.
/**
* Simulates the next executions for the given {@link Rule} until the given {@link Date}.
*
* @param rule {@link Rule} to be simulated.
* @param from {@link ZonedDateTime} earliest time to be contained in the rule simulation.
* @param until {@link ZonedDateTime} latest time to be contained in the rule simulation.
* @return List of expected {@link RuleExecution}.
*/
private List<RuleExecution> simulateExecutionsForRule(Rule rule, ZonedDateTime from, ZonedDateTime until) {
final List<RuleExecution> executions = new ArrayList<>();
for (Trigger trigger : rule.getTriggers()) {
TriggerHandler triggerHandler = (TriggerHandler) this.ruleEngine.getModuleHandler(trigger, rule.getUID());
// Only triggers that are time-based will be considered within the simulation
if (triggerHandler instanceof TimeBasedTriggerHandler) {
SchedulerTemporalAdjuster temporalAdjuster = ((TimeBasedTriggerHandler) triggerHandler).getTemporalAdjuster();
if (temporalAdjuster != null) {
executions.addAll(simulateExecutionsForCronBasedRule(rule, from, until, temporalAdjuster));
}
}
}
logger.debug("Created {} rule simulations for rule {}.", executions.size(), rule.getName());
return executions;
}
Aggregations