use of org.joda.time.LocalDateTime in project SmartApplianceEnabler by camueller.
the class Schedule method getCurrentOrNextTimeframeInterval.
/**
* Returns the current or next timeframe if the remaining time is greater than maximum running time; otherwise the next timeframe is returned.
* @param now the time reference
* @param schedules the list of timeframes to choose from (current timeframe has to be first)
* @param onlyAlreadyStarted consider only timeframe intervals already started
* @param onlySufficient if true consider timeframe already started only if time to interval end exceeds min running time
* @return the next timeframe becoming valid or null
*/
public static TimeframeInterval getCurrentOrNextTimeframeInterval(LocalDateTime now, List<Schedule> schedules, boolean onlyAlreadyStarted, boolean onlySufficient) {
if (schedules == null || schedules.size() == 0) {
return null;
}
Map<Long, TimeframeInterval> startDelayOfTimeframeInterval = new TreeMap<>();
for (Schedule schedule : schedules) {
Timeframe timeframe = schedule.getTimeframe();
timeframe.setSchedule(schedule);
List<TimeframeInterval> timeframeIntervals = timeframe.getIntervals(now);
for (TimeframeInterval timeframeInterval : timeframeIntervals) {
Interval interval = timeframeInterval.getInterval();
if (interval.contains(now.toDateTime())) {
// interval already started ...
if (onlySufficient) {
if (now.plusSeconds(schedule.getMaxRunningTime()).plusSeconds(additionalRunningTime).isBefore(new LocalDateTime(interval.getEnd()))) {
// ... with remaining running time sufficient
return timeframeInterval;
}
} else {
return timeframeInterval;
}
} else if (!onlyAlreadyStarted) {
// interval starts in future
startDelayOfTimeframeInterval.put(interval.getStartMillis() - now.toDateTime().getMillis(), timeframeInterval);
}
}
}
if (startDelayOfTimeframeInterval.size() > 0) {
Long startDelay = startDelayOfTimeframeInterval.keySet().iterator().next();
return startDelayOfTimeframeInterval.get(startDelay);
}
return null;
}
Aggregations