diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/sync/PumpDbEntry.kt b/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/sync/PumpDbEntry.kt index 65bd3d0728..c84312cebc 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/sync/PumpDbEntry.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/sync/PumpDbEntry.kt @@ -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, diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.kt index bd32f2f509..5bfc2150bf 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.kt @@ -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 } } \ No newline at end of file diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpStatus.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpStatus.kt index 42bc97f6ee..8c467ef234 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpStatus.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpStatus.kt @@ -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.* /** 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 2fe0482bd1..12eb731d35 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 @@ -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) 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 b8f37cf379..a38ec2647e 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,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 // && // 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 22068001b2..9acae80415 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 @@ -62,6 +62,7 @@ class MedtronicHistoryData @Inject constructor( ) { val allHistory: MutableList = mutableListOf() + private var allPumpIds: MutableSet = mutableSetOf() private var newHistory: MutableList = mutableListOf() private var isInit = false @@ -80,7 +81,7 @@ class MedtronicHistoryData @Inject constructor( val validEntries: List = result.validEntries val newEntries: MutableList = 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 = 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,33 +274,34 @@ class MedtronicHistoryData @Inject constructor( } else false } + private fun getDataForPumpSuspends(): MutableList { - val newAndAll: MutableList = mutableListOf() - if (isCollectionNotEmpty(allHistory)) { - newAndAll.addAll(allHistory) - } - if (isCollectionNotEmpty(newHistory)) { - for (pumpHistoryEntry in newHistory) { - if (!newAndAll.contains(pumpHistoryEntry)) { - newAndAll.add(pumpHistoryEntry) - } + val newAndAll: MutableList = mutableListOf() + if (isCollectionNotEmpty(allHistory)) { + newAndAll.addAll(allHistory) + } + if (isCollectionNotEmpty(newHistory)) { + for (pumpHistoryEntry in newHistory) { + if (!newAndAll.contains(pumpHistoryEntry)) { + newAndAll.add(pumpHistoryEntry) } } - if (newAndAll.isEmpty()) return newAndAll - this.sort(newAndAll) - var newAndAll2: MutableList = getFilteredItems(newAndAll, // - setOf(PumpHistoryEntryType.Bolus, // - PumpHistoryEntryType.TempBasalCombined, // - PumpHistoryEntryType.Prime, // - PumpHistoryEntryType.SuspendPump, // - PumpHistoryEntryType.ResumePump, // - PumpHistoryEntryType.Rewind, // - PumpHistoryEntryType.NoDeliveryAlarm, // - PumpHistoryEntryType.BatteryChange, // - PumpHistoryEntryType.BasalProfileStart)) - newAndAll2 = filterPumpSuspend(newAndAll2, 10) - return newAndAll2 } + if (newAndAll.isEmpty()) return newAndAll + this.sort(newAndAll) + var newAndAll2: MutableList = getFilteredItems(newAndAll, // + setOf(PumpHistoryEntryType.Bolus, // + PumpHistoryEntryType.TempBasalCombined, // + PumpHistoryEntryType.Prime, // + PumpHistoryEntryType.SuspendPump, // + PumpHistoryEntryType.ResumePump, // + PumpHistoryEntryType.Rewind, // + PumpHistoryEntryType.NoDeliveryAlarm, // + PumpHistoryEntryType.BatteryChange, // + PumpHistoryEntryType.BasalProfileStart)) + newAndAll2 = filterPumpSuspend(newAndAll2, 10) + return newAndAll2 + } private fun filterPumpSuspend(newAndAll: MutableList, filterCount: Int): MutableList { if (newAndAll.size <= filterCount) { @@ -389,7 +402,8 @@ class MedtronicHistoryData @Inject constructor( private fun processPrime(primeRecords: List) { 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,44 +413,58 @@ 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) { 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) { 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,24 +695,29 @@ class MedtronicHistoryData @Inject constructor( private fun removeCancelTBRTemporaryRecord(tempBasalProcessDTO: TempBasalProcessDTO, tbrRecords: MutableList) { - //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()) + } + + // private fun processTBREntries_Old(entryList: MutableList) { // Collections.reverse(entryList) // val tbr = entryList[0].getDecodedDataEntry("Object") as TempBasalPair? @@ -777,8 +818,8 @@ class MedtronicHistoryData @Inject constructor( */ private fun findDbEntry_Old(treatment: PumpHistoryEntry?, entriesFromHistory: List): 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 - } } \ No newline at end of file diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.kt index c54bbcf90d..1ebad72414 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.kt @@ -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