Sync bolus initially when bolus command is accepted by pump
This commit is contained in:
parent
17aecd912a
commit
e9964604a1
3 changed files with 57 additions and 19 deletions
|
@ -77,7 +77,6 @@ import kotlin.math.round
|
||||||
private val uiInteraction: UiInteraction,
|
private val uiInteraction: UiInteraction,
|
||||||
private val profileFunction: ProfileFunction,
|
private val profileFunction: ProfileFunction,
|
||||||
private val pumpSync: PumpSync,
|
private val pumpSync: PumpSync,
|
||||||
private val detailedBolusInfoStorage: DetailedBolusInfoStorage,
|
|
||||||
private val temporaryBasalStorage: TemporaryBasalStorage
|
private val temporaryBasalStorage: TemporaryBasalStorage
|
||||||
) : PumpPluginBase(
|
) : PumpPluginBase(
|
||||||
PluginDescription()
|
PluginDescription()
|
||||||
|
@ -233,9 +232,8 @@ import kotlin.math.round
|
||||||
detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(Constraint(detailedBolusInfo.insulin)).value()
|
detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(Constraint(detailedBolusInfo.insulin)).value()
|
||||||
return if (detailedBolusInfo.insulin > 0 && detailedBolusInfo.carbs == 0.0) {
|
return if (detailedBolusInfo.insulin > 0 && detailedBolusInfo.carbs == 0.0) {
|
||||||
aapsLogger.debug(LTag.PUMP, "deliverTreatment: Delivering bolus: " + detailedBolusInfo.insulin + "U")
|
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 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)
|
val result = PumpEnactResult(injector)
|
||||||
result.success = connectionOK && abs(detailedBolusInfo.insulin - t.insulin) < pumpDescription.bolusStep
|
result.success = connectionOK && abs(detailedBolusInfo.insulin - t.insulin) < pumpDescription.bolusStep
|
||||||
result.bolusDelivered = t.insulin
|
result.bolusDelivered = t.insulin
|
||||||
|
|
|
@ -107,24 +107,40 @@ class GetRecordPacket(injector: HasAndroidInjector, private val recordIndex: Int
|
||||||
LTag.PUMPCOMM,
|
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"
|
"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) {
|
if (bolusType == BolusType.NORMAL) {
|
||||||
val detailedBolusInfo = detailedBolusInfoStorage.findDetailedBolusInfo(bolusStartTime, bolusNormalDelivered)
|
val detailedBolusInfo = detailedBolusInfoStorage.findDetailedBolusInfo(bolusStartTime, bolusNormalDelivered)
|
||||||
val newRecord = pumpSync.syncBolusWithPumpId(
|
var newRecord = false
|
||||||
timestamp = bolusStartTime,
|
if (detailedBolusInfo != null) {
|
||||||
amount = bolusNormalDelivered,
|
val success = pumpSync.syncBolusWithTempId(
|
||||||
type = detailedBolusInfo?.bolusType,
|
timestamp = bolusStartTime,
|
||||||
pumpId = bolusStartTime,
|
amount = bolusNormalDelivered,
|
||||||
pumpType = medtrumPump.pumpType(),
|
temporaryId = detailedBolusInfo.timestamp,
|
||||||
pumpSerial = medtrumPump.pumpSN.toString(radix = 16)
|
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(
|
aapsLogger.debug(
|
||||||
LTag.PUMPCOMM,
|
LTag.PUMPCOMM,
|
||||||
"from record: ${if (newRecord) "**NEW** " else ""}EVENT BOLUS ${dateUtil.dateAndTimeString(bolusStartTime)} ($bolusStartTime) Bolus: ${bolusNormalDelivered}U "
|
"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) {
|
if (bolusStartTime > medtrumPump.lastBolusTime) {
|
||||||
medtrumPump.lastBolusTime = bolusStartTime
|
medtrumPump.lastBolusTime = bolusStartTime
|
||||||
medtrumPump.lastBolusAmount = bolusNormalDelivered
|
medtrumPump.lastBolusAmount = bolusNormalDelivered
|
||||||
|
|
|
@ -14,6 +14,8 @@ import info.nightscout.interfaces.notifications.Notification
|
||||||
import info.nightscout.interfaces.plugin.ActivePlugin
|
import info.nightscout.interfaces.plugin.ActivePlugin
|
||||||
import info.nightscout.interfaces.profile.Profile
|
import info.nightscout.interfaces.profile.Profile
|
||||||
import info.nightscout.interfaces.profile.ProfileFunction
|
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.PumpSync
|
||||||
import info.nightscout.interfaces.pump.defs.PumpType
|
import info.nightscout.interfaces.pump.defs.PumpType
|
||||||
import info.nightscout.interfaces.queue.Callback
|
import info.nightscout.interfaces.queue.Callback
|
||||||
|
@ -70,6 +72,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
@Inject lateinit var bleComm: BLEComm
|
@Inject lateinit var bleComm: BLEComm
|
||||||
@Inject lateinit var fabricPrivacy: FabricPrivacy
|
@Inject lateinit var fabricPrivacy: FabricPrivacy
|
||||||
@Inject lateinit var pumpSync: PumpSync
|
@Inject lateinit var pumpSync: PumpSync
|
||||||
|
@Inject lateinit var detailedBolusInfoStorage: DetailedBolusInfoStorage
|
||||||
@Inject lateinit var dateUtil: DateUtil
|
@Inject lateinit var dateUtil: DateUtil
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -245,18 +248,39 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
return sendPacketAndGetResponse(SetPatchPacket(injector))
|
return sendPacketAndGetResponse(SetPatchPacket(injector))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setBolus(insulin: Double, t: EventOverviewBolusProgress.Treatment): Boolean {
|
fun setBolus(detailedBolusInfo: DetailedBolusInfo, t: EventOverviewBolusProgress.Treatment): Boolean {
|
||||||
if (!isConnected) return false
|
if (!isConnected) return false
|
||||||
|
val insulin = detailedBolusInfo.insulin
|
||||||
val result = sendPacketAndGetResponse(SetBolusPacket(injector, insulin))
|
val result = sendPacketAndGetResponse(SetBolusPacket(injector, insulin))
|
||||||
|
|
||||||
|
val bolusStart = System.currentTimeMillis()
|
||||||
|
|
||||||
medtrumPump.bolusDone = false
|
medtrumPump.bolusDone = false
|
||||||
medtrumPump.bolusingTreatment = t
|
medtrumPump.bolusingTreatment = t
|
||||||
medtrumPump.bolusAmountToBeDelivered = insulin
|
medtrumPump.bolusAmountToBeDelivered = insulin
|
||||||
medtrumPump.bolusStopped = false
|
medtrumPump.bolusStopped = false
|
||||||
medtrumPump.bolusStopForced = 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
|
val bolusingEvent = EventOverviewBolusProgress
|
||||||
while (medtrumPump.bolusStopped == false && result == true && medtrumPump.bolusDone == false) {
|
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()) {
|
if (System.currentTimeMillis() - medtrumPump.bolusProgressLastTimeStamp > T.secs(15).msecs()) {
|
||||||
medtrumPump.bolusStopped = true
|
medtrumPump.bolusStopped = true
|
||||||
medtrumPump.bolusStopForced = true
|
medtrumPump.bolusStopForced = true
|
||||||
aapsLogger.debug(LTag.PUMPCOMM, "Communication stopped")
|
aapsLogger.warn(LTag.PUMPCOMM, "Communication stopped")
|
||||||
bleComm.disconnect("Communication stopped")
|
bleComm.disconnect("Communication stopped")
|
||||||
} else {
|
} else {
|
||||||
bolusingEvent.t = medtrumPump.bolusingTreatment
|
bolusingEvent.t = medtrumPump.bolusingTreatment
|
||||||
|
|
Loading…
Reference in a new issue