From 922e6c5e9e23d41327155ab097f507a54646d924 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Tue, 9 Feb 2021 12:32:21 +0100 Subject: [PATCH] data receiver fixes --- .../general/nsclient/NSClientWorker.java | 7 ++-- .../smsCommunicator/SmsCommunicatorPlugin.kt | 3 +- .../androidaps/plugins/source/DexcomPlugin.kt | 3 +- .../plugins/source/EversensePlugin.kt | 3 +- .../androidaps/plugins/source/XdripPlugin.kt | 20 +++++++---- .../androidaps/receivers/DataReceiver.kt | 34 ++++++++----------- 6 files changed, 37 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientWorker.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientWorker.java index a6af4e431f..b36557c534 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientWorker.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientWorker.java @@ -13,6 +13,7 @@ import javax.inject.Inject; import dagger.android.HasAndroidInjector; import info.nightscout.androidaps.receivers.BundleStore; +import info.nightscout.androidaps.receivers.DataReceiver; // cannot be inner class because of needed injection public class NSClientWorker extends Worker { @@ -30,9 +31,9 @@ public class NSClientWorker extends Worker { @NotNull @Override public Result doWork() { - Bundle bundle = bundleStore.pickup(getInputData().getLong("storeKey", -1)); - if (bundle == null) Result.failure(); - String action = getInputData().getString("action"); + Bundle bundle = bundleStore.pickup(getInputData().getLong(DataReceiver.STORE_KEY, -1)); + if (bundle == null) return Result.failure(); + String action = getInputData().getString(DataReceiver.ACTION_KEY); nsClientPlugin.handleNewDataFromNSClient(action, bundle); return Result.success(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt index b752c7ceab..5988927460 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -35,6 +35,7 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorP import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin import info.nightscout.androidaps.queue.Callback import info.nightscout.androidaps.receivers.BundleStore +import info.nightscout.androidaps.receivers.DataReceiver import info.nightscout.androidaps.utils.* import info.nightscout.androidaps.utils.extensions.plusAssign import info.nightscout.androidaps.utils.resources.ResourceHelper @@ -170,7 +171,7 @@ class SmsCommunicatorPlugin @Inject constructor( } override fun doWork(): Result { - val bundle = bundleStore.pickup(inputData.getLong("storeKey", -1)) + val bundle = bundleStore.pickup(inputData.getLong(DataReceiver.STORE_KEY, -1)) ?: return Result.failure() val format = bundle.getString("format") ?: return Result.failure() val pdus = bundle["pdus"] as Array<*> 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 6c7212ea86..a576a8ba02 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 @@ -21,6 +21,7 @@ import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.general.nsclient.NSUpload import info.nightscout.androidaps.receivers.BundleStore +import info.nightscout.androidaps.receivers.DataReceiver import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.XDripBroadcast @@ -92,7 +93,7 @@ class DexcomPlugin @Inject constructor( override fun doWork(): Result { if (!dexcomPlugin.isEnabled(PluginType.BGSOURCE)) return Result.failure() - val bundle = bundleStore.pickup(inputData.getLong("storeKey", -1)) + val bundle = bundleStore.pickup(inputData.getLong(DataReceiver.STORE_KEY, -1)) ?: return Result.failure() try { val sourceSensor = when (bundle.getString("sensorType") ?: "") { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/source/EversensePlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/source/EversensePlugin.kt index 853e222cb9..8bf32255c8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/source/EversensePlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/source/EversensePlugin.kt @@ -19,6 +19,7 @@ import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.general.nsclient.NSUpload import info.nightscout.androidaps.receivers.BundleStore +import info.nightscout.androidaps.receivers.DataReceiver import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.XDripBroadcast import info.nightscout.androidaps.utils.resources.ResourceHelper @@ -78,7 +79,7 @@ class EversensePlugin @Inject constructor( override fun doWork(): Result { if (!eversensePlugin.isEnabled(PluginType.BGSOURCE)) return Result.failure() - val bundle = bundleStore.pickup(inputData.getLong("storeKey", -1)) + val bundle = bundleStore.pickup(inputData.getLong(DataReceiver.STORE_KEY, -1)) ?: return Result.failure() if (bundle.containsKey("currentCalibrationPhase")) aapsLogger.debug(LTag.BGSOURCE, "currentCalibrationPhase: " + bundle.getString("currentCalibrationPhase")) if (bundle.containsKey("placementModeInProgress")) aapsLogger.debug(LTag.BGSOURCE, "placementModeInProgress: " + bundle.getBoolean("placementModeInProgress")) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/source/XdripPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/source/XdripPlugin.kt index 73e6be3319..ae90274d52 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/source/XdripPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/source/XdripPlugin.kt @@ -14,6 +14,8 @@ import info.nightscout.androidaps.interfaces.PluginDescription import info.nightscout.androidaps.interfaces.PluginType import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag +import info.nightscout.androidaps.receivers.BundleStore +import info.nightscout.androidaps.receivers.DataReceiver import info.nightscout.androidaps.services.Intents import info.nightscout.androidaps.utils.resources.ResourceHelper import io.reactivex.disposables.CompositeDisposable @@ -67,6 +69,7 @@ class XdripPlugin @Inject constructor( @Inject lateinit var xdripPlugin: XdripPlugin @Inject lateinit var repository: AppRepository + @Inject lateinit var bundleStore: BundleStore init { (context.applicationContext as HasAndroidInjector).androidInjector().inject(this) @@ -74,15 +77,18 @@ class XdripPlugin @Inject constructor( override fun doWork(): Result { if (!xdripPlugin.isEnabled(PluginType.BGSOURCE)) return Result.failure() - xdripPlugin.aapsLogger.debug(LTag.BGSOURCE, "Received xDrip data: $inputData") + val bundle = bundleStore.pickup(inputData.getLong(DataReceiver.STORE_KEY, -1)) + ?: return Result.failure() + + xdripPlugin.aapsLogger.debug(LTag.BGSOURCE, "Received xDrip data: $bundle") val glucoseValues = mutableListOf() glucoseValues += CgmSourceTransaction.TransactionGlucoseValue( - timestamp = inputData.getLong(Intents.EXTRA_TIMESTAMP, 0), - value = inputData.getDouble(Intents.EXTRA_BG_ESTIMATE, 0.0), - raw = inputData.getDouble(Intents.EXTRA_RAW, 0.0), + timestamp = bundle.getLong(Intents.EXTRA_TIMESTAMP, 0), + value = bundle.getDouble(Intents.EXTRA_BG_ESTIMATE, 0.0), + raw = bundle.getDouble(Intents.EXTRA_RAW, 0.0), noise = null, - trendArrow = GlucoseValue.TrendArrow.fromString(inputData.getString(Intents.EXTRA_BG_SLOPE_NAME)), - sourceSensor = GlucoseValue.SourceSensor.fromString(inputData.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION) + trendArrow = GlucoseValue.TrendArrow.fromString(bundle.getString(Intents.EXTRA_BG_SLOPE_NAME)), + sourceSensor = GlucoseValue.SourceSensor.fromString(bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION) ?: "") ) xdripPlugin.disposable += repository.runTransactionForResult(CgmSourceTransaction(glucoseValues, emptyList(), null)).subscribe({ savedValues -> @@ -92,7 +98,7 @@ class XdripPlugin @Inject constructor( }, { xdripPlugin.aapsLogger.error(LTag.BGSOURCE, "Error while saving values from Eversense App", it) }) - xdripPlugin.sensorBatteryLevel = inputData.getInt(Intents.EXTRA_SENSOR_BATTERY, -1) + xdripPlugin.sensorBatteryLevel = bundle.getInt(Intents.EXTRA_SENSOR_BATTERY, -1) return Result.success() } } diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/DataReceiver.kt b/app/src/main/java/info/nightscout/androidaps/receivers/DataReceiver.kt index a676211745..e764f35b2d 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/DataReceiver.kt +++ b/app/src/main/java/info/nightscout/androidaps/receivers/DataReceiver.kt @@ -2,6 +2,7 @@ package info.nightscout.androidaps.receivers import android.content.Context import android.content.Intent +import android.os.Bundle import android.provider.Telephony import androidx.work.Data import androidx.work.ExistingWorkPolicy @@ -39,10 +40,7 @@ open class DataReceiver : DaggerBroadcastReceiver() { when (intent.action) { Intents.ACTION_NEW_BG_ESTIMATE -> OneTimeWorkRequest.Builder(XdripPlugin.XdripWorker::class.java) - .setInputData(Data.Builder().also { - it.copyString("collection", bundle, null) - it.copyString("data", bundle) - }.build()).build() + .setInputData(bundleInputData(bundle, intent)).build() Intents.POCTECH_BG -> OneTimeWorkRequest.Builder(PoctechPlugin.PoctechWorker::class.java) .setInputData(Data.Builder().also { @@ -84,36 +82,32 @@ open class DataReceiver : DaggerBroadcastReceiver() { }.build()).build() Telephony.Sms.Intents.SMS_RECEIVED_ACTION -> OneTimeWorkRequest.Builder(SmsCommunicatorPlugin.SmsCommunicatorWorker::class.java) - .setInputData(Data.Builder().also { - it.putLong("storeKey", bundleStore.store(bundle)) - it.putString("action", intent.action) - }.build()).build() + .setInputData(bundleInputData(bundle, intent)).build() Intents.EVERSENSE_BG -> OneTimeWorkRequest.Builder(EversensePlugin.EversenseWorker::class.java) - .setInputData(Data.Builder().also { - it.putLong("storeKey", bundleStore.store(bundle)) - it.putString("action", intent.action) - }.build()).build() + .setInputData(bundleInputData(bundle, intent)).build() Intents.DEXCOM_BG -> OneTimeWorkRequest.Builder(DexcomPlugin.DexcomWorker::class.java) - .setInputData(Data.Builder().also { - it.putLong("storeKey", bundleStore.store(bundle)) - it.putString("action", intent.action) - }.build()).build() + .setInputData(bundleInputData(bundle, intent)).build() Intents.ACTION_NEW_TREATMENT, Intents.ACTION_CHANGED_TREATMENT, Intents.ACTION_REMOVED_TREATMENT, Intents.ACTION_NEW_CAL, Intents.ACTION_NEW_MBG -> OneTimeWorkRequest.Builder(NSClientWorker::class.java) - .setInputData(Data.Builder().also { - it.putLong("storeKey", bundleStore.store(bundle)) - it.putString("action", intent.action) - }.build()).build() + .setInputData(bundleInputData(bundle, intent)).build() else -> null }?.let { request -> WorkManager.getInstance(context) .enqueueUniqueWork(jobGroupName, ExistingWorkPolicy.APPEND_OR_REPLACE , request) } } + + private fun bundleInputData(bundle: Bundle, intent: Intent) = + Data.Builder().putLong(STORE_KEY, bundleStore.store(bundle)).putString(ACTION_KEY, intent.action).build() + + companion object { + const val STORE_KEY = "storeKey" + const val ACTION_KEY = "action" + } } \ No newline at end of file