G6: calibration fix

This commit is contained in:
Milos Kozak 2022-01-27 21:35:12 +01:00
parent a20f4ff822
commit 900c5db0dc
3 changed files with 44 additions and 20 deletions

View file

@ -7,6 +7,8 @@ import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.work.ListenableWorker
import androidx.work.workDataOf
import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.R
import info.nightscout.androidaps.database.AppRepository
@ -32,6 +34,8 @@ import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import java.util.concurrent.TimeUnit
@ -48,6 +52,7 @@ class BGSourceFragment : DaggerFragment() {
@Inject lateinit var aapsSchedulers: AapsSchedulers
@Inject lateinit var uel: UserEntryLogger
@Inject lateinit var activePlugin: ActivePlugin
@Inject lateinit var aapsLogger: AAPSLogger
private val disposable = CompositeDisposable()
private val millsToThePast = T.hours(36).msecs()
@ -154,7 +159,10 @@ class BGSourceFragment : DaggerFragment() {
Action.BG_REMOVED, source,
ValueWithUnit.Timestamp(glucoseValue.timestamp)
)
disposable += repository.runTransaction(InvalidateGlucoseValueTransaction(glucoseValue.id)).subscribe()
repository.runTransactionForResult(InvalidateGlucoseValueTransaction(glucoseValue.id))
.doOnError { aapsLogger.error(LTag.DATABASE, "Error while invalidating BG value", it) }
.blockingGet()
.also { result -> result.invalidated.forEach { aapsLogger.debug(LTag.DATABASE, "Invalidated bg $it") } }
})
}
}

View file

@ -17,6 +17,7 @@ import info.nightscout.androidaps.database.entities.UserEntry.Action
import info.nightscout.androidaps.database.entities.UserEntry.Sources
import info.nightscout.androidaps.database.entities.ValueWithUnit
import info.nightscout.androidaps.database.transactions.CgmSourceTransaction
import info.nightscout.androidaps.database.transactions.InvalidateGlucoseValueTransaction
import info.nightscout.androidaps.extensions.fromConstant
import info.nightscout.androidaps.interfaces.*
import info.nightscout.androidaps.logging.UserEntryLogger
@ -144,18 +145,6 @@ class DexcomPlugin @Inject constructor(
sourceSensor = sourceSensor
)
}
// G6 calibration bug workaround (2 additional GVs are created within 1 second from previous record)
if (sourceSensor == GlucoseValue.SourceSensor.DEXCOM_G6_NATIVE) {
glucoseValues.sortBy { it.timestamp }
for (i in glucoseValues.indices) {
if (i < glucoseValues.size - 1) {
if (abs(glucoseValues[i].timestamp - glucoseValues[i + 1].timestamp) < 1000) {
aapsLogger.debug(LTag.DATABASE, "Excluding bg ${glucoseValues[i + 1]}")
glucoseValues.removeAt(i + 1)
}
}
}
}
val sensorStartTime = if (sp.getBoolean(R.string.key_dexcom_lognssensorchange, false) && bundle.containsKey("sensorInsertionTime")) {
bundle.getLong("sensorInsertionTime", 0) * 1000
} else {
@ -168,9 +157,27 @@ class DexcomPlugin @Inject constructor(
}
.blockingGet()
.also { result ->
result.inserted.forEach {
xDripBroadcast.send(it)
aapsLogger.debug(LTag.DATABASE, "Inserted bg $it")
// G6 calibration bug workaround (2 additional GVs are created within 1 minute)
for (i in result.inserted.indices) {
if (sourceSensor == GlucoseValue.SourceSensor.DEXCOM_G6_NATIVE) {
if (i < result.inserted.size - 1) {
if (abs(result.inserted[i].timestamp - result.inserted[i + 1].timestamp) < T.mins(1).msecs()) {
repository.runTransactionForResult(InvalidateGlucoseValueTransaction(result.inserted[i].id))
.doOnError { aapsLogger.error(LTag.DATABASE, "Error while invalidating BG value", it) }
.blockingGet()
.also { result1 -> result1.invalidated.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted and invalidated bg $it") } }
repository.runTransactionForResult(InvalidateGlucoseValueTransaction(result.inserted[i + 1].id))
.doOnError { aapsLogger.error(LTag.DATABASE, "Error while invalidating BG value", it) }
.blockingGet()
.also { result1 -> result1.invalidated.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted and invalidated bg $it") } }
result.inserted.removeAt(i + 1)
result.inserted.removeAt(i)
continue
}
}
}
xDripBroadcast.send(result.inserted[i])
aapsLogger.debug(LTag.DATABASE, "Inserted bg ${result.inserted[i]}")
}
result.updated.forEach {
xDripBroadcast.send(it)

View file

@ -1,15 +1,24 @@
package info.nightscout.androidaps.database.transactions
import info.nightscout.androidaps.database.transactions.Transaction
import info.nightscout.androidaps.database.entities.GlucoseValue
/**
* Invalidates the GlucoseValue with the specified id
*/
class InvalidateGlucoseValueTransaction(val id: Long) : Transaction<Unit>() {
override fun run() {
class InvalidateGlucoseValueTransaction(val id: Long) : Transaction<InvalidateGlucoseValueTransaction.TransactionResult>() {
override fun run(): TransactionResult {
val result = TransactionResult()
val glucoseValue = database.glucoseValueDao.findById(id)
?: throw IllegalArgumentException("There is no such GlucoseValue with the specified ID.")
glucoseValue.isValid = false
database.glucoseValueDao.updateExistingEntry(glucoseValue)
result.invalidated.add(glucoseValue)
return result
}
class TransactionResult {
val invalidated = mutableListOf<GlucoseValue>()
}
}