Search in sources :

Example 1 with TriggerHandler

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);
        }
    });
}
Also used : TriggerHandler(org.openhab.core.automation.handler.TriggerHandler) SystemTriggerHandler(org.openhab.core.automation.internal.module.handler.SystemTriggerHandler) ConditionHandler(org.openhab.core.automation.handler.ConditionHandler) TriggerHandlerCallback(org.openhab.core.automation.handler.TriggerHandlerCallback) ActionHandler(org.openhab.core.automation.handler.ActionHandler)

Example 2 with TriggerHandler

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;
}
Also used : Action(org.openhab.core.automation.Action) TriggerHandler(org.openhab.core.automation.handler.TriggerHandler) CompositeConditionType(org.openhab.core.automation.type.CompositeConditionType) LinkedHashMap(java.util.LinkedHashMap) ModuleHandler(org.openhab.core.automation.handler.ModuleHandler) CompositeTriggerType(org.openhab.core.automation.type.CompositeTriggerType) ModuleType(org.openhab.core.automation.type.ModuleType) Trigger(org.openhab.core.automation.Trigger) ArrayList(java.util.ArrayList) List(java.util.List) CompositeActionType(org.openhab.core.automation.type.CompositeActionType) ActionHandler(org.openhab.core.automation.handler.ActionHandler) Nullable(org.eclipse.jdt.annotation.Nullable) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 3 with TriggerHandler

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;
}
Also used : SchedulerTemporalAdjuster(org.openhab.core.scheduler.SchedulerTemporalAdjuster) Trigger(org.openhab.core.automation.Trigger) RuleExecution(org.openhab.core.automation.RuleExecution) TriggerHandler(org.openhab.core.automation.handler.TriggerHandler) TimeBasedTriggerHandler(org.openhab.core.automation.handler.TimeBasedTriggerHandler) ArrayList(java.util.ArrayList) TimeBasedTriggerHandler(org.openhab.core.automation.handler.TimeBasedTriggerHandler)

Aggregations

TriggerHandler (org.openhab.core.automation.handler.TriggerHandler)3 ArrayList (java.util.ArrayList)2 Trigger (org.openhab.core.automation.Trigger)2 ActionHandler (org.openhab.core.automation.handler.ActionHandler)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 Action (org.openhab.core.automation.Action)1 RuleExecution (org.openhab.core.automation.RuleExecution)1 ConditionHandler (org.openhab.core.automation.handler.ConditionHandler)1 ModuleHandler (org.openhab.core.automation.handler.ModuleHandler)1 TimeBasedTriggerHandler (org.openhab.core.automation.handler.TimeBasedTriggerHandler)1 TriggerHandlerCallback (org.openhab.core.automation.handler.TriggerHandlerCallback)1 SystemTriggerHandler (org.openhab.core.automation.internal.module.handler.SystemTriggerHandler)1 CompositeActionType (org.openhab.core.automation.type.CompositeActionType)1 CompositeConditionType (org.openhab.core.automation.type.CompositeConditionType)1 CompositeTriggerType (org.openhab.core.automation.type.CompositeTriggerType)1 ModuleType (org.openhab.core.automation.type.ModuleType)1 SchedulerTemporalAdjuster (org.openhab.core.scheduler.SchedulerTemporalAdjuster)1