diff --git a/omnipod/build.gradle b/omnipod/build.gradle index bea836e961..50f97665c2 100644 --- a/omnipod/build.gradle +++ b/omnipod/build.gradle @@ -108,6 +108,7 @@ dependencies { testImplementation "org.mockito:mockito-core:2.8.47" testImplementation "org.powermock:powermock-api-mockito2:$powermockVersion" testImplementation "org.powermock:powermock-module-junit4:${powermockVersion}" + testImplementation 'joda-time:joda-time:2.10.6' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java index 15c775cc42..ec8626d19f 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java @@ -76,6 +76,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.driver.definition.Activat import info.nightscout.androidaps.plugins.pump.omnipod.driver.definition.AlertConfiguration; import info.nightscout.androidaps.plugins.pump.omnipod.driver.definition.PodProgressStatus; import info.nightscout.androidaps.plugins.pump.omnipod.driver.manager.PodStateManager; +import info.nightscout.androidaps.plugins.pump.omnipod.driver.util.TimeUtil; import info.nightscout.androidaps.plugins.pump.omnipod.event.EventOmnipodPumpValuesChanged; import info.nightscout.androidaps.plugins.pump.omnipod.event.EventOmnipodTbrChanged; import info.nightscout.androidaps.plugins.pump.omnipod.manager.AapsOmnipodManager; @@ -497,9 +498,7 @@ public class OmnipodPumpPlugin extends PumpPluginBase implements PumpInterface, return 0.0d; } - DateTime now = DateTime.now(); - Duration offset = new Duration(now.withTimeAtStartOfDay(), now); - return podStateManager.getBasalSchedule().rateAt(offset); + return podStateManager.getBasalSchedule().rateAt(TimeUtil.toDuration(DateTime.now())); } @Override diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/manager/PodStateManager.java b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/manager/PodStateManager.java index 764467e15e..34726daffb 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/manager/PodStateManager.java +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/manager/PodStateManager.java @@ -34,6 +34,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.driver.definition.Omnipod import info.nightscout.androidaps.plugins.pump.omnipod.driver.definition.OmnipodCrc; import info.nightscout.androidaps.plugins.pump.omnipod.driver.definition.PodProgressStatus; import info.nightscout.androidaps.plugins.pump.omnipod.driver.definition.schedule.BasalSchedule; +import info.nightscout.androidaps.plugins.pump.omnipod.driver.util.TimeUtil; // TODO add nullchecks on some setters public abstract class PodStateManager { @@ -365,8 +366,7 @@ public abstract class PodStateManager { } public final Duration getScheduleOffset() { - DateTime now = getTime(); - return new Duration(now.withTimeAtStartOfDay(), now); + return TimeUtil.toDuration(getTime()); } public final BasalSchedule getBasalSchedule() { @@ -503,7 +503,7 @@ public abstract class PodStateManager { if (isTempBasalRunningAt(time)) { return getTempBasalAmount(); } - Duration offset = new Duration(time.withTimeAtStartOfDay(), time); + Duration offset = TimeUtil.toDuration(time); return basalSchedule.rateAt(offset); } diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/util/TimeUtil.java b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/util/TimeUtil.java new file mode 100644 index 0000000000..dc37e42b54 --- /dev/null +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/util/TimeUtil.java @@ -0,0 +1,21 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.driver.util; + +import org.joda.time.DateTime; +import org.joda.time.Duration; + +public final class TimeUtil { + private TimeUtil() { + } + + /** + * @param dateTime DateTime to convert to duration + * @return duration from the start of the day, not taking DST into account + * (thus always having 24 hours in a day, not 23 or 25 in days where DST changes) + */ + public static Duration toDuration(DateTime dateTime) { + if (dateTime == null) { + throw new IllegalArgumentException("dateTime can not be null"); + } + return new Duration(dateTime.toLocalTime().getMillisOfDay()); + } +} diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/ui/OmnipodOverviewFragment.kt b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/ui/OmnipodOverviewFragment.kt index 6370f4599d..0aec406de1 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/ui/OmnipodOverviewFragment.kt +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/ui/OmnipodOverviewFragment.kt @@ -27,6 +27,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.driver.definition.Activat import info.nightscout.androidaps.plugins.pump.omnipod.driver.definition.OmnipodConstants import info.nightscout.androidaps.plugins.pump.omnipod.driver.definition.PodProgressStatus import info.nightscout.androidaps.plugins.pump.omnipod.driver.manager.PodStateManager +import info.nightscout.androidaps.plugins.pump.omnipod.driver.util.TimeUtil import info.nightscout.androidaps.plugins.pump.omnipod.event.EventOmnipodPumpValuesChanged import info.nightscout.androidaps.plugins.pump.omnipod.manager.AapsOmnipodManager import info.nightscout.androidaps.plugins.pump.omnipod.queue.command.* @@ -54,6 +55,7 @@ import kotlin.collections.ArrayList class OmnipodOverviewFragment : DaggerFragment() { companion object { + private val REFRESH_INTERVAL_MILLIS = 15 * 1000L // 15 seconds private val PLACEHOLDER = "-" // 15 seconds } @@ -278,11 +280,9 @@ class OmnipodOverviewFragment : DaggerFragment() { errors.add(resourceHelper.gs(R.string.omnipod_pod_status_pod_fault_description, faultEventCode.value, faultEventCode.name)) } - val now = DateTime.now() - // base basal rate omnipod_overview_base_basal_rate.text = if (podStateManager.isPodActivationCompleted) { - resourceHelper.gs(R.string.pump_basebasalrate, omnipodPumpPlugin.model().determineCorrectBasalSize(podStateManager.basalSchedule.rateAt(Duration(now.withTimeAtStartOfDay(), now)))) + resourceHelper.gs(R.string.pump_basebasalrate, omnipodPumpPlugin.model().determineCorrectBasalSize(podStateManager.basalSchedule.rateAt(TimeUtil.toDuration(DateTime.now())))) } else { PLACEHOLDER } @@ -598,6 +598,7 @@ class OmnipodOverviewFragment : DaggerFragment() { } inner class DisplayResultDialogCallback(private val errorMessagePrefix: String, private val withSoundOnError: Boolean) : Callback() { + private var messageOnSuccess: String? = null override fun run() { diff --git a/omnipod/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/util/TimeUtilTest.java b/omnipod/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/util/TimeUtilTest.java new file mode 100644 index 0000000000..49ee7c02ba --- /dev/null +++ b/omnipod/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/util/TimeUtilTest.java @@ -0,0 +1,47 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.driver.util; + +import net.danlew.android.joda.JodaTimeAndroid; + +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +public class TimeUtilTest { + + @Before + public void setUp() { + DateTimeZone.setDefault(DateTimeZone.forID("Europe/Amsterdam")); + } + + @Test + public void testDateTimeToDuration() { + DateTime dateTime = new DateTime(1991, 8, 13, 23, 5, 1); + + assertEquals(83101, TimeUtil.toDuration(dateTime).getStandardSeconds()); + } + + @Test + public void testDateTimeToDurationAtDstChange() { + DateTime dateTime = new DateTime(2020, 10, 25, 23, 5, 1); + + assertEquals(83101, TimeUtil.toDuration(dateTime).getStandardSeconds()); + } + + @Test + public void testDateTimeToDurationAtDstReverseChange() { + DateTime dateTime = new DateTime(2020, 3, 29, 23, 5, 1); + + assertEquals(83101, TimeUtil.toDuration(dateTime).getStandardSeconds()); + } + + @Test + public void testDateTimeToDurationWithNullDateTime() { + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> TimeUtil.toDuration(null)); + assertEquals("dateTime can not be null", ex.getMessage()); + } + +} \ No newline at end of file