- finished implementation of TBR and Bolus, needs more testing

- missing 35 minutes
This commit is contained in:
Andy Rozman 2021-05-06 20:30:18 +01:00
parent 9526e8228b
commit f285b59b44
7 changed files with 138 additions and 81 deletions

View file

@ -10,7 +10,8 @@ data class PumpDbEntry constructor(var temporaryId: Long,
var pumpType: PumpType,
var serialNumber: String,
var bolusData: PumpDbEntryBolus? = null,
var tbrData: PumpDbEntryTBR? = null ) {
var tbrData: PumpDbEntryTBR? = null,
var pumpId: Long? = null) {
constructor(temporaryId: Long,
date: Long,

View file

@ -78,7 +78,7 @@ abstract class PumpPluginAbstract protected constructor(
get() = field
set(value) {
field = value
pumpDescription.setPumpDescription(value)
pumpDescription.fillFor(value)
}
@ -90,19 +90,19 @@ abstract class PumpPluginAbstract protected constructor(
super.onStart()
initPumpStatusData()
val intent = Intent(context, serviceClass)
context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
context.bindService(intent, serviceConnection!!, Context.BIND_AUTO_CREATE)
serviceRunning = true
disposable.add(rxBus
.toObservable(EventAppExit::class.java)
.observeOn(aapsSchedulers.io)
.subscribe({ event: EventAppExit? -> context.unbindService(serviceConnection) }) { throwable: Throwable? -> fabricPrivacy.logException(throwable!!) }
.subscribe({ event: EventAppExit? -> context.unbindService(serviceConnection!!) }) { throwable: Throwable? -> fabricPrivacy.logException(throwable!!) }
)
onStartCustomActions()
}
override fun onStop() {
aapsLogger.debug(LTag.PUMP, deviceID() + " onStop()")
context.unbindService(serviceConnection)
context.unbindService(serviceConnection!!)
serviceRunning = false
disposable.clear()
super.onStop()
@ -250,7 +250,7 @@ abstract class PumpPluginAbstract protected constructor(
val extended = JSONObject()
try {
battery.put("percent", pumpStatusData.batteryRemaining)
status.put("status", if (pumpStatusData.pumpStatusType != null) pumpStatusData.pumpStatusType.status else "normal")
status.put("status", pumpStatusData.pumpStatusType.status)
extended.put("Version", version)
try {
extended.put("ActiveProfile", profileName)
@ -386,12 +386,8 @@ abstract class PumpPluginAbstract protected constructor(
}
init {
pumpDescription.setPumpDescription(pumpType)
pumpDescription.fillFor(pumpType)
this.pumpType = pumpType
this.dateUtil = dateUtil
this.aapsSchedulers = aapsSchedulers
//this.pumpSync = pumpSync
}
}

View file

@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.pump.common.data
import info.nightscout.androidaps.plugins.pump.common.defs.PumpStatusType
import info.nightscout.androidaps.plugins.pump.common.defs.PumpType
import info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntry
import java.util.*
/**

View file

@ -48,6 +48,7 @@ import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.BasalProfile
import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.BasalProfile.Companion.getProfilesByHourToString
import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.BasalProfileEntry
import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.TempBasalPair
import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.TempBasalProcessDTO
import info.nightscout.androidaps.plugins.pump.medtronic.defs.*
import info.nightscout.androidaps.plugins.pump.medtronic.defs.MedtronicCommandType.Companion.getSettings
import info.nightscout.androidaps.plugins.pump.medtronic.driver.MedtronicPumpStatus
@ -1011,9 +1012,36 @@ class MedtronicPumpPlugin @Inject constructor(
// activePlugin.activeTreatments.addToHistoryTempBasal(tempBasal)
// TODO need to find solution for this !?
val tempData = PumpDbEntryTBR(0.0, true, 0, TemporaryBasalType.NORMAL)
//val tempData = PumpDbEntryTBR(0.0, true, 0, TemporaryBasalType.NORMAL)
pumpSyncStorage.addTemporaryBasalRateWithTempId(tempData, true, this)
val runningTBR = medtronicPumpStatus.runningTBR
if (runningTBR!=null) {
if (medtronicHistoryData.isTBRActive(runningTBR)) {
val differenceTime = System.currentTimeMillis() - runningTBR.date
val tbrData = runningTBR.tbrData!!
val result = pumpSync.syncTemporaryBasalWithPumpId(
runningTBR.date,
tbrData.rate,
differenceTime,
tbrData.isAbsolute,
tbrData.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))
}
}
//pumpSyncStorage.addTemporaryBasalRateWithTempId(tempData, true, this)
PumpEnactResult(injector).success(true).enacted(true) //
.isTempCancel(true)

View file

@ -72,7 +72,7 @@ class PumpHistoryEntry : MedtronicHistoryEntry() {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is PumpHistoryEntry) return false
val that = other as PumpHistoryEntry
val that = other //as PumpHistoryEntry
return this.pumpId === that.pumpId
// return entryType == that.entryType && //
// atechDateTime === that.atechDateTime // && //

View file

@ -62,6 +62,7 @@ class MedtronicHistoryData @Inject constructor(
) {
val allHistory: MutableList<PumpHistoryEntry> = mutableListOf()
private var allPumpIds: MutableSet<Long> = mutableSetOf()
private var newHistory: MutableList<PumpHistoryEntry> = mutableListOf()
private var isInit = false
@ -80,7 +81,7 @@ class MedtronicHistoryData @Inject constructor(
val validEntries: List<PumpHistoryEntry> = result.validEntries
val newEntries: MutableList<PumpHistoryEntry> = mutableListOf()
for (validEntry in validEntries) {
if (!allHistory.contains(validEntry)) {
if (!allPumpIds.contains(validEntry.pumpId)) {
newEntries.add(validEntry)
} else {
val entryByPumpId = getEntryByPumpId(validEntry.pumpId!!)
@ -137,7 +138,7 @@ class MedtronicHistoryData @Inject constructor(
//aapsLogger.debug(LTag.PUMP, "Filter new entries: Before {}", newHistory);
if (!isCollectionEmpty(newHistory)) {
for (pumpHistoryEntry in newHistory) {
if (!allHistory.contains(pumpHistoryEntry)) {
if (!allPumpIds.contains(pumpHistoryEntry.pumpId)) {
val type = pumpHistoryEntry.entryType
if (type === PumpHistoryEntryType.TempBasalRate || type === PumpHistoryEntryType.TempBasalDuration) {
tbrs.add(pumpHistoryEntry)
@ -160,7 +161,16 @@ class MedtronicHistoryData @Inject constructor(
extendBolusRecords(bolusEstimates, newHistory2)
}
newHistory2.addAll(tbrs)
newHistory = newHistory2
val newHistory3: MutableList<PumpHistoryEntry> = mutableListOf()
for (pumpHistoryEntry in newHistory2) {
if (!allPumpIds.contains(pumpHistoryEntry.pumpId)) {
newHistory3.add(pumpHistoryEntry)
}
}
newHistory = newHistory3
sort(newHistory)
}
aapsLogger.debug(LTag.PUMP, "New History entries found: " + newHistory.size)
@ -192,10 +202,11 @@ class MedtronicHistoryData @Inject constructor(
// add new entries
newHistory.reverse()
for (pumpHistoryEntry in newHistory) {
if (!allHistory.contains(pumpHistoryEntry)) {
if (!allPumpIds.contains(pumpHistoryEntry.pumpId)) {
lastIdUsed++
pumpHistoryEntry.id = lastIdUsed
allHistory.add(pumpHistoryEntry)
allPumpIds.add(pumpHistoryEntry.pumpId!!)
}
}
// if (pheLast == null) // if we don't have any valid record we don't do the filtering and setting
@ -215,6 +226,7 @@ class MedtronicHistoryData @Inject constructor(
for (pumpHistoryEntry in allHistory) {
if (!pumpHistoryEntry.isAfter(dtRemove)) {
removeList.add(pumpHistoryEntry)
allPumpIds.remove(pumpHistoryEntry.pumpId!!)
}
}
allHistory.removeAll(removeList)
@ -262,6 +274,7 @@ class MedtronicHistoryData @Inject constructor(
} else false
}
private fun getDataForPumpSuspends(): MutableList<PumpHistoryEntry> {
val newAndAll: MutableList<PumpHistoryEntry> = mutableListOf()
if (isCollectionNotEmpty(allHistory)) {
@ -389,7 +402,8 @@ class MedtronicHistoryData @Inject constructor(
private fun processPrime(primeRecords: List<PumpHistoryEntry?>) {
val maxAllowedTimeInPast = DateTimeUtil.getATDWithAddedMinutes(GregorianCalendar(), -30)
var lastPrimeRecord = 0L
var lastPrimeRecordTime = 0L
var lastPrimeRecord: PumpHistoryEntry? = null
for (primeRecord in primeRecords) {
val fixedAmount = primeRecord!!.getDecodedDataEntry("FixedAmount")
if (fixedAmount != null && fixedAmount as Float == 0.0f) {
@ -399,43 +413,57 @@ class MedtronicHistoryData @Inject constructor(
continue
}
if (primeRecord.atechDateTime!! > maxAllowedTimeInPast) {
if (lastPrimeRecord < primeRecord.atechDateTime!!) {
lastPrimeRecord = primeRecord.atechDateTime!!
if (lastPrimeRecordTime < primeRecord.atechDateTime!!) {
lastPrimeRecordTime = primeRecord.atechDateTime!!
lastPrimeRecord = primeRecord
}
}
}
if (lastPrimeRecord != 0L) {
val lastPrimeFromAAPS = sp.getLong(MedtronicConst.Statistics.LastPrime, 0L)
if (lastPrimeRecord != lastPrimeFromAAPS) {
uploadCareportalEvent(DateTimeUtil.toMillisFromATD(lastPrimeRecord), DetailedBolusInfo.EventType.CANNULA_CHANGE)
sp.putLong(MedtronicConst.Statistics.LastPrime, lastPrimeRecord)
}
if (lastPrimeRecord != null) {
uploadCareportalEventIfFoundInHistory(lastPrimeRecord,
MedtronicConst.Statistics.LastPrime,
DetailedBolusInfo.EventType.CANNULA_CHANGE)
}
}
private fun processRewind(rewindRecords: List<PumpHistoryEntry?>) {
val maxAllowedTimeInPast = DateTimeUtil.getATDWithAddedMinutes(GregorianCalendar(), -30)
var lastRewindRecord = 0L
var lastRewindRecordTime = 0L
var lastRewindRecord: PumpHistoryEntry? = null
for (rewindRecord in rewindRecords) {
if (rewindRecord!!.atechDateTime!! > maxAllowedTimeInPast) {
if (lastRewindRecord < rewindRecord.atechDateTime!!) {
lastRewindRecord = rewindRecord.atechDateTime!!
if (lastRewindRecordTime < rewindRecord.atechDateTime!!) {
lastRewindRecordTime = rewindRecord.atechDateTime!!
lastRewindRecord = rewindRecord
}
}
}
if (lastRewindRecord != 0L) {
val lastRewindFromAAPS = sp.getLong(MedtronicConst.Statistics.LastRewind, 0L)
if (lastRewindRecord != lastRewindFromAAPS) {
uploadCareportalEvent(DateTimeUtil.toMillisFromATD(lastRewindRecord), DetailedBolusInfo.EventType.INSULIN_CHANGE)
sp.putLong(MedtronicConst.Statistics.LastRewind, lastRewindRecord)
}
if (lastRewindRecord != null) {
uploadCareportalEventIfFoundInHistory(lastRewindRecord,
MedtronicConst.Statistics.LastRewind,
DetailedBolusInfo.EventType.INSULIN_CHANGE)
}
}
private fun uploadCareportalEvent(date: Long, event: DetailedBolusInfo.EventType) {
pumpSync.insertTherapyEventIfNewWithTimestamp(date, event, null, null,
medtronicPumpStatus.pumpType, medtronicPumpStatus.serialNumber!!)
private fun uploadCareportalEventIfFoundInHistory(historyRecord: PumpHistoryEntry, eventSP: String, eventType: DetailedBolusInfo.EventType) {
val lastPrimeFromAAPS = sp.getLong(eventSP, 0L)
if (historyRecord.atechDateTime != lastPrimeFromAAPS) {
var result = pumpSync.insertTherapyEventIfNewWithTimestamp(
DateTimeUtil.toMillisFromATD(historyRecord.atechDateTime!!),
eventType, null,
historyRecord.pumpId,
medtronicPumpStatus.pumpType,
medtronicPumpStatus.serialNumber!!)
aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "insertTherapyEventIfNewWithTimestamp [date=%d, eventType=%d, pumpId=%d, pumpSerial=%s] - Result: %b",
historyRecord.atechDateTime!!, eventType, historyRecord.pumpId,
medtronicPumpStatus.serialNumber!!, result))
sp.putLong(eventSP, historyRecord.atechDateTime!!)
}
}
private fun processTDDs(tddsIn: MutableList<PumpHistoryEntry>) {
val tdds = filterTDDs(tddsIn)
@ -616,7 +644,7 @@ class MedtronicHistoryData @Inject constructor(
val tbrEntry = tempBasalProcessDTO.itemOne!!.getDecodedDataEntry("Object") as TempBasalPair
removeCancelTBRTemporaryRecord(tempBasalProcessDTO, tbrRecords) // TODO
//removeCancelTBRTemporaryRecord(tempBasalProcessDTO, tbrRecords)
if (entryWithTempId!=null) {
val result = pumpSync.syncTemporaryBasalWithTempId(
@ -636,6 +664,14 @@ class MedtronicHistoryData @Inject constructor(
medtronicPumpStatus.serialNumber!!, result))
pumpSyncStorage.removeTemporaryBasalWithTemporaryId(entryWithTempId.temporaryId)
tbrRecords.remove(entryWithTempId)
entryWithTempId.pumpId = tempBasalProcessDTO.pumpId
entryWithTempId.date = tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime)
if (isTBRActive(entryWithTempId)) {
medtronicPumpStatus.runningTBR = entryWithTempId
}
} else {
val result = pumpSync.syncTemporaryBasalWithPumpId(
@ -659,21 +695,26 @@ class MedtronicHistoryData @Inject constructor(
private fun removeCancelTBRTemporaryRecord(tempBasalProcessDTO: TempBasalProcessDTO, tbrRecords: MutableList<PumpDbEntry>) {
//fun syncTemporaryBasalWithPumpId(timestamp: Long, rate: Double, duration: Long, isAbsolute: Boolean, type: PumpSync.TemporaryBasalType?, pumpId: Long, pumpType: PumpType, pumpSerial: String): Boolean
if (tempBasalProcessDTO.cancelPresent) {
//val dateTime : Long = DateTimeUtil.getMillisFromATDWithAddedMinutes(tempBasalProcessDTO.atechDateTime, tempBasalProcessDTO.duration)
val dbEntry = findDbEntry(tempBasalProcessDTO.itemTwo!!, tbrRecords)
if (dbEntry!=null) {
if (dbEntry.tbrData!!.durationInMinutes == 0) {
pumpSync.invalidateTemporaryBasal(dbEntry.temporaryId) // TODO fix
pumpSync.invalidateTemporaryBasalWithTempId(dbEntry.temporaryId)
pumpSyncStorage.removeTemporaryBasalWithTemporaryId(dbEntry.temporaryId)
tbrRecords.remove(dbEntry)
}
}
}
}
//
}
fun isTBRActive(dbEntry: PumpDbEntry) : Boolean {
val endDate = dbEntry.date + (dbEntry.tbrData!!.durationInMinutes * 60 * 1000)
return (endDate > System.currentTimeMillis())
}
@ -777,8 +818,8 @@ class MedtronicHistoryData @Inject constructor(
*/
private fun findDbEntry_Old(treatment: PumpHistoryEntry?, entriesFromHistory: List<DbObjectBase>): DbObjectBase? {
val proposedTime = DateTimeUtil.toMillisFromATD(treatment!!.atechDateTime!!)
//proposedTime += (this.pumpTime.timeDifference * 1000);
if (doubleBolusDebug) aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "DoubleBolusDebug: findDbEntry Treatment=%s, FromDb=%s", treatment, gson.toJson(entriesFromHistory)))
if (entriesFromHistory.size == 0) {
if (doubleBolusDebug) aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "DoubleBolusDebug: findDbEntry Treatment=%s, FromDb=null", treatment))
@ -1268,16 +1309,4 @@ class MedtronicHistoryData @Inject constructor(
const val doubleBolusDebug = false
}
init {
//allHistory = ArrayList()
//this.injector = injector
//this.aapsLogger = aapsLogger
// this.sp = sp
// this.activePlugin = activePlugin
// this.medtronicUtil = medtronicUtil
// this.medtronicPumpHistoryDecoder = medtronicPumpHistoryDecoder
// this.medtronicPumpStatus = medtronicPumpStatus
// databaseHelper = databaseHelperInterface
//this.pumpSync = pumpSync
}
}

View file

@ -8,6 +8,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.PumpDbEntry
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
@ -33,6 +34,7 @@ class MedtronicPumpStatus @Inject constructor(private val resourceHelper: Resour
var pumpFrequency: String? = null
var maxBolus: Double? = null
var maxBasal: Double? = null
var runningTBR: PumpDbEntry? = null
// statuses
var pumpDeviceState = PumpDeviceState.NeverContacted