ApsMode Enum - add secureValueOf to avoid IllegalArgumentException

This commit is contained in:
piotrek 2022-11-30 12:02:51 +01:00
parent 0d132c1263
commit 2b3e965725
6 changed files with 22 additions and 10 deletions

View file

@ -3,5 +3,17 @@ package info.nightscout.interfaces
enum class ApsMode { enum class ApsMode {
OPEN, OPEN,
CLOSED, CLOSED,
LGS; LGS,
UNDEFINED;
companion object {
fun secureValueOf(stringValue: String): ApsMode {
return try {
valueOf(stringValue)
} catch (e: IllegalArgumentException) {
UNDEFINED
}
}
}
} }

View file

@ -183,7 +183,7 @@ class LoopPlugin @Inject constructor(
get() { get() {
val closedLoopEnabled = constraintChecker.isClosedLoopAllowed() val closedLoopEnabled = constraintChecker.isClosedLoopAllowed()
val maxIobAllowed = constraintChecker.getMaxIOBAllowed().value() 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 val pump = activePlugin.activePump
var isLGS = false var isLGS = false
if (!isSuspended && !pump.isSuspended()) if (closedLoopEnabled.value()) if (maxIobAllowed == HardLimits.MAX_IOB_LGS || apsMode == ApsMode.LGS) isLGS = true if (!isSuspended && !pump.isSuspended()) if (closedLoopEnabled.value()) if (maxIobAllowed == HardLimits.MAX_IOB_LGS || apsMode == ApsMode.LGS) isLGS = true

View file

@ -16,7 +16,7 @@ class Objective6(injector: HasAndroidInjector) : Objective(injector, "maxiob", R
tasks.add(MinimumDurationTask(this, T.days(1).msecs())) tasks.add(MinimumDurationTask(this, T.days(1).msecs()))
tasks.add( tasks.add(
object : Task(this, R.string.closedmodeenabled) { 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( tasks.add(
object : Task(this, R.string.maxiobset) { object : Task(this, R.string.maxiobset) {

View file

@ -67,7 +67,7 @@ class SafetyPlugin @Inject constructor(
} }
override fun isClosedLoopAllowed(value: Constraint<Boolean>): Constraint<Boolean> { override fun isClosedLoopAllowed(value: Constraint<Boolean>): Constraint<Boolean> {
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 (mode == ApsMode.OPEN) value.set(aapsLogger, false, rh.gs(R.string.closedmodedisabledinpreferences), this)
if (!config.isEngineeringModeOrRelease()) { if (!config.isEngineeringModeOrRelease()) {
if (value.value()) { if (value.value()) {
@ -160,7 +160,7 @@ class SafetyPlugin @Inject constructor(
} }
override fun applyMaxIOBConstraints(maxIob: Constraint<Double>): Constraint<Double> { override fun applyMaxIOBConstraints(maxIob: Constraint<Double>): Constraint<Double> {
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) 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 return maxIob
} }

View file

@ -1288,7 +1288,7 @@ class SmsCommunicatorPlugin @Inject constructor(
} }
private fun getApsModeText(): String = 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.OPEN -> rh.gs(R.string.openloop)
ApsMode.CLOSED -> rh.gs(R.string.closedloop) ApsMode.CLOSED -> rh.gs(R.string.closedloop)
ApsMode.LGS -> rh.gs(R.string.lowglucosesuspend) ApsMode.LGS -> rh.gs(R.string.lowglucosesuspend)

View file

@ -162,7 +162,7 @@ class LoopDialog : DaggerDialogFragment() {
val closedLoopAllowed = constraintChecker.isClosedLoopAllowed(Constraint(true)) val closedLoopAllowed = constraintChecker.isClosedLoopAllowed(Constraint(true))
val closedLoopAllowed2 = activePlugin.activeObjectives?.isAccomplished(Objectives.MAXIOB_OBJECTIVE) ?: false val closedLoopAllowed2 = activePlugin.activeObjectives?.isAccomplished(Objectives.MAXIOB_OBJECTIVE) ?: false
val lgsEnabled = constraintChecker.isLgsAllowed(Constraint(true)) 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 val pump = activePlugin.activePump
binding.overviewDisconnect15m.visibility = pumpDescription.tempDurationStep15mAllowed.toVisibility() binding.overviewDisconnect15m.visibility = pumpDescription.tempDurationStep15mAllowed.toVisibility()
@ -212,19 +212,19 @@ class LoopDialog : DaggerDialogFragment() {
binding.overviewLoop.visibility = View.VISIBLE binding.overviewLoop.visibility = View.VISIBLE
binding.overviewEnable.visibility = View.GONE binding.overviewEnable.visibility = View.GONE
when { when {
apsMode == "closed" -> { apsMode == ApsMode.CLOSED -> {
binding.overviewCloseloop.visibility = View.GONE binding.overviewCloseloop.visibility = View.GONE
binding.overviewLgsloop.visibility = View.VISIBLE binding.overviewLgsloop.visibility = View.VISIBLE
binding.overviewOpenloop.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.overviewCloseloop.visibility = closedLoopAllowed.value().toVisibility() //show Close loop button only if Close loop allowed
binding.overviewLgsloop.visibility = View.GONE binding.overviewLgsloop.visibility = View.GONE
binding.overviewOpenloop.visibility = View.VISIBLE binding.overviewOpenloop.visibility = View.VISIBLE
} }
apsMode == "open" -> { apsMode == ApsMode.OPEN -> {
binding.overviewCloseloop.visibility = binding.overviewCloseloop.visibility =
closedLoopAllowed2.toVisibility() //show CloseLoop button only if Objective 6 is completed (closedLoopAllowed always false in open loop mode) 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.overviewLgsloop.visibility = lgsEnabled.value().toVisibility()