Search in sources :

Example 1 with SchedulerTemporalAdjuster

use of org.openhab.core.scheduler.SchedulerTemporalAdjuster 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)

Example 2 with SchedulerTemporalAdjuster

use of org.openhab.core.scheduler.SchedulerTemporalAdjuster in project openhab-core by openhab.

the class SchedulerImpl method schedule.

private <T> void schedule(ScheduledCompletableFutureRecurring<T> recurringSchedule, SchedulerRunnable runnable, TemporalAdjuster temporalAdjuster) {
    final Temporal newTime = recurringSchedule.getScheduledTime().with(temporalAdjuster);
    final ScheduledCompletableFutureOnce<T> deferred = new ScheduledCompletableFutureOnce<>(ZonedDateTime.from(newTime));
    deferred.thenAccept(v -> {
        if (temporalAdjuster instanceof SchedulerTemporalAdjuster) {
            final SchedulerTemporalAdjuster schedulerTemporalAdjuster = (SchedulerTemporalAdjuster) temporalAdjuster;
            if (!schedulerTemporalAdjuster.isDone(newTime)) {
                schedule(recurringSchedule, runnable, temporalAdjuster);
                return;
            }
        }
        recurringSchedule.complete(v);
    });
    recurringSchedule.setScheduledPromise(deferred);
    atInternal(deferred, () -> {
        runnable.run();
        return null;
    });
}
Also used : SchedulerTemporalAdjuster(org.openhab.core.scheduler.SchedulerTemporalAdjuster) Temporal(java.time.temporal.Temporal)

Aggregations

SchedulerTemporalAdjuster (org.openhab.core.scheduler.SchedulerTemporalAdjuster)2 Temporal (java.time.temporal.Temporal)1 ArrayList (java.util.ArrayList)1 RuleExecution (org.openhab.core.automation.RuleExecution)1 Trigger (org.openhab.core.automation.Trigger)1 TimeBasedTriggerHandler (org.openhab.core.automation.handler.TimeBasedTriggerHandler)1 TriggerHandler (org.openhab.core.automation.handler.TriggerHandler)1