use bolus size is DetailedBolusInfo search

This commit is contained in:
Milos Kozak 2019-10-03 21:21:12 +02:00
parent 397d3992c6
commit 98f874d1fe
8 changed files with 162 additions and 61 deletions

View file

@ -1,44 +0,0 @@
package info.nightscout.androidaps.plugins.configBuilder;
import androidx.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.logging.L;
/**
* Created by mike on 08.08.2017.
*/
public class DetailedBolusInfoStorage {
private static Logger log = LoggerFactory.getLogger(L.PUMP);
private static List<DetailedBolusInfo> store = new ArrayList<>();
public static synchronized void add(DetailedBolusInfo detailedBolusInfo) {
log.debug("Stored bolus info: " + detailedBolusInfo);
store.add(detailedBolusInfo);
}
@Nullable
public static synchronized DetailedBolusInfo findDetailedBolusInfo(long bolustime) {
DetailedBolusInfo found = null;
for (int i = 0; i < store.size(); i++) {
long infoTime = store.get(i).date;
if (L.isEnabled(L.PUMP))
log.debug("Existing bolus info: " + store.get(i));
if (bolustime > infoTime - 60 * 1000 && bolustime < infoTime + 60 * 1000) {
found = store.get(i);
if (L.isEnabled(L.PUMP))
log.debug("Using & removing bolus info: " + store.get(i));
store.remove(i);
break;
}
}
return found;
}
}

View file

@ -0,0 +1,46 @@
package info.nightscout.androidaps.plugins.pump.common.bolusInfo
import info.nightscout.androidaps.data.DetailedBolusInfo
import info.nightscout.androidaps.logging.L
import info.nightscout.androidaps.utils.T
import org.slf4j.LoggerFactory
import java.util.*
import kotlin.math.abs
object DetailedBolusInfoStorage {
private val log = LoggerFactory.getLogger(L.PUMP)
val store = ArrayList<DetailedBolusInfo>()
@Synchronized
fun add(detailedBolusInfo: DetailedBolusInfo) {
log.debug("Stored bolus info: $detailedBolusInfo")
store.add(detailedBolusInfo)
}
@Synchronized
fun findDetailedBolusInfo(bolusTime: Long, bolus: Double): DetailedBolusInfo? {
// Look for info with bolus
for (i in store.indices) {
val d = store[i]
if (L.isEnabled(L.PUMP))
log.debug("Existing bolus info: " + store[i])
if (bolusTime > d.date - T.mins(1).msecs() && bolusTime < d.date + T.mins(1).msecs() && abs(store[i].insulin - bolus) < 0.01) {
if (L.isEnabled(L.PUMP))
log.debug("Using & removing bolus info: " + store[i])
store.removeAt(i)
return d
}
}
// If not found use time only
for (i in store.indices) {
val d = store[i]
if (bolusTime > d.date - T.mins(1).msecs() && bolusTime < d.date + T.mins(1).msecs() && bolus <= store[i].insulin + 0.01) {
if (L.isEnabled(L.PUMP))
log.debug("Using & removing bolus info: " + store[i])
store.removeAt(i)
return d
}
}
return null
}
}

View file

@ -40,7 +40,7 @@ import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.common.ManufacturerType;
import info.nightscout.androidaps.plugins.configBuilder.DetailedBolusInfoStorage;
import info.nightscout.androidaps.plugins.pump.common.bolusInfo.DetailedBolusInfoStorage;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.general.actions.defs.CustomAction;
import info.nightscout.androidaps.plugins.general.actions.defs.CustomActionType;
@ -399,7 +399,7 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
if (carbTime == 0) carbTime--; // better set 1 min back to prevents clash with insulin
detailedBolusInfo.carbTime = 0;
DetailedBolusInfoStorage.add(detailedBolusInfo); // will be picked up on reading history
DetailedBolusInfoStorage.INSTANCE.add(detailedBolusInfo); // will be picked up on reading history
Treatment t = new Treatment();
t.isSMB = detailedBolusInfo.isSMB;

View file

