From faeddef68edcab3749c595f3732e1b4de4a54d4d Mon Sep 17 00:00:00 2001 From: Jakub Kuczys Date: Wed, 15 Nov 2023 06:14:05 +0100 Subject: [PATCH] Add support for SensorStartedAt extra in xDrip+ BG source --- .../aaps/core/interfaces/receivers/Intents.kt | 1 + .../aaps/plugins/source/XdripSourcePlugin.kt | 30 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/core/interfaces/src/main/kotlin/app/aaps/core/interfaces/receivers/Intents.kt b/core/interfaces/src/main/kotlin/app/aaps/core/interfaces/receivers/Intents.kt index fbcfca67e6..c4d74505a0 100644 --- a/core/interfaces/src/main/kotlin/app/aaps/core/interfaces/receivers/Intents.kt +++ b/core/interfaces/src/main/kotlin/app/aaps/core/interfaces/receivers/Intents.kt @@ -25,6 +25,7 @@ interface Intents { const val EXTRA_BG_SLOPE = "com.eveningoutpost.dexdrip.Extras.BgSlope" const val EXTRA_BG_SLOPE_NAME = "com.eveningoutpost.dexdrip.Extras.BgSlopeName" const val EXTRA_SENSOR_BATTERY = "com.eveningoutpost.dexdrip.Extras.SensorBattery" + const val EXTRA_SENSOR_STARTED_AT = "com.eveningoutpost.dexdrip.Extras.SensorStartedAt" const val EXTRA_TIMESTAMP = "com.eveningoutpost.dexdrip.Extras.Time" const val EXTRA_RAW = "com.eveningoutpost.dexdrip.Extras.Raw" const val XDRIP_DATA_SOURCE_DESCRIPTION = "com.eveningoutpost.dexdrip.Extras.SourceDesc" diff --git a/plugins/source/src/main/kotlin/app/aaps/plugins/source/XdripSourcePlugin.kt b/plugins/source/src/main/kotlin/app/aaps/plugins/source/XdripSourcePlugin.kt index 7bcdddc2d0..1942e424be 100644 --- a/plugins/source/src/main/kotlin/app/aaps/plugins/source/XdripSourcePlugin.kt +++ b/plugins/source/src/main/kotlin/app/aaps/plugins/source/XdripSourcePlugin.kt @@ -5,6 +5,7 @@ import androidx.work.WorkerParameters import androidx.work.workDataOf import app.aaps.core.interfaces.logging.AAPSLogger import app.aaps.core.interfaces.logging.LTag +import app.aaps.core.interfaces.logging.UserEntryLogger import app.aaps.core.interfaces.plugin.PluginBase import app.aaps.core.interfaces.plugin.PluginDescription import app.aaps.core.interfaces.plugin.PluginType @@ -12,9 +13,14 @@ import app.aaps.core.interfaces.receivers.Intents import app.aaps.core.interfaces.resources.ResourceHelper import app.aaps.core.interfaces.source.BgSource import app.aaps.core.interfaces.source.XDripSource +import app.aaps.core.interfaces.utils.DateUtil +import app.aaps.core.interfaces.utils.T import app.aaps.core.main.utils.worker.LoggingWorker import app.aaps.core.utils.receivers.DataWorkerStorage import app.aaps.database.entities.GlucoseValue +import app.aaps.database.entities.UserEntry.Action +import app.aaps.database.entities.UserEntry.Sources +import app.aaps.database.entities.ValueWithUnit import app.aaps.database.impl.AppRepository import app.aaps.database.impl.transactions.CgmSourceTransaction import app.aaps.database.transactions.TransactionGlucoseValue @@ -22,6 +28,7 @@ import dagger.android.HasAndroidInjector import kotlinx.coroutines.Dispatchers import javax.inject.Inject import javax.inject.Singleton +import kotlin.math.abs import kotlin.math.round @Singleton @@ -63,8 +70,10 @@ class XdripSourcePlugin @Inject constructor( ) : LoggingWorker(context, params, Dispatchers.IO) { @Inject lateinit var xdripSourcePlugin: XdripSourcePlugin + @Inject lateinit var dateUtil: DateUtil @Inject lateinit var repository: AppRepository @Inject lateinit var dataWorkerStorage: DataWorkerStorage + @Inject lateinit var uel: UserEntryLogger override suspend fun doWorkAndLog(): Result { var ret = Result.success() @@ -86,17 +95,32 @@ class XdripSourcePlugin @Inject constructor( ?: "" ) ) - repository.runTransactionForResult(CgmSourceTransaction(glucoseValues, emptyList(), null)) + val now = dateUtil.now() + var sensorStartTime: Long? = bundle.getLong(Intents.EXTRA_SENSOR_STARTED_AT, 0) + // check start time validity + sensorStartTime?.let { + if (abs(it - now) > T.months(1).msecs() || it > now) sensorStartTime = null + } + repository.runTransactionForResult(CgmSourceTransaction(glucoseValues, emptyList(), sensorStartTime)) .doOnError { aapsLogger.error(LTag.DATABASE, "Error while saving values from Xdrip", it) ret = Result.failure(workDataOf("Error" to it.toString())) } .blockingGet() - .also { savedValues -> - savedValues.all().forEach { + .also { result -> + result.all().forEach { xdripSourcePlugin.detectSource(it) aapsLogger.debug(LTag.DATABASE, "Inserted bg $it") } + result.sensorInsertionsInserted.forEach { + uel.log( + Action.CAREPORTAL, + Sources.Xdrip, + ValueWithUnit.Timestamp(it.timestamp), + ValueWithUnit.TherapyEventType(it.type) + ) + aapsLogger.debug(LTag.DATABASE, "Inserted sensor insertion $it") + } } xdripSourcePlugin.sensorBatteryLevel = bundle.getInt(Intents.EXTRA_SENSOR_BATTERY, -1) return ret