From 00e36392bd8006cd114ac3177ffb05ac29d64142 Mon Sep 17 00:00:00 2001 From: Jakub Kuczys Date: Fri, 1 Dec 2023 22:50:05 +0100 Subject: [PATCH] Auto-add "BG Check" treatment for Contour Link readings --- .../app/aaps/core/interfaces/pump/PumpSync.kt | 21 +++++++++ .../pump/PumpSyncImplementation.kt | 38 ++++++++++++++++ .../pump/MedtronicPumpHistoryDecoder.kt | 15 +++++-- .../medtronic/data/MedtronicHistoryData.kt | 43 ++++++++++++++++++- 4 files changed, 112 insertions(+), 5 deletions(-) diff --git a/core/interfaces/src/main/kotlin/app/aaps/core/interfaces/pump/PumpSync.kt b/core/interfaces/src/main/kotlin/app/aaps/core/interfaces/pump/PumpSync.kt index 5a887617e8..ad30c2b047 100644 --- a/core/interfaces/src/main/kotlin/app/aaps/core/interfaces/pump/PumpSync.kt +++ b/core/interfaces/src/main/kotlin/app/aaps/core/interfaces/pump/PumpSync.kt @@ -1,5 +1,6 @@ package app.aaps.core.interfaces.pump +import app.aaps.core.interfaces.db.GlucoseUnit import app.aaps.core.interfaces.profile.Profile import app.aaps.core.interfaces.pump.defs.PumpType import app.aaps.core.interfaces.utils.DateUtil @@ -257,6 +258,26 @@ interface PumpSync { **/ fun insertTherapyEventIfNewWithTimestamp(timestamp: Long, type: DetailedBolusInfo.EventType, note: String? = null, pumpId: Long? = null, pumpType: PumpType, pumpSerial: String): Boolean + /** + * Synchronization of FINGER_STICK_BG_VALUE events + * + * Assuming there will be no clash on timestamp from different pumps + * only timestamp and type is compared + * + * If db record doesn't exist, new record is created. + * If exists, data is ignored + * + * @param timestamp timestamp of event from pump history + * @param glucose glucose value + * @param glucoseUnit glucose unit + * @param note note + * @param pumpId pump id from history if available + * @param pumpType pump type like PumpType.ACCU_CHEK_COMBO + * @param pumpSerial pump serial number + * @return true if new record is created + **/ + fun insertFingerBgIfNewWithTimestamp(timestamp: Long, glucose: Double, glucoseUnit: GlucoseUnit, note: String? = null, pumpId: Long? = null, pumpType: PumpType, pumpSerial: String): Boolean + /** * Create an announcement * diff --git a/implementation/src/main/kotlin/app/aaps/implementation/pump/PumpSyncImplementation.kt b/implementation/src/main/kotlin/app/aaps/implementation/pump/PumpSyncImplementation.kt index 1db60cde05..bac571d297 100644 --- a/implementation/src/main/kotlin/app/aaps/implementation/pump/PumpSyncImplementation.kt +++ b/implementation/src/main/kotlin/app/aaps/implementation/pump/PumpSyncImplementation.kt @@ -1,5 +1,6 @@ package app.aaps.implementation.pump +import app.aaps.core.interfaces.db.GlucoseUnit import app.aaps.core.interfaces.logging.AAPSLogger import app.aaps.core.interfaces.logging.LTag import app.aaps.core.interfaces.logging.UserEntryLogger @@ -16,6 +17,7 @@ import app.aaps.core.interfaces.sharedPreferences.SP import app.aaps.core.interfaces.utils.DateUtil import app.aaps.core.interfaces.utils.T import app.aaps.core.main.events.EventNewNotification +import app.aaps.core.main.extensions.fromConstant import app.aaps.core.main.pump.fromDbPumpType import app.aaps.core.main.pump.toDbPumpType import app.aaps.core.main.pump.toDbSource @@ -291,6 +293,42 @@ class PumpSyncImplementation @Inject constructor( } } + override fun insertFingerBgIfNewWithTimestamp(timestamp: Long, glucose: Double, glucoseUnit: GlucoseUnit, note: String?, pumpId: Long?, pumpType: PumpType, pumpSerial: String): Boolean { + if (!confirmActivePump(timestamp, pumpType, pumpSerial)) return false + var type = TherapyEvent.Type.FINGER_STICK_BG_VALUE + val therapyEvent = TherapyEvent( + timestamp = timestamp, + type = type, + duration = 0, + note = note, + enteredBy = "AndroidAPS", + glucose = glucose, + glucoseType = TherapyEvent.MeterType.FINGER, + glucoseUnit = TherapyEvent.GlucoseUnit.fromConstant(glucoseUnit), + interfaceIDs_backing = InterfaceIDs( + pumpId = pumpId, + pumpType = pumpType.toDbPumpType(), + pumpSerial = pumpSerial + ) + ) + uel.log( + action = UserEntry.Action.CAREPORTAL, + source = pumpType.source.toDbSource(), + note = note, + timestamp = timestamp, + ValueWithUnit.Timestamp(timestamp), ValueWithUnit.TherapyEventType(type) + ) + repository.runTransactionForResult(InsertIfNewByTimestampTherapyEventTransaction(therapyEvent)) + .doOnError { + aapsLogger.error(LTag.DATABASE, "Error while saving TherapyEvent", it) + } + .blockingGet() + .also { result -> + result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted TherapyEvent $it") } + return result.inserted.size > 0 + } + } + override fun insertAnnouncement(error: String, pumpId: Long?, pumpType: PumpType, pumpSerial: String) { if (!confirmActivePump(dateUtil.now(), pumpType, pumpSerial)) return disposable += repository.runTransaction(InsertTherapyEventAnnouncementTransaction(error, pumpId, pumpType.toDbPumpType(), pumpSerial)) diff --git a/pump/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoder.kt b/pump/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoder.kt index 782599ff5a..606d1058e3 100644 --- a/pump/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoder.kt +++ b/pump/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoder.kt @@ -153,7 +153,6 @@ class MedtronicPumpHistoryDecoder @Inject constructor( PumpHistoryEntryType.ClearAlarm, PumpHistoryEntryType.ChangeAlarmNotifyMode, PumpHistoryEntryType.EnableDisableRemote, - PumpHistoryEntryType.BGReceived, PumpHistoryEntryType.SensorAlert, PumpHistoryEntryType.ChangeTimeFormat, PumpHistoryEntryType.ChangeReservoirWarningTime, @@ -188,7 +187,6 @@ class MedtronicPumpHistoryDecoder @Inject constructor( PumpHistoryEntryType.ChangeWatchdogEnable, PumpHistoryEntryType.ChangeOtherDeviceID, PumpHistoryEntryType.ReadOtherDevicesIDs, - PumpHistoryEntryType.BGReceived512, PumpHistoryEntryType.SensorStatus, PumpHistoryEntryType.ReadCaptureEventEnabled, PumpHistoryEntryType.ChangeCaptureEventEnable, @@ -206,6 +204,12 @@ class MedtronicPumpHistoryDecoder @Inject constructor( PumpHistoryEntryType.UnabsorbedInsulin, PumpHistoryEntryType.UnabsorbedInsulin512 -> RecordDecodeStatus.Ignored + PumpHistoryEntryType.BGReceived, + PumpHistoryEntryType.BGReceived512 -> { + decodeBgReceived(entry) + RecordDecodeStatus.OK + } + PumpHistoryEntryType.DailyTotals522, PumpHistoryEntryType.DailyTotals523, PumpHistoryEntryType.DailyTotals515, @@ -409,8 +413,11 @@ class MedtronicPumpHistoryDecoder @Inject constructor( } private fun decodeBgReceived(entry: PumpHistoryEntry) { - entry.addDecodedData("amount", (ByteUtil.asUINT8(entry.getRawDataByIndex(0)) shl 3) + (ByteUtil.asUINT8(entry.getRawDataByIndex(3)) shr 5)) - entry.addDecodedData("meter", ByteUtil.substring(entry.rawData, 6, 3)) // index moved from 1 -> 0 + val glucoseMgdl = (ByteUtil.asUINT8(entry.head[0]) shl 3) + (ByteUtil.asUINT8(entry.datetime[2]) shr 5) + val meterSerial = ByteUtil.shortHexStringWithoutSpaces(entry.body) + entry.addDecodedData("GlucoseMgdl", glucoseMgdl) + entry.addDecodedData("MeterSerial", meterSerial) + entry.displayableValue = String.format("Glucose: %d mg/dl, Meter Serial: %s", glucoseMgdl, meterSerial) } private fun decodeCalBGForPH(entry: PumpHistoryEntry) { diff --git a/pump/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt b/pump/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt index 4ab8cb87bf..b8856d1a1f 100644 --- a/pump/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt +++ b/pump/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt @@ -4,6 +4,7 @@ import app.aaps.core.interfaces.logging.AAPSLogger import app.aaps.core.interfaces.logging.LTag import app.aaps.core.interfaces.notifications.Notification import app.aaps.core.interfaces.plugin.ActivePlugin +import app.aaps.core.interfaces.profile.ProfileUtil import app.aaps.core.interfaces.pump.DetailedBolusInfo import app.aaps.core.interfaces.pump.PumpSync import app.aaps.core.interfaces.pump.defs.PumpType @@ -67,7 +68,8 @@ class MedtronicHistoryData @Inject constructor( val medtronicPumpStatus: MedtronicPumpStatus, private val pumpSync: PumpSync, private val pumpSyncStorage: PumpSyncStorage, - private val uiInteraction: UiInteraction + private val uiInteraction: UiInteraction, + private val profileUtil: ProfileUtil ) { val allHistory: MutableList = mutableListOf() @@ -322,6 +324,17 @@ class MedtronicHistoryData @Inject constructor( * Process History Data: Boluses(Treatments), TDD, TBRs, Suspend-Resume (or other pump stops: battery, prime) */ fun processNewHistoryData() { + // Finger BG (for adding entry to careportal) + val bgRecords: MutableList = getFilteredItems(setOf(PumpHistoryEntryType.BGReceived, PumpHistoryEntryType.BGReceived512)) + aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "ProcessHistoryData: BGReceived [count=%d, items=%s]", bgRecords.size, gson.toJson(bgRecords))) + if (isCollectionNotEmpty(bgRecords)) { + try { + processBgReceived(bgRecords) + } catch (ex: Exception) { + aapsLogger.error(LTag.PUMP, "ProcessHistoryData: Error processing BGReceived entries: " + ex.message, ex) + throw ex + } + } // Prime (for resetting autosense) val primeRecords: MutableList = getFilteredItems(PumpHistoryEntryType.Prime) @@ -419,6 +432,34 @@ class MedtronicHistoryData @Inject constructor( } } + private fun processBgReceived(bgRecords: List) { + for (bgRecord in bgRecords) { + val glucoseMgdl = bgRecord.getDecodedDataEntry("GlucoseMgdl") + if (glucoseMgdl == null || glucoseMgdl as Int == 0) { + continue + } + + val glucose = profileUtil.fromMgdlToUnits(glucoseMgdl.toDouble()) + val glucoseUnit = profileUtil.units + + val result = pumpSync.insertFingerBgIfNewWithTimestamp( + DateTimeUtil.toMillisFromATD(bgRecord.atechDateTime), + glucose, glucoseUnit, null, + bgRecord.pumpId, + medtronicPumpStatus.pumpType, + medtronicPumpStatus.serialNumber + ) + + aapsLogger.debug( + LTag.PUMP, String.format( + Locale.ROOT, "insertFingerBgIfNewWithTimestamp [date=%d, glucose=%f, glucoseUnit=%s, pumpId=%d, pumpSerial=%s] - Result: %b", + bgRecord.atechDateTime, glucose, glucoseUnit, bgRecord.pumpId, + medtronicPumpStatus.serialNumber, result + ) + ) + } + } + private fun processPrime(primeRecords: List) { val maxAllowedTimeInPast = DateTimeUtil.getATDWithAddedMinutes(GregorianCalendar(), -30) var lastPrimeRecordTime = 0L