From ee5413ed067c5382cd8461a96c86bae5f1391794 Mon Sep 17 00:00:00 2001 From: Andy Rozman Date: Tue, 8 Jun 2021 22:31:15 +0100 Subject: [PATCH] - refactoring TempBasalProcess - changed some logging --- .../medtronic/data/MedtronicHistoryData.kt | 140 ++++++++++-------- .../medtronic/data/dto/TempBasalProcessDTO.kt | 32 +++- 2 files changed, 105 insertions(+), 67 deletions(-) diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt index 5c8fa3750f..bcc090302f 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt @@ -615,85 +615,96 @@ class MedtronicHistoryData @Inject constructor( for (tempBasalProcessDTO in processList) { aapsLogger.debug(LTag.PUMP, "DD: tempBasalProcessDTO.itemOne: " + gson.toJson(tempBasalProcessDTO.itemOne)) + aapsLogger.debug(LTag.PUMP, "DD: tempBasalProcessDTO.itemTwo: " + (if (tempBasalProcessDTO.itemTwo == null) "null" else gson.toJson(tempBasalProcessDTO.itemTwo!!))) val entryWithTempId = findDbEntry(tempBasalProcessDTO.itemOne, tbrRecords) aapsLogger.debug(LTag.PUMP, "DD: entryWithTempId: " + (if (entryWithTempId == null) "null" else entryWithTempId.toString())) - val tbrEntry = tempBasalProcessDTO.itemOne.getDecodedDataEntry("Object") as TempBasalPair + val tbrEntry = tempBasalProcessDTO.itemOneTbr //.getDecodedDataEntry("Object") as TempBasalPair aapsLogger.debug(LTag.PUMP, String.format("DD: tbrEntry=%s, tempBasalProcessDTO=%s", gson.toJson(tbrEntry), gson.toJson(tempBasalProcessDTO))) if (entryWithTempId != null) { - aapsLogger.debug(LTag.PUMP, String.format("DD: tempIdEntry=%s, tbrEntry=%s, tempBasalProcessDTO=%s, pumpType=%s, serial=%s", - gson.toJson(entryWithTempId), gson.toJson(tbrEntry), gson.toJson(tempBasalProcessDTO), medtronicPumpStatus.pumpType, medtronicPumpStatus.serialNumber)) + if (tbrEntry != null) { + aapsLogger.debug(LTag.PUMP, String.format("DD: tempIdEntry=%s, tbrEntry=%s, tempBasalProcessDTO=%s, pumpType=%s, serial=%s", + gson.toJson(entryWithTempId), gson.toJson(tbrEntry), gson.toJson(tempBasalProcessDTO), medtronicPumpStatus.pumpType, medtronicPumpStatus.serialNumber)) - aapsLogger.debug(LTag.PUMP, "BEFORE syncTemporaryBasalWithTempId " + - "[date=${tempBasalProcessDTO.atechDateTime}, dateProcess=${tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime)}, " + - "tbrEntry.insulinRate=${tbrEntry.insulinRate}, duration=${tempBasalProcessDTO.duration * 60L * 1000L}, " + - "isAbsolute=${!tbrEntry.isPercent}, temporaryId=${entryWithTempId.temporaryId}, " + - "pumpId=${tempBasalProcessDTO.pumpId}, pumpType=${medtronicPumpStatus.pumpType}, " + - "pumpSerial=${medtronicPumpStatus.serialNumber}]") + aapsLogger.debug(LTag.PUMP, "BEFORE syncTemporaryBasalWithTempId " + + "[date=${tempBasalProcessDTO.atechDateTime}, dateProcess=${tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime)}, " + + "tbrEntry.insulinRate=${tbrEntry.insulinRate}, duration=${tempBasalProcessDTO.duration * 60L * 1000L}, " + + "isAbsolute=${!tbrEntry.isPercent}, temporaryId=${entryWithTempId.temporaryId}, " + + "pumpId=${tempBasalProcessDTO.pumpId}, pumpType=${medtronicPumpStatus.pumpType}, " + + "pumpSerial=${medtronicPumpStatus.serialNumber}]") - val result = pumpSync.syncTemporaryBasalWithTempId( - tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime), - tbrEntry.insulinRate, - tempBasalProcessDTO.duration * 60L * 1000L, - !tbrEntry.isPercent, - entryWithTempId.temporaryId, - PumpSync.TemporaryBasalType.NORMAL, - tempBasalProcessDTO.pumpId, - medtronicPumpStatus.pumpType, - medtronicPumpStatus.serialNumber) + val result = pumpSync.syncTemporaryBasalWithTempId( + tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime), + tbrEntry.insulinRate, + tempBasalProcessDTO.duration * 60L * 1000L, + !tbrEntry.isPercent, + entryWithTempId.temporaryId, + PumpSync.TemporaryBasalType.NORMAL, + tempBasalProcessDTO.pumpId, + medtronicPumpStatus.pumpType, + medtronicPumpStatus.serialNumber) - aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "syncTemporaryBasalWithTempId [date=%d, temporaryId=%d, pumpId=%d, rate=%.2f %s, duration=%d, pumpSerial=%s] - Result: %b", - tempBasalProcessDTO.atechDateTime, entryWithTempId.temporaryId, tempBasalProcessDTO.pumpId, - tbrEntry.insulinRate, (if (tbrEntry.isPercent) "%" else "U"), tempBasalProcessDTO.duration, - medtronicPumpStatus.serialNumber, result)) + aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "syncTemporaryBasalWithTempId [date=%d, temporaryId=%d, pumpId=%d, rate=%.2f %s, duration=%d, pumpSerial=%s] - Result: %b", + tempBasalProcessDTO.atechDateTime, entryWithTempId.temporaryId, tempBasalProcessDTO.pumpId, + tbrEntry.insulinRate, (if (tbrEntry.isPercent) "%" else "U"), tempBasalProcessDTO.duration, + medtronicPumpStatus.serialNumber, result)) - pumpSyncStorage.removeTemporaryBasalWithTemporaryId(entryWithTempId.temporaryId) - tbrRecords.remove(entryWithTempId) + pumpSyncStorage.removeTemporaryBasalWithTemporaryId(entryWithTempId.temporaryId) + tbrRecords.remove(entryWithTempId) - entryWithTempId.pumpId = tempBasalProcessDTO.pumpId - entryWithTempId.date = tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime) + entryWithTempId.pumpId = tempBasalProcessDTO.pumpId + entryWithTempId.date = tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime) - if (isTBRActive(entryWithTempId)) { - medtronicPumpStatus.runningTBR = entryWithTempId + if (isTBRActive(entryWithTempId)) { + medtronicPumpStatus.runningTBR = entryWithTempId + } + } else { + aapsLogger.warn(LTag.PUMP, "tbrEntry (itemOne) is null, shouldn't be.") } } else { - val result = pumpSync.syncTemporaryBasalWithPumpId( - tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime), - tbrEntry.insulinRate, - tempBasalProcessDTO.duration * 60L * 1000L, - !tbrEntry.isPercent, - PumpSync.TemporaryBasalType.NORMAL, - tempBasalProcessDTO.pumpId, - medtronicPumpStatus.pumpType, - medtronicPumpStatus.serialNumber) - aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "syncTemporaryBasalWithPumpId [date=%d, pumpId=%d, rate=%.2f %s, duration=%d, pumpSerial=%s] - Result: %b", - tempBasalProcessDTO.atechDateTime, tempBasalProcessDTO.pumpId, - tbrEntry.insulinRate, (if (tbrEntry.isPercent) "%" else "U"), tempBasalProcessDTO.duration, - medtronicPumpStatus.serialNumber, result)) + if (tbrEntry != null) { - if (medtronicPumpStatus.runningTBR != null) { - if (!isTBRActive(medtronicPumpStatus.runningTBR!!)) { - medtronicPumpStatus.runningTBR = null + val result = pumpSync.syncTemporaryBasalWithPumpId( + tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime), + tbrEntry.insulinRate, + tempBasalProcessDTO.duration * 60L * 1000L, + !tbrEntry.isPercent, + PumpSync.TemporaryBasalType.NORMAL, + tempBasalProcessDTO.pumpId, + medtronicPumpStatus.pumpType, + medtronicPumpStatus.serialNumber) + + aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "syncTemporaryBasalWithPumpId [date=%d, pumpId=%d, rate=%.2f %s, duration=%d, pumpSerial=%s] - Result: %b", + tempBasalProcessDTO.atechDateTime, tempBasalProcessDTO.pumpId, + tbrEntry.insulinRate, (if (tbrEntry.isPercent) "%" else "U"), tempBasalProcessDTO.duration, + medtronicPumpStatus.serialNumber, result)) + + if (medtronicPumpStatus.runningTBR != null) { + if (!isTBRActive(medtronicPumpStatus.runningTBR!!)) { + medtronicPumpStatus.runningTBR = null + } } - } - if (isTBRActive(tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime), tempBasalProcessDTO.duration)) { - if (medtronicPumpStatus.runningTBR == null) { - medtronicPumpStatus.runningTBR = info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntry(0L, - tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime), - medtronicPumpStatus.pumpType, - medtronicPumpStatus.serialNumber, - null, - info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntryTBR(tbrEntry.insulinRate, !tbrEntry.isPercent, tempBasalProcessDTO.duration, PumpSync.TemporaryBasalType.NORMAL), - tempBasalProcessDTO.pumpId) + if (isTBRActive(tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime), tempBasalProcessDTO.duration)) { + if (medtronicPumpStatus.runningTBR == null) { + medtronicPumpStatus.runningTBR = info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntry(0L, + tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime), + medtronicPumpStatus.pumpType, + medtronicPumpStatus.serialNumber, + null, + info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntryTBR(tbrEntry.insulinRate, !tbrEntry.isPercent, tempBasalProcessDTO.duration, PumpSync.TemporaryBasalType.NORMAL), + tempBasalProcessDTO.pumpId) + } } + } else { + aapsLogger.warn(LTag.PUMP, "tbrEntry (itemOne) is null, shouldn't be.") } } } // for @@ -852,11 +863,14 @@ class MedtronicHistoryData @Inject constructor( Collections.reverse(filtered2Items) var i = 0 while (i < filtered2Items.size) { - outList.add(TempBasalProcessDTO( + val tbrProcess = TempBasalProcessDTO( itemOne = filtered2Items[i], - itemTwo = filtered2Items[i + 1], processOperation = TempBasalProcessDTO.Operation.Add, - aapsLogger = aapsLogger)) + aapsLogger = aapsLogger) + + tbrProcess.itemTwo = filtered2Items[i + 1] + + outList.add(tbrProcess) i += 2 } @@ -918,11 +932,15 @@ class MedtronicHistoryData @Inject constructor( var itemTwo = items[0] items = getFilteredItems(tempData, PumpHistoryEntryType.NoDeliveryAlarm) if (items.size > 0) { - outList.add(TempBasalProcessDTO( + val tbrProcess = TempBasalProcessDTO( itemOne = items[items.size - 1], - itemTwo = itemTwo, processOperation = TempBasalProcessDTO.Operation.Add, - aapsLogger = aapsLogger)) + aapsLogger = aapsLogger) + + tbrProcess.itemTwo = itemTwo + + outList.add(tbrProcess) + return outList } items = getFilteredItems(tempData, PumpHistoryEntryType.Rewind) diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/TempBasalProcessDTO.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/TempBasalProcessDTO.kt index 9cbb0b6dbc..04a7983e3c 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/TempBasalProcessDTO.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/TempBasalProcessDTO.kt @@ -1,14 +1,23 @@ package info.nightscout.androidaps.plugins.pump.medtronic.data.dto import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.common.utils.DateTimeUtil import info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump.PumpHistoryEntry class TempBasalProcessDTO constructor(var itemOne: PumpHistoryEntry, - var itemTwo: PumpHistoryEntry? = null, var processOperation: Operation = Operation.None, var aapsLogger: AAPSLogger) { + var itemTwo: PumpHistoryEntry? = null + set(value) { + field = value + itemTwoTbr = value!!.getDecodedDataEntry("Object") as TempBasalPair + } + + var itemOneTbr: TempBasalPair? = null + var itemTwoTbr: TempBasalPair? = null + var cancelPresent: Boolean = false val atechDateTime: Long @@ -19,17 +28,28 @@ class TempBasalProcessDTO constructor(var itemOne: PumpHistoryEntry, val duration: Int get() = if (itemTwo == null) { - val tbr = itemOne.getDecodedDataEntry("Object") as? TempBasalPair - if (tbr != null) - tbr.durationMinutes - else { + if (itemOneTbr != null) { + aapsLogger.debug("TemporaryBasalPair: $itemOneTbr") + itemOneTbr!!.durationMinutes + } else { aapsLogger.error("Couldn't find TempBasalPair in entry: $itemOne") 0 } } else { - DateTimeUtil.getATechDateDiferenceAsMinutes(itemOne.atechDateTime, itemTwo!!.atechDateTime) + aapsLogger.debug(LTag.PUMP, "Found 2 items for duration: itemOne=$itemOne, itemTwo=$itemTwo") + val minuteDiff = DateTimeUtil.getATechDateDiferenceAsMinutes(itemOne.atechDateTime, itemTwo!!.atechDateTime) + aapsLogger.debug(LTag.PUMP, "Difference in minutes: $minuteDiff") + minuteDiff } + init { + itemOneTbr = itemOne.getDecodedDataEntry("Object") as TempBasalPair + } + + override fun toString(): String { + return "ItemOne: $itemOne, ItemTwo: $itemTwo, Duration: $duration, Operation: $processOperation" + } + enum class Operation { None, Add, Edit }