From 0603e741be6c6cd23d2d4b6c84c70de528b63a43 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 14 Jan 2022 18:19:05 +0100 Subject: [PATCH] G5: Fix double BG value after calibration --- .../androidaps/plugins/source/DexcomPlugin.kt | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/source/DexcomPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/source/DexcomPlugin.kt index 1c766f66a1..7049589bbd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/source/DexcomPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/source/DexcomPlugin.kt @@ -104,20 +104,6 @@ class DexcomPlugin @Inject constructor( "G5" -> GlucoseValue.SourceSensor.DEXCOM_G5_NATIVE else -> GlucoseValue.SourceSensor.DEXCOM_NATIVE_UNKNOWN } - val glucoseValuesBundle = bundle.getBundle("glucoseValues") - ?: return Result.failure(workDataOf("Error" to "missing glucoseValues")) - val glucoseValues = mutableListOf() - for (i in 0 until glucoseValuesBundle.size()) { - val glucoseValueBundle = glucoseValuesBundle.getBundle(i.toString())!! - glucoseValues += CgmSourceTransaction.TransactionGlucoseValue( - timestamp = glucoseValueBundle.getLong("timestamp") * 1000, - value = glucoseValueBundle.getInt("glucoseValue").toDouble(), - noise = null, - raw = null, - trendArrow = GlucoseValue.TrendArrow.fromString(glucoseValueBundle.getString("trendArrow")!!), - sourceSensor = sourceSensor - ) - } val calibrations = mutableListOf() bundle.getBundle("meters")?.let { meters -> for (i in 0 until meters.size()) { @@ -137,6 +123,26 @@ class DexcomPlugin @Inject constructor( } } } + val glucoseValuesBundle = bundle.getBundle("glucoseValues") + ?: return Result.failure(workDataOf("Error" to "missing glucoseValues")) + val glucoseValues = mutableListOf() + for (i in 0 until glucoseValuesBundle.size()) { + val glucoseValueBundle = glucoseValuesBundle.getBundle(i.toString())!! + val timestamp = glucoseValueBundle.getLong("timestamp") * 1000 + // G5 calibration bug workaround (calibration is sent as glucoseValue too) + var valid = true + if (sourceSensor == GlucoseValue.SourceSensor.DEXCOM_G5_NATIVE) + calibrations.forEach { calibration -> if (calibration.timestamp == timestamp) valid = false } + if (valid) + glucoseValues += CgmSourceTransaction.TransactionGlucoseValue( + timestamp = timestamp, + value = glucoseValueBundle.getInt("glucoseValue").toDouble(), + noise = null, + raw = null, + trendArrow = GlucoseValue.TrendArrow.fromString(glucoseValueBundle.getString("trendArrow")!!), + sourceSensor = sourceSensor + ) + } val sensorStartTime = if (sp.getBoolean(R.string.key_dexcom_lognssensorchange, false) && bundle.containsKey("sensorInsertionTime")) { bundle.getLong("sensorInsertionTime", 0) * 1000 } else {