double SMB fix

This commit is contained in:
Milos Kozak 2020-05-26 19:58:50 +02:00
parent b9806d0ea9
commit c5aa12c5ba
6 changed files with 52 additions and 29 deletions

View file

@ -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())

View file

@ -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<DatabaseHelper> {
}
}
/**
* Returns the newest record with insulin > 0
*/
@Nullable
public Treatment getLastBolus(boolean excludeSMB) {
try {
QueryBuilder<Treatment, Long> 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<Treatment> 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())

View file

@ -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

View file

@ -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())
}

View file

@ -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

View file

@ -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