From 49f345d42514c35d86c1852710181c85229d9579 Mon Sep 17 00:00:00 2001 From: Andy Rozman Date: Sun, 2 May 2021 13:45:56 +0100 Subject: [PATCH] - startted adding 35 minut - before reading functionality - some refactoring --- .../pump/medtronic/MedtronicPumpPlugin.kt | 12 ++-- .../comm/history/MedtronicHistoryDecoder.kt | 21 +++--- .../comm/history/pump/PumpHistoryEntry.kt | 15 ++++- .../comm/ui/MedtronicUIPostprocessor.kt | 1 + .../pump/medtronic/comm/ui/MedtronicUITask.kt | 9 +-- .../medtronic/data/MedtronicHistoryData.kt | 65 +++++++++++++------ .../dialog/RileyLinkStatusDeviceMedtronic.kt | 9 +-- 7 files changed, 81 insertions(+), 51 deletions(-) diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.kt index 8a1c1ff14e..511f6ef70e 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.kt @@ -466,7 +466,7 @@ class MedtronicPumpPlugin @Inject constructor( } private val basalProfiles: Unit - private get() { + get() { val medtronicUITask = rileyLinkMedtronicService!!.medtronicUIComm.executeCommand(MedtronicCommandType.GetBasalProfileSTD) if (medtronicUITask.responseType === MedtronicUIResponseType.Error) { rileyLinkMedtronicService!!.medtronicUIComm.executeCommand(MedtronicCommandType.GetBasalProfileSTD) @@ -816,7 +816,7 @@ class MedtronicPumpPlugin @Inject constructor( medtronicHistoryData.processLastBasalProfileChange(pumpDescription.pumpType, medtronicPumpStatus) } val previousState = pumpState - if (medtronicHistoryData.isPumpSuspended) { + if (medtronicHistoryData.isPumpSuspended()) { pumpState = PumpDriverState.Suspended aapsLogger.debug(LTag.PUMP, logPrefix + "isPumpSuspended: true") } else { @@ -852,20 +852,19 @@ class MedtronicPumpPlugin @Inject constructor( lastHistoryRecordTime = lastHistoryRecordTime.minusHours(12) // we get last 12 hours of history to // determine pump state // (we don't process that data), we process only - if (timeMinus36h.isAfter(lastHistoryRecordTime)) { - targetDate = timeMinus36h - } targetDate = if (timeMinus36h.isAfter(lastHistoryRecordTime)) timeMinus36h else lastHistoryRecordTime if (debugHistory) aapsLogger.debug(LTag.PUMP, logPrefix + "readPumpHistoryLogic(): targetDate: " + targetDate) } } else { // all other reads if (debugHistory) aapsLogger.debug(LTag.PUMP, logPrefix + "readPumpHistoryLogic(): lastPumpHistoryEntry: not null - " + medtronicUtil.gsonInstance.toJson(lastPumpHistoryEntry)) medtronicHistoryData.setIsInInit(false) + // we need to read 35 minutes in the past so that we can repair any TBR or Bolus values if neeeded + targetDate = LocalDateTime(DateTimeUtil.getMillisFromATDWithAddedMinutes(lastPumpHistoryEntry!!.atechDateTime!!, -35)) } //aapsLogger.debug(LTag.PUMP, "HST: Target Date: " + targetDate); val responseTask2 = rileyLinkMedtronicService!!.medtronicUIComm.executeCommand(MedtronicCommandType.GetHistoryData, - arrayListOf(lastPumpHistoryEntry, targetDate) as ArrayList?) + arrayListOf(/*lastPumpHistoryEntry*/ null, targetDate) as ArrayList?) if (debugHistory) aapsLogger.debug(LTag.PUMP, "HST: After task") val historyResult = responseTask2.result as PumpHistoryResult? if (debugHistory) aapsLogger.debug(LTag.PUMP, "HST: History Result: " + historyResult.toString()) @@ -949,7 +948,6 @@ class MedtronicPumpPlugin @Inject constructor( HashMap(statusRefreshMap) } - else -> null } } diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/MedtronicHistoryDecoder.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/MedtronicHistoryDecoder.kt index 91d64f3efc..65b03e70cb 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/MedtronicHistoryDecoder.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/MedtronicHistoryDecoder.kt @@ -35,7 +35,7 @@ abstract class MedtronicHistoryDecoder : MedtronicHi // TODO_ extend this to also use bigger pages (for now we support only 1024 pages) @Throws(RuntimeException::class) - private fun checkPage(page: RawHistoryPage, partial: Boolean): MutableList { + private fun checkPage(page: RawHistoryPage): MutableList { //val byteList: MutableList = mutableListOf() if (medtronicUtil.medtronicPumpModel == null) { @@ -51,8 +51,14 @@ abstract class MedtronicHistoryDecoder : MedtronicHi } } - fun processPageAndCreateRecords(rawHistoryPage: RawHistoryPage): List { - return processPageAndCreateRecords(rawHistoryPage, false) + fun processPageAndCreateRecords(rawHistoryPage: RawHistoryPage): MutableList { + val dataClear = checkPage(rawHistoryPage) + val records: MutableList = createRecords(dataClear) + for (record in records) { + decodeRecord(record) + } + runPostDecodeTasks() + return records } protected fun prepareStatistics() { @@ -117,13 +123,4 @@ abstract class MedtronicHistoryDecoder : MedtronicHi return StringUtil.getFormatedValueUS(value, decimals) } - private fun processPageAndCreateRecords(rawHistoryPage: RawHistoryPage, partial: Boolean): MutableList { - val dataClear = checkPage(rawHistoryPage, partial) - val records: MutableList = createRecords(dataClear) - for (record in records) { - decodeRecord(record) - } - runPostDecodeTasks() - return records - } } \ No newline at end of file diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntry.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntry.kt index a5a7cbfaa8..b8f37cf379 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntry.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntry.kt @@ -72,9 +72,10 @@ class PumpHistoryEntry : MedtronicHistoryEntry() { override fun equals(other: Any?): Boolean { if (this === other) return true if (other !is PumpHistoryEntry) return false - val that = other - return entryType == that.entryType && // - atechDateTime === that.atechDateTime // && // + val that = other as PumpHistoryEntry + return this.pumpId === that.pumpId + // return entryType == that.entryType && // + // atechDateTime === that.atechDateTime // && // // Objects.equals(this.decodedData, that.decodedData); } @@ -111,4 +112,12 @@ class PumpHistoryEntry : MedtronicHistoryEntry() { set(pumpId) { super.pumpId = pumpId } + + fun hasBolusOrTBRDataChanged(entry: PumpHistoryEntry) : Boolean { + if (entryType!=null && (entryType!! == PumpHistoryEntryType.Bolus || entryType!! == PumpHistoryEntryType.TempBasalCombined)) { + return false // TODO needs to be implemented + } + + return false + } } \ No newline at end of file diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/ui/MedtronicUIPostprocessor.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/ui/MedtronicUIPostprocessor.kt index 9c340798be..f4398bee62 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/ui/MedtronicUIPostprocessor.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/ui/MedtronicUIPostprocessor.kt @@ -55,6 +55,7 @@ class MedtronicUIPostprocessor @Inject constructor( //aapsLogger.debug("D: basal profile on read: " + basalProfile); try { + // TODO need to refactor val profilesByHour = basalProfile!!.getProfilesByHour(medtronicPumpPlugin.pumpDescription.pumpType) if (profilesByHour != null) { medtronicPumpStatus.basalsByHour = profilesByHour diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/ui/MedtronicUITask.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/ui/MedtronicUITask.kt index addcd5d2c3..be334adf76 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/ui/MedtronicUITask.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/ui/MedtronicUITask.kt @@ -101,7 +101,8 @@ class MedtronicUITask { result = communicationManager.cancelTBR() } - MedtronicCommandType.SetBasalProfileSTD, MedtronicCommandType.SetBasalProfileA -> { + MedtronicCommandType.SetBasalProfileSTD, + MedtronicCommandType.SetBasalProfileA -> { val profile = parameters!![0] as BasalProfile result = communicationManager.setBasalProfile(profile) } @@ -128,7 +129,7 @@ class MedtronicUITask { } private fun getTbrSettings(): TempBasalPair? { - return TempBasalPair(getDoubleFromParameters(0), // + return TempBasalPair(getDoubleFromParameters(0)!!, // false, // getIntegerFromParameters(1)) } @@ -137,8 +138,8 @@ class MedtronicUITask { return parameters!![index] as Float } - fun getDoubleFromParameters(index: Int): Double { - return parameters!![index] as Double + fun getDoubleFromParameters(index: Int): Double? { + return parameters!![index] as Double? } private fun getIntegerFromParameters(index: Int): Int { 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 1e82d2b35e..e731eb1ce9 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 @@ -43,9 +43,10 @@ import javax.inject.Singleton // all times that time changed (TZ, DST, etc.). Data needs to be returned in batches (time_changed batches, so that we can // handle it. It would help to assign sort_ids to items (from oldest (1) to newest (x) // -// TODO New Database changes, we need to read 35 minutes from history on each read and then compare if items have the same -// amounts, if not send them back to database changes. ALSO we need to remove and invalidate TBRs that are cancels from -// PumpSyncStorage +// TODO New Database changes: +// + we need to read 35 minutes from history on each read +// - compare all read items if they have the same amounts, if not send them back to database changes. +// - we need to remove and invalidate TBRs that are cancels from PumpSyncStorage @Suppress("DEPRECATION") @Singleton class MedtronicHistoryData @Inject constructor( @@ -82,12 +83,35 @@ class MedtronicHistoryData @Inject constructor( for (validEntry in validEntries) { if (!allHistory.contains(validEntry)) { newEntries.add(validEntry) + } else { + val entryByPumpId = getEntryByPumpId(validEntry.pumpId!!) + + // TODO not implemented + if (entryByPumpId!=null && entryByPumpId.hasBolusOrTBRDataChanged(validEntry)) { + newEntries.add(validEntry) + allHistory.remove(entryByPumpId) + } } } newHistory = newEntries showLogs("List of history (before filtering): [" + newHistory.size + "]", gson.toJson(newHistory)) } + private fun getEntryByPumpId(pumpId: Long): PumpHistoryEntry? { + val findFirst = this.allHistory.stream() + .filter { f -> f.pumpId!! == pumpId } + .findFirst() + + return if (findFirst.isPresent()) findFirst.get() else null + // + // for (pumpHistoryEntry in allHistory) { + // if (pumpHistoryEntry.pumpId == pumpId) { + // return pumpHistoryEntry + // } + // } + // return null + } + private fun showLogs(header: String?, data: String) { if (header != null) { aapsLogger.debug(LTag.PUMP, header) @@ -223,22 +247,21 @@ class MedtronicHistoryData @Inject constructor( } - val isPumpSuspended: Boolean - get() { - val items = getDataForPumpSuspends() - showLogs("isPumpSuspended: ", gson.toJson(items)) - return if (isCollectionNotEmpty(items)) { - val pumpHistoryEntryType = items[0].entryType - val isSuspended = !(pumpHistoryEntryType === PumpHistoryEntryType.TempBasalCombined || // - pumpHistoryEntryType === PumpHistoryEntryType.BasalProfileStart || // - pumpHistoryEntryType === PumpHistoryEntryType.Bolus || // - pumpHistoryEntryType === PumpHistoryEntryType.ResumePump || // - pumpHistoryEntryType === PumpHistoryEntryType.BatteryChange || // - pumpHistoryEntryType === PumpHistoryEntryType.Prime) - aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "isPumpSuspended. Last entry type=%s, isSuspended=%b", pumpHistoryEntryType, isSuspended)) - isSuspended - } else false - } + fun isPumpSuspended(): Boolean { + val items = getDataForPumpSuspends() + showLogs("isPumpSuspended: ", gson.toJson(items)) + return if (isCollectionNotEmpty(items)) { + val pumpHistoryEntryType = items[0].entryType + val isSuspended = !(pumpHistoryEntryType === PumpHistoryEntryType.TempBasalCombined || // + pumpHistoryEntryType === PumpHistoryEntryType.BasalProfileStart || // + pumpHistoryEntryType === PumpHistoryEntryType.Bolus || // + pumpHistoryEntryType === PumpHistoryEntryType.ResumePump || // + pumpHistoryEntryType === PumpHistoryEntryType.BatteryChange || // + pumpHistoryEntryType === PumpHistoryEntryType.Prime) + aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "isPumpSuspended. Last entry type=%s, isSuspended=%b", pumpHistoryEntryType, isSuspended)) + isSuspended + } else false + } private fun getDataForPumpSuspends(): MutableList { val newAndAll: MutableList = mutableListOf() @@ -246,7 +269,7 @@ class MedtronicHistoryData @Inject constructor( newAndAll.addAll(allHistory) } if (isCollectionNotEmpty(newHistory)) { - for (pumpHistoryEntry in newHistory!!) { + for (pumpHistoryEntry in newHistory) { if (!newAndAll.contains(pumpHistoryEntry)) { newAndAll.add(pumpHistoryEntry) } @@ -563,7 +586,7 @@ class MedtronicHistoryData @Inject constructor( var processDTO: TempBasalProcessDTO? = null val processList: MutableList = ArrayList() for (treatment in entryList) { - val tbr2 = treatment!!.getDecodedDataEntry("Object") as TempBasalPair? + val tbr2 = treatment.getDecodedDataEntry("Object") as TempBasalPair? if (tbr2!!.isCancelTBR) { if (processDTO != null) { processDTO.itemTwo = treatment diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/dialog/RileyLinkStatusDeviceMedtronic.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/dialog/RileyLinkStatusDeviceMedtronic.kt index 3fe1eb4d8e..fade5fbed0 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/dialog/RileyLinkStatusDeviceMedtronic.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/dialog/RileyLinkStatusDeviceMedtronic.kt @@ -112,16 +112,17 @@ class RileyLinkStatusDeviceMedtronic : DaggerFragment(), RefreshableInterface { var view = viewIn val viewHolder: ViewHolder // General ListView optimization code. - if (view == null) { +// if (view == null) { view = mInflator.inflate(R.layout.rileylink_status_device_item, null) viewHolder = ViewHolder() viewHolder.itemTime = view.findViewById(R.id.rileylink_history_time) viewHolder.itemSource = view.findViewById(R.id.rileylink_history_source) viewHolder.itemDescription = view.findViewById(R.id.rileylink_history_description) view.tag = viewHolder - } else { - viewHolder = view.tag as ViewHolder - } + // } + // else { + // viewHolder = view.tag as ViewHolder + // } val item = historyItemList[i] viewHolder.itemTime!!.text = StringUtil.toDateTimeString(dateUtil, item.dateTime) viewHolder.itemSource!!.text = "Riley Link" // for now