diff --git a/app/src/main/java/com/jens/automation2/Trigger.java b/app/src/main/java/com/jens/automation2/Trigger.java index 7bd2ec9..174ed00 100644 --- a/app/src/main/java/com/jens/automation2/Trigger.java +++ b/app/src/main/java/com/jens/automation2/Trigger.java @@ -1337,7 +1337,7 @@ public class Trigger // the simple stuff: - if(lastExec == null) // rule never run, go in any case + if(lastExec == null) // rule never ran, go in any case return true; else if(tf.getRepetition() <= 0) // is not set to repeat at all return false; @@ -1348,12 +1348,13 @@ public class Trigger * we're inside the specified timeframe. */ - Calendar timeSupposedToRunNext = DateTimeListener.getNextRepeatedExecution(this); + Calendar lastRepetitionNotExecutedYet = DateTimeListener.getNextRepeatedExecution(this); + lastRepetitionNotExecutedYet.add(Calendar.SECOND, (int) -(tf.getRepetition())); Miscellaneous.logEvent("i", "isSupposedToRepeatSinceLastExecution()", "Last execution: " + Miscellaneous.formatDate(lastExec.getTime()), 5); - Miscellaneous.logEvent("i", "isSupposedToRepeatSinceLastExecution()", "Next execution would be: " + Miscellaneous.formatDate(timeSupposedToRunNext.getTime()), 5); + Miscellaneous.logEvent("i", "isSupposedToRepeatSinceLastExecution()", "lastRepetitionNotExecutedYet: " + Miscellaneous.formatDate(lastRepetitionNotExecutedYet.getTime()), 5); - if(now.getTimeInMillis() > timeSupposedToRunNext.getTimeInMillis()) + if(now.getTimeInMillis() > lastRepetitionNotExecutedYet.getTimeInMillis()) return true; return false; diff --git a/app/src/main/java/com/jens/automation2/receivers/DateTimeListener.java b/app/src/main/java/com/jens/automation2/receivers/DateTimeListener.java index 59211ac..4b29b2e 100644 --- a/app/src/main/java/com/jens/automation2/receivers/DateTimeListener.java +++ b/app/src/main/java/com/jens/automation2/receivers/DateTimeListener.java @@ -53,11 +53,11 @@ public class DateTimeListener extends BroadcastReceiver implements AutomationLis { Miscellaneous.logEvent("i", "AlarmListener", "Alarm received", 2); - ArrayList allRulesWithNowInTimeFrame = Rule.findRuleCandidates(Trigger_Enum.timeFrame); - for(int i=0; i < allRulesWithNowInTimeFrame.size(); i++) + ArrayList allRulesWithTimeFrame = Rule.findRuleCandidates(Trigger_Enum.timeFrame); + for(int i=0; i < allRulesWithTimeFrame.size(); i++) { - if(allRulesWithNowInTimeFrame.get(i).getsGreenLight(context)) - allRulesWithNowInTimeFrame.get(i).activate(automationServiceRef, false); + if(allRulesWithTimeFrame.get(i).getsGreenLight(context)) + allRulesWithTimeFrame.get(i).activate(automationServiceRef, false); } setOrResetAlarms(); @@ -461,26 +461,42 @@ public class DateTimeListener extends BroadcastReceiver implements AutomationLis calSchedule.add(Calendar.SECOND, (int) tf.getRepetition()); } } - else + else // not in timeframe { if (!trigger.getTriggerParameter()) { - if (trigger.getParentRule().getLastExecution() != null && tf.getDayList().contains(now.get(Calendar.DAY_OF_WEEK))) + if (trigger.getParentRule().getLastExecution() != null) { calculationStart = (Calendar) trigger.getParentRule().getLastExecution().clone(); + long differenceInSeconds = Math.abs(now.getTimeInMillis() - calculationStart.getTimeInMillis()) / 1000; + long nextExecutionMultiplier = Math.floorDiv(differenceInSeconds, tf.getRepetition()) + 1; + calSchedule = (Calendar) calculationStart.clone(); + calSchedule.add(Calendar.SECOND, (int) (nextExecutionMultiplier * tf.getRepetition())); + Miscellaneous.logEvent("i", "getNextRepeatedExecutionAfter()", "Chose " + Miscellaneous.formatDate(calSchedule.getTime()) + " as next repeated execution time.", 5); + return calSchedule; } else { calculationStart = (Calendar) now.clone(); - calculationStart.set(Calendar.HOUR_OF_DAY, tf.getTriggerTimeStop().getHours()); - calculationStart.set(Calendar.MINUTE, tf.getTriggerTimeStop().getMinutes()); - calculationStart.set(Calendar.SECOND, tf.getTriggerTimeStop().getSeconds()); - calculationStart.set(Calendar.MILLISECOND, 0); + if(tf.getDayList().contains(now.get(Calendar.DAY_OF_WEEK))) + { + calculationStart.set(Calendar.HOUR_OF_DAY, tf.getTriggerTimeStop().getHours()); + calculationStart.set(Calendar.MINUTE, tf.getTriggerTimeStop().getMinutes()); + calculationStart.set(Calendar.SECOND, tf.getTriggerTimeStop().getSeconds()); + calculationStart.set(Calendar.MILLISECOND, 0); + calculationStart.add(Calendar.SECOND, (int) tf.getRepetition()); + + int dayDelta = getDayDelta(now, getNextDayIntForExecution(trigger)); + calculationStart.add(Calendar.DAY_OF_WEEK, dayDelta); + } + calSchedule = (Calendar) calculationStart.clone(); + Miscellaneous.logEvent("i", "getNextRepeatedExecutionAfter()", "Chose " + Miscellaneous.formatDate(calSchedule.getTime()) + " as next repeated execution time.", 5); + return calSchedule; } - long differenceInSeconds = Math.abs(now.getTimeInMillis() - calculationStart.getTimeInMillis()) / 1000; + /*long differenceInSeconds = Math.abs(now.getTimeInMillis() - calculationStart.getTimeInMillis()) / 1000; long nextExecutionMultiplier = Math.floorDiv(differenceInSeconds, tf.getRepetition()) + 1; calSchedule = (Calendar) calculationStart.clone(); - calSchedule.add(Calendar.SECOND, (int) (nextExecutionMultiplier * tf.getRepetition())); + calSchedule.add(Calendar.SECOND, (int) (nextExecutionMultiplier * tf.getRepetition()));*/ } else { @@ -492,10 +508,7 @@ public class DateTimeListener extends BroadcastReceiver implements AutomationLis calSchedule = (Calendar) calculationStart.clone(); calSchedule.add(Calendar.SECOND, (int) (tf.getRepetition())); } - } - if(!areWeInTimeFrame(trigger, new Date())) - { if (Miscellaneous.compareTimes(calSchedule, now) > 0) calSchedule.add(Calendar.DAY_OF_MONTH, 1); }