Auto-add "BG Check" treatment for Contour Link readings

This commit is contained in:
Jakub Kuczys 2023-12-01 22:50:05 +01:00
parent 0ec3f4eae8
commit 00e36392bd
No known key found for this signature in database
GPG key ID: 9F02686F15FCBCD3
4 changed files with 112 additions and 5 deletions

View file

@ -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
*

View file

@ -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))

View file

@ -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) {

View file

@ -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<PumpHistoryEntry> = 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<PumpHistoryEntry> = 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<PumpHistoryEntry> = getFilteredItems(PumpHistoryEntryType.Prime)
@ -419,6 +432,34 @@ class MedtronicHistoryData @Inject constructor(
}
}
private fun processBgReceived(bgRecords: List<PumpHistoryEntry>) {
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<PumpHistoryEntry>) {
val maxAllowedTimeInPast = DateTimeUtil.getATDWithAddedMinutes(GregorianCalendar(), -30)
var lastPrimeRecordTime = 0L