- refactoring of PumpSyncStorage (two storeages entries instead of one), strongly typed objects

- changes on the code to accept that: MedtronicPumpPlugin, MedtronicHistoryData, MedtronicPumpStatus
This commit is contained in:
Andy Rozman 2021-10-09 22:57:22 +01:00
parent a8895668bd
commit 1391061f1c
5 changed files with 202 additions and 120 deletions

View file

@ -1006,24 +1006,23 @@ class MedtronicPumpPlugin @Inject constructor(
if (medtronicHistoryData.isTBRActive(runningTBR)) {
val differenceTime = System.currentTimeMillis() - runningTBR.date
val tbrData = runningTBR.tbrData!!
//val tbrData = runningTBR
val result = pumpSync.syncTemporaryBasalWithPumpId(
runningTBR.date,
tbrData.rate,
runningTBR.rate,
differenceTime,
tbrData.isAbsolute,
tbrData.tbrType,
runningTBR.isAbsolute,
runningTBR.tbrType,
runningTBR.pumpId!!,
runningTBR.pumpType,
runningTBR.serialNumber)
val differenceTimeMin = Math.floor(differenceTime / (60.0 * 1000.0))
aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "canceling running TBR - syncTemporaryBasalWithPumpId [date=%d, pumpId=%d, rate=%.2f U, duration=%d, pumpSerial=%s] - Result: %b",
runningTBR.date, runningTBR.pumpId,
tbrData.rate, differenceTimeMin.toInt(),
medtronicPumpStatus.serialNumber, result))
aapsLogger.debug(LTag.PUMP, "canceling running TBR - syncTemporaryBasalWithPumpId [date=${runningTBR.date}, " +
"pumpId=${runningTBR.pumpId}, rate=${runningTBR.rate} U, duration=${differenceTimeMin.toInt()}, " +
"pumpSerial=${medtronicPumpStatus.serialNumber}] - Result: $result")
}
}

View file

