From 58c185a75aba05a6a294914550440bf2fe6fa7a2 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 14 Apr 2021 15:54:09 +0200 Subject: [PATCH] Workers for NSAcks --- .../dependencyInjection/WorkersModule.kt | 4 + .../general/nsclient/NSClientAddAckWorker.kt | 237 +++++++++++++++++ .../nsclient/NSClientUpdateRemoveAckWorker.kt | 126 +++++++++ .../nsclient/services/NSClientService.java | 242 +----------------- .../androidaps/receivers/DataWorker.kt | 6 + 5 files changed, 384 insertions(+), 231 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddAckWorker.kt create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientUpdateRemoveAckWorker.kt diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/WorkersModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/WorkersModule.kt index a46b6b5264..0c67611761 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/WorkersModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/WorkersModule.kt @@ -3,9 +3,11 @@ package info.nightscout.androidaps.dependencyInjection import dagger.Module import dagger.android.ContributesAndroidInjector import info.nightscout.androidaps.plugins.general.food.FoodPlugin +import info.nightscout.androidaps.plugins.general.nsclient.NSClientAddAckWorker import info.nightscout.androidaps.plugins.general.nsclient.NSClientAddUpdateWorker import info.nightscout.androidaps.plugins.general.nsclient.NSClientMbgWorker import info.nightscout.androidaps.plugins.general.nsclient.NSClientRemoveWorker +import info.nightscout.androidaps.plugins.general.nsclient.NSClientUpdateRemoveAckWorker import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin import info.nightscout.androidaps.plugins.profile.ns.NSProfilePlugin import info.nightscout.androidaps.plugins.source.* @@ -25,6 +27,8 @@ abstract class WorkersModule { @ContributesAndroidInjector abstract fun contributesNSProfileWorker(): NSProfilePlugin.NSProfileWorker @ContributesAndroidInjector abstract fun contributesSmsCommunicatorWorker(): SmsCommunicatorPlugin.SmsCommunicatorWorker @ContributesAndroidInjector abstract fun contributesNSClientWorker(): NSClientAddUpdateWorker + @ContributesAndroidInjector abstract fun contributesNSClientAddAckWorker(): NSClientAddAckWorker + @ContributesAndroidInjector abstract fun contributesNSClientUpdateRemoveAckWorker(): NSClientUpdateRemoveAckWorker @ContributesAndroidInjector abstract fun contributesNSClientRemoveWorker(): NSClientRemoveWorker @ContributesAndroidInjector abstract fun contributesNSClientMbgWorker(): NSClientMbgWorker @ContributesAndroidInjector abstract fun contributesFoodWorker(): FoodPlugin.FoodWorker diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddAckWorker.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddAckWorker.kt new file mode 100644 index 0000000000..e07346e85b --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddAckWorker.kt @@ -0,0 +1,237 @@ +package info.nightscout.androidaps.plugins.general.nsclient + +import android.content.Context +import androidx.work.Worker +import androidx.work.WorkerParameters +import androidx.work.workDataOf +import dagger.android.HasAndroidInjector +import info.nightscout.androidaps.database.AppRepository +import info.nightscout.androidaps.database.entities.DeviceStatus +import info.nightscout.androidaps.database.transactions.* +import info.nightscout.androidaps.interfaces.DataSyncSelector +import info.nightscout.androidaps.interfaces.DataSyncSelector.* +import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.logging.LTag +import info.nightscout.androidaps.plugins.bus.RxBusWrapper +import info.nightscout.androidaps.plugins.general.nsclient.acks.NSAddAck +import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientNewLog +import info.nightscout.androidaps.receivers.DataWorker +import info.nightscout.androidaps.utils.rx.AapsSchedulers +import javax.inject.Inject + +class NSClientAddAckWorker( + context: Context, + params: WorkerParameters +) : Worker(context, params) { + + @Inject lateinit var dataWorker: DataWorker + @Inject lateinit var aapsLogger: AAPSLogger + @Inject lateinit var repository: AppRepository + @Inject lateinit var rxBus: RxBusWrapper + @Inject lateinit var dataSyncSelector: DataSyncSelector + @Inject lateinit var aapsSchedulers: AapsSchedulers + + override fun doWork(): Result { + var ret = Result.success() + + val ack = dataWorker.pickupObject(inputData.getLong(DataWorker.STORE_KEY, -1)) as NSAddAck? + ?: return Result.failure(workDataOf("Error" to "missing input data")) + + when (ack.originalObject) { + is PairTemporaryTarget -> { + val pair = ack.originalObject + pair.value.interfaceIDs.nightscoutId = ack.id + repository.runTransactionForResult(UpdateNsIdTemporaryTargetTransaction(pair.value)) + .doOnError { error -> + aapsLogger.error(LTag.DATABASE, "Updated ns id of TemporaryTarget failed", error) + ret = Result.failure((workDataOf("Error" to error))) + } + .doOnSuccess { + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + aapsLogger.debug(LTag.DATABASE, "Updated ns id of TemporaryTarget " + pair.value) + dataSyncSelector.confirmLastTempTargetsIdIfGreater(pair.updateRecordId) + } + .blockingGet() + rxBus.send(EventNSClientNewLog("DBADD", "Acked TemporaryTarget " + pair.value.interfaceIDs.nightscoutId)) + // Send new if waiting + dataSyncSelector.processChangedTempTargetsCompat() + } + + is PairGlucoseValue -> { + val pair = ack.originalObject + pair.value.interfaceIDs.nightscoutId = ack.id + repository.runTransactionForResult(UpdateNsIdGlucoseValueTransaction(pair.value)) + .doOnError { error -> + aapsLogger.error(LTag.DATABASE, "Updated ns id of GlucoseValue failed", error) + ret = Result.failure((workDataOf("Error" to error))) + } + .doOnSuccess { + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + aapsLogger.debug(LTag.DATABASE, "Updated ns id of GlucoseValue " + pair.value) + dataSyncSelector.confirmLastGlucoseValueIdIfGreater(pair.updateRecordId) + } + .blockingGet() + rxBus.send(EventNSClientNewLog("DBADD", "Acked GlucoseValue " + pair.value.interfaceIDs.nightscoutId)) + // Send new if waiting + dataSyncSelector.processChangedGlucoseValuesCompat() + } + + is PairFood -> { + val pair = ack.originalObject + pair.value.interfaceIDs.nightscoutId = ack.id + repository.runTransactionForResult(UpdateNsIdFoodTransaction(pair.value)) + .doOnError { error -> + aapsLogger.error(LTag.DATABASE, "Updated ns id of Food failed", error) + ret = Result.failure((workDataOf("Error" to error))) + } + .doOnSuccess { + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + aapsLogger.debug(LTag.DATABASE, "Updated ns id of Food " + pair.value) + dataSyncSelector.confirmLastFoodIdIfGreater(pair.updateRecordId) + } + .blockingGet() + rxBus.send(EventNSClientNewLog("DBADD", "Acked Food " + pair.value.interfaceIDs.nightscoutId)) + // Send new if waiting + dataSyncSelector.processChangedFoodsCompat() + } + + is PairTherapyEvent -> { + val pair = ack.originalObject + pair.value.interfaceIDs.nightscoutId = ack.id + repository.runTransactionForResult(UpdateNsIdTherapyEventTransaction(pair.value)) + .doOnError { error -> + aapsLogger.error(LTag.DATABASE, "Updated ns id of TherapyEvent failed", error) + ret = Result.failure((workDataOf("Error" to error))) + } + .doOnSuccess { + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + aapsLogger.debug(LTag.DATABASE, "Updated ns id of TherapyEvent " + pair.value) + dataSyncSelector.confirmLastTherapyEventIdIfGreater(pair.updateRecordId) + } + .blockingGet() + rxBus.send(EventNSClientNewLog("DBADD", "Acked TherapyEvent " + pair.value.interfaceIDs.nightscoutId)) + // Send new if waiting + dataSyncSelector.processChangedTherapyEventsCompat() + } + + is PairBolus -> { + val pair = ack.originalObject + pair.value.interfaceIDs.nightscoutId = ack.id + repository.runTransactionForResult(UpdateNsIdBolusTransaction(pair.value)) + .doOnError { error -> + aapsLogger.error(LTag.DATABASE, "Updated ns id of Bolus failed", error) + ret = Result.failure((workDataOf("Error" to error))) + } + .doOnSuccess { + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + aapsLogger.debug(LTag.DATABASE, "Updated ns id of Bolus " + pair.value) + dataSyncSelector.confirmLastBolusIdIfGreater(pair.updateRecordId) + } + .blockingGet() + rxBus.send(EventNSClientNewLog("DBADD", "Acked Bolus " + pair.value.interfaceIDs.nightscoutId)) + // Send new if waiting + dataSyncSelector.processChangedBolusesCompat() + } + + is PairCarbs -> { + val pair = ack.originalObject + pair.value.interfaceIDs.nightscoutId = ack.id + repository.runTransactionForResult(UpdateNsIdCarbsTransaction(pair.value)) + .doOnError { error -> + aapsLogger.error(LTag.DATABASE, "Updated ns id of Carbs failed", error) + ret = Result.failure((workDataOf("Error" to error))) + } + .doOnSuccess { + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + aapsLogger.debug(LTag.DATABASE, "Updated ns id of Carbs " + pair.value) + dataSyncSelector.confirmLastCarbsIdIfGreater(pair.updateRecordId) + } + .blockingGet() + rxBus.send(EventNSClientNewLog("DBADD", "Acked Carbs" + pair.value.interfaceIDs.nightscoutId)) + // Send new if waiting + dataSyncSelector.processChangedCarbsCompat() + } + + is PairBolusCalculatorResult -> { + val pair = ack.originalObject + pair.value.interfaceIDs.nightscoutId = ack.id + repository.runTransactionForResult(UpdateNsIdBolusCalculatorResultTransaction(pair.value)) + .doOnError { error -> + aapsLogger.error(LTag.DATABASE, "Updated ns id of BolusCalculatorResult failed", error) + ret = Result.failure((workDataOf("Error" to error))) + } + .doOnSuccess { + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + aapsLogger.debug(LTag.DATABASE, "Updated ns id of BolusCalculatorResult " + pair.value) + dataSyncSelector.confirmLastBolusCalculatorResultsIdIfGreater(pair.updateRecordId) + } + .blockingGet() + rxBus.send(EventNSClientNewLog("DBADD", "Acked BolusCalculatorResult" + pair.value.interfaceIDs.nightscoutId)) + // Send new if waiting + dataSyncSelector.processChangedBolusCalculatorResultsCompat() + } + + is PairTemporaryBasal -> { + val pair = ack.originalObject + pair.value.interfaceIDs.nightscoutId = ack.id + repository.runTransactionForResult(UpdateNsIdTemporaryBasalTransaction(pair.value)) + .doOnError { error -> + aapsLogger.error(LTag.DATABASE, "Updated ns id of TemporaryBasal failed", error) + ret = Result.failure((workDataOf("Error" to error))) + } + .doOnSuccess { + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + aapsLogger.debug(LTag.DATABASE, "Updated ns id of TemporaryBasal " + pair.value) + dataSyncSelector.confirmLastTemporaryBasalIdIfGreater(pair.updateRecordId) + } + .blockingGet() + rxBus.send(EventNSClientNewLog("DBADD", "Acked TemporaryBasal" + pair.value.interfaceIDs.nightscoutId)) + // Send new if waiting + dataSyncSelector.processChangedTemporaryBasalsCompat() + } + + is PairExtendedBolus -> { + val pair = ack.originalObject + pair.value.interfaceIDs.nightscoutId = ack.id + repository.runTransactionForResult(UpdateNsIdExtendedBolusTransaction(pair.value)) + .doOnError { error -> + aapsLogger.error(LTag.DATABASE, "Updated ns id of ExtendedBolus failed", error) + ret = Result.failure((workDataOf("Error" to error))) + } + .doOnSuccess { + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + aapsLogger.debug(LTag.DATABASE, "Updated ns id of ExtendedBolus " + pair.value) + dataSyncSelector.confirmLastExtendedBolusIdIfGreater(pair.updateRecordId) + } + .blockingGet() + rxBus.send(EventNSClientNewLog("DBADD", "Acked ExtendedBolus" + pair.value.interfaceIDs.nightscoutId)) + // Send new if waiting + dataSyncSelector.processChangedTemporaryBasalsCompat() + } + + is DeviceStatus -> { + val deviceStatus = ack.originalObject + deviceStatus.interfaceIDs.nightscoutId = ack.id + repository.runTransactionForResult(UpdateNsIdDeviceStatusTransaction(deviceStatus)) + .doOnError { error -> + aapsLogger.error(LTag.DATABASE, "Updated ns id of DeviceStatus failed", error) + ret = Result.failure((workDataOf("Error" to error))) + } + .doOnSuccess { + ret = Result.success(workDataOf("ProcessedData" to deviceStatus)) + aapsLogger.debug(LTag.DATABASE, "Updated ns id of DeviceStatus $deviceStatus") + dataSyncSelector.confirmLastDeviceStatusIdIfGreater(deviceStatus.id) + } + .blockingGet() + rxBus.send(EventNSClientNewLog("DBADD", "Acked DeviceStatus" + deviceStatus.interfaceIDs.nightscoutId)) + // Send new if waiting + dataSyncSelector.processChangedBolusCalculatorResultsCompat() + } + } + return ret + } + + init { + (context.applicationContext as HasAndroidInjector).androidInjector().inject(this) + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientUpdateRemoveAckWorker.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientUpdateRemoveAckWorker.kt new file mode 100644 index 0000000000..b061350786 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientUpdateRemoveAckWorker.kt @@ -0,0 +1,126 @@ +package info.nightscout.androidaps.plugins.general.nsclient + +import android.content.Context +import androidx.work.Worker +import androidx.work.WorkerParameters +import androidx.work.workDataOf +import dagger.android.HasAndroidInjector +import info.nightscout.androidaps.database.AppRepository +import info.nightscout.androidaps.interfaces.DataSyncSelector +import info.nightscout.androidaps.interfaces.DataSyncSelector.* +import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.plugins.bus.RxBusWrapper +import info.nightscout.androidaps.plugins.general.nsclient.acks.NSUpdateAck +import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientNewLog +import info.nightscout.androidaps.receivers.DataWorker +import info.nightscout.androidaps.utils.rx.AapsSchedulers +import javax.inject.Inject + +class NSClientUpdateRemoveAckWorker( + context: Context, + params: WorkerParameters +) : Worker(context, params) { + + @Inject lateinit var dataWorker: DataWorker + @Inject lateinit var aapsLogger: AAPSLogger + @Inject lateinit var repository: AppRepository + @Inject lateinit var rxBus: RxBusWrapper + @Inject lateinit var dataSyncSelector: DataSyncSelector + @Inject lateinit var aapsSchedulers: AapsSchedulers + + override fun doWork(): Result { + var ret = Result.success() + + val ack = dataWorker.pickupObject(inputData.getLong(DataWorker.STORE_KEY, -1)) as NSUpdateAck? + ?: return Result.failure(workDataOf("Error" to "missing input data")) + + // new room way + when (ack.originalObject) { + is PairTemporaryTarget -> { + val pair = ack.originalObject + dataSyncSelector.confirmLastTempTargetsIdIfGreater(pair.updateRecordId) + rxBus.send(EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked TemporaryTarget" + ack._id)) + // Send new if waiting + dataSyncSelector.processChangedTempTargetsCompat() + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + } + + is PairGlucoseValue -> { + val pair = ack.originalObject + dataSyncSelector.confirmLastGlucoseValueIdIfGreater(pair.updateRecordId) + rxBus.send(EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked GlucoseValue " + ack._id)) + // Send new if waiting + dataSyncSelector.processChangedGlucoseValuesCompat() + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + } + + is PairFood -> { + val pair = ack.originalObject + dataSyncSelector.confirmLastFoodIdIfGreater(pair.updateRecordId) + rxBus.send(EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked Food " + ack._id)) + // Send new if waiting + dataSyncSelector.processChangedFoodsCompat() + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + } + + is PairTherapyEvent -> { + val pair = ack.originalObject + dataSyncSelector.confirmLastTherapyEventIdIfGreater(pair.updateRecordId) + rxBus.send(EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked TherapyEvent " + ack._id)) + // Send new if waiting + dataSyncSelector.processChangedTherapyEventsCompat() + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + } + + is PairBolus -> { + val pair = ack.originalObject + dataSyncSelector.confirmLastBolusIdIfGreater(pair.updateRecordId) + rxBus.send(EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked Bolus " + ack._id)) + // Send new if waiting + dataSyncSelector.processChangedBolusesCompat() + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + } + + is PairCarbs -> { + val pair = ack.originalObject + dataSyncSelector.confirmLastCarbsIdIfGreater(pair.updateRecordId) + rxBus.send(EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked Carbs " + ack._id)) + // Send new if waiting + dataSyncSelector.processChangedCarbsCompat() + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + } + + is PairBolusCalculatorResult -> { + val pair = ack.originalObject + dataSyncSelector.confirmLastBolusCalculatorResultsIdIfGreater(pair.updateRecordId) + rxBus.send(EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked BolusCalculatorResult " + ack._id)) + // Send new if waiting + dataSyncSelector.processChangedBolusCalculatorResultsCompat() + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + } + + is PairTemporaryBasal -> { + val pair = ack.originalObject + dataSyncSelector.confirmLastTemporaryBasalIdIfGreater(pair.updateRecordId) + rxBus.send(EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked TemporaryBasal " + ack._id)) + // Send new if waiting + dataSyncSelector.processChangedTemporaryBasalsCompat() + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + } + + is PairExtendedBolus -> { + val pair = ack.originalObject + dataSyncSelector.confirmLastExtendedBolusIdIfGreater(pair.updateRecordId) + rxBus.send(EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked ExtendedBolus " + ack._id)) + // Send new if waiting + dataSyncSelector.processChangedExtendedBolusesCompat() + ret = Result.success(workDataOf("ProcessedData" to pair.toString())) + } + } + return ret + } + + init { + (context.applicationContext as HasAndroidInjector).androidInjector().inject(this) + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.java index 35671b20f5..3768a35cd6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.java @@ -55,10 +55,12 @@ import info.nightscout.androidaps.logging.AAPSLogger; import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.bus.RxBusWrapper; import info.nightscout.androidaps.plugins.general.food.FoodPlugin; +import info.nightscout.androidaps.plugins.general.nsclient.NSClientAddAckWorker; import info.nightscout.androidaps.plugins.general.nsclient.NSClientAddUpdateWorker; import info.nightscout.androidaps.plugins.general.nsclient.NSClientMbgWorker; import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin; import info.nightscout.androidaps.plugins.general.nsclient.NSClientRemoveWorker; +import info.nightscout.androidaps.plugins.general.nsclient.NSClientUpdateRemoveAckWorker; import info.nightscout.androidaps.plugins.general.nsclient.acks.NSAddAck; import info.nightscout.androidaps.plugins.general.nsclient.acks.NSAuthAck; import info.nightscout.androidaps.plugins.general.nsclient.acks.NSUpdateAck; @@ -230,166 +232,11 @@ public class NSClientService extends DaggerService { public void processAddAck(NSAddAck ack) { lastAckTime = dateUtil.now(); // new room way - if (ack.getOriginalObject() instanceof DataSyncSelector.PairTemporaryTarget) { - DataSyncSelector.PairTemporaryTarget pair = (DataSyncSelector.PairTemporaryTarget) ack.getOriginalObject(); - pair.getValue().getInterfaceIDs().setNightscoutId(ack.getId()); + dataWorker.enqueue( + new OneTimeWorkRequest.Builder(NSClientAddAckWorker.class) + .setInputData(dataWorker.storeInputData(ack, null)) + .build()); - disposable.add(repository.runTransactionForResult(new UpdateNsIdTemporaryTargetTransaction((pair.getValue()))) - .observeOn(aapsSchedulers.getIo()) - .subscribe( - result -> aapsLogger.debug(LTag.DATABASE, "Updated ns id of temporary target " + pair.getValue()), - error -> aapsLogger.error(LTag.DATABASE, "Updated ns id of temporary target failed") - )); - dataSyncSelector.confirmLastTempTargetsIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBADD", "Acked TemporaryTarget " + pair.getValue().getInterfaceIDs().getNightscoutId())); - // Send new if waiting - dataSyncSelector.processChangedTempTargetsCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairGlucoseValue) { - DataSyncSelector.PairGlucoseValue pair = (DataSyncSelector.PairGlucoseValue) ack.getOriginalObject(); - pair.getValue().getInterfaceIDs().setNightscoutId(ack.getId()); - - disposable.add(repository.runTransactionForResult(new UpdateNsIdGlucoseValueTransaction(pair.getValue())) - .observeOn(aapsSchedulers.getIo()) - .subscribe( - result -> aapsLogger.debug(LTag.DATABASE, "Updated ns id of glucose value " + pair.getValue()), - error -> aapsLogger.error(LTag.DATABASE, "Updated ns id of glucose value failed", error) - )); - dataSyncSelector.confirmLastGlucoseValueIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBADD", "Acked GlucoseValue " + pair.getValue().getInterfaceIDs().getNightscoutId())); - // Send new if waiting - dataSyncSelector.processChangedGlucoseValuesCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairFood) { - DataSyncSelector.PairFood pair = (DataSyncSelector.PairFood) ack.getOriginalObject(); - pair.getValue().getInterfaceIDs().setNightscoutId(ack.getId()); - - disposable.add(repository.runTransactionForResult(new UpdateNsIdFoodTransaction(pair.getValue())) - .observeOn(aapsSchedulers.getIo()) - .subscribe( - result -> aapsLogger.debug(LTag.DATABASE, "Updated ns id of food " + pair.getValue()), - error -> aapsLogger.error(LTag.DATABASE, "Updated ns id of food failed", error) - )); - dataSyncSelector.confirmLastFoodIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBADD", "Acked Food " + pair.getValue().getInterfaceIDs().getNightscoutId())); - // Send new if waiting - dataSyncSelector.processChangedFoodsCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairTherapyEvent) { - DataSyncSelector.PairTherapyEvent pair = (DataSyncSelector.PairTherapyEvent) ack.getOriginalObject(); - pair.getValue().getInterfaceIDs().setNightscoutId(ack.getId()); - - disposable.add(repository.runTransactionForResult(new UpdateNsIdTherapyEventTransaction(pair.getValue())) - .observeOn(aapsSchedulers.getIo()) - .subscribe( - result -> aapsLogger.debug(LTag.DATABASE, "Updated ns id of therapy event " + pair.getValue()), - error -> aapsLogger.error(LTag.DATABASE, "Updated ns id of therapy event failed", error) - )); - dataSyncSelector.confirmLastTherapyEventIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBADD", "Acked TherapyEvent " + pair.getValue().getInterfaceIDs().getNightscoutId())); - // Send new if waiting - dataSyncSelector.processChangedTherapyEventsCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairBolus) { - DataSyncSelector.PairBolus pair = (DataSyncSelector.PairBolus) ack.getOriginalObject(); - pair.getValue().getInterfaceIDs().setNightscoutId(ack.getId()); - - disposable.add(repository.runTransactionForResult(new UpdateNsIdBolusTransaction(pair.getValue())) - .observeOn(aapsSchedulers.getIo()) - .subscribe( - result -> aapsLogger.debug(LTag.DATABASE, "Updated ns id of bolus " + pair.getValue()), - error -> aapsLogger.error(LTag.DATABASE, "Updated ns id of bolus failed", error) - )); - dataSyncSelector.confirmLastBolusIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBADD", "Acked Bolus " + pair.getValue().getInterfaceIDs().getNightscoutId())); - // Send new if waiting - dataSyncSelector.processChangedBolusesCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairCarbs) { - DataSyncSelector.PairCarbs pair = (DataSyncSelector.PairCarbs) ack.getOriginalObject(); - pair.getValue().getInterfaceIDs().setNightscoutId(ack.getId()); - - disposable.add(repository.runTransactionForResult(new UpdateNsIdCarbsTransaction(pair.getValue())) - .observeOn(aapsSchedulers.getIo()) - .subscribe( - result -> aapsLogger.debug(LTag.DATABASE, "Updated ns id of carbs " + pair.getValue()), - error -> aapsLogger.error(LTag.DATABASE, "Updated ns id of carbs failed", error) - )); - dataSyncSelector.confirmLastCarbsIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBADD", "Acked Carbs" + pair.getValue().getInterfaceIDs().getNightscoutId())); - // Send new if waiting - dataSyncSelector.processChangedCarbsCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairBolusCalculatorResult) { - DataSyncSelector.PairBolusCalculatorResult pair = (DataSyncSelector.PairBolusCalculatorResult) ack.getOriginalObject(); - pair.getValue().getInterfaceIDs().setNightscoutId(ack.getId()); - - disposable.add(repository.runTransactionForResult(new UpdateNsIdBolusCalculatorResultTransaction(pair.getValue())) - .observeOn(aapsSchedulers.getIo()) - .subscribe( - result -> aapsLogger.debug(LTag.DATABASE, "Updated ns id of BolusCalculatorResult " + pair.getValue()), - error -> aapsLogger.error(LTag.DATABASE, "Updated ns id of BolusCalculatorResult failed", error) - )); - dataSyncSelector.confirmLastBolusCalculatorResultsIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBADD", "Acked BolusCalculatorResult" + pair.getValue().getInterfaceIDs().getNightscoutId())); - // Send new if waiting - dataSyncSelector.processChangedBolusCalculatorResultsCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairTemporaryBasal) { - DataSyncSelector.PairTemporaryBasal pair = (DataSyncSelector.PairTemporaryBasal) ack.getOriginalObject(); - pair.getValue().getInterfaceIDs().setNightscoutId(ack.getId()); - - disposable.add(repository.runTransactionForResult(new UpdateNsIdTemporaryBasalTransaction(pair.getValue())) - .observeOn(aapsSchedulers.getIo()) - .subscribe( - result -> aapsLogger.debug(LTag.DATABASE, "Updated ns id of TemporaryBasal " + pair.getValue()), - error -> aapsLogger.error(LTag.DATABASE, "Updated ns id of TemporaryBasal failed", error) - )); - dataSyncSelector.confirmLastTemporaryBasalIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBADD", "Acked TemporaryBasal" + pair.getValue().getInterfaceIDs().getNightscoutId())); - // Send new if waiting - dataSyncSelector.processChangedTemporaryBasalsCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairExtendedBolus) { - DataSyncSelector.PairExtendedBolus pair = (DataSyncSelector.PairExtendedBolus) ack.getOriginalObject(); - pair.getValue().getInterfaceIDs().setNightscoutId(ack.getId()); - - disposable.add(repository.runTransactionForResult(new UpdateNsIdExtendedBolusTransaction(pair.getValue())) - .observeOn(aapsSchedulers.getIo()) - .subscribe( - result -> aapsLogger.debug(LTag.DATABASE, "Updated ns id of ExtendedBolus " + pair.getValue()), - error -> aapsLogger.error(LTag.DATABASE, "Updated ns id of ExtendedBolus failed", error) - )); - dataSyncSelector.confirmLastExtendedBolusIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBADD", "Acked ExtendedBolus" + pair.getValue().getInterfaceIDs().getNightscoutId())); - // Send new if waiting - dataSyncSelector.processChangedTemporaryBasalsCompat(); - return; - } - if (ack.getOriginalObject() instanceof DeviceStatus) { - DeviceStatus deviceStatus = (DeviceStatus) ack.getOriginalObject(); - deviceStatus.getInterfaceIDs().setNightscoutId(ack.getId()); - - disposable.add(repository.runTransactionForResult(new UpdateNsIdDeviceStatusTransaction(deviceStatus)) - .observeOn(aapsSchedulers.getIo()) - .subscribe( - result -> aapsLogger.debug(LTag.DATABASE, "Updated ns id of DeviceStatus " + deviceStatus), - error -> aapsLogger.error(LTag.DATABASE, "Updated ns id of DeviceStatus failed", error) - )); - dataSyncSelector.confirmLastDeviceStatusIdIfGreater(deviceStatus.getId()); - rxBus.send(new EventNSClientNewLog("DBADD", "Acked DeviceStatus" + deviceStatus.getInterfaceIDs().getNightscoutId())); - // Send new if waiting - dataSyncSelector.processChangedBolusCalculatorResultsCompat(); - return; - } // old way if (ack.nsClientID != null) { uploadQueue.removeByNsClientIdIfExists(ack.json); @@ -402,78 +249,11 @@ public class NSClientService extends DaggerService { public void processUpdateAck(NSUpdateAck ack) { lastAckTime = dateUtil.now(); // new room way - if (ack.getOriginalObject() instanceof DataSyncSelector.PairTemporaryTarget) { - DataSyncSelector.PairTemporaryTarget pair = (DataSyncSelector.PairTemporaryTarget) ack.getOriginalObject(); - dataSyncSelector.confirmLastTempTargetsIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked TemporaryTarget" + ack.get_id())); - // Send new if waiting - dataSyncSelector.processChangedTempTargetsCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairGlucoseValue) { - DataSyncSelector.PairGlucoseValue pair = (DataSyncSelector.PairGlucoseValue) ack.getOriginalObject(); - dataSyncSelector.confirmLastGlucoseValueIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked GlucoseValue " + ack.get_id())); - // Send new if waiting - dataSyncSelector.processChangedGlucoseValuesCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairFood) { - DataSyncSelector.PairFood pair = (DataSyncSelector.PairFood) ack.getOriginalObject(); - dataSyncSelector.confirmLastFoodIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked Food " + ack.get_id())); - // Send new if waiting - dataSyncSelector.processChangedFoodsCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairTherapyEvent) { - DataSyncSelector.PairTherapyEvent pair = (DataSyncSelector.PairTherapyEvent) ack.getOriginalObject(); - dataSyncSelector.confirmLastTherapyEventIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked TherapyEvent " + ack.get_id())); - // Send new if waiting - dataSyncSelector.processChangedTherapyEventsCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairBolus) { - DataSyncSelector.PairBolus pair = (DataSyncSelector.PairBolus) ack.getOriginalObject(); - dataSyncSelector.confirmLastBolusIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked Bolus " + ack.get_id())); - // Send new if waiting - dataSyncSelector.processChangedBolusesCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairCarbs) { - DataSyncSelector.PairCarbs pair = (DataSyncSelector.PairCarbs) ack.getOriginalObject(); - dataSyncSelector.confirmLastCarbsIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked Carbs " + ack.get_id())); - // Send new if waiting - dataSyncSelector.processChangedCarbsCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairBolusCalculatorResult) { - DataSyncSelector.PairBolusCalculatorResult pair = (DataSyncSelector.PairBolusCalculatorResult) ack.getOriginalObject(); - dataSyncSelector.confirmLastBolusCalculatorResultsIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked BolusCalculatorResult " + ack.get_id())); - // Send new if waiting - dataSyncSelector.processChangedBolusCalculatorResultsCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairTemporaryBasal) { - DataSyncSelector.PairTemporaryBasal pair = (DataSyncSelector.PairTemporaryBasal) ack.getOriginalObject(); - dataSyncSelector.confirmLastTemporaryBasalIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked TemporaryBasal " + ack.get_id())); - // Send new if waiting - dataSyncSelector.processChangedTemporaryBasalsCompat(); - return; - } - if (ack.getOriginalObject() instanceof DataSyncSelector.PairExtendedBolus) { - DataSyncSelector.PairExtendedBolus pair = (DataSyncSelector.PairExtendedBolus) ack.getOriginalObject(); - dataSyncSelector.confirmLastExtendedBolusIdIfGreater(pair.getUpdateRecordId()); - rxBus.send(new EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked ExtendedBolus " + ack.get_id())); - // Send new if waiting - dataSyncSelector.processChangedExtendedBolusesCompat(); - return; - } + dataWorker.enqueue( + new OneTimeWorkRequest.Builder(NSClientUpdateRemoveAckWorker.class) + .setInputData(dataWorker.storeInputData(ack, null)) + .build()); + // old way if (ack.getResult()) { uploadQueue.removeByMongoId(ack.getAction(), ack.get_id()); diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/DataWorker.kt b/app/src/main/java/info/nightscout/androidaps/receivers/DataWorker.kt index d46491b15e..fed3db6598 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/DataWorker.kt +++ b/app/src/main/java/info/nightscout/androidaps/receivers/DataWorker.kt @@ -32,6 +32,12 @@ class DataWorker @Inject constructor( return value as Bundle? } + @Synchronized fun pickupObject(key: Long): Any? { + val value = store[key] + store.remove(key) + return value + } + @Synchronized fun pickupString(key: Long): String? { val value = store[key] store.remove(key)