Workaround for 0 duration TBR end not syncing

This commit is contained in:
jbr7rr 2023-07-02 21:00:27 +02:00
parent 09aa78f1d1
commit c941926a7e
4 changed files with 37 additions and 33 deletions

View file

@ -413,6 +413,18 @@ class MedtrumPump @Inject constructor(
} else if (basalType == BasalType.NONE && expectedTemporaryBasal?.rate != basalRate && expectedTemporaryBasal?.duration != T.mins(FAKE_TBR_LENGTH).msecs()) { } else if (basalType == BasalType.NONE && expectedTemporaryBasal?.rate != basalRate && expectedTemporaryBasal?.duration != T.mins(FAKE_TBR_LENGTH).msecs()) {
// Pump suspended, set fake TBR // Pump suspended, set fake TBR
setFakeTBR() setFakeTBR()
} else if (basalType == BasalType.STANDARD) {
if (expectedTemporaryBasal != null && System.currentTimeMillis() - basalStartTime > 1000) {
// Pump resumed, but end was not synced before, sync it now
aapsLogger.warn(LTag.PUMPCOMM, "handleBasalStatusUpdate: Pump resumed, but end was not synced before, sync it now")
val success = pumpSync.syncStopTemporaryBasalWithPumpId(
timestamp = basalStartTime + 500, // Time of normal basal start = time of tbr end
endPumpId = basalStartTime + 500, // +500ms Make sure there is time between start and stop of TBR
pumpType = pumpType(),
pumpSerial = pumpSN.toString(radix = 16)
)
aapsLogger.warn(LTag.PUMPCOMM, "handleBasalStatusUpdate: EVENT TEMP_END ${dateUtil.dateAndTimeString(basalStartTime)} ($basalStartTime) success: $success")
}
} }
// Update medtrum pump state // Update medtrum pump state

View file

@ -50,15 +50,15 @@ class CancelTempBasalPacket(injector: HasAndroidInjector) : MedtrumPacket(inject
if (basalType == BasalType.STANDARD) { if (basalType == BasalType.STANDARD) {
// If we have standard here, means TBR is cancelled successfully // If we have standard here, means TBR is cancelled successfully
pumpSync.syncStopTemporaryBasalWithPumpId( val success = pumpSync.syncStopTemporaryBasalWithPumpId(
timestamp = basalStartTime, // Time of normal basal start = time of tbr end timestamp = basalStartTime + 250, // Time of normal basal start = time of tbr end
endPumpId = basalStartTime, endPumpId = basalStartTime + 250, // +250ms Make sure there is time between start and stop of TBR
pumpType = medtrumPump.pumpType(), pumpType = medtrumPump.pumpType(),
pumpSerial = medtrumPump.pumpSN.toString(radix = 16) pumpSerial = medtrumPump.pumpSN.toString(radix = 16)
) )
aapsLogger.debug( aapsLogger.debug(
LTag.PUMPCOMM, LTag.PUMPCOMM,
"CancelTempBasalPacket: EVENT TEMP_END ${dateUtil.dateAndTimeString(basalStartTime)} ($basalStartTime) " "CancelTempBasalPacket: EVENT TEMP_END ${dateUtil.dateAndTimeString(basalStartTime)} ($basalStartTime) success: $success"
) )
} }
} }

View file

@ -220,35 +220,26 @@ class GetRecordPacket(injector: HasAndroidInjector, private val recordIndex: Int
BasalType.ABSOLUTE_TEMP, BasalType.RELATIVE_TEMP -> { BasalType.ABSOLUTE_TEMP, BasalType.RELATIVE_TEMP -> {
aapsLogger.debug(LTag.PUMPCOMM, "GetRecordPacket HandleResponse: BASAL_RECORD: Absolute temp basal") aapsLogger.debug(LTag.PUMPCOMM, "GetRecordPacket HandleResponse: BASAL_RECORD: Absolute temp basal")
val duration = (basalEndTime - basalStartTime) var duration = (basalEndTime - basalStartTime)
if (duration > 0) { // Sync start and end // Work around for pumpSync not accepting 0 duration.
val newRecord = pumpSync.syncTemporaryBasalWithPumpId( // sometimes we get 0 duration for very short basal because the pump only reports time in seconds
timestamp = basalStartTime, if (duration < 250) duration = 250 // 250ms to make sure timestamp is unique
rate = if (basalType == BasalType.ABSOLUTE_TEMP) basalRate else basalPercent.toDouble(),
duration = duration, val newRecord = pumpSync.syncTemporaryBasalWithPumpId(
isAbsolute = (basalType == BasalType.ABSOLUTE_TEMP), timestamp = basalStartTime,
type = PumpSync.TemporaryBasalType.NORMAL, rate = if (basalType == BasalType.ABSOLUTE_TEMP) basalRate else basalPercent.toDouble(),
pumpId = basalStartTime, duration = duration,
pumpType = medtrumPump.pumpType(), isAbsolute = (basalType == BasalType.ABSOLUTE_TEMP),
pumpSerial = medtrumPump.pumpSN.toString(radix = 16) type = PumpSync.TemporaryBasalType.NORMAL,
) pumpId = basalStartTime,
aapsLogger.debug( pumpType = medtrumPump.pumpType(),
LTag.PUMPCOMM, pumpSerial = medtrumPump.pumpSN.toString(radix = 16)
"handleBasalStatusUpdate from record: ${if (newRecord) "**NEW** " else ""}EVENT TEMP_SYNC: ($basalType) ${dateUtil.dateAndTimeString(basalStartTime)} ($basalStartTime) " + )
"Rate: $basalRate Duration: ${duration}" aapsLogger.debug(
) LTag.PUMPCOMM,
} else { // Sync only end ? "handleBasalStatusUpdate from record: ${if (newRecord) "**NEW** " else ""}EVENT TEMP_SYNC: ($basalType) ${dateUtil.dateAndTimeString(basalStartTime)} ($basalStartTime) " +
pumpSync.syncStopTemporaryBasalWithPumpId( "Rate: $basalRate Duration: ${duration}"
timestamp = basalEndTime, )
endPumpId = basalEndTime,
pumpType = medtrumPump.pumpType(),
pumpSerial = medtrumPump.pumpSN.toString(radix = 16)
)
aapsLogger.warn(
LTag.PUMPCOMM,
"handleBasalStatusUpdate from record: EVENT TEMP_END ($basalType) ${dateUtil.dateAndTimeString(basalEndTime)} ($basalEndTime) " + "Rate: $basalRate Duration: ${duration}"
)
}
} }
in BasalType.SUSPEND_LOW_GLUCOSE..BasalType.STOP -> { in BasalType.SUSPEND_LOW_GLUCOSE..BasalType.STOP -> {
aapsLogger.debug(LTag.PUMPCOMM, "GetRecordPacket HandleResponse: BASAL_RECORD: Suspend basal") aapsLogger.debug(LTag.PUMPCOMM, "GetRecordPacket HandleResponse: BASAL_RECORD: Suspend basal")

View file

@ -266,6 +266,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
aapsLogger.warn(LTag.PUMPCOMM, "Alarm cleared: $i") aapsLogger.warn(LTag.PUMPCOMM, "Alarm cleared: $i")
break break
} }
SystemClock.sleep(50)
} }
} }
} }