diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgo.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgo.kt index db57bb159a..4b7a56771a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgo.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgo.kt @@ -35,7 +35,7 @@ class TriggerBolusAgo(injector: HasAndroidInjector) : Trigger(injector) { } override fun shouldRun(): Boolean { - val lastBolusTime = treatmentsPlugin.getLastBolusTime(false) + val lastBolusTime = treatmentsPlugin.getLastBolusTime(true) if (lastBolusTime == 0L) return if (comparator.value == Comparator.Compare.IS_NOT_AVAILABLE) { aapsLogger.debug(LTag.AUTOMATION, "Ready for execution: " + friendlyDescription()) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java index 60bd9b9b8c..18d997178e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java @@ -45,6 +45,7 @@ import info.nightscout.androidaps.plugins.bus.RxBusWrapper; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventNewHistoryData; import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicPumpPlugin; import info.nightscout.androidaps.plugins.pump.medtronic.data.MedtronicHistoryData; +import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.JsonHelper; import io.reactivex.disposables.CompositeDisposable; @@ -588,6 +589,30 @@ public class TreatmentService extends OrmLiteBaseService { } } + /** + * Returns the newest record with insulin > 0 + */ + @Nullable + public Treatment getLastBolus(boolean excludeSMB) { + try { + QueryBuilder queryBuilder = getDao().queryBuilder(); + Where where = queryBuilder.where(); + where.gt("insulin", 0); + where.and().le("date", DateUtil.now()); + where.and().eq("isValid", true); + if (excludeSMB) where.and().eq("isSMB", false); + queryBuilder.orderBy("date", false); + queryBuilder.limit(1L); + + List result = getDao().query(queryBuilder.prepare()); + if (result.isEmpty()) + return null; + return result.get(0); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public void deleteNS(JSONObject json) { String _id = JsonHelper.safeGetString(json, "_id"); if (_id != null && !_id.isEmpty()) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java index 0af7ac8d33..8d723cea71 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java @@ -335,33 +335,27 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface @Override public long getLastBolusTime() { - long now = System.currentTimeMillis(); - long last = 0; - synchronized (treatments) { - for (Treatment t : treatments) { - if (!t.isValid) - continue; - if (t.date > last && t.insulin > 0 && t.date <= now) - last = t.date; - } + Treatment last = getService().getLastBolus(false); + if (last == null) { + getAapsLogger().debug(LTag.DATATREATMENTS, "Last bolus time: NOTHING FOUND"); + return 0; + } + else { + getAapsLogger().debug(LTag.DATATREATMENTS, "Last bolus time: " + dateUtil.dateAndTimeString(last.date)); + return last.date; } - getAapsLogger().debug(LTag.DATATREATMENTS, "Last bolus time: " + dateUtil.dateAndTimeString(last)); - return last; } - public long getLastBolusTime(boolean isSMB) { - long now = System.currentTimeMillis(); - long last = 0; - synchronized (treatments) { - for (Treatment t : treatments) { - if (!t.isValid) - continue; - if (t.date > last && t.insulin > 0 && t.date <= now && isSMB == t.isSMB) - last = t.date; - } + public long getLastBolusTime(boolean excludeSMB) { + Treatment last = getService().getLastBolus(excludeSMB); + if (last == null) { + getAapsLogger().debug(LTag.DATATREATMENTS, "Last manual bolus time: NOTHING FOUND"); + return 0; + } + else { + getAapsLogger().debug(LTag.DATATREATMENTS, "Last manual bolus time: " + dateUtil.dateAndTimeString(last.date)); + return last.date; } - getAapsLogger().debug(LTag.DATATREATMENTS, "Last manual bolus time: " + dateUtil.dateAndTimeString(last)); - return last; } @Override diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgoTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgoTest.kt index fbc4cd8a8b..a1400ae55f 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgoTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgoTest.kt @@ -30,7 +30,7 @@ class TriggerBolusAgoTest : TriggerTestBase() { @Test fun shouldRunTest() { - `when`(treatmentsPlugin.getLastBolusTime(false)).thenReturn(now) // Set last bolus time to now + `when`(treatmentsPlugin.getLastBolusTime(true)).thenReturn(now) // Set last bolus time to now `when`(DateUtil.now()).thenReturn(now + 10 * 60 * 1000) // set current time to now + 10 min var t = TriggerBolusAgo(injector).setValue(110).comparator(Comparator.Compare.IS_EQUAL) Assert.assertEquals(110, t.minutesAgo.value) @@ -53,7 +53,7 @@ class TriggerBolusAgoTest : TriggerTestBase() { Assert.assertTrue(t.shouldRun()) t = TriggerBolusAgo(injector).setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER) Assert.assertTrue(t.shouldRun()) - PowerMockito.`when`(treatmentsPlugin.getLastBolusTime(false)).thenReturn(0L) // Set last bolus time to 0 + PowerMockito.`when`(treatmentsPlugin.getLastBolusTime(true)).thenReturn(0L) // Set last bolus time to 0 t = TriggerBolusAgo(injector).comparator(Comparator.Compare.IS_NOT_AVAILABLE) Assert.assertTrue(t.shouldRun()) } diff --git a/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRS_Packet_Bolus_Get_Step_Bolus_Information.kt b/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRS_Packet_Bolus_Get_Step_Bolus_Information.kt index d6d7801acc..7cc2d3aa65 100644 --- a/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRS_Packet_Bolus_Get_Step_Bolus_Information.kt +++ b/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRS_Packet_Bolus_Get_Step_Bolus_Information.kt @@ -1,10 +1,11 @@ package info.nightscout.androidaps.danars.comm import dagger.android.HasAndroidInjector -import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.dana.DanaPump import info.nightscout.androidaps.danars.encryption.BleEncryption +import info.nightscout.androidaps.logging.LTag import org.joda.time.DateTime +import org.joda.time.DateTimeZone import javax.inject.Inject class DanaRS_Packet_Bolus_Get_Step_Bolus_Information( @@ -24,7 +25,8 @@ class DanaRS_Packet_Bolus_Get_Step_Bolus_Information( danaPump.initialBolusAmount = intFromBuff(data, 2, 2) / 100.0 val hours = intFromBuff(data, 4, 1) val minutes = intFromBuff(data, 5, 1) - danaPump.lastBolusTime = DateTime.now().withHourOfDay(hours).withMinuteOfHour(minutes).millis + if (danaPump.usingUTC) danaPump.lastBolusTime = DateTime.now().withZone(DateTimeZone.UTC).withHourOfDay(hours).withMinuteOfHour(minutes).millis + else danaPump.lastBolusTime = DateTime.now().withHourOfDay(hours).withMinuteOfHour(minutes).millis danaPump.lastBolusAmount = intFromBuff(data, 6, 2) / 100.0 danaPump.maxBolus = intFromBuff(data, 8, 2) / 100.0 danaPump.bolusStep = intFromBuff(data, 10, 1) / 100.0 diff --git a/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRS_Packet_General_Get_More_Information.kt b/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRS_Packet_General_Get_More_Information.kt index 4f9d15410e..0d110deb71 100644 --- a/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRS_Packet_General_Get_More_Information.kt +++ b/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRS_Packet_General_Get_More_Information.kt @@ -5,6 +5,7 @@ import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.dana.DanaPump import info.nightscout.androidaps.danars.encryption.BleEncryption import org.joda.time.DateTime +import org.joda.time.DateTimeZone import javax.inject.Inject class DanaRS_Packet_General_Get_More_Information( @@ -30,7 +31,8 @@ class DanaRS_Packet_General_Get_More_Information( // val remainRate = intFromBuff(data, 7, 2) / 100.0 val hours = intFromBuff(data, 9, 1) val minutes = intFromBuff(data, 10, 1) - danaPump.lastBolusTime = DateTime.now().withHourOfDay(hours).withMinuteOfHour(minutes).millis + if (danaPump.usingUTC) danaPump.lastBolusTime = DateTime.now().withZone(DateTimeZone.UTC).withHourOfDay(hours).withMinuteOfHour(minutes).millis + else danaPump.lastBolusTime = DateTime.now().withHourOfDay(hours).withMinuteOfHour(minutes).millis danaPump.lastBolusAmount = intFromBuff(data, 11, 2) / 100.0 // On DanaRS DailyUnits can't be more than 160 if (danaPump.dailyTotalUnits > 160) failed = true