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.
This commit is contained in:
piotrek 2022-11-29 22:10:06 +01:00
parent df63495795
commit 3f67e69023
11 changed files with 87 additions and 27 deletions

View file

@ -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<Boolean> = 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)

View file

@ -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)

View file

@ -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")

View file

@ -0,0 +1,7 @@
package info.nightscout.interfaces
enum class ApsMode {
OPEN,
CLOSED,
LGS;
}

View file

@ -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"

View file

@ -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) {

View file

@ -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<Boolean>): Constraint<Boolean> {
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<Double>): Constraint<Double> {
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
}

View file

@ -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
}
}
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)
}
}

View file

@ -63,6 +63,9 @@
<string name="smscommunicator_message_body">Błędny tekst wiadomości</string>
<string name="smscommunicator_report_pump_unreachable_summary">Wyślij SMS, jeśli wyzwolone jest zdarzenie pompy nieosiągalnej</string>
<string name="smscommunicator_pump_unreachable">Zgłoś nieosiągalną pompę</string>
<string name="smscommunicator_set_lgs_reply_with_code">Aby przełączyć pętlę w tryb LGS (zawieszenie przy niskiej glikemii) wprowadź kod %1$s</string>
<string name="smscommunicator_set_closed_loop_reply_with_code">Aby przełączyć pętlę w tryb pętli zamkniętej wprowadź kod %1$s</string>
<string name="smscommunicator_current_loop_mode">Obecny tryb pętli: %1$s</string>
<string name="wrong_format">Błędny format</string>
<string name="sms_actual_bg">BG:</string>
<string name="sms_last_bg">Ostatnia BG:</string>

View file

@ -95,6 +95,9 @@
<string name="smscommunicator_message_body">Invalid message body</string>
<string name="smscommunicator_report_pump_unreachable_summary">Send SMS if unreachable pump event is triggered</string>
<string name="smscommunicator_pump_unreachable">Report pump unreachable</string>
<string name="smscommunicator_set_lgs_reply_with_code">In order to switch Loop mode to LGS (Low Glucose Suspend) reply with code %1$s</string>
<string name="smscommunicator_set_closed_loop_reply_with_code">In order to switch Loop mode to Closed loop reply with code %1$s</string>
<string name="smscommunicator_current_loop_mode">Current loop mode: %1$s</string>
<string name="wrong_format">Wrong format</string>
<string name="sms_actual_bg">BG:</string>
<string name="sms_last_bg">Last BG:</string>

View file

@ -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 = {