@ -17,7 +17,7 @@ import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.events.EventPumpStatusChanged;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.configBuilder.DetailedBolusInfoStorage;
import info.nightscout.androidaps.plugins.pump.common.bolusInfo.DetailedBolusInfoStorage;
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.utils.DateUtil;
@ -129,7 +129,7 @@ public class DanaRS_Packet_APS_History_Events extends DanaRS_Packet {
status = "EXTENDEDSTOP " + DateUtil.timeString(datetime);
break;
case DanaRPump.BOLUS:
DetailedBolusInfo detailedBolusInfo = DetailedBolusInfoStorage.findDetailedBolusInfo(datetime);
DetailedBolusInfo detailedBolusInfo = DetailedBolusInfoStorage.INSTANCE.findDetailedBolusInfo(datetime, param1 / 100d);
if (detailedBolusInfo == null) {
detailedBolusInfo = new DetailedBolusInfo();
}
@ -144,7 +144,7 @@ public class DanaRS_Packet_APS_History_Events extends DanaRS_Packet {
status = "BOLUS " + DateUtil.timeString(datetime);
break;
case DanaRPump.DUALBOLUS:
detailedBolusInfo = DetailedBolusInfoStorage.findDetailedBolusInfo(datetime);
detailedBolusInfo = DetailedBolusInfoStorage.INSTANCE.findDetailedBolusInfo(datetime, param1 / 100d);
if (detailedBolusInfo == null) {
detailedBolusInfo = new DetailedBolusInfo();
}

View file

@ -5,8 +5,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import androidx.fragment.app.FragmentActivity;
import androidx.appcompat.app.AlertDialog;
import com.squareup.otto.Subscribe;
@ -19,11 +17,8 @@ import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.interfaces.Constraint;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderFragment;
import info.nightscout.androidaps.plugins.configBuilder.DetailedBolusInfoStorage;
import info.nightscout.androidaps.plugins.pump.common.bolusInfo.DetailedBolusInfoStorage;
import info.nightscout.androidaps.plugins.pump.common.defs.PumpType;
import info.nightscout.androidaps.plugins.pump.danaR.AbstractDanaRPlugin;
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgBolusStartWithSpeed;
@ -156,7 +151,7 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin {
if (carbTime == 0) carbTime--; // better set 1 man back to prevent clash with insulin
detailedBolusInfo.carbTime = 0;
DetailedBolusInfoStorage.add(detailedBolusInfo); // will be picked up on reading history
DetailedBolusInfoStorage.INSTANCE.add(detailedBolusInfo); // will be picked up on reading history
Treatment t = new Treatment();
t.isSMB = detailedBolusInfo.isSMB;

View file

@ -13,7 +13,7 @@ import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.events.EventPumpStatusChanged;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.configBuilder.DetailedBolusInfoStorage;
import info.nightscout.androidaps.plugins.pump.common.bolusInfo.DetailedBolusInfoStorage;
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump;
import info.nightscout.androidaps.plugins.pump.danaR.comm.MessageBase;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
@ -110,7 +110,7 @@ public class MsgHistoryEvents_v2 extends MessageBase {
status = "EXTENDEDSTOP " + DateUtil.timeString(datetime);
break;
case DanaRPump.BOLUS:
DetailedBolusInfo detailedBolusInfo = DetailedBolusInfoStorage.findDetailedBolusInfo(datetime);
DetailedBolusInfo detailedBolusInfo = DetailedBolusInfoStorage.INSTANCE.findDetailedBolusInfo(datetime, param1 / 100d);
if (detailedBolusInfo == null) {
detailedBolusInfo = new DetailedBolusInfo();
}
@ -125,7 +125,7 @@ public class MsgHistoryEvents_v2 extends MessageBase {
status = "BOLUS " + DateUtil.timeString(datetime);
break;
case DanaRPump.DUALBOLUS:
detailedBolusInfo = DetailedBolusInfoStorage.findDetailedBolusInfo(datetime);
detailedBolusInfo = DetailedBolusInfoStorage.INSTANCE.findDetailedBolusInfo(datetime, param1 / 100d);
if (detailedBolusInfo == null) {
detailedBolusInfo = new DetailedBolusInfo();
}

View file

@ -27,7 +27,7 @@ import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.db.TDD;
import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.configBuilder.DetailedBolusInfoStorage;
import info.nightscout.androidaps.plugins.pump.common.bolusInfo.DetailedBolusInfoStorage;
import info.nightscout.androidaps.plugins.pump.common.utils.DateTimeUtil;
import info.nightscout.androidaps.plugins.pump.common.utils.StringUtil;
import info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump.MedtronicPumpHistoryDecoder;
@ -831,7 +831,7 @@ public class MedtronicHistoryData {
} else {
DetailedBolusInfo detailedBolusInfo = DetailedBolusInfoStorage.findDetailedBolusInfo(treatment.date);
DetailedBolusInfo detailedBolusInfo = DetailedBolusInfoStorage.INSTANCE.findDetailedBolusInfo(treatment.date, bolusDTO.getDeliveredAmount());
if (detailedBolusInfo == null) {
detailedBolusInfo = new DetailedBolusInfo();
}

View file

@ -0,0 +1,104 @@
package info.nightscout.androidaps.plugins.pump.common.bolusInfo
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.data.DetailedBolusInfo
import info.nightscout.androidaps.logging.L
import info.nightscout.androidaps.utils.SP
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mockito
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
@RunWith(PowerMockRunner::class)
class DetailedBolusInfoStorageTest {
private val info1 = DetailedBolusInfo()
private val info2 = DetailedBolusInfo()
private val info3 = DetailedBolusInfo()
init {
info1.date = 1000000
info1.insulin = 3.0
info2.date = 1000001
info2.insulin = 4.0
info3.date = 2000000
info3.insulin = 5.0
}
private fun setUp() {
DetailedBolusInfoStorage.store.clear()
DetailedBolusInfoStorage.add(info1)
DetailedBolusInfoStorage.add(info2)
DetailedBolusInfoStorage.add(info3)
}
@Test
fun add() {
DetailedBolusInfoStorage.store.clear()
assertEquals(0, DetailedBolusInfoStorage.store.size)
DetailedBolusInfoStorage.add(info1)
assertEquals(1, DetailedBolusInfoStorage.store.size)
}
@Test
@PrepareForTest(MainApp::class, L::class, SP::class)
fun findDetailedBolusInfo() {
prepareMainApp()
prepareSP()
prepareLogging()
// Look for exact bolus
setUp()
var d: DetailedBolusInfo? = DetailedBolusInfoStorage.findDetailedBolusInfo(1000000, 4.0)
assertEquals(4.0, d!!.insulin, 0.01)
assertEquals(2, DetailedBolusInfoStorage.store.size)
// Look for exact bolus
setUp()
d = DetailedBolusInfoStorage.findDetailedBolusInfo(1000000, 3.0)
assertEquals(3.0, d!!.insulin, 0.01)
assertEquals(2, DetailedBolusInfoStorage.store.size)
// With less insulin (bolus not delivered completely). Should return first one matching date
setUp()
d = DetailedBolusInfoStorage.findDetailedBolusInfo(1000500, 2.0)
assertEquals(3.0, d!!.insulin, 0.01)
assertEquals(2, DetailedBolusInfoStorage.store.size)
// With less insulin (bolus not delivered completely). Should return first one matching date
setUp()
d = DetailedBolusInfoStorage.findDetailedBolusInfo(1000500, 3.5)
assertEquals(4.0, d!!.insulin, 0.01)
assertEquals(2, DetailedBolusInfoStorage.store.size)
// With more insulin should return null
setUp()
d = DetailedBolusInfoStorage.findDetailedBolusInfo(1000500, 4.5)
assertNull(d)
assertEquals(3, DetailedBolusInfoStorage.store.size)
// With more than one minute off should return null
setUp()
d = DetailedBolusInfoStorage.findDetailedBolusInfo(1070000, 4.0)
assertNull(d)
assertEquals(3, DetailedBolusInfoStorage.store.size)
}
private fun prepareMainApp() {
PowerMockito.mockStatic(MainApp::class.java)
val mainApp = Mockito.mock<MainApp>(MainApp::class.java)
Mockito.`when`(MainApp.instance()).thenReturn(mainApp)
Mockito.`when`(MainApp.gs(ArgumentMatchers.anyInt())).thenReturn("some dummy string")
}
private fun prepareSP() {
PowerMockito.mockStatic(SP::class.java)
}
private fun prepareLogging() {
PowerMockito.mockStatic(L::class.java)
Mockito.`when`(L.isEnabled(Mockito.any())).thenReturn(true)
}
}