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()) {
// Pump suspended, set fake TBR
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

View file

@ -50,15 +50,15 @@ class CancelTempBasalPacket(injector: HasAndroidInjector) : MedtrumPacket(inject
if (basalType == BasalType.STANDARD) {
// If we have standard here, means TBR is cancelled successfully
pumpSync.syncStopTemporaryBasalWithPumpId(
timestamp = basalStartTime, // Time of normal basal start = time of tbr end
endPumpId = basalStartTime,
val success = pumpSync.syncStopTemporaryBasalWithPumpId(
timestamp = basalStartTime + 250, // Time of normal basal start = time of tbr end
endPumpId = basalStartTime + 250, // +250ms Make sure there is time between start and stop of TBR
pumpType = medtrumPump.pumpType(),
pumpSerial = medtrumPump.pumpSN.toString(radix = 16)
)
aapsLogger.debug(
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 -> {
aapsLogger.debug(LTag.PUMPCOMM, "GetRecordPacket HandleResponse: BASAL_RECORD: Absolute temp basal")
val duration = (basalEndTime - basalStartTime)
if (duration > 0) { // Sync start and end
val newRecord = pumpSync.syncTemporaryBasalWithPumpId(
timestamp = basalStartTime,
rate = if (basalType == BasalType.ABSOLUTE_TEMP) basalRate else basalPercent.toDouble(),
duration = duration,
isAbsolute = (basalType == BasalType.ABSOLUTE_TEMP),
type = PumpSync.TemporaryBasalType.NORMAL,
pumpId = basalStartTime,
pumpType = medtrumPump.pumpType(),
pumpSerial = medtrumPump.pumpSN.toString(radix = 16)
)
aapsLogger.debug(
LTag.PUMPCOMM,
"handleBasalStatusUpdate from record: ${if (newRecord) "**NEW** " else ""}EVENT TEMP_SYNC: ($basalType) ${dateUtil.dateAndTimeString(basalStartTime)} ($basalStartTime) " +
"Rate: $basalRate Duration: ${duration}"
)
} else { // Sync only end ?
pumpSync.syncStopTemporaryBasalWithPumpId(
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}"
)
}
var duration = (basalEndTime - basalStartTime)
// Work around for pumpSync not accepting 0 duration.
// sometimes we get 0 duration for very short basal because the pump only reports time in seconds
if (duration < 250) duration = 250 // 250ms to make sure timestamp is unique
val newRecord = pumpSync.syncTemporaryBasalWithPumpId(
timestamp = basalStartTime,
rate = if (basalType == BasalType.ABSOLUTE_TEMP) basalRate else basalPercent.toDouble(),
duration = duration,
isAbsolute = (basalType == BasalType.ABSOLUTE_TEMP),
type = PumpSync.TemporaryBasalType.NORMAL,
pumpId = basalStartTime,
pumpType = medtrumPump.pumpType(),
pumpSerial = medtrumPump.pumpSN.toString(radix = 16)
)
aapsLogger.debug(
LTag.PUMPCOMM,
"handleBasalStatusUpdate from record: ${if (newRecord) "**NEW** " else ""}EVENT TEMP_SYNC: ($basalType) ${dateUtil.dateAndTimeString(basalStartTime)} ($basalStartTime) " +
"Rate: $basalRate Duration: ${duration}"
)
}
in BasalType.SUSPEND_LOW_GLUCOSE..BasalType.STOP -> {
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")
break
}
SystemClock.sleep(50)
}
}
}