Sync bolus initially when bolus command is accepted by pump

This commit is contained in:
jbr7rr 2023-06-22 18:55:58 +02:00
parent 17aecd912a
commit e9964604a1
3 changed files with 57 additions and 19 deletions

View file

@ -77,7 +77,6 @@ import kotlin.math.round
private val uiInteraction: UiInteraction,
private val profileFunction: ProfileFunction,
private val pumpSync: PumpSync,
private val detailedBolusInfoStorage: DetailedBolusInfoStorage,
private val temporaryBasalStorage: TemporaryBasalStorage
) : PumpPluginBase(
PluginDescription()
@ -233,9 +232,8 @@ import kotlin.math.round
detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(Constraint(detailedBolusInfo.insulin)).value()
return if (detailedBolusInfo.insulin > 0 && detailedBolusInfo.carbs == 0.0) {
aapsLogger.debug(LTag.PUMP, "deliverTreatment: Delivering bolus: " + detailedBolusInfo.insulin + "U")
detailedBolusInfoStorage.add(detailedBolusInfo) // will be picked up on reading history
val t = EventOverviewBolusProgress.Treatment(0.0, 0, detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.id)
val connectionOK = medtrumService?.setBolus(detailedBolusInfo.insulin, t) ?: false
val connectionOK = medtrumService?.setBolus(detailedBolusInfo, t) ?: false
val result = PumpEnactResult(injector)
result.success = connectionOK && abs(detailedBolusInfo.insulin - t.insulin) < pumpDescription.bolusStep
result.bolusDelivered = t.insulin

View file

@ -107,24 +107,40 @@ class GetRecordPacket(injector: HasAndroidInjector, private val recordIndex: Int
LTag.PUMPCOMM,
"GetRecordPacket HandleResponse: BOLUS_RECORD: typeAndWizard: $typeAndWizard, bolusCause: $bolusCause, unknown: $unknown, bolusStartTime: $bolusStartTime, " + "bolusNormalAmount: $bolusNormalAmount, bolusNormalDelivered: $bolusNormalDelivered, bolusExtendedAmount: $bolusExtendedAmount, bolusExtendedDuration: $bolusExtendedDuration, " + "bolusExtendedDelivered: $bolusExtendedDelivered, bolusCarb: $bolusCarb, bolusGlucose: $bolusGlucose, bolusIOB: $bolusIOB, unkown1: $unkown1, unkown2: $unkown2, " + "bolusType: $bolusType, bolusWizard: $bolusWizard"
)
if (bolusType == BolusType.NORMAL) {
val detailedBolusInfo = detailedBolusInfoStorage.findDetailedBolusInfo(bolusStartTime, bolusNormalDelivered)
val newRecord = pumpSync.syncBolusWithPumpId(
timestamp = bolusStartTime,
amount = bolusNormalDelivered,
type = detailedBolusInfo?.bolusType,
pumpId = bolusStartTime,
pumpType = medtrumPump.pumpType(),
pumpSerial = medtrumPump.pumpSN.toString(radix = 16)
)
var newRecord = false
if (detailedBolusInfo != null) {
val success = pumpSync.syncBolusWithTempId(
timestamp = bolusStartTime,
amount = bolusNormalDelivered,
temporaryId = detailedBolusInfo.timestamp,
type = detailedBolusInfo.bolusType,
pumpId = bolusStartTime,
pumpType = medtrumPump.pumpType(),
pumpSerial = medtrumPump.pumpSN.toString(radix = 16)
)
if (success == false) {
aapsLogger.warn(LTag.PUMPCOMM, "GetRecordPacket HandleResponse: BOLUS_RECORD: Failed to sync bolus with tempId: ${detailedBolusInfo.timestamp}")
// detailedInfo can be from another similar record. Reinsert
detailedBolusInfoStorage.add(detailedBolusInfo)
}
} else {
newRecord = pumpSync.syncBolusWithPumpId(
timestamp = bolusStartTime,
amount = bolusNormalDelivered,
type = null,
pumpId = bolusStartTime,
pumpType = medtrumPump.pumpType(),
pumpSerial = medtrumPump.pumpSN.toString(radix = 16)
)
}
aapsLogger.debug(
LTag.PUMPCOMM,
"from record: ${if (newRecord) "**NEW** " else ""}EVENT BOLUS ${dateUtil.dateAndTimeString(bolusStartTime)} ($bolusStartTime) Bolus: ${bolusNormalDelivered}U "
)
if (!newRecord && detailedBolusInfo != null) {
// detailedInfo can be from another similar record. Reinsert
detailedBolusInfoStorage.add(detailedBolusInfo)
}
if (bolusStartTime > medtrumPump.lastBolusTime) {
medtrumPump.lastBolusTime = bolusStartTime
medtrumPump.lastBolusAmount = bolusNormalDelivered

View file

@ -14,6 +14,8 @@ import info.nightscout.interfaces.notifications.Notification
import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.interfaces.profile.Profile
import info.nightscout.interfaces.profile.ProfileFunction
import info.nightscout.interfaces.pump.DetailedBolusInfo
import info.nightscout.interfaces.pump.DetailedBolusInfoStorage
import info.nightscout.interfaces.pump.PumpSync
import info.nightscout.interfaces.pump.defs.PumpType
import info.nightscout.interfaces.queue.Callback
@ -70,6 +72,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
@Inject lateinit var bleComm: BLEComm
@Inject lateinit var fabricPrivacy: FabricPrivacy
@Inject lateinit var pumpSync: PumpSync
@Inject lateinit var detailedBolusInfoStorage: DetailedBolusInfoStorage
@Inject lateinit var dateUtil: DateUtil
companion object {
@ -245,18 +248,39 @@ class MedtrumService : DaggerService(), BLECommCallback {
return sendPacketAndGetResponse(SetPatchPacket(injector))
}
fun setBolus(insulin: Double, t: EventOverviewBolusProgress.Treatment): Boolean {
fun setBolus(detailedBolusInfo: DetailedBolusInfo, t: EventOverviewBolusProgress.Treatment): Boolean {
if (!isConnected) return false
val insulin = detailedBolusInfo.insulin
val result = sendPacketAndGetResponse(SetBolusPacket(injector, insulin))
val bolusStart = System.currentTimeMillis()
medtrumPump.bolusDone = false
medtrumPump.bolusingTreatment = t
medtrumPump.bolusAmountToBeDelivered = insulin
medtrumPump.bolusStopped = false
medtrumPump.bolusStopForced = false
medtrumPump.bolusProgressLastTimeStamp = dateUtil.now()
medtrumPump.bolusProgressLastTimeStamp = bolusStart
val bolusStart = System.currentTimeMillis()
detailedBolusInfo.timestamp = bolusStart // Make sure the timestamp is set to the start of the bolus
detailedBolusInfoStorage.add(detailedBolusInfo) // will be picked up on reading history
// Sync the initial bolus
val newRecord = pumpSync.addBolusWithTempId(
timestamp = detailedBolusInfo.timestamp,
amount = detailedBolusInfo.insulin,
temporaryId = detailedBolusInfo.timestamp,
type = detailedBolusInfo.bolusType,
pumpType = medtrumPump.pumpType(),
pumpSerial = medtrumPump.pumpSN.toString(radix = 16)
)
if (newRecord) {
aapsLogger.debug(
LTag.PUMPCOMM,
"set bolus: **NEW** EVENT BOLUS (tempId) ${dateUtil.dateAndTimeString(detailedBolusInfo.timestamp)} (${detailedBolusInfo.timestamp}) Bolus: ${detailedBolusInfo.insulin}U "
)
} else {
aapsLogger.error(LTag.PUMPCOMM, "Bolus with tempId ${detailedBolusInfo.timestamp} already exists")
}
val bolusingEvent = EventOverviewBolusProgress
while (medtrumPump.bolusStopped == false && result == true && medtrumPump.bolusDone == false) {
@ -264,7 +288,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
if (System.currentTimeMillis() - medtrumPump.bolusProgressLastTimeStamp > T.secs(15).msecs()) {
medtrumPump.bolusStopped = true
medtrumPump.bolusStopForced = true
aapsLogger.debug(LTag.PUMPCOMM, "Communication stopped")
aapsLogger.warn(LTag.PUMPCOMM, "Communication stopped")
bleComm.disconnect("Communication stopped")
} else {
bolusingEvent.t = medtrumPump.bolusingTreatment