@ -10,6 +10,7 @@ import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.pump.common.defs.PumpType
import info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntry
import info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntryBolus
import info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntryTBR
import info.nightscout.androidaps.plugins.pump.common.utils.DateTimeUtil
import info.nightscout.androidaps.plugins.pump.common.utils.StringUtil
@ -490,17 +491,17 @@ class MedtronicHistoryData @Inject constructor(
var temporaryId: Long? = null
if (!multiwave) {
val entryWithTempId = findDbEntry(bolus, boluses)
val entryWithTempId = findDbEntry(bolus, boluses as MutableList<PumpDbEntry>) as PumpDbEntryBolus?
aapsLogger.debug(LTag.PUMP, "DD: entryWithTempId=$entryWithTempId")
if (entryWithTempId != null) {
aapsLogger.debug(LTag.PUMP, String.format("DD: entryWithTempId.bolusData=%s", if (entryWithTempId.bolusData == null) "null" else entryWithTempId.bolusData))
//aapsLogger.debug(LTag.PUMP, String.format("DD: entryWithTempId.bolusData=%s", if (entryWithTempId.bolusData == null) "null" else entryWithTempId.bolusData))
temporaryId = entryWithTempId.temporaryId
pumpSyncStorage.removeBolusWithTemporaryId(temporaryId)
boluses.remove(entryWithTempId)
type = entryWithTempId.bolusData!!.bolusType
type = entryWithTempId.bolusType
}
}
@ -619,7 +620,7 @@ class MedtronicHistoryData @Inject constructor(
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)
val entryWithTempId = findDbEntry(tempBasalProcessDTO.itemOne, tbrRecords as MutableList<PumpDbEntry>) as PumpDbEntryTBR?
aapsLogger.debug(LTag.PUMP, "DD: entryWithTempId: " + (if (entryWithTempId == null) "null" else entryWithTempId.toString()))
@ -697,16 +698,16 @@ class MedtronicHistoryData @Inject constructor(
if (isTBRActive(startTimestamp = tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime),
durationSeconds = tempBasalProcessDTO.durationAsSeconds)) {
if (medtronicPumpStatus.runningTBR == null) {
medtronicPumpStatus.runningTBR = info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntry(0L,
tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime),
medtronicPumpStatus.pumpType,
medtronicPumpStatus.serialNumber,
null,
PumpDbEntryTBR(rate = tbrEntry.insulinRate,
medtronicPumpStatus.runningTBR = PumpDbEntryTBR(
temporaryId = 0L,
date = tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime),
pumpType = medtronicPumpStatus.pumpType,
serialNumber = medtronicPumpStatus.serialNumber,
entry = PumpDbEntryTBR(rate = tbrEntry.insulinRate,
isAbsolute = !tbrEntry.isPercent,
durationInSeconds = tempBasalProcessDTO.durationAsSeconds,
tbrType = PumpSync.TemporaryBasalType.NORMAL),
tempBasalProcessDTO.pumpId)
pumpId = tempBasalProcessDTO.pumpId)
}
}
} else {
@ -717,10 +718,12 @@ class MedtronicHistoryData @Inject constructor(
} // collection
}
fun isTBRActive(dbEntry: PumpDbEntry): Boolean {
fun isTBRActive(dbEntry: PumpDbEntryTBR): Boolean {
return isTBRActive(
startTimestamp = dbEntry.date,
durationSeconds = dbEntry.tbrData!!.durationInSeconds)
durationSeconds = dbEntry.durationInSeconds)
}
fun isTBRActive(startTimestamp: Long, durationSeconds: Int): Boolean {

View file

@ -7,6 +7,7 @@ import info.nightscout.androidaps.plugins.pump.common.events.EventRileyLinkDevic
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.data.RLHistoryItem
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkTargetDevice
import info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntryTBR
import info.nightscout.androidaps.plugins.pump.medtronic.defs.BasalProfileStatus
import info.nightscout.androidaps.plugins.pump.medtronic.defs.BatteryType
import info.nightscout.androidaps.plugins.pump.medtronic.defs.MedtronicDeviceType
@ -32,7 +33,7 @@ class MedtronicPumpStatus @Inject constructor(private val resourceHelper: Resour
var pumpFrequency: String? = null
var maxBolus: Double? = null
var maxBasal: Double? = null
var runningTBR: info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntry? = null
var runningTBR: PumpDbEntryTBR? = null
// statuses
var pumpDeviceState = PumpDeviceState.NeverContacted

View file

@ -4,45 +4,75 @@ import info.nightscout.androidaps.data.DetailedBolusInfo
import info.nightscout.androidaps.interfaces.PumpSync
import info.nightscout.androidaps.plugins.pump.common.defs.PumpType
data class PumpDbEntry constructor(var temporaryId: Long,
var date: Long,
var pumpType: PumpType,
var serialNumber: String,
var bolusData: PumpDbEntryBolus? = null,
var tbrData: PumpDbEntryTBR? = null,
var pumpId: Long? = null) {
// data class PumpDbEntry constructor(var temporaryId: Long,
// var date: Long,
// var pumpType: PumpType,
// var serialNumber: String,
// var bolusData: PumpDbEntryBolus? = null,
// var tbrData: PumpDbEntryTBR? = null,
// var pumpId: Long? = null) {
//
// constructor(temporaryId: Long,
// date: Long,
// pumpType: PumpType,
// serialNumber: String,
// detailedBolusInfo: DetailedBolusInfo) : this(temporaryId, date, pumpType, serialNumber) {
// this.bolusData = PumpDbEntryBolus(
// detailedBolusInfo.insulin,
// detailedBolusInfo.carbs,
// detailedBolusInfo.bolusType)
// }
//
// constructor(temporaryId: Long,
// date: Long,
// pumpType: PumpType,
// serialNumber: String,
// rate: Double,
// isAbsolute: Boolean,
// durationInMinutes: Int,
// tbrType: PumpSync.TemporaryBasalType) : this(temporaryId, date, pumpType, serialNumber) {
// this.tbrData = PumpDbEntryTBR(
// rate,
// isAbsolute,
// durationInMinutes,
// tbrType)
// }
//
// }
constructor(temporaryId: Long,
date: Long,
pumpType: PumpType,
serialNumber: String,
detailedBolusInfo: DetailedBolusInfo) : this(temporaryId, date, pumpType, serialNumber) {
this.bolusData = PumpDbEntryBolus(
detailedBolusInfo.insulin,
detailedBolusInfo.carbs,
detailedBolusInfo.bolusType)
}
constructor(temporaryId: Long,
date: Long,
pumpType: PumpType,
serialNumber: String,
rate: Double,
isAbsolute: Boolean,
durationInMinutes: Int,
tbrType: PumpSync.TemporaryBasalType) : this(temporaryId, date, pumpType, serialNumber) {
this.tbrData = PumpDbEntryTBR(
rate,
isAbsolute,
durationInMinutes,
tbrType)
}
interface PumpDbEntry {
var temporaryId: Long
var date: Long
var pumpType: PumpType
var serialNumber: String
var pumpId: Long?
}
data class PumpDbEntryBolus(var insulin: Double,
data class PumpDbEntryBolus(override var temporaryId: Long,
override var date: Long,
override var pumpType: PumpType,
override var serialNumber: String,
override var pumpId: Long? = null,
var insulin: Double,
var carbs: Double,
var bolusType: DetailedBolusInfo.BolusType)
var bolusType: DetailedBolusInfo.BolusType) : PumpDbEntry {
constructor(temporaryId: Long,
date: Long,
pumpType: PumpType,
serialNumber: String,
detailedBolusInfo: DetailedBolusInfo) : this(temporaryId, date, pumpType, serialNumber, null,
detailedBolusInfo.insulin,
detailedBolusInfo.carbs,
detailedBolusInfo.bolusType) {
}
}
data class PumpDbEntryCarbs(var date: Long,
var carbs: Double,
@ -57,7 +87,29 @@ data class PumpDbEntryCarbs(var date: Long,
creator.serialNumber())
}
data class PumpDbEntryTBR(var rate: Double,
data class PumpDbEntryTBR(override var temporaryId: Long,
override var date: Long,
override var pumpType: PumpType,
override var serialNumber: String,
override var pumpId: Long? = null,
var rate: Double,
var isAbsolute: Boolean,
var durationInSeconds: Int,
var tbrType: PumpSync.TemporaryBasalType)
var tbrType: PumpSync.TemporaryBasalType) : PumpDbEntry {
constructor(rate: Double,
isAbsolute: Boolean,
durationInSeconds: Int,
tbrType: PumpSync.TemporaryBasalType) : this(0, 0, PumpType.GENERIC_AAPS, "", null,
rate, isAbsolute, durationInSeconds, tbrType)
constructor(temporaryId: Long,
date: Long,
pumpType: PumpType,
serialNumber: String,
entry: PumpDbEntryTBR,
pumpId: Long?
) : this(temporaryId, date, pumpType, serialNumber, pumpId,
entry.rate, entry.isAbsolute, entry.durationInSeconds, entry.tbrType)
}

View file

@ -22,13 +22,13 @@ class PumpSyncStorage @Inject constructor(
) {
companion object {
const val pumpSyncStorageKey: String = "pump_sync_storage_xstream_v2"
const val TBR: String = "TBR"
const val BOLUS: String = "BOLUS"
const val pumpSyncStorageBolusKey: String = "pump_sync_storage_bolus"
const val pumpSyncStorageTBRKey: String = "pump_sync_storage_tbr"
}
var pumpSyncStorage: MutableMap<String, MutableList<PumpDbEntry>> = mutableMapOf()
var pumpSyncStorageBolus: MutableList<PumpDbEntryBolus> = mutableListOf()
var pumpSyncStorageTBR: MutableList<PumpDbEntryTBR> = mutableListOf()
private var storageInitialized: Boolean = false
private var xstream: XStream = XStream()
@ -41,38 +41,58 @@ class PumpSyncStorage @Inject constructor(
if (storageInitialized)
return
var loaded = false
xstream.addPermission(AnyTypePermission.ANY)
if (sp.contains(pumpSyncStorageKey)) {
val jsonData: String = sp.getString(pumpSyncStorageKey, "")
if (sp.contains(pumpSyncStorageBolusKey)) {
val jsonData: String = sp.getString(pumpSyncStorageBolusKey, "")
if (jsonData.isNotBlank()) {
xstream.addPermission(AnyTypePermission.ANY)
@Suppress("UNCHECKED_CAST")
pumpSyncStorage = xstream.fromXML(jsonData, MutableMap::class.java) as MutableMap<String, MutableList<PumpDbEntry>>
pumpSyncStorageBolus = xstream.fromXML(jsonData, MutableList::class.java) as
MutableList<PumpDbEntryBolus>
aapsLogger.debug(LTag.PUMP, String.format("Loading Pump Sync Storage: boluses=%d, tbrs=%d.", pumpSyncStorage[BOLUS]!!.size, pumpSyncStorage[TBR]!!.size))
aapsLogger.debug(LTag.PUMP, "DD: PumpSyncStorage=$pumpSyncStorage")
loaded = true
aapsLogger.debug(LTag.PUMP, "Loading Pump Sync Storage Bolus: boluses=${pumpSyncStorageBolus.size}")
aapsLogger.debug(LTag.PUMP, "DD: PumpSyncStorageBolus=$pumpSyncStorageBolus")
}
}
if (!loaded) {
pumpSyncStorage[BOLUS] = mutableListOf()
pumpSyncStorage[TBR] = mutableListOf()
if (sp.contains(pumpSyncStorageTBRKey)) {
val jsonData: String = sp.getString(pumpSyncStorageTBRKey, "")
if (jsonData.isNotBlank()) {
@Suppress("UNCHECKED_CAST")
pumpSyncStorageTBR = xstream.fromXML(jsonData, MutableList::class.java) as
MutableList<PumpDbEntryTBR>
aapsLogger.debug(LTag.PUMP, "Loading Pump Sync Storage: tbrs=${pumpSyncStorageTBR.size}.")
aapsLogger.debug(LTag.PUMP, "DD: PumpSyncStorageTBR=$pumpSyncStorageTBR")
}
}
}
fun saveStorageBolus() {
if (!pumpSyncStorageBolus.isEmpty()) {
sp.putString(pumpSyncStorageBolusKey, xstream.toXML(pumpSyncStorageBolus))
aapsLogger.debug(LTag.PUMP,"Saving Pump Sync Storage: boluses=${pumpSyncStorageBolus.size}")
} else {
if (sp.contains(pumpSyncStorageBolusKey))
sp.remove(pumpSyncStorageBolusKey)
}
}
fun saveStorage() {
if (!isStorageEmpty()) {
sp.putString(pumpSyncStorageKey, xstream.toXML(pumpSyncStorage))
aapsLogger.debug(String.format("Saving Pump Sync Storage: boluses=%d, tbrs=%d.", pumpSyncStorage[BOLUS]!!.size, pumpSyncStorage[TBR]!!.size))
fun saveStorageTBR() {
if (!pumpSyncStorageTBR.isEmpty()) {
sp.putString(pumpSyncStorageTBRKey, xstream.toXML(pumpSyncStorageTBR))
aapsLogger.debug(LTag.PUMP, "Saving Pump Sync Storage: tbr=${pumpSyncStorageTBR.size}")
} else {
if (sp.contains(pumpSyncStorageTBRKey))
sp.remove(pumpSyncStorageTBRKey)
}
}
private fun cleanOldStorage() {
val oldSpKeys = setOf("pump_sync_storage", "pump_sync_storage_xstream")
val oldSpKeys = setOf("pump_sync_storage", "pump_sync_storage_xstream", "pump_sync_storage_xstream_v2")
for (oldSpKey in oldSpKeys) {
if (sp.contains(oldSpKey))
@ -80,16 +100,12 @@ class PumpSyncStorage @Inject constructor(
}
}
private fun isStorageEmpty(): Boolean {
return pumpSyncStorage[BOLUS]!!.isEmpty() && pumpSyncStorage[TBR]!!.isEmpty()
fun getBoluses(): MutableList<PumpDbEntryBolus> {
return pumpSyncStorageBolus
}
fun getBoluses(): MutableList<PumpDbEntry> {
return pumpSyncStorage[BOLUS]!!
}
fun getTBRs(): MutableList<PumpDbEntry> {
return pumpSyncStorage[TBR]!!
fun getTBRs(): MutableList<PumpDbEntryTBR> {
return pumpSyncStorageTBR
}
fun addBolusWithTempId(detailedBolusInfo: DetailedBolusInfo, writeToInternalHistory: Boolean, creator: PumpSyncEntriesCreator): Boolean {
@ -102,24 +118,25 @@ class PumpSyncStorage @Inject constructor(
creator.model(),
creator.serialNumber())
aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "addBolusWithTempId [date=%d, temporaryId=%d, insulin=%.2f, type=%s, pumpSerial=%s] - Result: %b",
detailedBolusInfo.timestamp, temporaryId, detailedBolusInfo.insulin, detailedBolusInfo.bolusType,
creator.serialNumber(), result))
aapsLogger.debug(LTag.PUMP, "addBolusWithTempId [date=${detailedBolusInfo.timestamp}, temporaryId=$temporaryId, " +
"insulin=${detailedBolusInfo.insulin}, type=${detailedBolusInfo.bolusType}, pumpSerial=${creator.serialNumber()}] - " +
"Result: $result")
if (detailedBolusInfo.carbs > 0.0) {
addCarbs(PumpDbEntryCarbs(detailedBolusInfo, creator))
}
if (result && writeToInternalHistory) {
val innerList: MutableList<PumpDbEntry> = pumpSyncStorage[BOLUS]!!
val dbEntry = PumpDbEntryBolus(temporaryId = temporaryId,
date = detailedBolusInfo.timestamp,
pumpType = creator.model(),
serialNumber = creator.serialNumber(),
detailedBolusInfo = detailedBolusInfo)
val dbEntry = PumpDbEntry(temporaryId, detailedBolusInfo.timestamp, creator.model(), creator.serialNumber(), detailedBolusInfo)
aapsLogger.debug("PumpDbEntryBolus: $dbEntry")
aapsLogger.debug("PumpDbEntry: $dbEntry")
innerList.add(dbEntry)
pumpSyncStorage[BOLUS] = innerList
saveStorage()
pumpSyncStorageBolus.add(dbEntry)
saveStorageBolus()
}
return result
}
@ -132,8 +149,8 @@ class PumpSyncStorage @Inject constructor(
carbsDto.pumpType,
carbsDto.serialNumber)
aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "syncCarbsWithTimestamp [date=%d, carbs=%.2f, pumpSerial=%s] - Result: %b",
carbsDto.date, carbsDto.carbs, carbsDto.serialNumber, result))
aapsLogger.debug(LTag.PUMP, "syncCarbsWithTimestamp [date=${carbsDto.date}, " +
"carbs=${carbsDto.carbs}, pumpSerial=${carbsDto.serialNumber}] - Result: $result")
}
fun addTemporaryBasalRateWithTempId(temporaryBasal: PumpDbEntryTBR, writeToInternalHistory: Boolean, creator: PumpSyncEntriesCreator): Boolean {
@ -151,42 +168,52 @@ class PumpSyncStorage @Inject constructor(
creator.serialNumber())
if (response && writeToInternalHistory) {
val innerList: MutableList<PumpDbEntry> = pumpSyncStorage[TBR]!!
val dbEntry = PumpDbEntryTBR(temporaryId = temporaryId,
date = timeNow,
pumpType = creator.model(),
serialNumber = creator.serialNumber(),
entry = temporaryBasal,
pumpId=null)
innerList.add(PumpDbEntry(temporaryId, timeNow, creator.model(), creator.serialNumber(), null, temporaryBasal))
pumpSyncStorage[BOLUS] = innerList
saveStorage()
aapsLogger.debug("PumpDbEntryTBR: $dbEntry")
pumpSyncStorageTBR.add(dbEntry)
saveStorageTBR()
}
return response
}
fun removeBolusWithTemporaryId(temporaryId: Long) {
val bolusList = removeTemporaryId(temporaryId, pumpSyncStorage[BOLUS]!!)
pumpSyncStorage[BOLUS] = bolusList
saveStorage()
}
var dbEntry: PumpDbEntryBolus? = null
fun removeTemporaryBasalWithTemporaryId(temporaryId: Long) {
val tbrList = removeTemporaryId(temporaryId, pumpSyncStorage[TBR]!!)
pumpSyncStorage[TBR] = tbrList
saveStorage()
}
private fun removeTemporaryId(temporaryId: Long, list: MutableList<PumpDbEntry>): MutableList<PumpDbEntry> {
var dbEntry: PumpDbEntry? = null
for (pumpDbEntry in list) {
for (pumpDbEntry in pumpSyncStorageBolus) {
if (pumpDbEntry.temporaryId == temporaryId) {
dbEntry = pumpDbEntry
}
}
if (dbEntry != null) {
list.remove(dbEntry)
pumpSyncStorageBolus.remove(dbEntry)
}
return list
saveStorageBolus()
}
fun removeTemporaryBasalWithTemporaryId(temporaryId: Long) {
var dbEntry: PumpDbEntryTBR? = null
for (pumpDbEntry in pumpSyncStorageTBR) {
if (pumpDbEntry.temporaryId == temporaryId) {
dbEntry = pumpDbEntry
}
}
if (dbEntry != null) {
pumpSyncStorageTBR.remove(dbEntry)
}
saveStorageTBR()
}
}