fix TriggerRecurringTime

This commit is contained in:
Milos Kozak 2020-05-24 20:23:22 +02:00
parent c8516f7d40
commit 1cb648c09a
2 changed files with 4 additions and 11 deletions

View file

@ -37,9 +37,8 @@ class TriggerRecurringTime(injector: HasAndroidInjector) : Trigger(injector) {
override fun shouldRun(): Boolean { override fun shouldRun(): Boolean {
val currentMinSinceMidnight = getMinSinceMidnight(DateUtil.now()) val currentMinSinceMidnight = getMinSinceMidnight(DateUtil.now())
val scheduledDayOfWeek = Calendar.getInstance()[Calendar.DAY_OF_WEEK] val scheduledDayOfWeek = Calendar.getInstance()[Calendar.DAY_OF_WEEK]
val scheduled = getMinSinceMidnight(time.value.toLong())
if (days.isSet(Objects.requireNonNull(InputWeekDay.DayOfWeek.fromCalendarInt(scheduledDayOfWeek)))) { if (days.isSet(Objects.requireNonNull(InputWeekDay.DayOfWeek.fromCalendarInt(scheduledDayOfWeek)))) {
if (currentMinSinceMidnight >= scheduled && currentMinSinceMidnight - scheduled < 5) { if (currentMinSinceMidnight >= time.value && currentMinSinceMidnight - time.value < 5) {
aapsLogger.debug(LTag.AUTOMATION, "Ready for execution: " + friendlyDescription()) aapsLogger.debug(LTag.AUTOMATION, "Ready for execution: " + friendlyDescription())
return true return true
} }

View file

@ -14,7 +14,7 @@ import org.powermock.modules.junit4.PowerMockRunner
@PrepareForTest(DateUtil::class) @PrepareForTest(DateUtil::class)
class TriggerRecurringTimeTest : TriggerTestBase() { class TriggerRecurringTimeTest : TriggerTestBase() {
var now = 1514766900000L var now = 1514766900000L // 95 min from midnight
@Before fun mock() { @Before fun mock() {
PowerMockito.mockStatic(DateUtil::class.java) PowerMockito.mockStatic(DateUtil::class.java)
@ -26,20 +26,14 @@ class TriggerRecurringTimeTest : TriggerTestBase() {
@Test fun shouldRunTest() { @Test fun shouldRunTest() {
// limit by validTo var t: TriggerRecurringTime = TriggerRecurringTime(injector).time(89)
var t: TriggerRecurringTime = TriggerRecurringTime(injector).time(94)
t.days.setAll(true) t.days.setAll(true)
Assert.assertFalse(t.shouldRun()) Assert.assertFalse(t.shouldRun())
// scheduled 1 min before // scheduled 1 min before
// t = new TriggerRecurringTime().hour(1).minute(34);
// t.setAll(true);
// Assert.assertTrue(t.shouldRun());
// already run
t = TriggerRecurringTime(injector).time(94) t = TriggerRecurringTime(injector).time(94)
t.days.setAll(true) t.days.setAll(true)
Assert.assertFalse(t.shouldRun()) Assert.assertTrue(t.shouldRun())
} }
private var timeJson = "{\"data\":{\"WEDNESDAY\":false,\"MONDAY\":false,\"THURSDAY\":false,\"SUNDAY\":false,\"TUESDAY\":false,\"FRIDAY\":false,\"SATURDAY\":false,\"time\":4444},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerRecurringTime\"}" private var timeJson = "{\"data\":{\"WEDNESDAY\":false,\"MONDAY\":false,\"THURSDAY\":false,\"SUNDAY\":false,\"TUESDAY\":false,\"FRIDAY\":false,\"SATURDAY\":false,\"time\":4444},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerRecurringTime\"}"