From 4120e9eef9412725629d77113228bb0620d50a63 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 9 Apr 2021 08:02:46 +0200 Subject: [PATCH 1/2] DanaR working --- .../plugins/general/food/FoodFragment.kt | 4 +- .../DataSyncSelectorImplementation.kt | 1 + .../nsclient/NSClientAddUpdateWorker.kt | 92 ++++++++++--------- .../IobCobCalculatorPlugin.kt | 3 +- .../plugins/source/RandomBgPlugin.kt | 2 +- .../TreatmentsExtendedBolusesFragment.kt | 3 +- .../TreatmentsTemporaryBasalsFragment.kt | 63 ++++++++++--- .../res/layout/treatments_tempbasals_item.xml | 36 ++++++-- .../androidaps/extensions/BolusExtension.kt | 3 + .../androidaps/extensions/CarbsExtension.kt | 3 + .../extensions/ExtendedBolusExtension.kt | 6 +- .../extensions/TemporaryBasalExtension.kt | 13 +-- .../extensions/TemporaryTargetExtension.kt | 2 + .../extensions/TherapyEventExtension.kt | 2 + .../androidaps/interfaces/DanaRInterface.kt | 1 + .../androidaps/dana/DanaFragment.kt | 3 +- .../androidaps/dana/DanaPumpInterface.kt | 5 - .../danaRKorean/DanaRKoreanPlugin.java | 35 ++++--- .../androidaps/danaRv2/DanaRv2Plugin.java | 2 +- .../androidaps/danar/AbstractDanaRPlugin.java | 34 ++++++- .../androidaps/danar/DanaRPlugin.java | 11 ++- .../danar/comm/MsgInitConnStatusBasic.kt | 10 +- .../danar/comm/MsgSetExtendedBolusStop.kt | 2 +- .../danar/comm/MsgStatusBolusExtended.kt | 22 ++++- .../danar/comm/MsgStatusTempBasal.kt | 23 +++-- .../AbstractDanaRExecutionService.java | 5 +- .../androidaps/danars/DanaRSPlugin.kt | 3 +- ...et_Bolus_Get_Extended_Menu_Option_State.kt | 2 +- .../androidaps/database/AppRepository.kt | 7 +- .../androidaps/database/daos/BolusDao.kt | 3 + .../database/entities/ExtendedBolus.kt | 4 + .../database/entities/TemporaryBasal.kt | 5 + 32 files changed, 281 insertions(+), 129 deletions(-) delete mode 100644 dana/src/main/java/info/nightscout/androidaps/dana/DanaPumpInterface.kt diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodFragment.kt index d250387bb9..73c240264b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodFragment.kt @@ -53,8 +53,8 @@ class FoodFragment : DaggerFragment() { @Inject lateinit var uel: UserEntryLogger private val disposable = CompositeDisposable() - private lateinit var unfiltered: List - private lateinit var filtered: MutableList + private var unfiltered: List = arrayListOf() + private var filtered: MutableList = arrayListOf() private var _binding: FoodFragmentBinding? = null diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/DataSyncSelectorImplementation.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/DataSyncSelectorImplementation.kt index 345ac3f382..ddd26a64c0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/DataSyncSelectorImplementation.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/DataSyncSelectorImplementation.kt @@ -32,6 +32,7 @@ class DataSyncSelectorImplementation @Inject constructor( sp.remove(R.string.key_ns_device_status_last_synced_id) sp.remove(R.string.key_ns_temporary_basal_last_synced_id) sp.remove(R.string.key_ns_extended_bolus_last_synced_id) + sp.remove(R.string.key_ns_therapy_event_last_synced_id) } override fun confirmLastBolusIdIfGreater(lastSynced: Long) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt index 6a611fc4b4..86f860bcb2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt @@ -27,7 +27,6 @@ import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.JsonHelper import info.nightscout.androidaps.utils.JsonHelper.safeGetLong import info.nightscout.androidaps.utils.buildHelper.BuildHelper -import info.nightscout.androidaps.utils.extensions.* import info.nightscout.androidaps.utils.sharedPreferences.SP import java.util.concurrent.TimeUnit import javax.inject.Inject @@ -60,7 +59,7 @@ class NSClientAddUpdateWorker( var latestDateInReceivedData = 0L for (i in 0 until treatments.length()) { - val json = treatments.getJSONObject(i) + var json = treatments.getJSONObject(i) // new DB model val insulin = JsonHelper.safeGetDouble(json, "insulin") val carbs = JsonHelper.safeGetDouble(json, "carbs") @@ -133,6 +132,13 @@ class NSClientAddUpdateWorker( } } ?: aapsLogger.error("Error parsing bolus json $json") } + // Convert back emulated TBR -> EB + if (eventType == TherapyEvent.Type.TEMPORARY_BASAL.text && json.has("extendedEmulated")) { + val ebJson = json.getJSONObject("extendedEmulated") + ebJson.put("_id", json.getString("_id")) + ebJson.put("isValid", json.getBoolean("isValid")) + json = ebJson + } when { insulin > 0 || carbs > 0 -> Any() eventType == TherapyEvent.Type.TEMPORARY_TARGET.text -> @@ -195,9 +201,9 @@ class NSClientAddUpdateWorker( .blockingGet() .also { result -> val action = when (eventType) { - TherapyEvent.Type.CANNULA_CHANGE.text -> Action.SITE_CHANGE - TherapyEvent.Type.INSULIN_CHANGE.text -> Action.RESERVOIR_CHANGE - else -> Action.CAREPORTAL + TherapyEvent.Type.CANNULA_CHANGE.text -> Action.SITE_CHANGE + TherapyEvent.Type.INSULIN_CHANGE.text -> Action.RESERVOIR_CHANGE + else -> Action.CAREPORTAL } result.inserted.forEach { uel.log(action, Sources.NSClient, @@ -220,44 +226,6 @@ class NSClientAddUpdateWorker( } } } ?: aapsLogger.error("Error parsing TherapyEvent json $json") - eventType == TherapyEvent.Type.TEMPORARY_BASAL.text -> - temporaryBasalFromJson(json)?.let { temporaryBasal -> - repository.runTransactionForResult(SyncNsTemporaryBasalTransaction(temporaryBasal)) - .doOnError { - aapsLogger.error(LTag.DATABASE, "Error while saving temporary basal", it) - ret = Result.failure(workDataOf("Error" to it)) - } - .blockingGet() - .also { result -> - result.inserted.forEach { - uel.log(Action.TEMP_BASAL, Sources.NSClient, - ValueWithUnit.Timestamp(it.timestamp), - ValueWithUnit.UnitPerHour(it.rate), - ValueWithUnit.Minute(TimeUnit.MILLISECONDS.toMinutes(it.duration).toInt()) - ) - aapsLogger.debug(LTag.DATABASE, "Inserted TemporaryBasal $it") - } - result.invalidated.forEach { - uel.log(Action.TEMP_BASAL_REMOVED, Sources.NSClient, - ValueWithUnit.Timestamp(it.timestamp), - ValueWithUnit.UnitPerHour(it.rate), - ValueWithUnit.Minute(TimeUnit.MILLISECONDS.toMinutes(it.duration).toInt()) - ) - aapsLogger.debug(LTag.DATABASE, "Invalidated TemporaryBasal $it") - } - result.ended.forEach { - uel.log(Action.CANCEL_TEMP_BASAL, Sources.NSClient, - ValueWithUnit.Timestamp(it.timestamp), - ValueWithUnit.UnitPerHour(it.rate), - ValueWithUnit.Minute(TimeUnit.MILLISECONDS.toMinutes(it.duration).toInt()) - ) - aapsLogger.debug(LTag.DATABASE, "Updated TemporaryBasal $it") - } - result.updatedNsId.forEach { - aapsLogger.debug(LTag.DATABASE, "Updated nsId TemporaryBasal $it") - } - } - } ?: aapsLogger.error("Error parsing TemporaryBasal json $json") eventType == TherapyEvent.Type.COMBO_BOLUS.text -> extendedBolusFromJson(json)?.let { extendedBolus -> repository.runTransactionForResult(SyncNsExtendedBolusTransaction(extendedBolus)) @@ -299,6 +267,44 @@ class NSClientAddUpdateWorker( } } } ?: aapsLogger.error("Error parsing ExtendedBolus json $json") + eventType == TherapyEvent.Type.TEMPORARY_BASAL.text -> + temporaryBasalFromJson(json)?.let { temporaryBasal -> + repository.runTransactionForResult(SyncNsTemporaryBasalTransaction(temporaryBasal)) + .doOnError { + aapsLogger.error(LTag.DATABASE, "Error while saving temporary basal", it) + ret = Result.failure(workDataOf("Error" to it)) + } + .blockingGet() + .also { result -> + result.inserted.forEach { + uel.log(Action.TEMP_BASAL, Sources.NSClient, + ValueWithUnit.Timestamp(it.timestamp), + ValueWithUnit.UnitPerHour(it.rate), + ValueWithUnit.Minute(TimeUnit.MILLISECONDS.toMinutes(it.duration).toInt()) + ) + aapsLogger.debug(LTag.DATABASE, "Inserted TemporaryBasal $it") + } + result.invalidated.forEach { + uel.log(Action.TEMP_BASAL_REMOVED, Sources.NSClient, + ValueWithUnit.Timestamp(it.timestamp), + ValueWithUnit.UnitPerHour(it.rate), + ValueWithUnit.Minute(TimeUnit.MILLISECONDS.toMinutes(it.duration).toInt()) + ) + aapsLogger.debug(LTag.DATABASE, "Invalidated TemporaryBasal $it") + } + result.ended.forEach { + uel.log(Action.CANCEL_TEMP_BASAL, Sources.NSClient, + ValueWithUnit.Timestamp(it.timestamp), + ValueWithUnit.UnitPerHour(it.rate), + ValueWithUnit.Minute(TimeUnit.MILLISECONDS.toMinutes(it.duration).toInt()) + ) + aapsLogger.debug(LTag.DATABASE, "Ended TemporaryBasal $it") + } + result.updatedNsId.forEach { + aapsLogger.debug(LTag.DATABASE, "Updated nsId TemporaryBasal $it") + } + } + } ?: aapsLogger.error("Error parsing TemporaryBasal json $json") eventType == TherapyEvent.Type.PROFILE_SWITCH.text -> databaseHelper.createProfileSwitchFromJsonIfNotExists(json) } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.kt index 8619bd1296..f4e8cf6d17 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.kt @@ -637,7 +637,8 @@ open class IobCobCalculatorPlugin @Inject constructor( result.slopeFromMaxDeviation = autosensData.slopeFromMaxDeviation result.usedMinCarbsImpact = autosensData.usedMinCarbsImpact } - result.lastBolusTime = repository.getLastBolusRecord()?.timestamp ?: 0L + val lastBolus = repository.getLastBolusRecordWrapped().blockingGet() + result.lastBolusTime = if (lastBolus is ValueWrapper.Existing) lastBolus.value.timestamp else 0L return result } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/source/RandomBgPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/source/RandomBgPlugin.kt index 30a814e5e1..49d30ea7f7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/source/RandomBgPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/source/RandomBgPlugin.kt @@ -59,7 +59,7 @@ class RandomBgPlugin @Inject constructor( const val interval = 5L // minutes const val min = 70 // mgdl const val max = 190 // mgdl - const val period = 30.0 // minutes + const val period = 90.0 // minutes } init { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsExtendedBolusesFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsExtendedBolusesFragment.kt index 470ffd9ddb..320429bbf8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsExtendedBolusesFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsExtendedBolusesFragment.kt @@ -121,7 +121,8 @@ class TreatmentsExtendedBolusesFragment : DaggerFragment() { override fun onBindViewHolder(holder: ExtendedBolusesViewHolder, position: Int) { val extendedBolus = extendedBolusList[position] - holder.binding.ph.visibility = (extendedBolus.interfaceIDs.nightscoutId != null).toVisibility() + holder.binding.ns.visibility = (extendedBolus.interfaceIDs.nightscoutId != null).toVisibility() + holder.binding.ph.visibility = (extendedBolus.interfaceIDs.pumpId != null).toVisibility() holder.binding.invalid.visibility = extendedBolus.isValid.not().toVisibility() @SuppressLint("SetTextI18n") if (extendedBolus.isInProgress(dateUtil)) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTemporaryBasalsFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTemporaryBasalsFragment.kt index ecd4b5d288..7fbd7cbab9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTemporaryBasalsFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTemporaryBasalsFragment.kt @@ -25,6 +25,7 @@ import info.nightscout.androidaps.events.EventAutosensCalculationFinished import info.nightscout.androidaps.events.EventTempBasalChange import info.nightscout.androidaps.extensions.iobCalc import info.nightscout.androidaps.extensions.toStringFull +import info.nightscout.androidaps.extensions.toTemporaryBasal import info.nightscout.androidaps.extensions.toVisibility import info.nightscout.androidaps.interfaces.ActivePluginProvider import info.nightscout.androidaps.interfaces.ProfileFunction @@ -76,18 +77,49 @@ class TreatmentsTemporaryBasalsFragment : DaggerFragment() { binding.recyclerview.layoutManager = LinearLayoutManager(view.context) } + private fun tempBasalsWithInvalid(now: Long) = repository + .getTemporaryBasalsDataIncludingInvalidFromTime(now - millsToThePast, false) + + private fun tempBasals(now: Long) = repository + .getTemporaryBasalsDataFromTime(now - millsToThePast, false) + + private fun extendedBolusesWithInvalid(now: Long) = repository + .getExtendedBolusDataIncludingInvalidFromTime(now - millsToThePast, false) + .map { eb -> eb.map { profileFunction.getProfile(it.timestamp)?.let { profile -> it.toTemporaryBasal(profile) } } } + + private fun extendedBoluses(now: Long) = repository + .getExtendedBolusDataFromTime(now - millsToThePast, false) + .map { eb -> eb.map { profileFunction.getProfile(it.timestamp)?.let { profile -> it.toTemporaryBasal(profile) } } } + fun swapAdapter() { val now = System.currentTimeMillis() - if (binding.showInvalidated.isChecked) - repository - .getTemporaryBasalsDataIncludingInvalidFromTime(now - millsToThePast, false) - .observeOn(aapsSchedulers.main) - .subscribe { list -> binding.recyclerview.swapAdapter(RecyclerViewAdapter(list), true) } - else - repository - .getTemporaryBasalsDataFromTime(now - millsToThePast, false) - .observeOn(aapsSchedulers.main) - .subscribe { list -> binding.recyclerview.swapAdapter(RecyclerViewAdapter(list), true) } + disposable += + if (activePlugin.activePump.isFakingTempsByExtendedBoluses) { + if (binding.showInvalidated.isChecked) + tempBasalsWithInvalid(now) + .zipWith(extendedBolusesWithInvalid(now)) { first, second -> first + second } + .map { list -> list.filterNotNull() } + .map { list -> list.sortedByDescending { it.timestamp } } + .observeOn(aapsSchedulers.main) + .subscribe { list -> binding.recyclerview.swapAdapter(RecyclerViewAdapter(list), true) } + else + tempBasals(now) + .zipWith(extendedBoluses(now)) { first, second -> first + second } + .map { list -> list.filterNotNull() } + .map { list -> list.sortedByDescending { it.timestamp } } + .observeOn(aapsSchedulers.main) + .subscribe { list -> binding.recyclerview.swapAdapter(RecyclerViewAdapter(list), true) } + } else { + if (binding.showInvalidated.isChecked) + tempBasalsWithInvalid(now) + .observeOn(aapsSchedulers.main) + .subscribe { list -> binding.recyclerview.swapAdapter(RecyclerViewAdapter(list), true) } + else + tempBasals(now) + .observeOn(aapsSchedulers.main) + .subscribe { list -> binding.recyclerview.swapAdapter(RecyclerViewAdapter(list), true) } + } + } @Synchronized @@ -95,16 +127,15 @@ class TreatmentsTemporaryBasalsFragment : DaggerFragment() { super.onResume() swapAdapter() - disposable.add(rxBus + disposable += rxBus .toObservable(EventTempBasalChange::class.java) .observeOn(aapsSchedulers.main) .subscribe({ swapAdapter() }, fabricPrivacy::logException) - ) - disposable.add(rxBus + + disposable += rxBus .toObservable(EventAutosensCalculationFinished::class.java) .observeOn(aapsSchedulers.main) .subscribe({ swapAdapter() }, fabricPrivacy::logException) - ) } @Synchronized @@ -146,8 +177,10 @@ class TreatmentsTemporaryBasalsFragment : DaggerFragment() { if (profile != null) iob = tempBasal.iobCalc(now, profile, activePlugin.activeInsulin) holder.binding.iob.text = resourceHelper.gs(R.string.formatinsulinunits, iob.basaliob) holder.binding.extendedFlag.visibility = (tempBasal.type == TemporaryBasal.Type.FAKE_EXTENDED).toVisibility() + holder.binding.suspendFlag.visibility = (tempBasal.type == TemporaryBasal.Type.PUMP_SUSPEND).toVisibility() + holder.binding.emulatedSuspendFlag.visibility = (tempBasal.type == TemporaryBasal.Type.EMULATED_PUMP_SUSPEND).toVisibility() + holder.binding.superBolusFlag.visibility = (tempBasal.type == TemporaryBasal.Type.SUPERBOLUS).toVisibility() if (abs(iob.basaliob) > 0.01) holder.binding.iob.setTextColor(resourceHelper.gc(R.color.colorActive)) else holder.binding.iob.setTextColor(holder.binding.duration.currentTextColor) - holder.binding.type.text = tempBasal.type.toString() holder.binding.remove.tag = tempBasal } diff --git a/app/src/main/res/layout/treatments_tempbasals_item.xml b/app/src/main/res/layout/treatments_tempbasals_item.xml index 801a308f72..cd9d6fcc4f 100644 --- a/app/src/main/res/layout/treatments_tempbasals_item.xml +++ b/app/src/main/res/layout/treatments_tempbasals_item.xml @@ -69,6 +69,33 @@ android:textColor="@color/colorSetExtendedButton" tools:ignore="HardcodedText,RtlSymmetry" /> + + + + + + - - 0L) { // not ending event if (units == Constants.MMOL) { diff --git a/core/src/main/java/info/nightscout/androidaps/extensions/TherapyEventExtension.kt b/core/src/main/java/info/nightscout/androidaps/extensions/TherapyEventExtension.kt index d83157cd32..df425c3bde 100644 --- a/core/src/main/java/info/nightscout/androidaps/extensions/TherapyEventExtension.kt +++ b/core/src/main/java/info/nightscout/androidaps/extensions/TherapyEventExtension.kt @@ -71,6 +71,8 @@ fun therapyEventFromJson(jsonObject: JSONObject): TherapyEvent? { val id = JsonHelper.safeGetStringAllowNull(jsonObject, "_id", null) ?: return null val isValid = JsonHelper.safeGetBoolean(jsonObject, "isValid", true) + if (timestamp == 0L) return null + val te = TherapyEvent( timestamp = timestamp, duration = TimeUnit.MINUTES.toMillis(duration), diff --git a/core/src/main/java/info/nightscout/androidaps/interfaces/DanaRInterface.kt b/core/src/main/java/info/nightscout/androidaps/interfaces/DanaRInterface.kt index 2ab57d52ed..ec2170614d 100644 --- a/core/src/main/java/info/nightscout/androidaps/interfaces/DanaRInterface.kt +++ b/core/src/main/java/info/nightscout/androidaps/interfaces/DanaRInterface.kt @@ -7,4 +7,5 @@ interface DanaRInterface { fun loadHistory(type: Byte): PumpEnactResult // for history browser fun loadEvents(): PumpEnactResult // events history to build treatments from fun setUserOptions(): PumpEnactResult // like AnyDana does + fun clearPairing() } \ No newline at end of file diff --git a/dana/src/main/java/info/nightscout/androidaps/dana/DanaFragment.kt b/dana/src/main/java/info/nightscout/androidaps/dana/DanaFragment.kt index 9b30c8887c..0152e83c48 100644 --- a/dana/src/main/java/info/nightscout/androidaps/dana/DanaFragment.kt +++ b/dana/src/main/java/info/nightscout/androidaps/dana/DanaFragment.kt @@ -32,6 +32,7 @@ import info.nightscout.androidaps.utils.userEntry.UserEntryMapper.Sources import info.nightscout.androidaps.utils.WarnColors import info.nightscout.androidaps.utils.alertDialogs.OKDialog import info.nightscout.androidaps.extensions.toVisibility +import info.nightscout.androidaps.interfaces.DanaRInterface import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.rx.AapsSchedulers import info.nightscout.androidaps.utils.sharedPreferences.SP @@ -111,7 +112,7 @@ class DanaFragment : DaggerFragment() { activity?.let { OKDialog.showConfirmation(it, resourceHelper.gs(R.string.resetpairing)) { uel.log(Action.CLEAR_PAIRING_KEYS, Sources.Dana) - (activePlugin.activePump as DanaPumpInterface).clearPairing() + (activePlugin.activePump as DanaRInterface).clearPairing() } } true diff --git a/dana/src/main/java/info/nightscout/androidaps/dana/DanaPumpInterface.kt b/dana/src/main/java/info/nightscout/androidaps/dana/DanaPumpInterface.kt deleted file mode 100644 index 56bbede985..0000000000 --- a/dana/src/main/java/info/nightscout/androidaps/dana/DanaPumpInterface.kt +++ /dev/null @@ -1,5 +0,0 @@ -package info.nightscout.androidaps.dana - -interface DanaPumpInterface { - fun clearPairing() -} \ No newline at end of file diff --git a/danar/src/main/java/info/nightscout/androidaps/danaRKorean/DanaRKoreanPlugin.java b/danar/src/main/java/info/nightscout/androidaps/danaRKorean/DanaRKoreanPlugin.java index 5515253904..2b5ad8633e 100644 --- a/danar/src/main/java/info/nightscout/androidaps/danaRKorean/DanaRKoreanPlugin.java +++ b/danar/src/main/java/info/nightscout/androidaps/danaRKorean/DanaRKoreanPlugin.java @@ -50,7 +50,6 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { private final ResourceHelper resourceHelper; private final ConstraintChecker constraintChecker; private final FabricPrivacy fabricPrivacy; - private final PumpSync pumpSync; @Inject public DanaRKoreanPlugin( @@ -58,23 +57,22 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { AAPSLogger aapsLogger, AapsSchedulers aapsSchedulers, RxBusWrapper rxBus, - DanaPump danaPump, Context context, ResourceHelper resourceHelper, ConstraintChecker constraintChecker, ActivePluginProvider activePlugin, SP sp, CommandQueueProvider commandQueue, + DanaPump danaPump, DateUtil dateUtil, - PumpSync pumpSync, - FabricPrivacy fabricPrivacy + FabricPrivacy fabricPrivacy, + PumpSync pumpSync ) { super(injector, danaPump, resourceHelper, constraintChecker, aapsLogger, aapsSchedulers, commandQueue, rxBus, activePlugin, sp, dateUtil, pumpSync); this.aapsLogger = aapsLogger; this.context = context; this.resourceHelper = resourceHelper; this.constraintChecker = constraintChecker; - this.pumpSync = pumpSync; this.fabricPrivacy = fabricPrivacy; getPluginDescription().description(R.string.description_pump_dana_r_korean); @@ -111,6 +109,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { @Override protected void onStop() { context.unbindService(mConnection); + disposable.clear(); super.onStop(); } @@ -166,22 +165,21 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(new Constraint<>(detailedBolusInfo.insulin)).value(); if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { - EventOverviewBolusProgress.Treatment treatment = new EventOverviewBolusProgress.Treatment(0, 0, detailedBolusInfo.getBolusType() == DetailedBolusInfo.BolusType.SMB); + EventOverviewBolusProgress.Treatment t = new EventOverviewBolusProgress.Treatment(0, 0, detailedBolusInfo.getBolusType() == DetailedBolusInfo.BolusType.SMB); boolean connectionOK = false; if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) - connectionOK = sExecutionService.bolus(detailedBolusInfo.insulin, (int) detailedBolusInfo.carbs, detailedBolusInfo.carbTime, treatment); + connectionOK = sExecutionService.bolus(detailedBolusInfo.insulin, (int) detailedBolusInfo.carbs, detailedBolusInfo.carbTime, t); PumpEnactResult result = new PumpEnactResult(getInjector()); - result.success(connectionOK && Math.abs(detailedBolusInfo.insulin - treatment.insulin) < pumpDescription.getBolusStep()) - .bolusDelivered(treatment.insulin) + result.success(connectionOK && Math.abs(detailedBolusInfo.insulin - t.insulin) < pumpDescription.getBolusStep()) + .bolusDelivered(t.insulin) .carbsDelivered(detailedBolusInfo.carbs); if (!result.getSuccess()) - result.comment(resourceHelper.gs(R.string.boluserrorcode, detailedBolusInfo.insulin, treatment.insulin, danaPump.getBolusStartErrorCode())); + result.comment(resourceHelper.gs(R.string.boluserrorcode, detailedBolusInfo.insulin, t.insulin, danaPump.getBolusStartErrorCode())); else result.comment(R.string.ok); aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.getBolusDelivered()); - detailedBolusInfo.insulin = treatment.insulin; + detailedBolusInfo.insulin = t.insulin; detailedBolusInfo.timestamp = System.currentTimeMillis(); - if (detailedBolusInfo.insulin > 0) pumpSync.syncBolusWithPumpId( detailedBolusInfo.timestamp, @@ -262,7 +260,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { // Check if some temp is already in progress if (danaPump.isTempBasalInProgress()) { // Correct basal already set ? - aapsLogger.debug(LTag.PUMP, "setTempBasalAbsolute: currently running: " + danaPump.getTempBasalPercent() + "%"); + aapsLogger.debug(LTag.PUMP, "setTempBasalAbsolute: currently running: " + danaPump.temporaryBasalToString()); if (danaPump.getTempBasalPercent() == percentRate && danaPump.getTempBasalRemainingMin() > 4) { if (enforceNew) { cancelTempBasal(true); @@ -348,7 +346,16 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { PumpEnactResult result = new PumpEnactResult(getInjector()); if (danaPump.isTempBasalInProgress()) { sExecutionService.tempBasalStop(); - result.enacted(true).isTempCancel(true); + if (!danaPump.isTempBasalInProgress()) { + pumpSync.syncStopTemporaryBasalWithPumpId( + dateUtil._now(), + dateUtil._now(), + getPumpDescription().getPumpType(), + serialNumber() + ); + result.success(true).enacted(true).isTempCancel(true); + } else + result.success(false).enacted(false).isTempCancel(true); } else { result.success(true).isTempCancel(true).comment(R.string.ok); aapsLogger.debug(LTag.PUMP, "cancelRealTempBasal: OK"); diff --git a/danar/src/main/java/info/nightscout/androidaps/danaRv2/DanaRv2Plugin.java b/danar/src/main/java/info/nightscout/androidaps/danaRv2/DanaRv2Plugin.java index cdec52119c..cd88622ed5 100644 --- a/danar/src/main/java/info/nightscout/androidaps/danaRv2/DanaRv2Plugin.java +++ b/danar/src/main/java/info/nightscout/androidaps/danaRv2/DanaRv2Plugin.java @@ -62,12 +62,12 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin { AapsSchedulers aapsSchedulers, RxBusWrapper rxBus, Context context, - DanaPump danaPump, ResourceHelper resourceHelper, ConstraintChecker constraintChecker, ActivePluginProvider activePlugin, SP sp, CommandQueueProvider commandQueue, + DanaPump danaPump, DetailedBolusInfoStorage detailedBolusInfoStorage, TemporaryBasalStorage temporaryBasalStorage, DateUtil dateUtil, diff --git a/danar/src/main/java/info/nightscout/androidaps/danar/AbstractDanaRPlugin.java b/danar/src/main/java/info/nightscout/androidaps/danar/AbstractDanaRPlugin.java index b5ac839e33..17df4dbfe5 100644 --- a/danar/src/main/java/info/nightscout/androidaps/danar/AbstractDanaRPlugin.java +++ b/danar/src/main/java/info/nightscout/androidaps/danar/AbstractDanaRPlugin.java @@ -10,7 +10,6 @@ import java.util.Date; import dagger.android.HasAndroidInjector; import info.nightscout.androidaps.dana.DanaFragment; import info.nightscout.androidaps.dana.DanaPump; -import info.nightscout.androidaps.dana.DanaPumpInterface; import info.nightscout.androidaps.dana.comm.RecordTypes; import info.nightscout.androidaps.danar.services.AbstractDanaRExecutionService; import info.nightscout.androidaps.data.Profile; @@ -49,7 +48,7 @@ import io.reactivex.disposables.CompositeDisposable; * Created by mike on 28.01.2018. */ -public abstract class AbstractDanaRPlugin extends PumpPluginBase implements PumpInterface, DanaRInterface, ConstraintsInterface, DanaPumpInterface { +public abstract class AbstractDanaRPlugin extends PumpPluginBase implements PumpInterface, DanaRInterface, ConstraintsInterface { protected AbstractDanaRExecutionService sExecutionService; protected CompositeDisposable disposable = new CompositeDisposable(); @@ -283,6 +282,14 @@ public abstract class AbstractDanaRPlugin extends PumpPluginBase implements Pump getAapsLogger().debug(LTag.PUMP, "setExtendedBolus: Correct extended bolus already set. Current: " + danaPump.getExtendedBolusAmount() + " Asked: " + insulin); return result; } + if (danaPump.isExtendedInProgress()) { + cancelExtendedBolus(); + if (danaPump.isExtendedInProgress()) { + result.enacted(false).success(false); + getAapsLogger().debug(LTag.PUMP, "cancelExtendedBolus failed. aborting setExtendedBolus"); + return result; + } + } boolean connectionOK = sExecutionService.extendedBolus(insulin, durationInHalfHours); if (connectionOK && danaPump.isExtendedInProgress() && Math.abs(danaPump.getExtendedBolusAmount() - insulin) < getPumpDescription().getExtendedBolusStep()) { result.enacted(true) @@ -294,11 +301,21 @@ public abstract class AbstractDanaRPlugin extends PumpPluginBase implements Pump .isPercent(false); if (!sp.getBoolean("danar_useextended", false)) result.bolusDelivered(danaPump.getExtendedBolusAmount()); + pumpSync.syncExtendedBolusWithPumpId( + danaPump.getExtendedBolusStart(), + danaPump.getExtendedBolusAmount(), + danaPump.getExtendedBolusDuration(), + sp.getBoolean("danar_useextended", false), + danaPump.getExtendedBolusStart(), + getPumpDescription().getPumpType(), + serialNumber() + ); getAapsLogger().debug(LTag.PUMP, "setExtendedBolus: OK"); return result; } result.enacted(false).success(false).comment(R.string.danar_valuenotsetproperly); getAapsLogger().error("setExtendedBolus: Failed to extended bolus"); + getAapsLogger().error("inProgress: " + danaPump.isExtendedInProgress() + " start: " + danaPump.getExtendedBolusStart() + " amount: " + danaPump.getExtendedBolusAmount() + " duration: " + danaPump.getExtendedBolusDuration()); return result; } @@ -307,9 +324,18 @@ public abstract class AbstractDanaRPlugin extends PumpPluginBase implements Pump PumpEnactResult result = new PumpEnactResult(getInjector()); if (danaPump.isExtendedInProgress()) { sExecutionService.extendedBolusStop(); - result.enacted(true).isTempCancel(true); + if (!danaPump.isExtendedInProgress()) { + result.success(true).enacted(true).isTempCancel(true); + pumpSync.syncStopExtendedBolusWithPumpId( + dateUtil._now(), + dateUtil._now(), + getPumpDescription().getPumpType(), + serialNumber() + ); + } else + result.success(false).enacted(false).isTempCancel(true); } else { - result.success(true).comment(R.string.ok); + result.success(true).comment(R.string.ok).isTempCancel(true); getAapsLogger().debug(LTag.PUMP, "cancelExtendedBolus: OK"); } return result; diff --git a/danar/src/main/java/info/nightscout/androidaps/danar/DanaRPlugin.java b/danar/src/main/java/info/nightscout/androidaps/danar/DanaRPlugin.java index 8a5b785d2a..a4b20df5bc 100644 --- a/danar/src/main/java/info/nightscout/androidaps/danar/DanaRPlugin.java +++ b/danar/src/main/java/info/nightscout/androidaps/danar/DanaRPlugin.java @@ -340,7 +340,16 @@ public class DanaRPlugin extends AbstractDanaRPlugin { PumpEnactResult result = new PumpEnactResult(getInjector()); if (danaPump.isTempBasalInProgress()) { sExecutionService.tempBasalStop(); - result.enacted(true).isTempCancel(true); + if (!danaPump.isTempBasalInProgress()) { + pumpSync.syncStopTemporaryBasalWithPumpId( + dateUtil._now(), + dateUtil._now(), + getPumpDescription().getPumpType(), + serialNumber() + ); + result.success(true).enacted(true).isTempCancel(true); + } else + result.success(false).enacted(false).isTempCancel(true); } else { result.success(true).isTempCancel(true).comment(R.string.ok); aapsLogger.debug(LTag.PUMP, "cancelRealTempBasal: OK"); diff --git a/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgInitConnStatusBasic.kt b/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgInitConnStatusBasic.kt index c0b3864df1..c69078a09b 100644 --- a/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgInitConnStatusBasic.kt +++ b/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgInitConnStatusBasic.kt @@ -23,9 +23,9 @@ class MsgInitConnStatusBasic( danaPump.reservoirRemainingUnits = intFromBuff(bytes, 7, 3) / 750.0 danaPump.bolusBlocked = intFromBuff(bytes, 10, 1) == 1 danaPump.currentBasal = intFromBuff(bytes, 11, 2) / 100.0 - danaPump.tempBasalPercent = intFromBuff(bytes, 13, 1) - danaPump.isExtendedInProgress = intFromBuff(bytes, 14, 1) == 1 - danaPump.isTempBasalInProgress = intFromBuff(bytes, 15, 1) == 1 + val tempBasalPercent = intFromBuff(bytes, 13, 1) + val isExtendedInProgress = intFromBuff(bytes, 14, 1) == 1 + val isTempBasalInProgress = intFromBuff(bytes, 15, 1) == 1 val statusBasalUDOption = intFromBuff(bytes, 16, 1) danaPump.isDualBolusInProgress = intFromBuff(bytes, 17, 1) == 1 val extendedBolusRate = intFromBuff(bytes, 18, 2) / 100.0 @@ -46,8 +46,8 @@ class MsgInitConnStatusBasic( aapsLogger.debug(LTag.PUMPCOMM, "Reservoir remaining units: " + danaPump.reservoirRemainingUnits) aapsLogger.debug(LTag.PUMPCOMM, "Bolus blocked: " + danaPump.bolusBlocked) aapsLogger.debug(LTag.PUMPCOMM, "Current basal: " + danaPump.currentBasal) - aapsLogger.debug(LTag.PUMPCOMM, "Current temp basal percent: " + danaPump.tempBasalPercent) - aapsLogger.debug(LTag.PUMPCOMM, "Is extended bolus running: " + danaPump.isExtendedInProgress) + aapsLogger.debug(LTag.PUMPCOMM, "Current temp basal percent: " + tempBasalPercent) + aapsLogger.debug(LTag.PUMPCOMM, "Is extended bolus running: " + isExtendedInProgress) aapsLogger.debug(LTag.PUMPCOMM, "statusBasalUDOption: $statusBasalUDOption") aapsLogger.debug(LTag.PUMPCOMM, "Is dual bolus running: " + danaPump.isDualBolusInProgress) aapsLogger.debug(LTag.PUMPCOMM, "Extended bolus rate: $extendedBolusRate") diff --git a/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgSetExtendedBolusStop.kt b/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgSetExtendedBolusStop.kt index 422fef6875..1e6c825f0d 100644 --- a/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgSetExtendedBolusStop.kt +++ b/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgSetExtendedBolusStop.kt @@ -19,7 +19,7 @@ class MsgSetExtendedBolusStop( aapsLogger.debug(LTag.PUMPBTCOMM, "Set extended bolus stop result: $result FAILED!!!") } else { failed = false - aapsLogger.debug(LTag.PUMPBTCOMM, "Set extended bolus stop result: $result") + aapsLogger.debug(LTag.PUMPBTCOMM, "Set extended bolus stop result: $result OK") } } } \ No newline at end of file diff --git a/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgStatusBolusExtended.kt b/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgStatusBolusExtended.kt index bf95b17c80..6795b472ef 100644 --- a/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgStatusBolusExtended.kt +++ b/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgStatusBolusExtended.kt @@ -3,7 +3,8 @@ package info.nightscout.androidaps.danar.comm import dagger.android.HasAndroidInjector import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.utils.T -import kotlin.math.ceil +import kotlin.math.abs +import kotlin.math.floor class MsgStatusBolusExtended( injector: HasAndroidInjector @@ -27,9 +28,17 @@ class MsgStatusBolusExtended( val extendedBolusAbsoluteRate = if (isExtendedInProgress) extendedBolusAmount / extendedBolusMinutes * 60 else 0.0 val extendedBolusStart = if (isExtendedInProgress) getDateFromSecAgo(extendedBolusSoFarInSecs) else 0 val extendedBolusRemainingMinutes = extendedBolusMinutes - extendedBolusSoFarInMinutes - danaPump.extendedBolusDuration = T.mins(extendedBolusMinutes).msecs() - danaPump.extendedBolusAmount = extendedBolusAmount - danaPump.extendedBolusStart = extendedBolusStart + if (isExtendedInProgress && !isWithin3Sec(extendedBolusStart)) { + danaPump.extendedBolusDuration = T.mins(extendedBolusMinutes).msecs() + danaPump.extendedBolusAmount = extendedBolusAmount + danaPump.extendedBolusStart = extendedBolusStart + aapsLogger.debug(LTag.PUMPCOMM, "New extended bolus detected") + } else if (!isExtendedInProgress) { + aapsLogger.debug(LTag.PUMPCOMM, "Extended bolus stopped. Previous state: ${danaPump.isExtendedInProgress}") + danaPump.isExtendedInProgress = false + } else { + aapsLogger.debug(LTag.PUMPCOMM, "No change in extended bolus. Current state: ${danaPump.isExtendedInProgress}") + } aapsLogger.debug(LTag.PUMPCOMM, "Is extended bolus running: $isExtendedInProgress") aapsLogger.debug(LTag.PUMPCOMM, "Extended bolus min: $extendedBolusMinutes") aapsLogger.debug(LTag.PUMPCOMM, "Extended bolus amount: $extendedBolusAmount") @@ -40,6 +49,9 @@ class MsgStatusBolusExtended( } private fun getDateFromSecAgo(tempBasalAgoSecs: Int): Long { - return (ceil(System.currentTimeMillis() / 1000.0) - tempBasalAgoSecs).toLong() * 1000 + return (floor(System.currentTimeMillis() / 1000.0) - tempBasalAgoSecs).toLong() * 1000 } + + // because there is no fixed timestamp of start allow update of tbr only if tbr start differs more + private fun isWithin3Sec(newStart: Long) = abs(newStart - danaPump.tempBasalStart) < 3000 } \ No newline at end of file diff --git a/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgStatusTempBasal.kt b/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgStatusTempBasal.kt index b71099dd1b..07cb3a461d 100644 --- a/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgStatusTempBasal.kt +++ b/danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgStatusTempBasal.kt @@ -2,8 +2,10 @@ package info.nightscout.androidaps.danar.comm import dagger.android.HasAndroidInjector import info.nightscout.androidaps.logging.LTag +import info.nightscout.androidaps.utils.T import kotlin.math.abs import kotlin.math.ceil +import kotlin.math.floor class MsgStatusTempBasal( injector: HasAndroidInjector @@ -19,26 +21,33 @@ class MsgStatusTempBasal( val isAPSTempBasalInProgress = intFromBuff(bytes, 0, 1) and 0x02 == 0x02 var tempBasalPercent = intFromBuff(bytes, 1, 1) if (tempBasalPercent > 200) tempBasalPercent = (tempBasalPercent - 200) * 10 - val tempBasalTotalSec: Int = if (intFromBuff(bytes, 2, 1) == 150) 15 * 60 else if (intFromBuff(bytes, 2, 1) == 160) 30 * 60 else intFromBuff(bytes, 2, 1) * 60 * 60 + var tempBasalTotalMin = intFromBuff(bytes, 2, 1) * 60 + tempBasalTotalMin = if (tempBasalTotalMin == 150) 15 else if (tempBasalTotalMin == 160) 30 else tempBasalTotalMin + val tempBasalTotalSec = tempBasalTotalMin * 60 val tempBasalRunningSeconds = intFromBuff(bytes, 3, 3) val tempBasalRemainingMin = (tempBasalTotalSec - tempBasalRunningSeconds) / 60 - val tempBasalStart = if (isTempBasalInProgress) getDateFromTempBasalSecAgo(tempBasalRunningSeconds) else 0 + val tempBasalStart = if (isTempBasalInProgress) getDateFromSecAgo(tempBasalRunningSeconds) else 0 if (isTempBasalInProgress && !isWithin3Sec(tempBasalStart)) { danaPump.tempBasalStart = tempBasalStart danaPump.tempBasalPercent = tempBasalPercent - danaPump.tempBasalTotalSec = tempBasalTotalSec.toLong() + danaPump.tempBasalDuration = T.mins(tempBasalTotalMin.toLong()).msecs() + aapsLogger.debug(LTag.PUMPCOMM, "New temp basal detected") + } else if (!isTempBasalInProgress) { + aapsLogger.debug(LTag.PUMPCOMM, "Temp basal stopped. Previous state: ${danaPump.isTempBasalInProgress}") + danaPump.isTempBasalInProgress = false + } else { + aapsLogger.debug(LTag.PUMPCOMM, "No change in temp basal. Current state: ${danaPump.isTempBasalInProgress}") } - if (!isTempBasalInProgress) danaPump.isTempBasalInProgress = false aapsLogger.debug(LTag.PUMPCOMM, "Is temp basal running: $isTempBasalInProgress") aapsLogger.debug(LTag.PUMPCOMM, "Is APS temp basal running: $isAPSTempBasalInProgress") aapsLogger.debug(LTag.PUMPCOMM, "Current temp basal percent: $tempBasalPercent") aapsLogger.debug(LTag.PUMPCOMM, "Current temp basal remaining min: $tempBasalRemainingMin") - aapsLogger.debug(LTag.PUMPCOMM, "Current temp basal total sec: $tempBasalTotalSec") + aapsLogger.debug(LTag.PUMPCOMM, "Current temp basal total min: $tempBasalTotalMin") aapsLogger.debug(LTag.PUMPCOMM, "Current temp basal start: " + dateUtil.dateAndTimeString(tempBasalStart)) } - private fun getDateFromTempBasalSecAgo(tempBasalAgoSecs: Int): Long { - return (ceil(System.currentTimeMillis() / 1000.0) - tempBasalAgoSecs).toLong() * 1000 + private fun getDateFromSecAgo(tempBasalAgoSecs: Int): Long { + return (floor(System.currentTimeMillis() / 1000.0) - tempBasalAgoSecs).toLong() * 1000 } // because there is no fixed timestamp of start allow update of tbr only if tbr start differs more diff --git a/danar/src/main/java/info/nightscout/androidaps/danar/services/AbstractDanaRExecutionService.java b/danar/src/main/java/info/nightscout/androidaps/danar/services/AbstractDanaRExecutionService.java index 12a4c16b3a..aad07e6a3f 100644 --- a/danar/src/main/java/info/nightscout/androidaps/danar/services/AbstractDanaRExecutionService.java +++ b/danar/src/main/java/info/nightscout/androidaps/danar/services/AbstractDanaRExecutionService.java @@ -295,7 +295,7 @@ public abstract class AbstractDanaRExecutionService extends DaggerService { if (pumpState.getTemporaryBasal() != null) { if (danaPump.isTempBasalInProgress()) { if (pumpState.getTemporaryBasal().getRate() != danaPump.getTempBasalPercent() - || pumpState.getTemporaryBasal().getTimestamp() != danaPump.getTempBasalStart() + || Math.abs(pumpState.getTemporaryBasal().getTimestamp() - danaPump.getTempBasalStart()) > 10000 ) { // Close current temp basal Notification notification = new Notification(Notification.UNSUPPORTED_ACTION_IN_PUMP, resourceHelper.gs(R.string.unsupported_action_in_pump), Notification.URGENT); rxBus.send(new EventNewNotification(notification)); @@ -341,7 +341,8 @@ public abstract class AbstractDanaRExecutionService extends DaggerService { if (pumpState.getExtendedBolus() != null) { if (danaPump.isExtendedInProgress()) { if (pumpState.getExtendedBolus().getRate() != danaPump.getExtendedBolusAbsoluteRate() - || pumpState.getExtendedBolus().getTimestamp() != danaPump.getExtendedBolusStart()) { // Close current extended + || Math.abs(pumpState.getExtendedBolus().getTimestamp() - danaPump.getExtendedBolusStart()) > 10000 + ) { // Close current extended Notification notification = new Notification(Notification.UNSUPPORTED_ACTION_IN_PUMP, resourceHelper.gs(R.string.unsupported_action_in_pump), Notification.URGENT); rxBus.send(new EventNewNotification(notification)); aapsLogger.error(LTag.PUMP, "Different extended bolus found running AAPS: " + (pumpState.getExtendedBolus() + " DanaPump " + danaPump.extendedBolusToString())); diff --git a/danars/src/main/java/info/nightscout/androidaps/danars/DanaRSPlugin.kt b/danars/src/main/java/info/nightscout/androidaps/danars/DanaRSPlugin.kt index f70e3df2f0..c985da149d 100644 --- a/danars/src/main/java/info/nightscout/androidaps/danars/DanaRSPlugin.kt +++ b/danars/src/main/java/info/nightscout/androidaps/danars/DanaRSPlugin.kt @@ -9,7 +9,6 @@ import android.text.format.DateFormat import androidx.preference.Preference import dagger.android.HasAndroidInjector import info.nightscout.androidaps.dana.DanaPump -import info.nightscout.androidaps.dana.DanaPumpInterface import info.nightscout.androidaps.danars.events.EventDanaRSDeviceChange import info.nightscout.androidaps.danars.services.DanaRSService import info.nightscout.androidaps.data.DetailedBolusInfo @@ -72,7 +71,7 @@ class DanaRSPlugin @Inject constructor( .preferencesId(R.xml.pref_danars) .description(R.string.description_pump_dana_rs), injector, aapsLogger, resourceHelper, commandQueue -), PumpInterface, DanaRInterface, ConstraintsInterface, DanaPumpInterface { +), PumpInterface, DanaRInterface, ConstraintsInterface { private val disposable = CompositeDisposable() private var danaRSService: DanaRSService? = null diff --git a/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRS_Packet_Bolus_Get_Extended_Menu_Option_State.kt b/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRS_Packet_Bolus_Get_Extended_Menu_Option_State.kt index 3757188d4a..d427520004 100644 --- a/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRS_Packet_Bolus_Get_Extended_Menu_Option_State.kt +++ b/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRS_Packet_Bolus_Get_Extended_Menu_Option_State.kt @@ -23,7 +23,7 @@ class DanaRS_Packet_Bolus_Get_Extended_Menu_Option_State( val extendedMenuOption = byteArrayToInt(getBytes(data, dataIndex, dataSize)) dataIndex += dataSize dataSize = 1 - danaPump.isExtendedInProgress = byteArrayToInt(getBytes(data, dataIndex, dataSize)) == 0x01 + val isExtendedInProgress = byteArrayToInt(getBytes(data, dataIndex, dataSize)) == 0x01 aapsLogger.debug(LTag.PUMPCOMM, "extendedMenuOption: $extendedMenuOption") } diff --git a/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt b/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt index b287f16eae..af800d028b 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt @@ -286,9 +286,14 @@ open class AppRepository @Inject internal constructor( database.bolusDao.getModifiedFrom(lastId) .subscribeOn(Schedulers.io()) - fun getLastBolusRecord(): Bolus? = + fun getLastBolusRecord():Bolus? = database.bolusDao.getLastBolusRecord() + fun getLastBolusRecordWrapped():Single> = + database.bolusDao.getLastBolusRecordMaybe() + .subscribeOn(Schedulers.io()) + .toWrappedSingle() + fun getLastBolusRecordOfType(type: Bolus.Type): Bolus? = database.bolusDao.getLastBolusRecordOfType(type) diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/BolusDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/BolusDao.kt index e5c7543b17..326e08e1dd 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/BolusDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/BolusDao.kt @@ -33,6 +33,9 @@ internal interface BolusDao : TraceableDao { @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND type <> :exclude AND referenceId IS NULL ORDER BY id ASC LIMIT 1") fun getLastBolusRecord(exclude: Bolus.Type = Bolus.Type.PRIMING): Bolus? + @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND type <> :exclude AND referenceId IS NULL ORDER BY id ASC LIMIT 1") + fun getLastBolusRecordMaybe(exclude: Bolus.Type = Bolus.Type.PRIMING): Maybe + @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND type == :only AND referenceId IS NULL ORDER BY id ASC LIMIT 1") fun getLastBolusRecordOfType(only: Bolus.Type): Bolus? diff --git a/database/src/main/java/info/nightscout/androidaps/database/entities/ExtendedBolus.kt b/database/src/main/java/info/nightscout/androidaps/database/entities/ExtendedBolus.kt index c64dfa8535..08a218aea8 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/entities/ExtendedBolus.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/entities/ExtendedBolus.kt @@ -33,6 +33,10 @@ data class ExtendedBolus( var isEmulatingTempBasal: Boolean = false ) : TraceableDBEntry, DBEntryWithTimeAndDuration { + init { + require(duration > 0) + } + val rate: Double // in U/h get() = amount * (60 * 60 * 1000.0) / duration } \ No newline at end of file diff --git a/database/src/main/java/info/nightscout/androidaps/database/entities/TemporaryBasal.kt b/database/src/main/java/info/nightscout/androidaps/database/entities/TemporaryBasal.kt index b72cac99f0..8f5e709790 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/entities/TemporaryBasal.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/entities/TemporaryBasal.kt @@ -34,6 +34,11 @@ data class TemporaryBasal( override var duration: Long ) : TraceableDBEntry, DBEntryWithTimeAndDuration { + init { + if (duration <= 0) + require(duration > 0) + } + enum class Type { NORMAL, EMULATED_PUMP_SUSPEND, From 4a51b276f93e582b1e9ef93b108ed15654e13638 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 9 Apr 2021 10:16:47 +0200 Subject: [PATCH 2/2] add DiaLink text --- rileylink/src/main/res/values/strings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rileylink/src/main/res/values/strings.xml b/rileylink/src/main/res/values/strings.xml index 0fbfee6e56..a2e2f723f1 100644 --- a/rileylink/src/main/res/values/strings.xml +++ b/rileylink/src/main/res/values/strings.xml @@ -77,7 +77,7 @@ No - Show battery level reported by OrangeLink/EmaLink + Show battery level reported by OrangeLink/EmaLink/DiaLink DOES NOT work with the original RileyLink. May not work with other RileyLink alternatives.