From 3f67e690235cee02921c95c9081492364449947d Mon Sep 17 00:00:00 2001 From: piotrek Date: Tue, 29 Nov 2022 22:10:06 +0100 Subject: [PATCH 01/13] Add new SMS command - LOOP LGS/CLOSED - Adding new SMS command in order to be able to switch between APS mode LGS/Closed via SMS. - Adding also feedback about chosen mode when LOOP STATUS is queried. - Refactor "magic strings" of APS mode and replace it with Enum. --- .../interfaces/ConstraintsCheckerTest.kt | 11 ++-- .../plugins/aps/loop/LoopPluginTest.kt | 3 +- .../plugins/safety/SafetyPluginTest.kt | 7 +-- .../info/nightscout/interfaces/ApsMode.kt | 7 +++ .../nightscout/plugins/aps/loop/LoopPlugin.kt | 6 ++- .../objectives/objectives/Objective6.kt | 3 +- .../constraints/safety/SafetyPlugin.kt | 9 ++-- .../smsCommunicator/SmsCommunicatorPlugin.kt | 51 ++++++++++++++++--- .../src/main/res/values-pl-rPL/strings.xml | 3 ++ plugins/main/src/main/res/values/strings.xml | 3 ++ .../info/nightscout/ui/dialogs/LoopDialog.kt | 11 ++-- 11 files changed, 87 insertions(+), 27 deletions(-) create mode 100644 core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt index ed5fca4627..3fdbede3d1 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt @@ -14,6 +14,7 @@ import info.nightscout.androidaps.plugins.pump.insight.LocalInsightPlugin import info.nightscout.core.iob.iobCobCalculator.GlucoseStatusProvider import info.nightscout.database.impl.AppRepository import info.nightscout.implementation.constraints.ConstraintsImpl +import info.nightscout.interfaces.ApsMode import info.nightscout.interfaces.constraints.Constraint import info.nightscout.interfaces.constraints.Constraints import info.nightscout.interfaces.constraints.Objectives @@ -266,13 +267,13 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { // 2x Safety & Objectives @Test fun isClosedLoopAllowedTest() { - `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("closed") + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) objectivesPlugin.objectives[Objectives.MAXIOB_ZERO_CL_OBJECTIVE].startedOn = 0 var c: Constraint = constraintChecker.isClosedLoopAllowed() aapsLogger.debug("Reason list: " + c.reasonList.toString()) // Assert.assertTrue(c.reasonList[0].toString().contains("Closed loop is disabled")) // Safety & Objectives Assert.assertEquals(false, c.value()) - `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("open") + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.OPEN.name) c = constraintChecker.isClosedLoopAllowed() Assert.assertTrue(c.reasonList[0].contains("Closed loop mode disabled in preferences")) // Safety & Objectives // Assert.assertEquals(3, c.reasonList.size) // 2x Safety & Objectives @@ -315,7 +316,7 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { openAPSSMBPlugin.setPluginEnabled(PluginType.APS, true) objectivesPlugin.objectives[Objectives.SMB_OBJECTIVE].startedOn = 0 `when`(sp.getBoolean(R.string.key_use_smb, false)).thenReturn(false) - `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("open") + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.OPEN.name) // `when`(constraintChecker.isClosedLoopAllowed()).thenReturn(Constraint(true)) val c = constraintChecker.isSMBModeEnabled() Assert.assertEquals(true, c.reasonList.size == 3) // 2x Safety & Objectives @@ -422,7 +423,7 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { @Test fun iobAMAShouldBeLimited() { // No limit by default - `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("closed") + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) `when`(sp.getDouble(R.string.key_openapsma_max_iob, 1.5)).thenReturn(1.5) `when`(sp.getString(R.string.key_age, "")).thenReturn("teenage") openAPSAMAPlugin.setPluginEnabled(PluginType.APS, true) @@ -438,7 +439,7 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { @Test fun iobSMBShouldBeLimited() { // No limit by default - `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("closed") + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) `when`(sp.getDouble(R.string.key_openapssmb_max_iob, 3.0)).thenReturn(3.0) `when`(sp.getString(R.string.key_age, "")).thenReturn("teenage") openAPSSMBPlugin.setPluginEnabled(PluginType.APS, true) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt index c2df9e12e0..33c27a8d84 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt @@ -8,6 +8,7 @@ import info.nightscout.androidaps.R import info.nightscout.androidaps.TestBase import info.nightscout.core.utils.fabric.FabricPrivacy import info.nightscout.database.impl.AppRepository +import info.nightscout.interfaces.ApsMode import info.nightscout.interfaces.Config import info.nightscout.interfaces.configBuilder.RunningConfiguration import info.nightscout.interfaces.constraints.Constraints @@ -71,7 +72,7 @@ class LoopPluginTest : TestBase() { fun testPluginInterface() { `when`(rh.gs(R.string.loop)).thenReturn("Loop") `when`(rh.gs(R.string.loop_shortname)).thenReturn("LOOP") - `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("closed") + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) val pumpDescription = PumpDescription() `when`(virtualPumpPlugin.pumpDescription).thenReturn(pumpDescription) Assert.assertEquals(LoopFragment::class.java.name, loopPlugin.pluginDescription.fragmentClass) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/safety/SafetyPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/safety/SafetyPluginTest.kt index 119e5d3226..19cc099966 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/safety/SafetyPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/safety/SafetyPluginTest.kt @@ -6,6 +6,7 @@ import info.nightscout.androidaps.HardLimitsMock import info.nightscout.androidaps.TestBaseWithProfile import info.nightscout.core.iob.iobCobCalculator.GlucoseStatusProvider import info.nightscout.database.impl.AppRepository +import info.nightscout.interfaces.ApsMode import info.nightscout.interfaces.Constants import info.nightscout.interfaces.constraints.Constraint import info.nightscout.interfaces.constraints.Constraints @@ -95,7 +96,7 @@ class SafetyPluginTest : TestBaseWithProfile() { @Test fun disabledEngineeringModeShouldLimitClosedLoop() { - `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("closed") + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) `when`(config.isEngineeringModeOrRelease()).thenReturn(false) var c = Constraint(true) c = safetyPlugin.isClosedLoopAllowed(c) @@ -105,7 +106,7 @@ class SafetyPluginTest : TestBaseWithProfile() { @Test fun setOpenLoopInPreferencesShouldLimitClosedLoop() { - `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("open") + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.OPEN.name) var c = Constraint(true) c = safetyPlugin.isClosedLoopAllowed(c) Assert.assertTrue(c.getReasons(aapsLogger).contains("Closed loop mode disabled in preferences")) @@ -275,7 +276,7 @@ Safety: Limiting max basal rate to 500.00 U/h because of pump limit openAPSSMBPlugin.setPluginEnabled(PluginType.APS, true) //`when`(openAPSSMBPlugin.isEnabled()).thenReturn(true) //`when`(openAPSAMAPlugin.isEnabled()).thenReturn(false) - `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("lgs") + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.LGS.name) `when`(sp.getDouble(info.nightscout.plugins.aps.R.string.key_openapsma_max_iob, 1.5)).thenReturn(1.5) `when`(sp.getDouble(info.nightscout.plugins.aps.R.string.key_openapssmb_max_iob, 3.0)).thenReturn(3.0) `when`(sp.getString(R.string.key_age, "")).thenReturn("teenage") diff --git a/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt new file mode 100644 index 0000000000..02f508316c --- /dev/null +++ b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt @@ -0,0 +1,7 @@ +package info.nightscout.interfaces + +enum class ApsMode { + OPEN, + CLOSED, + LGS; +} diff --git a/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt index 7365d6a89e..fe11080b7f 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt @@ -31,6 +31,7 @@ import info.nightscout.database.impl.transactions.InsertAndCancelCurrentOfflineE import info.nightscout.database.impl.transactions.InsertTherapyEventAnnouncementTransaction import info.nightscout.interfaces.Config import info.nightscout.interfaces.Constants +import info.nightscout.interfaces.ApsMode import info.nightscout.interfaces.aps.APSResult import info.nightscout.interfaces.aps.Loop import info.nightscout.interfaces.aps.Loop.LastRun @@ -182,10 +183,10 @@ class LoopPlugin @Inject constructor( get() { val closedLoopEnabled = constraintChecker.isClosedLoopAllowed() val maxIobAllowed = constraintChecker.getMaxIOBAllowed().value() - val apsMode = sp.getString(R.string.key_aps_mode, "open") + val apsMode = ApsMode.valueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) val pump = activePlugin.activePump var isLGS = false - if (!isSuspended && !pump.isSuspended()) if (closedLoopEnabled.value()) if (maxIobAllowed == HardLimits.MAX_IOB_LGS || apsMode == "lgs") isLGS = true + if (!isSuspended && !pump.isSuspended()) if (closedLoopEnabled.value()) if (maxIobAllowed == HardLimits.MAX_IOB_LGS || apsMode == ApsMode.LGS) isLGS = true return isLGS } @@ -782,6 +783,7 @@ class LoopPlugin @Inject constructor( configuration = runningConfiguration.configuration().toString() ) } + companion object { private const val CHANNEL_ID = "AAPS-OpenLoop" diff --git a/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt b/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt index da33f3cf8b..78be608347 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt @@ -1,6 +1,7 @@ package info.nightscout.plugins.constraints.objectives.objectives import dagger.android.HasAndroidInjector +import info.nightscout.interfaces.ApsMode import info.nightscout.interfaces.constraints.Constraints import info.nightscout.plugins.R import info.nightscout.shared.utils.T @@ -15,7 +16,7 @@ class Objective6(injector: HasAndroidInjector) : Objective(injector, "maxiob", R tasks.add(MinimumDurationTask(this, T.days(1).msecs())) tasks.add( object : Task(this, R.string.closedmodeenabled) { - override fun isCompleted(): Boolean = sp.getString(R.string.key_aps_mode, "open") == "closed" + override fun isCompleted(): Boolean = ApsMode.valueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) == ApsMode.CLOSED }) tasks.add( object : Task(this, R.string.maxiobset) { diff --git a/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt index e5fc77eac6..43e76f0fff 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt @@ -9,6 +9,7 @@ import info.nightscout.core.extensions.storeInt import info.nightscout.core.extensions.storeString import info.nightscout.core.events.EventNewNotification import info.nightscout.interfaces.Config +import info.nightscout.interfaces.ApsMode import info.nightscout.interfaces.constraints.Constraint import info.nightscout.interfaces.constraints.Constraints import info.nightscout.interfaces.constraints.Safety @@ -66,8 +67,8 @@ class SafetyPlugin @Inject constructor( } override fun isClosedLoopAllowed(value: Constraint): Constraint { - val mode = sp.getString(R.string.key_aps_mode, "open") - if (mode == "open") value.set(aapsLogger, false, rh.gs(R.string.closedmodedisabledinpreferences), this) + val mode = ApsMode.valueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) + if (mode == ApsMode.OPEN) value.set(aapsLogger, false, rh.gs(R.string.closedmodedisabledinpreferences), this) if (!config.isEngineeringModeOrRelease()) { if (value.value()) { val n = Notification(Notification.TOAST_ALARM, rh.gs(R.string.closed_loop_disabled_on_dev_branch), Notification.NORMAL) @@ -159,8 +160,8 @@ class SafetyPlugin @Inject constructor( } override fun applyMaxIOBConstraints(maxIob: Constraint): Constraint { - val apsMode = sp.getString(R.string.key_aps_mode, "open") - if (apsMode == "lgs") maxIob.setIfSmaller(aapsLogger, HardLimits.MAX_IOB_LGS, rh.gs(R.string.limiting_iob, HardLimits.MAX_IOB_LGS, rh.gs(R.string.lowglucosesuspend)), this) + val apsMode = ApsMode.valueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) + if (apsMode == ApsMode.LGS) maxIob.setIfSmaller(aapsLogger, HardLimits.MAX_IOB_LGS, rh.gs(R.string.limiting_iob, HardLimits.MAX_IOB_LGS, rh.gs(R.string.lowglucosesuspend)), this) return maxIob } diff --git a/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt index d049a6bf38..a92a6df507 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -30,6 +30,7 @@ import info.nightscout.database.impl.transactions.CancelCurrentOfflineEventIfAny import info.nightscout.database.impl.transactions.CancelCurrentTemporaryTargetIfAnyTransaction import info.nightscout.database.impl.transactions.InsertAndCancelCurrentOfflineEventTransaction import info.nightscout.database.impl.transactions.InsertAndCancelCurrentTemporaryTargetTransaction +import info.nightscout.interfaces.ApsMode import info.nightscout.interfaces.Config import info.nightscout.interfaces.Constants import info.nightscout.interfaces.GlucoseUnit @@ -125,7 +126,7 @@ class SmsCommunicatorPlugin @Inject constructor( val commands = mapOf( "BG" to "BG", - "LOOP" to "LOOP STOP/DISABLE/START/ENABLE/RESUME/STATUS\nLOOP SUSPEND 20", + "LOOP" to "LOOP STOP/DISABLE/START/ENABLE/RESUME/STATUS/CLOSED/LGS\nLOOP SUSPEND 20", "NSCLIENT" to "NSCLIENT RESTART", "PUMP" to "PUMP\nPUMP CONNECT\nPUMP DISCONNECT 30\n", "BASAL" to "BASAL STOP/CANCEL\nBASAL 0.3\nBASAL 0.3 20\nBASAL 30%\nBASAL 30% 20\n", @@ -420,10 +421,10 @@ class SmsCommunicatorPlugin @Inject constructor( receivedSms.processed = true } - "STATUS" -> { + "STATUS" -> { val reply = if (loop.enabled) { if (loop.isSuspended) rh.gs(R.string.sms_loop_suspended_for, loop.minutesToEndOfSuspend()) - else rh.gs(R.string.smscommunicator_loop_is_enabled) + else rh.gs(R.string.smscommunicator_loop_is_enabled) + " - " + getApsModeText() } else rh.gs(R.string.loopisdisabled) sendSMS(Sms(receivedSms.phoneNumber, reply)) @@ -458,7 +459,7 @@ class SmsCommunicatorPlugin @Inject constructor( }) } - "SUSPEND" -> { + "SUSPEND" -> { var duration = 0 if (divided.size == 3) duration = SafeParse.stringToInt(divided[2]) duration = max(0, duration) @@ -506,7 +507,37 @@ class SmsCommunicatorPlugin @Inject constructor( } } - else -> sendSMS(Sms(receivedSms.phoneNumber, rh.gs(R.string.wrong_format))) + "LGS" -> { + val passCode = generatePassCode() + val reply = rh.gs(R.string.smscommunicator_set_lgs_reply_with_code, passCode) + receivedSms.processed = true + messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction(pumpCommand = false) { + override fun run() { + uel.log(Action.LGS_LOOP_MODE, Sources.SMS) + sp.putString(R.string.key_aps_mode, ApsMode.LGS.name) + rxBus.send(EventPreferenceChange(rh.gs(R.string.lowglucosesuspend))) + val replyText = rh.gs(R.string.smscommunicator_current_loop_mode, getApsModeText()) + sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) + } + }) + } + + "CLOSED" -> { + val passCode = generatePassCode() + val reply = rh.gs(R.string.smscommunicator_set_closed_loop_reply_with_code, passCode) + receivedSms.processed = true + messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction(pumpCommand = false) { + override fun run() { + uel.log(Action.CLOSED_LOOP_MODE, Sources.SMS) + sp.putString(R.string.key_aps_mode, ApsMode.CLOSED.name) + rxBus.send(EventPreferenceChange(rh.gs(R.string.closedloop))) + val replyText = rh.gs(R.string.smscommunicator_current_loop_mode, getApsModeText()) + sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) + } + }) + } + + else -> sendSMS(Sms(receivedSms.phoneNumber, rh.gs(R.string.wrong_format))) } } @@ -1255,4 +1286,12 @@ class SmsCommunicatorPlugin @Inject constructor( knownNumbers.size > 1 } ?: false } -} \ No newline at end of file + + private fun getApsModeText(): String = + when (ApsMode.valueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name))) { + ApsMode.OPEN -> rh.gs(R.string.openloop) + ApsMode.CLOSED -> rh.gs(R.string.closedloop) + ApsMode.LGS -> rh.gs(R.string.lowglucosesuspend) + else -> rh.gs(R.string.unknown) + } +} diff --git a/plugins/main/src/main/res/values-pl-rPL/strings.xml b/plugins/main/src/main/res/values-pl-rPL/strings.xml index 7c650be88b..673efeafa0 100644 --- a/plugins/main/src/main/res/values-pl-rPL/strings.xml +++ b/plugins/main/src/main/res/values-pl-rPL/strings.xml @@ -63,6 +63,9 @@ Błędny tekst wiadomości Wyślij SMS, jeśli wyzwolone jest zdarzenie pompy nieosiągalnej Zgłoś nieosiągalną pompę + Aby przełączyć pętlę w tryb LGS (zawieszenie przy niskiej glikemii) wprowadź kod %1$s + Aby przełączyć pętlę w tryb pętli zamkniętej wprowadź kod %1$s + Obecny tryb pętli: %1$s Błędny format BG: Ostatnia BG: diff --git a/plugins/main/src/main/res/values/strings.xml b/plugins/main/src/main/res/values/strings.xml index 8ac29158ee..bca6786c6a 100644 --- a/plugins/main/src/main/res/values/strings.xml +++ b/plugins/main/src/main/res/values/strings.xml @@ -95,6 +95,9 @@ Invalid message body Send SMS if unreachable pump event is triggered Report pump unreachable + In order to switch Loop mode to LGS (Low Glucose Suspend) reply with code %1$s + In order to switch Loop mode to Closed loop reply with code %1$s + Current loop mode: %1$s Wrong format BG: Last BG: diff --git a/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt b/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt index 44b579c2ce..03705264d6 100644 --- a/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt +++ b/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt @@ -21,6 +21,7 @@ import info.nightscout.database.impl.AppRepository import info.nightscout.database.impl.transactions.CancelCurrentOfflineEventIfAnyTransaction import info.nightscout.database.impl.transactions.InsertAndCancelCurrentOfflineEventTransaction import info.nightscout.interfaces.ConfigBuilder +import info.nightscout.interfaces.ApsMode import info.nightscout.interfaces.aps.Loop import info.nightscout.interfaces.constraints.Constraint import info.nightscout.interfaces.constraints.Constraints @@ -161,7 +162,7 @@ class LoopDialog : DaggerDialogFragment() { val closedLoopAllowed = constraintChecker.isClosedLoopAllowed(Constraint(true)) val closedLoopAllowed2 = activePlugin.activeObjectives?.isAccomplished(Objectives.MAXIOB_OBJECTIVE) ?: false val lgsEnabled = constraintChecker.isLgsAllowed(Constraint(true)) - val apsMode = sp.getString(R.string.key_aps_mode, "open") + val apsMode = sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name) val pump = activePlugin.activePump binding.overviewDisconnect15m.visibility = pumpDescription.tempDurationStep15mAllowed.toVisibility() @@ -282,21 +283,21 @@ class LoopDialog : DaggerDialogFragment() { when (v.id) { R.id.overview_closeloop -> { uel.log(UserEntry.Action.CLOSED_LOOP_MODE, UserEntry.Sources.LoopDialog) - sp.putString(R.string.key_aps_mode, "closed") + sp.putString(R.string.key_aps_mode, ApsMode.CLOSED.name) rxBus.send(EventPreferenceChange(rh.gs(R.string.closedloop))) return true } R.id.overview_lgsloop -> { uel.log(UserEntry.Action.LGS_LOOP_MODE, UserEntry.Sources.LoopDialog) - sp.putString(R.string.key_aps_mode, "lgs") + sp.putString(R.string.key_aps_mode, ApsMode.LGS.name) rxBus.send(EventPreferenceChange(rh.gs(R.string.lowglucosesuspend))) return true } R.id.overview_openloop -> { uel.log(UserEntry.Action.OPEN_LOOP_MODE, UserEntry.Sources.LoopDialog) - sp.putString(R.string.key_aps_mode, "open") + sp.putString(R.string.key_aps_mode, ApsMode.OPEN.name) rxBus.send(EventPreferenceChange(rh.gs(R.string.lowglucosesuspend))) return true } @@ -449,7 +450,7 @@ class LoopDialog : DaggerDialogFragment() { override fun onResume() { super.onResume() - if(!queryingProtection) { + if (!queryingProtection) { queryingProtection = true activity?.let { activity -> val cancelFail = { From 0d132c12637027e1282181d55be59592eb4fa804 Mon Sep 17 00:00:00 2001 From: piotrek Date: Tue, 29 Nov 2022 22:50:25 +0100 Subject: [PATCH 02/13] Fix junit for new SMS command - LOOP LGS/CLOSED --- .../general/smsCommunicator/SmsCommunicatorPluginTest.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt b/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt index fe36868c62..4aa6872810 100644 --- a/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt +++ b/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt @@ -12,6 +12,7 @@ import info.nightscout.database.impl.transactions.CancelCurrentOfflineEventIfAny import info.nightscout.database.impl.transactions.InsertAndCancelCurrentOfflineEventTransaction import info.nightscout.database.impl.transactions.InsertAndCancelCurrentTemporaryTargetTransaction import info.nightscout.database.impl.transactions.Transaction +import info.nightscout.interfaces.ApsMode import info.nightscout.interfaces.Constants import info.nightscout.interfaces.GlucoseUnit import info.nightscout.interfaces.XDripBroadcast @@ -333,12 +334,14 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { //LOOP STATUS : enabled `when`(loop.enabled).thenReturn(true) `when`(loop.isSuspended).thenReturn(false) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) + `when`(rh.gs(R.string.closedloop)).thenReturn(ApsMode.CLOSED.name) smsCommunicatorPlugin.messages = ArrayList() sms = Sms("1234", "LOOP STATUS") smsCommunicatorPlugin.processSms(sms) Assert.assertFalse(sms.ignored) Assert.assertEquals("LOOP STATUS", smsCommunicatorPlugin.messages[0].text) - Assert.assertEquals("Loop is enabled", smsCommunicatorPlugin.messages[1].text) + Assert.assertEquals("Loop is enabled - CLOSED", smsCommunicatorPlugin.messages[1].text) //LOOP : wrong format `when`(loop.enabled).thenReturn(true) From 2b3e96572570612b1533b089e2fc72c1a5a38ebd Mon Sep 17 00:00:00 2001 From: piotrek Date: Wed, 30 Nov 2022 12:02:51 +0100 Subject: [PATCH 03/13] ApsMode Enum - add secureValueOf to avoid IllegalArgumentException --- .../java/info/nightscout/interfaces/ApsMode.kt | 14 +++++++++++++- .../info/nightscout/plugins/aps/loop/LoopPlugin.kt | 2 +- .../objectives/objectives/Objective6.kt | 2 +- .../plugins/constraints/safety/SafetyPlugin.kt | 4 ++-- .../smsCommunicator/SmsCommunicatorPlugin.kt | 2 +- .../java/info/nightscout/ui/dialogs/LoopDialog.kt | 8 ++++---- 6 files changed, 22 insertions(+), 10 deletions(-) diff --git a/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt index 02f508316c..569fd47ab5 100644 --- a/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt +++ b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt @@ -3,5 +3,17 @@ package info.nightscout.interfaces enum class ApsMode { OPEN, CLOSED, - LGS; + LGS, + UNDEFINED; + + companion object { + + fun secureValueOf(stringValue: String): ApsMode { + return try { + valueOf(stringValue) + } catch (e: IllegalArgumentException) { + UNDEFINED + } + } + } } diff --git a/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt index fe11080b7f..b75f82f401 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt @@ -183,7 +183,7 @@ class LoopPlugin @Inject constructor( get() { val closedLoopEnabled = constraintChecker.isClosedLoopAllowed() val maxIobAllowed = constraintChecker.getMaxIOBAllowed().value() - val apsMode = ApsMode.valueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) + val apsMode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) val pump = activePlugin.activePump var isLGS = false if (!isSuspended && !pump.isSuspended()) if (closedLoopEnabled.value()) if (maxIobAllowed == HardLimits.MAX_IOB_LGS || apsMode == ApsMode.LGS) isLGS = true diff --git a/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt b/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt index 78be608347..6e9ecef1bc 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt @@ -16,7 +16,7 @@ class Objective6(injector: HasAndroidInjector) : Objective(injector, "maxiob", R tasks.add(MinimumDurationTask(this, T.days(1).msecs())) tasks.add( object : Task(this, R.string.closedmodeenabled) { - override fun isCompleted(): Boolean = ApsMode.valueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) == ApsMode.CLOSED + override fun isCompleted(): Boolean = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) == ApsMode.CLOSED }) tasks.add( object : Task(this, R.string.maxiobset) { diff --git a/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt index 43e76f0fff..ec96275312 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt @@ -67,7 +67,7 @@ class SafetyPlugin @Inject constructor( } override fun isClosedLoopAllowed(value: Constraint): Constraint { - val mode = ApsMode.valueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) + val mode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) if (mode == ApsMode.OPEN) value.set(aapsLogger, false, rh.gs(R.string.closedmodedisabledinpreferences), this) if (!config.isEngineeringModeOrRelease()) { if (value.value()) { @@ -160,7 +160,7 @@ class SafetyPlugin @Inject constructor( } override fun applyMaxIOBConstraints(maxIob: Constraint): Constraint { - val apsMode = ApsMode.valueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) + val apsMode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) if (apsMode == ApsMode.LGS) maxIob.setIfSmaller(aapsLogger, HardLimits.MAX_IOB_LGS, rh.gs(R.string.limiting_iob, HardLimits.MAX_IOB_LGS, rh.gs(R.string.lowglucosesuspend)), this) return maxIob } diff --git a/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt index a92a6df507..d14ac5c35a 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -1288,7 +1288,7 @@ class SmsCommunicatorPlugin @Inject constructor( } private fun getApsModeText(): String = - when (ApsMode.valueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name))) { + when (ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name))) { ApsMode.OPEN -> rh.gs(R.string.openloop) ApsMode.CLOSED -> rh.gs(R.string.closedloop) ApsMode.LGS -> rh.gs(R.string.lowglucosesuspend) diff --git a/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt b/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt index 03705264d6..cd8398a653 100644 --- a/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt +++ b/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt @@ -162,7 +162,7 @@ class LoopDialog : DaggerDialogFragment() { val closedLoopAllowed = constraintChecker.isClosedLoopAllowed(Constraint(true)) val closedLoopAllowed2 = activePlugin.activeObjectives?.isAccomplished(Objectives.MAXIOB_OBJECTIVE) ?: false val lgsEnabled = constraintChecker.isLgsAllowed(Constraint(true)) - val apsMode = sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name) + val apsMode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) val pump = activePlugin.activePump binding.overviewDisconnect15m.visibility = pumpDescription.tempDurationStep15mAllowed.toVisibility() @@ -212,19 +212,19 @@ class LoopDialog : DaggerDialogFragment() { binding.overviewLoop.visibility = View.VISIBLE binding.overviewEnable.visibility = View.GONE when { - apsMode == "closed" -> { + apsMode == ApsMode.CLOSED -> { binding.overviewCloseloop.visibility = View.GONE binding.overviewLgsloop.visibility = View.VISIBLE binding.overviewOpenloop.visibility = View.VISIBLE } - apsMode == "lgs" -> { + apsMode == ApsMode.LGS -> { binding.overviewCloseloop.visibility = closedLoopAllowed.value().toVisibility() //show Close loop button only if Close loop allowed binding.overviewLgsloop.visibility = View.GONE binding.overviewOpenloop.visibility = View.VISIBLE } - apsMode == "open" -> { + apsMode == ApsMode.OPEN -> { binding.overviewCloseloop.visibility = closedLoopAllowed2.toVisibility() //show CloseLoop button only if Objective 6 is completed (closedLoopAllowed always false in open loop mode) binding.overviewLgsloop.visibility = lgsEnabled.value().toVisibility() From 9916b056d19d7aa8b59b05cef7745cccfc90f447 Mon Sep 17 00:00:00 2001 From: piotrek Date: Wed, 30 Nov 2022 12:04:17 +0100 Subject: [PATCH 04/13] Extend junit tests for new SMS command - LOOP LGS/CLOSED --- .../SmsCommunicatorPluginTest.kt | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt b/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt index 4aa6872810..a97a199c45 100644 --- a/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt +++ b/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt @@ -331,17 +331,48 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assert.assertEquals("LOOP STATUS", smsCommunicatorPlugin.messages[0].text) Assert.assertEquals("Suspended (10 m)", smsCommunicatorPlugin.messages[1].text) - //LOOP STATUS : enabled + //LOOP STATUS : enabled - APS mode - Closed + val modeClosed = "Closed Loop" `when`(loop.enabled).thenReturn(true) `when`(loop.isSuspended).thenReturn(false) `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) - `when`(rh.gs(R.string.closedloop)).thenReturn(ApsMode.CLOSED.name) + `when`(rh.gs(R.string.closedloop)).thenReturn(modeClosed) smsCommunicatorPlugin.messages = ArrayList() sms = Sms("1234", "LOOP STATUS") smsCommunicatorPlugin.processSms(sms) Assert.assertFalse(sms.ignored) Assert.assertEquals("LOOP STATUS", smsCommunicatorPlugin.messages[0].text) - Assert.assertEquals("Loop is enabled - CLOSED", smsCommunicatorPlugin.messages[1].text) + Assert.assertEquals("Loop is enabled - $modeClosed", smsCommunicatorPlugin.messages[1].text) + + //LOOP STATUS : enabled - APS mode - Open + val modeOpen = "Open Loop" + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.OPEN.name) + `when`(rh.gs(R.string.openloop)).thenReturn(modeOpen) + smsCommunicatorPlugin.messages = ArrayList() + smsCommunicatorPlugin.processSms(sms) + Assert.assertFalse(sms.ignored) + Assert.assertEquals("LOOP STATUS", smsCommunicatorPlugin.messages[0].text) + Assert.assertEquals("Loop is enabled - $modeOpen", smsCommunicatorPlugin.messages[1].text) + + //LOOP STATUS : enabled - APS mode - LGS + val modeLgs = "Low Glucose Suspend" + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.LGS.name) + `when`(rh.gs(R.string.lowglucosesuspend)).thenReturn(modeLgs) + smsCommunicatorPlugin.messages = ArrayList() + smsCommunicatorPlugin.processSms(sms) + Assert.assertFalse(sms.ignored) + Assert.assertEquals("LOOP STATUS", smsCommunicatorPlugin.messages[0].text) + Assert.assertEquals("Loop is enabled - $modeLgs", smsCommunicatorPlugin.messages[1].text) + + //LOOP STATUS : enabled - APS mode - unknown + val modeUnknown = "unknown" + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn("some wrong value") + `when`(rh.gs(R.string.unknown)).thenReturn(modeUnknown) + smsCommunicatorPlugin.messages = ArrayList() + smsCommunicatorPlugin.processSms(sms) + Assert.assertFalse(sms.ignored) + Assert.assertEquals("LOOP STATUS", smsCommunicatorPlugin.messages[0].text) + Assert.assertEquals("Loop is enabled - $modeUnknown", smsCommunicatorPlugin.messages[1].text) //LOOP : wrong format `when`(loop.enabled).thenReturn(true) From 1c82a65871c314d8f21fbf710559f111d555b7c9 Mon Sep 17 00:00:00 2001 From: piotrek Date: Wed, 30 Nov 2022 16:37:10 +0100 Subject: [PATCH 05/13] Add junit tests for new SMS command - LOOP LGS/CLOSED --- .../SmsCommunicatorPluginTest.kt | 51 +++++++++++++++---- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt b/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt index a97a199c45..12b5b9a03c 100644 --- a/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt +++ b/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt @@ -87,6 +87,10 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { private lateinit var smsCommunicatorPlugin: SmsCommunicatorPlugin private var hasBeenRun = false + private val modeClosed = "Closed Loop" + private val modeOpen = "Open Loop" + private val modeLgs = "Low Glucose Suspend" + private val modeUnknown = "unknown" @BeforeEach fun prepareTests() { val reading = GlucoseValue(raw = 0.0, noise = 0.0, value = 100.0, timestamp = 1514766900000, sourceSensor = GlucoseValue.SourceSensor.UNKNOWN, trendArrow = GlucoseValue.TrendArrow.FLAT) @@ -250,7 +254,13 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { `when`(rh.gsNotLocalised(R.string.smscommunicator_tempbasal_canceled)).thenReturn("Temp basal canceled") `when`(rh.gsNotLocalised(R.string.smscommunicator_calibration_sent)).thenReturn("Calibration sent. Receiving must be enabled in xDrip+.") `when`(rh.gsNotLocalised(R.string.smscommunicator_tt_canceled)).thenReturn("Temp Target canceled successfully") - + `when`(rh.gs(R.string.closedloop)).thenReturn(modeClosed) + `when`(rh.gs(R.string.openloop)).thenReturn(modeOpen) + `when`(rh.gs(R.string.lowglucosesuspend)).thenReturn(modeLgs) + `when`(rh.gs(R.string.unknown)).thenReturn(modeUnknown) + `when`(rh.gs(R.string.smscommunicator_set_closed_loop_reply_with_code)).thenReturn("In order to switch Loop mode to Closed loop reply with code %1\$s") + `when`(rh.gs(R.string.smscommunicator_current_loop_mode)).thenReturn("Current loop mode: %1\$s") + `when`(rh.gs(R.string.smscommunicator_set_lgs_reply_with_code)).thenReturn("In order to switch Loop mode to LGS (Low Glucose Suspend) reply with code %1\$s") } @Test @@ -332,11 +342,9 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assert.assertEquals("Suspended (10 m)", smsCommunicatorPlugin.messages[1].text) //LOOP STATUS : enabled - APS mode - Closed - val modeClosed = "Closed Loop" `when`(loop.enabled).thenReturn(true) `when`(loop.isSuspended).thenReturn(false) `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) - `when`(rh.gs(R.string.closedloop)).thenReturn(modeClosed) smsCommunicatorPlugin.messages = ArrayList() sms = Sms("1234", "LOOP STATUS") smsCommunicatorPlugin.processSms(sms) @@ -345,9 +353,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assert.assertEquals("Loop is enabled - $modeClosed", smsCommunicatorPlugin.messages[1].text) //LOOP STATUS : enabled - APS mode - Open - val modeOpen = "Open Loop" `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.OPEN.name) - `when`(rh.gs(R.string.openloop)).thenReturn(modeOpen) smsCommunicatorPlugin.messages = ArrayList() smsCommunicatorPlugin.processSms(sms) Assert.assertFalse(sms.ignored) @@ -355,9 +361,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assert.assertEquals("Loop is enabled - $modeOpen", smsCommunicatorPlugin.messages[1].text) //LOOP STATUS : enabled - APS mode - LGS - val modeLgs = "Low Glucose Suspend" `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.LGS.name) - `when`(rh.gs(R.string.lowglucosesuspend)).thenReturn(modeLgs) smsCommunicatorPlugin.messages = ArrayList() smsCommunicatorPlugin.processSms(sms) Assert.assertFalse(sms.ignored) @@ -365,9 +369,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assert.assertEquals("Loop is enabled - $modeLgs", smsCommunicatorPlugin.messages[1].text) //LOOP STATUS : enabled - APS mode - unknown - val modeUnknown = "unknown" `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn("some wrong value") - `when`(rh.gs(R.string.unknown)).thenReturn(modeUnknown) smsCommunicatorPlugin.messages = ArrayList() smsCommunicatorPlugin.processSms(sms) Assert.assertFalse(sms.ignored) @@ -515,6 +517,37 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assert.assertEquals("LOOP BLABLA", smsCommunicatorPlugin.messages[0].text) Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages[1].text) + //LOOP CLOSED + var smsCommand = "LOOP CLOSED" + val replyClosed = "In order to switch Loop mode to Closed loop reply with code " + `when`(loop.enabled).thenReturn(true) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) + smsCommunicatorPlugin.messages = ArrayList() + sms = Sms("1234", smsCommand) + smsCommunicatorPlugin.processSms(sms) + Assert.assertFalse(sms.ignored) + Assert.assertEquals(smsCommand, smsCommunicatorPlugin.messages[0].text) + Assert.assertTrue(smsCommunicatorPlugin.messages[1].text.contains(replyClosed)) + passCode = smsCommunicatorPlugin.messageToConfirm?.confirmCode!! + smsCommunicatorPlugin.processSms(Sms("1234", passCode)) + Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text) + Assert.assertEquals("Current loop mode: $modeClosed", smsCommunicatorPlugin.messages[3].text) + + //LOOP LGS + smsCommand = "LOOP LGS" + val replyLgs = "In order to switch Loop mode to LGS (Low Glucose Suspend) reply with code " + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.LGS.name) + smsCommunicatorPlugin.messages = ArrayList() + sms = Sms("1234", smsCommand) + smsCommunicatorPlugin.processSms(sms) + Assert.assertFalse(sms.ignored) + Assert.assertEquals(smsCommand, smsCommunicatorPlugin.messages[0].text) + Assert.assertTrue(smsCommunicatorPlugin.messages[1].text.contains(replyLgs)) + passCode = smsCommunicatorPlugin.messageToConfirm?.confirmCode!! + smsCommunicatorPlugin.processSms(Sms("1234", passCode)) + Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text) + Assert.assertEquals("Current loop mode: $modeLgs", smsCommunicatorPlugin.messages[3].text) + //NSCLIENT RESTART `when`(loop.isEnabled()).thenReturn(true) `when`(loop.isSuspended).thenReturn(false) From 3a0305c4d2be9a334bbac406b32730c061e3ddca Mon Sep 17 00:00:00 2001 From: piotrek Date: Wed, 30 Nov 2022 17:17:23 +0100 Subject: [PATCH 06/13] add lowercase values in ApsMode Enum --- .../androidaps/interfaces/ConstraintsCheckerTest.kt | 10 +++++----- .../androidaps/plugins/aps/loop/LoopPluginTest.kt | 2 +- .../androidaps/plugins/safety/SafetyPluginTest.kt | 6 +++--- .../main/java/info/nightscout/interfaces/ApsMode.kt | 12 ++++++------ .../info/nightscout/plugins/aps/loop/LoopPlugin.kt | 2 +- .../constraints/objectives/objectives/Objective6.kt | 2 +- .../plugins/constraints/safety/SafetyPlugin.kt | 4 ++-- .../general/smsCommunicator/SmsCommunicatorPlugin.kt | 6 +++--- .../smsCommunicator/SmsCommunicatorPluginTest.kt | 12 ++++++------ .../java/info/nightscout/ui/dialogs/LoopDialog.kt | 8 ++++---- 10 files changed, 32 insertions(+), 32 deletions(-) diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt index 3fdbede3d1..a91106168f 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt @@ -267,13 +267,13 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { // 2x Safety & Objectives @Test fun isClosedLoopAllowedTest() { - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) objectivesPlugin.objectives[Objectives.MAXIOB_ZERO_CL_OBJECTIVE].startedOn = 0 var c: Constraint = constraintChecker.isClosedLoopAllowed() aapsLogger.debug("Reason list: " + c.reasonList.toString()) // Assert.assertTrue(c.reasonList[0].toString().contains("Closed loop is disabled")) // Safety & Objectives Assert.assertEquals(false, c.value()) - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.OPEN.name) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.OPEN.lowercase) c = constraintChecker.isClosedLoopAllowed() Assert.assertTrue(c.reasonList[0].contains("Closed loop mode disabled in preferences")) // Safety & Objectives // Assert.assertEquals(3, c.reasonList.size) // 2x Safety & Objectives @@ -316,7 +316,7 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { openAPSSMBPlugin.setPluginEnabled(PluginType.APS, true) objectivesPlugin.objectives[Objectives.SMB_OBJECTIVE].startedOn = 0 `when`(sp.getBoolean(R.string.key_use_smb, false)).thenReturn(false) - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.OPEN.name) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.OPEN.lowercase) // `when`(constraintChecker.isClosedLoopAllowed()).thenReturn(Constraint(true)) val c = constraintChecker.isSMBModeEnabled() Assert.assertEquals(true, c.reasonList.size == 3) // 2x Safety & Objectives @@ -423,7 +423,7 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { @Test fun iobAMAShouldBeLimited() { // No limit by default - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) `when`(sp.getDouble(R.string.key_openapsma_max_iob, 1.5)).thenReturn(1.5) `when`(sp.getString(R.string.key_age, "")).thenReturn("teenage") openAPSAMAPlugin.setPluginEnabled(PluginType.APS, true) @@ -439,7 +439,7 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { @Test fun iobSMBShouldBeLimited() { // No limit by default - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) `when`(sp.getDouble(R.string.key_openapssmb_max_iob, 3.0)).thenReturn(3.0) `when`(sp.getString(R.string.key_age, "")).thenReturn("teenage") openAPSSMBPlugin.setPluginEnabled(PluginType.APS, true) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt index 33c27a8d84..c9d75cc974 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt @@ -72,7 +72,7 @@ class LoopPluginTest : TestBase() { fun testPluginInterface() { `when`(rh.gs(R.string.loop)).thenReturn("Loop") `when`(rh.gs(R.string.loop_shortname)).thenReturn("LOOP") - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) val pumpDescription = PumpDescription() `when`(virtualPumpPlugin.pumpDescription).thenReturn(pumpDescription) Assert.assertEquals(LoopFragment::class.java.name, loopPlugin.pluginDescription.fragmentClass) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/safety/SafetyPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/safety/SafetyPluginTest.kt index 19cc099966..ac88291f3e 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/safety/SafetyPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/safety/SafetyPluginTest.kt @@ -96,7 +96,7 @@ class SafetyPluginTest : TestBaseWithProfile() { @Test fun disabledEngineeringModeShouldLimitClosedLoop() { - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) `when`(config.isEngineeringModeOrRelease()).thenReturn(false) var c = Constraint(true) c = safetyPlugin.isClosedLoopAllowed(c) @@ -106,7 +106,7 @@ class SafetyPluginTest : TestBaseWithProfile() { @Test fun setOpenLoopInPreferencesShouldLimitClosedLoop() { - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.OPEN.name) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.OPEN.lowercase) var c = Constraint(true) c = safetyPlugin.isClosedLoopAllowed(c) Assert.assertTrue(c.getReasons(aapsLogger).contains("Closed loop mode disabled in preferences")) @@ -276,7 +276,7 @@ Safety: Limiting max basal rate to 500.00 U/h because of pump limit openAPSSMBPlugin.setPluginEnabled(PluginType.APS, true) //`when`(openAPSSMBPlugin.isEnabled()).thenReturn(true) //`when`(openAPSAMAPlugin.isEnabled()).thenReturn(false) - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.LGS.name) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.LGS.lowercase) `when`(sp.getDouble(info.nightscout.plugins.aps.R.string.key_openapsma_max_iob, 1.5)).thenReturn(1.5) `when`(sp.getDouble(info.nightscout.plugins.aps.R.string.key_openapssmb_max_iob, 3.0)).thenReturn(3.0) `when`(sp.getString(R.string.key_age, "")).thenReturn("teenage") diff --git a/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt index 569fd47ab5..ed8dbe818a 100644 --- a/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt +++ b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt @@ -1,16 +1,16 @@ package info.nightscout.interfaces -enum class ApsMode { - OPEN, - CLOSED, - LGS, - UNDEFINED; +enum class ApsMode(val lowercase: String) { + OPEN("open"), + CLOSED("closed"), + LGS("lgs"), + UNDEFINED("undefined"); companion object { fun secureValueOf(stringValue: String): ApsMode { return try { - valueOf(stringValue) + valueOf(stringValue.uppercase()) } catch (e: IllegalArgumentException) { UNDEFINED } diff --git a/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt index b75f82f401..e8b4dfdd2b 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt @@ -183,7 +183,7 @@ class LoopPlugin @Inject constructor( get() { val closedLoopEnabled = constraintChecker.isClosedLoopAllowed() val maxIobAllowed = constraintChecker.getMaxIOBAllowed().value() - val apsMode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) + val apsMode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)) val pump = activePlugin.activePump var isLGS = false if (!isSuspended && !pump.isSuspended()) if (closedLoopEnabled.value()) if (maxIobAllowed == HardLimits.MAX_IOB_LGS || apsMode == ApsMode.LGS) isLGS = true diff --git a/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt b/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt index 6e9ecef1bc..32527bda16 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt @@ -16,7 +16,7 @@ class Objective6(injector: HasAndroidInjector) : Objective(injector, "maxiob", R tasks.add(MinimumDurationTask(this, T.days(1).msecs())) tasks.add( object : Task(this, R.string.closedmodeenabled) { - override fun isCompleted(): Boolean = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) == ApsMode.CLOSED + override fun isCompleted(): Boolean = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)) == ApsMode.CLOSED }) tasks.add( object : Task(this, R.string.maxiobset) { diff --git a/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt index ec96275312..5f8e38d2ef 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt @@ -67,7 +67,7 @@ class SafetyPlugin @Inject constructor( } override fun isClosedLoopAllowed(value: Constraint): Constraint { - val mode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) + val mode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)) if (mode == ApsMode.OPEN) value.set(aapsLogger, false, rh.gs(R.string.closedmodedisabledinpreferences), this) if (!config.isEngineeringModeOrRelease()) { if (value.value()) { @@ -160,7 +160,7 @@ class SafetyPlugin @Inject constructor( } override fun applyMaxIOBConstraints(maxIob: Constraint): Constraint { - val apsMode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) + val apsMode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)) if (apsMode == ApsMode.LGS) maxIob.setIfSmaller(aapsLogger, HardLimits.MAX_IOB_LGS, rh.gs(R.string.limiting_iob, HardLimits.MAX_IOB_LGS, rh.gs(R.string.lowglucosesuspend)), this) return maxIob } diff --git a/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt index d14ac5c35a..3ad271c339 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -514,7 +514,7 @@ class SmsCommunicatorPlugin @Inject constructor( messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction(pumpCommand = false) { override fun run() { uel.log(Action.LGS_LOOP_MODE, Sources.SMS) - sp.putString(R.string.key_aps_mode, ApsMode.LGS.name) + sp.putString(R.string.key_aps_mode, ApsMode.LGS.lowercase) rxBus.send(EventPreferenceChange(rh.gs(R.string.lowglucosesuspend))) val replyText = rh.gs(R.string.smscommunicator_current_loop_mode, getApsModeText()) sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) @@ -529,7 +529,7 @@ class SmsCommunicatorPlugin @Inject constructor( messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction(pumpCommand = false) { override fun run() { uel.log(Action.CLOSED_LOOP_MODE, Sources.SMS) - sp.putString(R.string.key_aps_mode, ApsMode.CLOSED.name) + sp.putString(R.string.key_aps_mode, ApsMode.CLOSED.lowercase) rxBus.send(EventPreferenceChange(rh.gs(R.string.closedloop))) val replyText = rh.gs(R.string.smscommunicator_current_loop_mode, getApsModeText()) sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) @@ -1288,7 +1288,7 @@ class SmsCommunicatorPlugin @Inject constructor( } private fun getApsModeText(): String = - when (ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name))) { + when (ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase))) { ApsMode.OPEN -> rh.gs(R.string.openloop) ApsMode.CLOSED -> rh.gs(R.string.closedloop) ApsMode.LGS -> rh.gs(R.string.lowglucosesuspend) diff --git a/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt b/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt index 12b5b9a03c..972adb3f41 100644 --- a/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt +++ b/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt @@ -344,7 +344,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { //LOOP STATUS : enabled - APS mode - Closed `when`(loop.enabled).thenReturn(true) `when`(loop.isSuspended).thenReturn(false) - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) smsCommunicatorPlugin.messages = ArrayList() sms = Sms("1234", "LOOP STATUS") smsCommunicatorPlugin.processSms(sms) @@ -353,7 +353,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assert.assertEquals("Loop is enabled - $modeClosed", smsCommunicatorPlugin.messages[1].text) //LOOP STATUS : enabled - APS mode - Open - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.OPEN.name) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.OPEN.lowercase) smsCommunicatorPlugin.messages = ArrayList() smsCommunicatorPlugin.processSms(sms) Assert.assertFalse(sms.ignored) @@ -361,7 +361,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assert.assertEquals("Loop is enabled - $modeOpen", smsCommunicatorPlugin.messages[1].text) //LOOP STATUS : enabled - APS mode - LGS - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.LGS.name) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.LGS.lowercase) smsCommunicatorPlugin.messages = ArrayList() smsCommunicatorPlugin.processSms(sms) Assert.assertFalse(sms.ignored) @@ -369,7 +369,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assert.assertEquals("Loop is enabled - $modeLgs", smsCommunicatorPlugin.messages[1].text) //LOOP STATUS : enabled - APS mode - unknown - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn("some wrong value") + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn("some wrong value") smsCommunicatorPlugin.messages = ArrayList() smsCommunicatorPlugin.processSms(sms) Assert.assertFalse(sms.ignored) @@ -521,7 +521,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { var smsCommand = "LOOP CLOSED" val replyClosed = "In order to switch Loop mode to Closed loop reply with code " `when`(loop.enabled).thenReturn(true) - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) smsCommunicatorPlugin.messages = ArrayList() sms = Sms("1234", smsCommand) smsCommunicatorPlugin.processSms(sms) @@ -536,7 +536,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { //LOOP LGS smsCommand = "LOOP LGS" val replyLgs = "In order to switch Loop mode to LGS (Low Glucose Suspend) reply with code " - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.LGS.name) + `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.LGS.lowercase) smsCommunicatorPlugin.messages = ArrayList() sms = Sms("1234", smsCommand) smsCommunicatorPlugin.processSms(sms) diff --git a/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt b/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt index cd8398a653..de2af90a0e 100644 --- a/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt +++ b/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt @@ -162,7 +162,7 @@ class LoopDialog : DaggerDialogFragment() { val closedLoopAllowed = constraintChecker.isClosedLoopAllowed(Constraint(true)) val closedLoopAllowed2 = activePlugin.activeObjectives?.isAccomplished(Objectives.MAXIOB_OBJECTIVE) ?: false val lgsEnabled = constraintChecker.isLgsAllowed(Constraint(true)) - val apsMode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) + val apsMode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)) val pump = activePlugin.activePump binding.overviewDisconnect15m.visibility = pumpDescription.tempDurationStep15mAllowed.toVisibility() @@ -283,21 +283,21 @@ class LoopDialog : DaggerDialogFragment() { when (v.id) { R.id.overview_closeloop -> { uel.log(UserEntry.Action.CLOSED_LOOP_MODE, UserEntry.Sources.LoopDialog) - sp.putString(R.string.key_aps_mode, ApsMode.CLOSED.name) + sp.putString(R.string.key_aps_mode, ApsMode.CLOSED.lowercase) rxBus.send(EventPreferenceChange(rh.gs(R.string.closedloop))) return true } R.id.overview_lgsloop -> { uel.log(UserEntry.Action.LGS_LOOP_MODE, UserEntry.Sources.LoopDialog) - sp.putString(R.string.key_aps_mode, ApsMode.LGS.name) + sp.putString(R.string.key_aps_mode, ApsMode.LGS.lowercase) rxBus.send(EventPreferenceChange(rh.gs(R.string.lowglucosesuspend))) return true } R.id.overview_openloop -> { uel.log(UserEntry.Action.OPEN_LOOP_MODE, UserEntry.Sources.LoopDialog) - sp.putString(R.string.key_aps_mode, ApsMode.OPEN.name) + sp.putString(R.string.key_aps_mode, ApsMode.OPEN.lowercase) rxBus.send(EventPreferenceChange(rh.gs(R.string.lowglucosesuspend))) return true } From db6409bb69ea79cf03a7bf6c2406aca5f406c88c Mon Sep 17 00:00:00 2001 From: piotrek Date: Sun, 4 Dec 2022 22:03:39 +0100 Subject: [PATCH 07/13] merge changes from origin/dev --- .../androidaps/interfaces/ConstraintsCheckerTest.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt index 0cd6a29032..0ec3b1d657 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt @@ -266,13 +266,13 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { // 2x Safety & Objectives @Test fun isClosedLoopAllowedTest() { - `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("closed") + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, "open")).thenReturn("closed") objectivesPlugin.objectives[Objectives.MAXIOB_ZERO_CL_OBJECTIVE].startedOn = 0 var c: Constraint = constraintChecker.isClosedLoopAllowed() aapsLogger.debug("Reason list: " + c.reasonList.toString()) // Assert.assertTrue(c.reasonList[0].toString().contains("Closed loop is disabled")) // Safety & Objectives Assert.assertEquals(false, c.value()) - `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("open") + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, "open")).thenReturn("open") c = constraintChecker.isClosedLoopAllowed() Assert.assertTrue(c.reasonList[0].contains("Closed loop mode disabled in preferences")) // Safety & Objectives // Assert.assertEquals(3, c.reasonList.size) // 2x Safety & Objectives @@ -314,8 +314,8 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { fun isSMBModeEnabledTest() { openAPSSMBPlugin.setPluginEnabled(PluginType.APS, true) objectivesPlugin.objectives[Objectives.SMB_OBJECTIVE].startedOn = 0 - `when`(sp.getBoolean(R.string.key_use_smb, false)).thenReturn(false) - `when`(sp.getString(R.string.key_aps_mode, "open")).thenReturn("open") + `when`(sp.getBoolean(info.nightscout.plugins.aps.R.string.key_use_smb, false)).thenReturn(false) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, "open")).thenReturn("open") // `when`(constraintChecker.isClosedLoopAllowed()).thenReturn(Constraint(true)) val c = constraintChecker.isSMBModeEnabled() Assert.assertEquals(true, c.reasonList.size == 3) // 2x Safety & Objectives From 1c8354a949b19e6e84357a32cb9ac792b63b95d4 Mon Sep 17 00:00:00 2001 From: piotrek Date: Sun, 4 Dec 2022 22:18:48 +0100 Subject: [PATCH 08/13] merge changes from origin/dev --- .../smsCommunicator/SmsCommunicatorPlugin.kt | 18 ++++++++--------- .../SmsCommunicatorPluginTest.kt | 20 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt index 8eefab8a68..4e1533db91 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -510,8 +510,8 @@ class SmsCommunicatorPlugin @Inject constructor( messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction(pumpCommand = false) { override fun run() { uel.log(Action.LGS_LOOP_MODE, Sources.SMS) - sp.putString(R.string.key_aps_mode, ApsMode.LGS.lowercase) - rxBus.send(EventPreferenceChange(rh.gs(R.string.lowglucosesuspend))) + sp.putString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.LGS.lowercase) + rxBus.send(EventPreferenceChange(rh.gs(info.nightscout.core.ui.R.string.lowglucosesuspend))) val replyText = rh.gs(R.string.smscommunicator_current_loop_mode, getApsModeText()) sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) } @@ -525,8 +525,8 @@ class SmsCommunicatorPlugin @Inject constructor( messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction(pumpCommand = false) { override fun run() { uel.log(Action.CLOSED_LOOP_MODE, Sources.SMS) - sp.putString(R.string.key_aps_mode, ApsMode.CLOSED.lowercase) - rxBus.send(EventPreferenceChange(rh.gs(R.string.closedloop))) + sp.putString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.CLOSED.lowercase) + rxBus.send(EventPreferenceChange(rh.gs(info.nightscout.core.ui.R.string.closedloop))) val replyText = rh.gs(R.string.smscommunicator_current_loop_mode, getApsModeText()) sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) } @@ -1284,10 +1284,10 @@ class SmsCommunicatorPlugin @Inject constructor( } private fun getApsModeText(): String = - when (ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase))) { - ApsMode.OPEN -> rh.gs(R.string.openloop) - ApsMode.CLOSED -> rh.gs(R.string.closedloop) - ApsMode.LGS -> rh.gs(R.string.lowglucosesuspend) - else -> rh.gs(R.string.unknown) + when (ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase))) { + ApsMode.OPEN -> rh.gs(info.nightscout.core.ui.R.string.openloop) + ApsMode.CLOSED -> rh.gs(info.nightscout.core.ui.R.string.closedloop) + ApsMode.LGS -> rh.gs(info.nightscout.core.ui.R.string.lowglucosesuspend) + else -> rh.gs(info.nightscout.core.ui.R.string.unknown) } } diff --git a/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt b/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt index 72c25d5757..cc2c8ad22c 100644 --- a/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt +++ b/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt @@ -254,10 +254,10 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { `when`(rh.gsNotLocalised(R.string.smscommunicator_tempbasal_canceled)).thenReturn("Temp basal canceled") `when`(rh.gsNotLocalised(R.string.smscommunicator_calibration_sent)).thenReturn("Calibration sent. Receiving must be enabled in xDrip+.") `when`(rh.gsNotLocalised(R.string.smscommunicator_tt_canceled)).thenReturn("Temp Target canceled successfully") - `when`(rh.gs(R.string.closedloop)).thenReturn(modeClosed) - `when`(rh.gs(R.string.openloop)).thenReturn(modeOpen) - `when`(rh.gs(R.string.lowglucosesuspend)).thenReturn(modeLgs) - `when`(rh.gs(R.string.unknown)).thenReturn(modeUnknown) + `when`(rh.gs(info.nightscout.core.ui.R.string.closedloop)).thenReturn(modeClosed) + `when`(rh.gs(info.nightscout.core.ui.R.string.openloop)).thenReturn(modeOpen) + `when`(rh.gs(info.nightscout.core.ui.R.string.lowglucosesuspend)).thenReturn(modeLgs) + `when`(rh.gs(info.nightscout.core.ui.R.string.unknown)).thenReturn(modeUnknown) `when`(rh.gs(R.string.smscommunicator_set_closed_loop_reply_with_code)).thenReturn("In order to switch Loop mode to Closed loop reply with code %1\$s") `when`(rh.gs(R.string.smscommunicator_current_loop_mode)).thenReturn("Current loop mode: %1\$s") `when`(rh.gs(R.string.smscommunicator_set_lgs_reply_with_code)).thenReturn("In order to switch Loop mode to LGS (Low Glucose Suspend) reply with code %1\$s") @@ -344,7 +344,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { //LOOP STATUS : enabled - APS mode - Closed `when`(loop.enabled).thenReturn(true) `when`(loop.isSuspended).thenReturn(false) - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) smsCommunicatorPlugin.messages = ArrayList() sms = Sms("1234", "LOOP STATUS") smsCommunicatorPlugin.processSms(sms) @@ -353,7 +353,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assert.assertEquals("Loop is enabled - $modeClosed", smsCommunicatorPlugin.messages[1].text) //LOOP STATUS : enabled - APS mode - Open - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.OPEN.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.OPEN.lowercase) smsCommunicatorPlugin.messages = ArrayList() smsCommunicatorPlugin.processSms(sms) Assert.assertFalse(sms.ignored) @@ -361,7 +361,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assert.assertEquals("Loop is enabled - $modeOpen", smsCommunicatorPlugin.messages[1].text) //LOOP STATUS : enabled - APS mode - LGS - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.LGS.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.LGS.lowercase) smsCommunicatorPlugin.messages = ArrayList() smsCommunicatorPlugin.processSms(sms) Assert.assertFalse(sms.ignored) @@ -369,7 +369,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assert.assertEquals("Loop is enabled - $modeLgs", smsCommunicatorPlugin.messages[1].text) //LOOP STATUS : enabled - APS mode - unknown - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn("some wrong value") + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn("some wrong value") smsCommunicatorPlugin.messages = ArrayList() smsCommunicatorPlugin.processSms(sms) Assert.assertFalse(sms.ignored) @@ -521,7 +521,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { var smsCommand = "LOOP CLOSED" val replyClosed = "In order to switch Loop mode to Closed loop reply with code " `when`(loop.enabled).thenReturn(true) - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) smsCommunicatorPlugin.messages = ArrayList() sms = Sms("1234", smsCommand) smsCommunicatorPlugin.processSms(sms) @@ -536,7 +536,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { //LOOP LGS smsCommand = "LOOP LGS" val replyLgs = "In order to switch Loop mode to LGS (Low Glucose Suspend) reply with code " - `when`(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.LGS.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.LGS.lowercase) smsCommunicatorPlugin.messages = ArrayList() sms = Sms("1234", smsCommand) smsCommunicatorPlugin.processSms(sms) From 5ea430ffc5ef4c83617ddf959e15701c77d5eba0 Mon Sep 17 00:00:00 2001 From: piotrek Date: Fri, 9 Dec 2022 19:03:18 +0100 Subject: [PATCH 09/13] ApsMode enum - discard usage of lowercase property --- .../interfaces/ConstraintsCheckerTest.kt | 4 ++-- .../plugins/aps/loop/LoopPluginTest.kt | 2 +- .../plugins/safety/SafetyPluginTest.kt | 6 +++--- .../java/info/nightscout/interfaces/ApsMode.kt | 10 +++++----- .../nightscout/plugins/aps/loop/LoopPlugin.kt | 2 +- .../objectives/objectives/Objective6.kt | 2 +- .../plugins/constraints/safety/SafetyPlugin.kt | 4 ++-- .../smsCommunicator/SmsCommunicatorPlugin.kt | 6 +++--- .../SmsCommunicatorPluginTest.kt | 12 ++++++------ .../info/nightscout/ui/dialogs/LoopDialog.kt | 18 +++++++++--------- 10 files changed, 33 insertions(+), 33 deletions(-) diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt index a5e9777c09..206ddafa79 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt @@ -430,7 +430,7 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { @Test fun iobAMAShouldBeLimited() { // No limit by default - `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) `when`(sp.getDouble(info.nightscout.plugins.aps.R.string.key_openapsma_max_iob, 1.5)).thenReturn(1.5) `when`(sp.getString(info.nightscout.core.utils.R.string.key_age, "")).thenReturn("teenage") openAPSAMAPlugin.setPluginEnabled(PluginType.APS, true) @@ -446,7 +446,7 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { @Test fun iobSMBShouldBeLimited() { // No limit by default - `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) `when`(sp.getDouble(info.nightscout.plugins.aps.R.string.key_openapssmb_max_iob, 3.0)).thenReturn(3.0) `when`(sp.getString(info.nightscout.core.utils.R.string.key_age, "")).thenReturn("teenage") openAPSSMBPlugin.setPluginEnabled(PluginType.APS, true) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt index 2aa4fe5aec..fefb05d9b3 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/aps/loop/LoopPluginTest.kt @@ -71,7 +71,7 @@ class LoopPluginTest : TestBase() { fun testPluginInterface() { `when`(rh.gs(info.nightscout.core.ui.R.string.loop)).thenReturn("Loop") `when`(rh.gs(info.nightscout.plugins.aps.R.string.loop_shortname)).thenReturn("LOOP") - `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) val pumpDescription = PumpDescription() `when`(virtualPumpPlugin.pumpDescription).thenReturn(pumpDescription) Assert.assertEquals(LoopFragment::class.java.name, loopPlugin.pluginDescription.fragmentClass) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/safety/SafetyPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/safety/SafetyPluginTest.kt index 800ff1b8ca..852eb5a0b2 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/safety/SafetyPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/safety/SafetyPluginTest.kt @@ -98,7 +98,7 @@ class SafetyPluginTest : TestBaseWithProfile() { @Test fun disabledEngineeringModeShouldLimitClosedLoop() { - `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) `when`(config.isEngineeringModeOrRelease()).thenReturn(false) var c = Constraint(true) c = safetyPlugin.isClosedLoopAllowed(c) @@ -108,7 +108,7 @@ class SafetyPluginTest : TestBaseWithProfile() { @Test fun setOpenLoopInPreferencesShouldLimitClosedLoop() { - `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.OPEN.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.OPEN.name) var c = Constraint(true) c = safetyPlugin.isClosedLoopAllowed(c) Assertions.assertTrue(c.getReasons(aapsLogger).contains("Closed loop mode disabled in preferences")) @@ -278,7 +278,7 @@ Safety: Limiting max basal rate to 500.00 U/h because of pump limit openAPSSMBPlugin.setPluginEnabled(PluginType.APS, true) //`when`(openAPSSMBPlugin.isEnabled()).thenReturn(true) //`when`(openAPSAMAPlugin.isEnabled()).thenReturn(false) - `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.LGS.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.LGS.name) `when`(sp.getDouble(info.nightscout.plugins.aps.R.string.key_openapsma_max_iob, 1.5)).thenReturn(1.5) `when`(sp.getDouble(info.nightscout.plugins.aps.R.string.key_openapssmb_max_iob, 3.0)).thenReturn(3.0) `when`(sp.getString(info.nightscout.core.utils.R.string.key_age, "")).thenReturn("teenage") diff --git a/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt index ed8dbe818a..11634c8356 100644 --- a/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt +++ b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt @@ -1,10 +1,10 @@ package info.nightscout.interfaces -enum class ApsMode(val lowercase: String) { - OPEN("open"), - CLOSED("closed"), - LGS("lgs"), - UNDEFINED("undefined"); +enum class ApsMode() { + OPEN, + CLOSED, + LGS, + UNDEFINED; companion object { diff --git a/plugins/aps/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt b/plugins/aps/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt index 6eb7d2bfce..91930bfbe7 100644 --- a/plugins/aps/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt +++ b/plugins/aps/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt @@ -183,7 +183,7 @@ class LoopPlugin @Inject constructor( get() { val closedLoopEnabled = constraintChecker.isClosedLoopAllowed() val maxIobAllowed = constraintChecker.getMaxIOBAllowed().value() - val apsMode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)) + val apsMode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) val pump = activePlugin.activePump var isLGS = false if (!isSuspended && !pump.isSuspended()) if (closedLoopEnabled.value()) if (maxIobAllowed == HardLimits.MAX_IOB_LGS || apsMode == ApsMode.LGS) isLGS = true diff --git a/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt b/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt index 2cac3333d9..6e170a93aa 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt @@ -16,7 +16,7 @@ class Objective6(injector: HasAndroidInjector) : Objective(injector, "maxiob", R tasks.add(MinimumDurationTask(this, T.days(1).msecs())) tasks.add( object : Task(this, R.string.closedmodeenabled) { - override fun isCompleted(): Boolean = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)) == ApsMode.CLOSED + override fun isCompleted(): Boolean = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) == ApsMode.CLOSED }) tasks.add( object : Task(this, R.string.maxiobset) { diff --git a/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt index 930fe307c7..7d5d20cb0c 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt @@ -67,7 +67,7 @@ class SafetyPlugin @Inject constructor( } override fun isClosedLoopAllowed(value: Constraint): Constraint { - val mode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)) + val mode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) if (mode == ApsMode.OPEN) value.set(aapsLogger, false, rh.gs(R.string.closedmodedisabledinpreferences), this) if (!config.isEngineeringModeOrRelease()) { if (value.value()) { @@ -160,7 +160,7 @@ class SafetyPlugin @Inject constructor( } override fun applyMaxIOBConstraints(maxIob: Constraint): Constraint { - val apsMode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)) + val apsMode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) if (apsMode == ApsMode.LGS) maxIob.setIfSmaller(aapsLogger, HardLimits.MAX_IOB_LGS, rh.gs(info.nightscout.core.ui.R.string.limiting_iob, HardLimits.MAX_IOB_LGS, rh.gs(info.nightscout.core.ui.R.string.lowglucosesuspend)), this) return maxIob } diff --git a/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt index 92b5e61a2d..f4a408362f 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -510,7 +510,7 @@ class SmsCommunicatorPlugin @Inject constructor( messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction(pumpCommand = false) { override fun run() { uel.log(Action.LGS_LOOP_MODE, Sources.SMS) - sp.putString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.LGS.lowercase) + sp.putString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.LGS.name) rxBus.send(EventPreferenceChange(rh.gs(info.nightscout.core.ui.R.string.lowglucosesuspend))) val replyText = rh.gs(R.string.smscommunicator_current_loop_mode, getApsModeText()) sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) @@ -525,7 +525,7 @@ class SmsCommunicatorPlugin @Inject constructor( messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction(pumpCommand = false) { override fun run() { uel.log(Action.CLOSED_LOOP_MODE, Sources.SMS) - sp.putString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.CLOSED.lowercase) + sp.putString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.CLOSED.name) rxBus.send(EventPreferenceChange(rh.gs(info.nightscout.core.ui.R.string.closedloop))) val replyText = rh.gs(R.string.smscommunicator_current_loop_mode, getApsModeText()) sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) @@ -1284,7 +1284,7 @@ class SmsCommunicatorPlugin @Inject constructor( } private fun getApsModeText(): String = - when (ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase))) { + when (ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name))) { ApsMode.OPEN -> rh.gs(info.nightscout.core.ui.R.string.openloop) ApsMode.CLOSED -> rh.gs(info.nightscout.core.ui.R.string.closedloop) ApsMode.LGS -> rh.gs(info.nightscout.core.ui.R.string.lowglucosesuspend) diff --git a/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt b/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt index 43491557c0..dd44bc7b56 100644 --- a/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt +++ b/plugins/main/src/test/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt @@ -343,7 +343,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { //LOOP STATUS : enabled - APS mode - Closed `when`(loop.enabled).thenReturn(true) `when`(loop.isSuspended).thenReturn(false) - `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) smsCommunicatorPlugin.messages = ArrayList() sms = Sms("1234", "LOOP STATUS") smsCommunicatorPlugin.processSms(sms) @@ -352,7 +352,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assertions.assertEquals("Loop is enabled - $modeClosed", smsCommunicatorPlugin.messages[1].text) //LOOP STATUS : enabled - APS mode - Open - `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.OPEN.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.OPEN.name) smsCommunicatorPlugin.messages = ArrayList() smsCommunicatorPlugin.processSms(sms) Assertions.assertFalse(sms.ignored) @@ -360,7 +360,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assertions.assertEquals("Loop is enabled - $modeOpen", smsCommunicatorPlugin.messages[1].text) //LOOP STATUS : enabled - APS mode - LGS - `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.LGS.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.LGS.name) smsCommunicatorPlugin.messages = ArrayList() smsCommunicatorPlugin.processSms(sms) Assertions.assertFalse(sms.ignored) @@ -368,7 +368,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assertions.assertEquals("Loop is enabled - $modeLgs", smsCommunicatorPlugin.messages[1].text) //LOOP STATUS : enabled - APS mode - unknown - `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn("some wrong value") + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn("some wrong value") smsCommunicatorPlugin.messages = ArrayList() smsCommunicatorPlugin.processSms(sms) Assertions.assertFalse(sms.ignored) @@ -520,7 +520,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { var smsCommand = "LOOP CLOSED" val replyClosed = "In order to switch Loop mode to Closed loop reply with code " `when`(loop.enabled).thenReturn(true) - `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.CLOSED.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) smsCommunicatorPlugin.messages = ArrayList() sms = Sms("1234", smsCommand) smsCommunicatorPlugin.processSms(sms) @@ -535,7 +535,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { //LOOP LGS smsCommand = "LOOP LGS" val replyLgs = "In order to switch Loop mode to LGS (Low Glucose Suspend) reply with code " - `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)).thenReturn(ApsMode.LGS.lowercase) + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.LGS.name) smsCommunicatorPlugin.messages = ArrayList() sms = Sms("1234", smsCommand) smsCommunicatorPlugin.processSms(sms) diff --git a/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt b/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt index 99b865dbdb..b1a30052ac 100644 --- a/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt +++ b/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt @@ -162,7 +162,7 @@ class LoopDialog : DaggerDialogFragment() { val closedLoopAllowed = constraintChecker.isClosedLoopAllowed(Constraint(true)) val closedLoopAllowed2 = activePlugin.activeObjectives?.isAccomplished(Objectives.MAXIOB_OBJECTIVE) ?: false val lgsEnabled = constraintChecker.isLgsAllowed(Constraint(true)) - val apsMode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase)) + val apsMode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) val pump = activePlugin.activePump binding.overviewDisconnect15m.visibility = pumpDescription.tempDurationStep15mAllowed.toVisibility() @@ -211,27 +211,27 @@ class LoopDialog : DaggerDialogFragment() { else -> { binding.overviewLoop.visibility = View.VISIBLE binding.overviewEnable.visibility = View.GONE - when { - apsMode == ApsMode.CLOSED -> { + when (apsMode) { + ApsMode.CLOSED -> { binding.overviewCloseloop.visibility = View.GONE binding.overviewLgsloop.visibility = View.VISIBLE binding.overviewOpenloop.visibility = View.VISIBLE } - apsMode == ApsMode.LGS -> { + ApsMode.LGS -> { binding.overviewCloseloop.visibility = closedLoopAllowed.value().toVisibility() //show Close loop button only if Close loop allowed binding.overviewLgsloop.visibility = View.GONE binding.overviewOpenloop.visibility = View.VISIBLE } - apsMode == ApsMode.OPEN -> { + ApsMode.OPEN -> { binding.overviewCloseloop.visibility = closedLoopAllowed2.toVisibility() //show CloseLoop button only if Objective 6 is completed (closedLoopAllowed always false in open loop mode) binding.overviewLgsloop.visibility = lgsEnabled.value().toVisibility() binding.overviewOpenloop.visibility = View.GONE } - else -> { + else -> { binding.overviewCloseloop.visibility = View.GONE binding.overviewLgsloop.visibility = View.GONE binding.overviewOpenloop.visibility = View.GONE @@ -283,21 +283,21 @@ class LoopDialog : DaggerDialogFragment() { when (v.id) { R.id.overview_closeloop -> { uel.log(UserEntry.Action.CLOSED_LOOP_MODE, UserEntry.Sources.LoopDialog) - sp.putString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.CLOSED.lowercase) + sp.putString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.CLOSED.name) rxBus.send(EventPreferenceChange(rh.gs(info.nightscout.core.ui.R.string.closedloop))) return true } R.id.overview_lgsloop -> { uel.log(UserEntry.Action.LGS_LOOP_MODE, UserEntry.Sources.LoopDialog) - sp.putString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.LGS.lowercase) + sp.putString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.LGS.name) rxBus.send(EventPreferenceChange(rh.gs(info.nightscout.core.ui.R.string.lowglucosesuspend))) return true } R.id.overview_openloop -> { uel.log(UserEntry.Action.OPEN_LOOP_MODE, UserEntry.Sources.LoopDialog) - sp.putString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.lowercase) + sp.putString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name) rxBus.send(EventPreferenceChange(rh.gs(info.nightscout.core.ui.R.string.lowglucosesuspend))) return true } From 4171bc61b611fd8da8a3d7afb3b06f7adcbcb9e4 Mon Sep 17 00:00:00 2001 From: piotrek Date: Fri, 9 Dec 2022 19:36:32 +0100 Subject: [PATCH 10/13] ApsMode enum - fix failing junit tests --- .../androidaps/interfaces/ConstraintsCheckerTest.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt index c743c3c716..24b8449fcc 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt @@ -275,13 +275,13 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { // 2x Safety & Objectives @Test fun isClosedLoopAllowedTest() { - `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, "open")).thenReturn("closed") + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.CLOSED.name) objectivesPlugin.objectives[Objectives.MAXIOB_ZERO_CL_OBJECTIVE].startedOn = 0 var c: Constraint = constraintChecker.isClosedLoopAllowed() aapsLogger.debug("Reason list: " + c.reasonList.toString()) // Assertions.assertTrue(c.reasonList[0].toString().contains("Closed loop is disabled")) // Safety & Objectives Assertions.assertEquals(false, c.value()) - `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, "open")).thenReturn("open") + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.OPEN.name) c = constraintChecker.isClosedLoopAllowed() Assertions.assertTrue(c.reasonList[0].contains("Closed loop mode disabled in preferences")) // Safety & Objectives // Assertions.assertEquals(3, c.reasonList.size) // 2x Safety & Objectives @@ -324,7 +324,7 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { openAPSSMBPlugin.setPluginEnabled(PluginType.APS, true) objectivesPlugin.objectives[Objectives.SMB_OBJECTIVE].startedOn = 0 `when`(sp.getBoolean(info.nightscout.plugins.aps.R.string.key_use_smb, false)).thenReturn(false) - `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, "open")).thenReturn("open") + `when`(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)).thenReturn(ApsMode.OPEN.name) // `when`(constraintChecker.isClosedLoopAllowed()).thenReturn(Constraint(true)) val c = constraintChecker.isSMBModeEnabled() Assertions.assertEquals(true, c.reasonList.size == 3) // 2x Safety & Objectives From 6205c9b893265693a61809a679234c93e9cf09be Mon Sep 17 00:00:00 2001 From: piotrek Date: Sun, 11 Dec 2022 23:07:37 +0100 Subject: [PATCH 11/13] ApsMode enum - refactor of secureValueOf --- .../src/main/java/info/nightscout/interfaces/ApsMode.kt | 8 +------- .../java/info/nightscout/plugins/aps/loop/LoopPlugin.kt | 2 +- .../constraints/objectives/objectives/Objective6.kt | 2 +- .../nightscout/plugins/constraints/safety/SafetyPlugin.kt | 4 ++-- .../general/smsCommunicator/SmsCommunicatorPlugin.kt | 2 +- ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt | 2 +- 6 files changed, 7 insertions(+), 13 deletions(-) diff --git a/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt index 11634c8356..c6f8106b6f 100644 --- a/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt +++ b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt @@ -8,12 +8,6 @@ enum class ApsMode() { companion object { - fun secureValueOf(stringValue: String): ApsMode { - return try { - valueOf(stringValue.uppercase()) - } catch (e: IllegalArgumentException) { - UNDEFINED - } - } + fun fromString(stringValue: String?) = values().firstOrNull { it.name == (stringValue?.uppercase() ?: UNDEFINED) } } } diff --git a/plugins/aps/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt b/plugins/aps/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt index 91930bfbe7..4a1512e9f9 100644 --- a/plugins/aps/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt +++ b/plugins/aps/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt @@ -183,7 +183,7 @@ class LoopPlugin @Inject constructor( get() { val closedLoopEnabled = constraintChecker.isClosedLoopAllowed() val maxIobAllowed = constraintChecker.getMaxIOBAllowed().value() - val apsMode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) + val apsMode = ApsMode.fromString(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) val pump = activePlugin.activePump var isLGS = false if (!isSuspended && !pump.isSuspended()) if (closedLoopEnabled.value()) if (maxIobAllowed == HardLimits.MAX_IOB_LGS || apsMode == ApsMode.LGS) isLGS = true diff --git a/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt b/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt index e70f85ddb4..a136d40160 100644 --- a/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt +++ b/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt @@ -16,7 +16,7 @@ class Objective6(injector: HasAndroidInjector) : Objective(injector, "maxiob", R tasks.add(MinimumDurationTask(this, T.days(1).msecs())) tasks.add( object : Task(this, R.string.closedmodeenabled) { - override fun isCompleted(): Boolean = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) == ApsMode.CLOSED + override fun isCompleted(): Boolean = ApsMode.fromString(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) == ApsMode.CLOSED }) tasks.add( object : Task(this, R.string.maxiobset) { diff --git a/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt b/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt index 2e543ef4c5..acc1163af0 100644 --- a/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt +++ b/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt @@ -68,7 +68,7 @@ class SafetyPlugin @Inject constructor( } override fun isClosedLoopAllowed(value: Constraint): Constraint { - val mode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) + val mode = ApsMode.fromString(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) if (mode == ApsMode.OPEN) value.set(aapsLogger, false, rh.gs(R.string.closedmodedisabledinpreferences), this) if (!config.isEngineeringModeOrRelease()) { if (value.value()) { @@ -160,7 +160,7 @@ class SafetyPlugin @Inject constructor( } override fun applyMaxIOBConstraints(maxIob: Constraint): Constraint { - val apsMode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) + val apsMode = ApsMode.fromString(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) if (apsMode == ApsMode.LGS) maxIob.setIfSmaller(aapsLogger, HardLimits.MAX_IOB_LGS, rh.gs(info.nightscout.core.ui.R.string.limiting_iob, HardLimits.MAX_IOB_LGS, rh.gs(info.nightscout.core.ui.R.string.lowglucosesuspend)), this) return maxIob } diff --git a/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt index f4a408362f..33e96f559b 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -1284,7 +1284,7 @@ class SmsCommunicatorPlugin @Inject constructor( } private fun getApsModeText(): String = - when (ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name))) { + when (ApsMode.fromString(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name))) { ApsMode.OPEN -> rh.gs(info.nightscout.core.ui.R.string.openloop) ApsMode.CLOSED -> rh.gs(info.nightscout.core.ui.R.string.closedloop) ApsMode.LGS -> rh.gs(info.nightscout.core.ui.R.string.lowglucosesuspend) diff --git a/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt b/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt index b1a30052ac..e668f6bee8 100644 --- a/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt +++ b/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt @@ -162,7 +162,7 @@ class LoopDialog : DaggerDialogFragment() { val closedLoopAllowed = constraintChecker.isClosedLoopAllowed(Constraint(true)) val closedLoopAllowed2 = activePlugin.activeObjectives?.isAccomplished(Objectives.MAXIOB_OBJECTIVE) ?: false val lgsEnabled = constraintChecker.isLgsAllowed(Constraint(true)) - val apsMode = ApsMode.secureValueOf(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) + val apsMode = ApsMode.fromString(sp.getString(info.nightscout.core.utils.R.string.key_aps_mode, ApsMode.OPEN.name)) val pump = activePlugin.activePump binding.overviewDisconnect15m.visibility = pumpDescription.tempDurationStep15mAllowed.toVisibility() From e8a5dc2627abbf8401e9dab649f519bff9132b32 Mon Sep 17 00:00:00 2001 From: piotrek Date: Mon, 12 Dec 2022 00:01:10 +0100 Subject: [PATCH 12/13] ApsMode enum - refactor of fromString --- .../src/main/java/info/nightscout/interfaces/ApsMode.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt index c6f8106b6f..c6d4b1fbd8 100644 --- a/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt +++ b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt @@ -8,6 +8,6 @@ enum class ApsMode() { companion object { - fun fromString(stringValue: String?) = values().firstOrNull { it.name == (stringValue?.uppercase() ?: UNDEFINED) } + fun fromString(stringValue: String?) = values().firstOrNull { it.name == (stringValue?.uppercase() ?: UNDEFINED.name) } ?: UNDEFINED } } From 97530865ef4a11648f4645ca8e5b089f0a1ad1b8 Mon Sep 17 00:00:00 2001 From: piotrek Date: Wed, 14 Dec 2022 10:23:50 +0100 Subject: [PATCH 13/13] ApsMode enum - refactor of fromString --- .../src/main/java/info/nightscout/interfaces/ApsMode.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt index c6d4b1fbd8..2af064a34c 100644 --- a/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt +++ b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt @@ -8,6 +8,6 @@ enum class ApsMode() { companion object { - fun fromString(stringValue: String?) = values().firstOrNull { it.name == (stringValue?.uppercase() ?: UNDEFINED.name) } ?: UNDEFINED + fun fromString(stringValue: String?) = values().firstOrNull { it.name == stringValue?.uppercase() } ?: UNDEFINED } }