MedtronicHistoryData lints

This commit is contained in:
Milos Kozak 2021-10-18 10:47:57 +02:00
parent 2bb28245c5
commit 48a521e51b

View file

@ -62,7 +62,6 @@ class MedtronicHistoryData @Inject constructor(
private var pumpTime: ClockDTO? = null private var pumpTime: ClockDTO? = null
private var lastIdUsed: Long = 0 private var lastIdUsed: Long = 0
private var gson: Gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() private var gson: Gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()
private var gsonCore: Gson = GsonBuilder().create()
/** /**
* Add New History entries * Add New History entries
@ -114,7 +113,7 @@ class MedtronicHistoryData @Inject constructor(
val newHistory2: MutableList<PumpHistoryEntry> = mutableListOf() val newHistory2: MutableList<PumpHistoryEntry> = mutableListOf()
var tbrs: MutableList<PumpHistoryEntry> = mutableListOf() var tbrs: MutableList<PumpHistoryEntry> = mutableListOf()
val bolusEstimates: MutableList<PumpHistoryEntry> = mutableListOf() val bolusEstimates: MutableList<PumpHistoryEntry> = mutableListOf()
val atechDate = DateTimeUtil.toATechDate(GregorianCalendar()) val aTechDate = DateTimeUtil.toATechDate(GregorianCalendar())
//aapsLogger.debug(LTag.PUMP, "Filter new entries: Before {}", newHistory); //aapsLogger.debug(LTag.PUMP, "Filter new entries: Before {}", newHistory);
if (!isCollectionEmpty(newHistory)) { if (!isCollectionEmpty(newHistory)) {
@ -128,7 +127,7 @@ class MedtronicHistoryData @Inject constructor(
newHistory2.add(pumpHistoryEntry) newHistory2.add(pumpHistoryEntry)
} else { } else {
if (type === PumpHistoryEntryType.EndResultTotals) { if (type === PumpHistoryEntryType.EndResultTotals) {
if (!DateTimeUtil.isSameDay(atechDate, pumpHistoryEntry.atechDateTime)) { if (!DateTimeUtil.isSameDay(aTechDate, pumpHistoryEntry.atechDateTime)) {
newHistory2.add(pumpHistoryEntry) newHistory2.add(pumpHistoryEntry)
} }
} else { } else {
@ -233,7 +232,7 @@ class MedtronicHistoryData @Inject constructor(
} }
private fun isCollectionNotEmpty(col: List<*>?): Boolean { private fun isCollectionNotEmpty(col: List<*>?): Boolean {
return col != null && !col.isEmpty() return col != null && col.isNotEmpty()
} }
fun isPumpSuspended(): Boolean { fun isPumpSuspended(): Boolean {
@ -280,6 +279,7 @@ class MedtronicHistoryData @Inject constructor(
return newAndAll2 return newAndAll2
} }
@Suppress("SameParameterValue")
private fun filterPumpSuspend(newAndAll: MutableList<PumpHistoryEntry>, filterCount: Int): MutableList<PumpHistoryEntry> { private fun filterPumpSuspend(newAndAll: MutableList<PumpHistoryEntry>, filterCount: Int): MutableList<PumpHistoryEntry> {
if (newAndAll.size <= filterCount) { if (newAndAll.size <= filterCount) {
return newAndAll return newAndAll
@ -296,7 +296,7 @@ class MedtronicHistoryData @Inject constructor(
*/ */
fun processNewHistoryData() { fun processNewHistoryData() {
// Prime (for reseting autosense) // Prime (for resetting autosense)
val primeRecords: MutableList<PumpHistoryEntry> = getFilteredItems(PumpHistoryEntryType.Prime) val primeRecords: MutableList<PumpHistoryEntry> = getFilteredItems(PumpHistoryEntryType.Prime)
aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "ProcessHistoryData: Prime [count=%d, items=%s]", primeRecords.size, gson.toJson(primeRecords))) aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "ProcessHistoryData: Prime [count=%d, items=%s]", primeRecords.size, gson.toJson(primeRecords)))
if (isCollectionNotEmpty(primeRecords)) { if (isCollectionNotEmpty(primeRecords)) {
@ -358,8 +358,7 @@ class MedtronicHistoryData @Inject constructor(
} }
// 'Delivery Suspend' // 'Delivery Suspend'
val suspends: MutableList<TempBasalProcessDTO> val suspends: MutableList<TempBasalProcessDTO> = try {
suspends = try {
getSuspendRecords() getSuspendRecords()
} catch (ex: Exception) { } catch (ex: Exception) {
aapsLogger.error("ProcessHistoryData: Error getting Suspend entries: " + ex.message, ex) aapsLogger.error("ProcessHistoryData: Error getting Suspend entries: " + ex.message, ex)
@ -461,6 +460,7 @@ class MedtronicHistoryData @Inject constructor(
} }
} }
@Suppress("unused")
private enum class ProcessHistoryRecord(val description: String) { private enum class ProcessHistoryRecord(val description: String) {
Bolus("Bolus"), Bolus("Bolus"),
TBR("TBR"), TBR("TBR"),
@ -491,6 +491,7 @@ class MedtronicHistoryData @Inject constructor(
var temporaryId: Long? = null var temporaryId: Long? = null
if (!multiwave) { if (!multiwave) {
@Suppress("Unchecked_Cast")
val entryWithTempId = findDbEntry(bolus, boluses as MutableList<PumpDbEntry>) as PumpDbEntryBolus? val entryWithTempId = findDbEntry(bolus, boluses as MutableList<PumpDbEntry>) as PumpDbEntryBolus?
aapsLogger.debug(LTag.PUMP, "DD: entryWithTempId=$entryWithTempId") aapsLogger.debug(LTag.PUMP, "DD: entryWithTempId=$entryWithTempId")
@ -568,7 +569,7 @@ class MedtronicHistoryData @Inject constructor(
} }
private fun processTBREntries(entryList: MutableList<PumpHistoryEntry>) { private fun processTBREntries(entryList: MutableList<PumpHistoryEntry>) {
Collections.reverse(entryList) entryList.reverse()
val tbr = entryList[0].getDecodedDataEntry("Object") as TempBasalPair val tbr = entryList[0].getDecodedDataEntry("Object") as TempBasalPair
var readOldItem = false var readOldItem = false
if (tbr.isCancelTBR) { if (tbr.isCancelTBR) {
@ -620,9 +621,10 @@ class MedtronicHistoryData @Inject constructor(
aapsLogger.debug(LTag.PUMP, "DD: tempBasalProcessDTO.itemOne: " + gson.toJson(tempBasalProcessDTO.itemOne)) 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!!))) aapsLogger.debug(LTag.PUMP, "DD: tempBasalProcessDTO.itemTwo: " + (if (tempBasalProcessDTO.itemTwo == null) "null" else gson.toJson(tempBasalProcessDTO.itemTwo!!)))
@Suppress("Unchecked_Cast")
val entryWithTempId = findDbEntry(tempBasalProcessDTO.itemOne, tbrRecords as MutableList<PumpDbEntry>) as PumpDbEntryTBR? val entryWithTempId = findDbEntry(tempBasalProcessDTO.itemOne, tbrRecords as MutableList<PumpDbEntry>) as PumpDbEntryTBR?
aapsLogger.debug(LTag.PUMP, "DD: entryWithTempId: " + (if (entryWithTempId == null) "null" else entryWithTempId.toString())) aapsLogger.debug(LTag.PUMP, "DD: entryWithTempId: " + (entryWithTempId?.toString() ?: "null"))
val tbrEntry = tempBasalProcessDTO.itemOneTbr //.getDecodedDataEntry("Object") as TempBasalPair val tbrEntry = tempBasalProcessDTO.itemOneTbr //.getDecodedDataEntry("Object") as TempBasalPair
@ -654,7 +656,7 @@ class MedtronicHistoryData @Inject constructor(
medtronicPumpStatus.pumpType, medtronicPumpStatus.pumpType,
medtronicPumpStatus.serialNumber) medtronicPumpStatus.serialNumber)
aapsLogger.debug(LTag.PUMP, "syncTemporaryBasalWithTempId - Result: ${result}") aapsLogger.debug(LTag.PUMP, "syncTemporaryBasalWithTempId - Result: $result")
pumpSyncStorage.removeTemporaryBasalWithTemporaryId(entryWithTempId.temporaryId) pumpSyncStorage.removeTemporaryBasalWithTemporaryId(entryWithTempId.temporaryId)
tbrRecords.remove(entryWithTempId) tbrRecords.remove(entryWithTempId)
@ -735,14 +737,12 @@ class MedtronicHistoryData @Inject constructor(
/** /**
* findDbEntry - finds Db entries in database, while theoretically this should have same dateTime they * findDbEntry - finds Db entries in database, while theoretically this should have same dateTime they
* don't. Entry on pump is few seconds before treatment in AAPS, and on manual boluses on pump there * don't. Entry on pump is few seconds before treatment in AAPS, and on manual boluses on pump there
* is no treatment at all. For now we look fro tratment that was from 0s - 1m59s within pump entry. * is no treatment at all. For now we look fro treatment that was from 0s - 1m59s within pump entry.
* *
* @param treatment Pump Entry * @param treatment Pump Entry
* @param entriesFromHistory entries from history * @param temporaryEntries entries from history
* @return DbObject from AAPS (if found) * @return DbObject from AAPS (if found)
*/ *
/**
* Looks at all boluses that have temporaryId and find one that is correct for us (if such entry exists) * Looks at all boluses that have temporaryId and find one that is correct for us (if such entry exists)
*/ */
private fun findDbEntry(treatment: PumpHistoryEntry, temporaryEntries: MutableList<PumpDbEntry>): PumpDbEntry? { private fun findDbEntry(treatment: PumpHistoryEntry, temporaryEntries: MutableList<PumpDbEntry>): PumpDbEntry? {
@ -757,7 +757,7 @@ class MedtronicHistoryData @Inject constructor(
this.pumpTime?.let { proposedTime += (it.timeDifference * 1000) } this.pumpTime?.let { proposedTime += (it.timeDifference * 1000) }
val proposedTimeDiff: LongArray = longArrayOf(proposedTime - (2 * 60 * 1000), proposedTime + (2L * 60L * 1000L)) val proposedTimeDiff: LongArray = longArrayOf(proposedTime - (2 * 60 * 1000), proposedTime + (2L * 60L * 1000L))
val tempEntriesList: MutableList<info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntry> = mutableListOf() val tempEntriesList: MutableList<PumpDbEntry> = mutableListOf()
for (temporaryEntry in temporaryEntries) { for (temporaryEntry in temporaryEntries) {
if (temporaryEntry.date > proposedTimeDiff[0] && temporaryEntry.date < proposedTimeDiff[1]) { if (temporaryEntry.date > proposedTimeDiff[0] && temporaryEntry.date < proposedTimeDiff[1]) {
@ -779,7 +779,7 @@ class MedtronicHistoryData @Inject constructor(
sec = 59 sec = 59
} }
val diff = sec * 1000 val diff = sec * 1000
val outList: MutableList<info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntry> = mutableListOf() val outList = mutableListOf<PumpDbEntry>()
for (treatment1 in tempEntriesList) { for (treatment1 in tempEntriesList) {
if (treatment1.date > proposedTime - diff && treatment1.date < proposedTime + diff) { if (treatment1.date > proposedTime - diff && treatment1.date < proposedTime + diff) {
outList.add(treatment1) outList.add(treatment1)
@ -849,7 +849,7 @@ class MedtronicHistoryData @Inject constructor(
// full resume suspends (S R S R) // full resume suspends (S R S R)
filtered2Items.addAll(filteredItems) filtered2Items.addAll(filteredItems)
} else if (filteredItems.size % 2 == 0 && filteredItems[0].entryType === PumpHistoryEntryType.SuspendPump) { } else if (filteredItems.size % 2 == 0 && filteredItems[0].entryType === PumpHistoryEntryType.SuspendPump) {
// not full suspends, need to retrive one more record and discard first one (R S R S) -> ([S] R S R [xS]) // not full suspends, need to retrieve one more record and discard first one (R S R S) -> ([S] R S R [xS])
filteredItems.removeAt(0) filteredItems.removeAt(0)
val oneMoreEntryFromHistory = getOneMoreEntryFromHistory(PumpHistoryEntryType.SuspendPump) val oneMoreEntryFromHistory = getOneMoreEntryFromHistory(PumpHistoryEntryType.SuspendPump)
if (oneMoreEntryFromHistory != null) { if (oneMoreEntryFromHistory != null) {
@ -876,7 +876,7 @@ class MedtronicHistoryData @Inject constructor(
} }
if (filtered2Items.size > 0) { if (filtered2Items.size > 0) {
sort(filtered2Items) sort(filtered2Items)
Collections.reverse(filtered2Items) filtered2Items.reverse()
var i = 0 var i = 0
while (i < filtered2Items.size) { while (i < filtered2Items.size) {
val tbrProcess = TempBasalProcessDTO( val tbrProcess = TempBasalProcessDTO(
@ -992,7 +992,7 @@ class MedtronicHistoryData @Inject constructor(
private fun getOneMoreEntryFromHistory(entryType: PumpHistoryEntryType): PumpHistoryEntry? { private fun getOneMoreEntryFromHistory(entryType: PumpHistoryEntryType): PumpHistoryEntry? {
val filteredItems: List<PumpHistoryEntry?> = getFilteredItems(allHistory, entryType) val filteredItems: List<PumpHistoryEntry?> = getFilteredItems(allHistory, entryType)
return if (filteredItems.size == 0) null else filteredItems[0] return if (filteredItems.isEmpty()) null else filteredItems[0]
} }
private fun filterTDDs(tdds: MutableList<PumpHistoryEntry>): MutableList<PumpHistoryEntry> { private fun filterTDDs(tdds: MutableList<PumpHistoryEntry>): MutableList<PumpHistoryEntry> {
@ -1005,8 +1005,8 @@ class MedtronicHistoryData @Inject constructor(
return if (tddsOut.size == 0) tdds else tddsOut return if (tddsOut.size == 0) tdds else tddsOut
} }
private fun tryToGetByLocalTime(atechDateTime: Long): Long { private fun tryToGetByLocalTime(aTechDateTime: Long): Long {
return DateTimeUtil.toMillisFromATD(atechDateTime) return DateTimeUtil.toMillisFromATD(aTechDateTime)
} }
private fun getTDDType(): PumpHistoryEntryType { private fun getTDDType(): PumpHistoryEntryType {
@ -1031,7 +1031,7 @@ class MedtronicHistoryData @Inject constructor(
fun hasBasalProfileChanged(): Boolean { fun hasBasalProfileChanged(): Boolean {
val filteredItems: List<PumpHistoryEntry?> = getFilteredItems(PumpHistoryEntryType.ChangeBasalProfile_NewProfile) val filteredItems: List<PumpHistoryEntry?> = getFilteredItems(PumpHistoryEntryType.ChangeBasalProfile_NewProfile)
aapsLogger.debug(LTag.PUMP, "hasBasalProfileChanged. Items: " + gson.toJson(filteredItems)) aapsLogger.debug(LTag.PUMP, "hasBasalProfileChanged. Items: " + gson.toJson(filteredItems))
return filteredItems.size > 0 return filteredItems.isNotEmpty()
} }
fun processLastBasalProfileChange(pumpType: PumpType, mdtPumpStatus: MedtronicPumpStatus) { fun processLastBasalProfileChange(pumpType: PumpType, mdtPumpStatus: MedtronicPumpStatus) {
@ -1074,19 +1074,19 @@ class MedtronicHistoryData @Inject constructor(
} }
private fun preProcessTBRs(TBRs_Input: MutableList<PumpHistoryEntry>): MutableList<PumpHistoryEntry> { private fun preProcessTBRs(TBRs_Input: MutableList<PumpHistoryEntry>): MutableList<PumpHistoryEntry> {
val TBRs: MutableList<PumpHistoryEntry> = mutableListOf() val tbrs: MutableList<PumpHistoryEntry> = mutableListOf()
val map: MutableMap<String?, PumpHistoryEntry?> = HashMap() val map: MutableMap<String?, PumpHistoryEntry?> = HashMap()
for (pumpHistoryEntry in TBRs_Input) { for (pumpHistoryEntry in TBRs_Input) {
if (map.containsKey(pumpHistoryEntry.DT)) { if (map.containsKey(pumpHistoryEntry.DT)) {
medtronicPumpHistoryDecoder.decodeTempBasal(map[pumpHistoryEntry.DT]!!, pumpHistoryEntry) medtronicPumpHistoryDecoder.decodeTempBasal(map[pumpHistoryEntry.DT]!!, pumpHistoryEntry)
pumpHistoryEntry.setEntryType(medtronicUtil.medtronicPumpModel, PumpHistoryEntryType.TempBasalCombined) pumpHistoryEntry.setEntryType(medtronicUtil.medtronicPumpModel, PumpHistoryEntryType.TempBasalCombined)
TBRs.add(pumpHistoryEntry) tbrs.add(pumpHistoryEntry)
map.remove(pumpHistoryEntry.DT) map.remove(pumpHistoryEntry.DT)
} else { } else {
map[pumpHistoryEntry.DT] = pumpHistoryEntry map[pumpHistoryEntry.DT] = pumpHistoryEntry
} }
} }
return TBRs return tbrs
} }
private fun getFilteredItems(entryTypes: Set<PumpHistoryEntryType>?): MutableList<PumpHistoryEntry> { private fun getFilteredItems(entryTypes: Set<PumpHistoryEntryType>?): MutableList<PumpHistoryEntry> {
@ -1103,7 +1103,7 @@ class MedtronicHistoryData @Inject constructor(
} else { } else {
val filteredItems: List<PumpHistoryEntry?> = getFilteredItems(entryTypes) val filteredItems: List<PumpHistoryEntry?> = getFilteredItems(entryTypes)
aapsLogger.debug(LTag.PUMP, "Items: $filteredItems") aapsLogger.debug(LTag.PUMP, "Items: $filteredItems")
filteredItems.size > 0 filteredItems.isNotEmpty()
} }
} }