Validate durationStep from tbrSettings in Pumptype when setting TBR via SMS

Fixes nightscout/AndroidAPS#38
This commit is contained in:
Sam Spycher 2020-10-16 18:27:04 +02:00
parent cbdee2b81b
commit b355511f33
3 changed files with 31 additions and 9 deletions

View file

@ -31,6 +31,7 @@ import info.nightscout.androidaps.plugins.general.smsCommunicator.events.EventSm
import info.nightscout.androidaps.plugins.general.smsCommunicator.otp.OneTimePassword import info.nightscout.androidaps.plugins.general.smsCommunicator.otp.OneTimePassword
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin
import info.nightscout.androidaps.plugins.pump.common.defs.PumpType
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
import info.nightscout.androidaps.queue.Callback import info.nightscout.androidaps.queue.Callback
import info.nightscout.androidaps.utils.* import info.nightscout.androidaps.utils.*
@ -578,12 +579,13 @@ class SmsCommunicatorPlugin @Inject constructor(
}) })
} else if (splitted[1].endsWith("%")) { } else if (splitted[1].endsWith("%")) {
var tempBasalPct = SafeParse.stringToInt(StringUtils.removeEnd(splitted[1], "%")) var tempBasalPct = SafeParse.stringToInt(StringUtils.removeEnd(splitted[1], "%"))
var durationStep = activePlugin.activePump.model().tbrSettings.durationStep
var duration = 30 var duration = 30
if (splitted.size > 2) duration = SafeParse.stringToInt(splitted[2]) if (splitted.size > 2) duration = SafeParse.stringToInt(splitted[2])
val profile = profileFunction.getProfile() val profile = profileFunction.getProfile()
if (profile == null) sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.noprofile))) if (profile == null) sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.noprofile)))
else if (tempBasalPct == 0 && splitted[1] != "0%") sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) else if (tempBasalPct == 0 && splitted[1] != "0%") sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat)))
else if (duration == 0) sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) else if (duration <= 0 || duration % durationStep != 0) sendSMS(Sms(receivedSms.phoneNumber, String.format(resourceHelper.gs(R.string.wrongTbrDuration), durationStep)))
else { else {
tempBasalPct = constraintChecker.applyBasalPercentConstraints(Constraint(tempBasalPct), profile).value() tempBasalPct = constraintChecker.applyBasalPercentConstraints(Constraint(tempBasalPct), profile).value()
val passCode = generatePasscode() val passCode = generatePasscode()
@ -610,12 +612,13 @@ class SmsCommunicatorPlugin @Inject constructor(
} }
} else { } else {
var tempBasal = SafeParse.stringToDouble(splitted[1]) var tempBasal = SafeParse.stringToDouble(splitted[1])
var durationStep = activePlugin.activePump.model().tbrSettings.durationStep
var duration = 30 var duration = 30
if (splitted.size > 2) duration = SafeParse.stringToInt(splitted[2]) if (splitted.size > 2) duration = SafeParse.stringToInt(splitted[2])
val profile = profileFunction.getProfile() val profile = profileFunction.getProfile()
if (profile == null) sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.noprofile))) if (profile == null) sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.noprofile)))
else if (tempBasal == 0.0 && splitted[1] != "0") sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) else if (tempBasal == 0.0 && splitted[1] != "0") sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat)))
else if (duration == 0) sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) else if (duration <= 0 || duration % durationStep != 0) sendSMS(Sms(receivedSms.phoneNumber, String.format(resourceHelper.gs(R.string.wrongTbrDuration), durationStep)))
else { else {
tempBasal = constraintChecker.applyBasalConstraints(Constraint(tempBasal), profile).value() tempBasal = constraintChecker.applyBasalConstraints(Constraint(tempBasal), profile).value()
val passCode = generatePasscode() val passCode = generatePasscode()

View file

@ -1176,6 +1176,7 @@
<string name="storage">internal storage constraint</string> <string name="storage">internal storage constraint</string>
<string name="diskfull">Free at least %1$d MB from internal storage! Loop disabled!</string> <string name="diskfull">Free at least %1$d MB from internal storage! Loop disabled!</string>
<string name="wrongformat">Wrong format</string> <string name="wrongformat">Wrong format</string>
<string name="wrongTbrDuration">TBR duration must a multiple of %1$d minutes and greater than 0.</string>
<string name="sms_wrongcode">Wrong code. Command cancelled.</string> <string name="sms_wrongcode">Wrong code. Command cancelled.</string>
<string name="notconfigured">Not configured</string> <string name="notconfigured">Not configured</string>
<string name="profileswitchcreated">Profile switch created</string> <string name="profileswitchcreated">Profile switch created</string>

View file

@ -23,6 +23,7 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.CobInfo
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin
import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin
import info.nightscout.androidaps.plugins.pump.common.defs.PumpType
import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin
import info.nightscout.androidaps.plugins.treatments.TreatmentService import info.nightscout.androidaps.plugins.treatments.TreatmentService
import info.nightscout.androidaps.queue.Callback import info.nightscout.androidaps.queue.Callback
@ -160,6 +161,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
`when`(virtualPumpPlugin.shortStatus(ArgumentMatchers.anyBoolean())).thenReturn("Virtual Pump") `when`(virtualPumpPlugin.shortStatus(ArgumentMatchers.anyBoolean())).thenReturn("Virtual Pump")
`when`(virtualPumpPlugin.isSuspended).thenReturn(false) `when`(virtualPumpPlugin.isSuspended).thenReturn(false)
`when`(virtualPumpPlugin.pumpDescription).thenReturn(PumpDescription()) `when`(virtualPumpPlugin.pumpDescription).thenReturn(PumpDescription())
`when`(virtualPumpPlugin.model()).thenReturn(PumpType.GenericAAPS);
`when`(treatmentsPlugin.lastCalculationTreatments).thenReturn(IobTotal(0)) `when`(treatmentsPlugin.lastCalculationTreatments).thenReturn(IobTotal(0))
`when`(treatmentsPlugin.lastCalculationTempBasals).thenReturn(IobTotal(0)) `when`(treatmentsPlugin.lastCalculationTempBasals).thenReturn(IobTotal(0))
@ -186,6 +188,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
`when`(resourceHelper.gs(R.string.smscommunicator_loopisdisabled)).thenReturn("Loop is disabled") `when`(resourceHelper.gs(R.string.smscommunicator_loopisdisabled)).thenReturn("Loop is disabled")
`when`(resourceHelper.gs(R.string.smscommunicator_loopisenabled)).thenReturn("Loop is enabled") `when`(resourceHelper.gs(R.string.smscommunicator_loopisenabled)).thenReturn("Loop is enabled")
`when`(resourceHelper.gs(R.string.wrongformat)).thenReturn("Wrong format") `when`(resourceHelper.gs(R.string.wrongformat)).thenReturn("Wrong format")
`when`(resourceHelper.gs(R.string.wrongTbrDuration)).thenReturn("TBR duration must a multiple of %1\$d minutes and greater than 0.")
`when`(resourceHelper.gs(R.string.smscommunicator_loophasbeendisabled)).thenReturn("Loop has been disabled") `when`(resourceHelper.gs(R.string.smscommunicator_loophasbeendisabled)).thenReturn("Loop has been disabled")
`when`(resourceHelper.gs(R.string.smscommunicator_loophasbeenenabled)).thenReturn("Loop has been enabled") `when`(resourceHelper.gs(R.string.smscommunicator_loophasbeenenabled)).thenReturn("Loop has been enabled")
`when`(resourceHelper.gs(R.string.smscommunicator_tempbasalcanceled)).thenReturn("Temp basal canceled") `when`(resourceHelper.gs(R.string.smscommunicator_tempbasalcanceled)).thenReturn("Temp basal canceled")
@ -777,19 +780,26 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
sms = Sms("1234", "BASAL 10% 0") sms = Sms("1234", "BASAL 10% 0")
smsCommunicatorPlugin.processSms(sms) smsCommunicatorPlugin.processSms(sms)
Assert.assertEquals("BASAL 10% 0", smsCommunicatorPlugin.messages[0].text) Assert.assertEquals("BASAL 10% 0", smsCommunicatorPlugin.messages[0].text)
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages[1].text) Assert.assertEquals("TBR duration must a multiple of 30 minutes and greater than 0.", smsCommunicatorPlugin.messages[1].text)
`when`(constraintChecker.applyBasalPercentConstraints(anyObject(), anyObject())).thenReturn(Constraint<Int>(20))
//BASAL 20% 20 //BASAL 20% 20
smsCommunicatorPlugin.messages = ArrayList() smsCommunicatorPlugin.messages = ArrayList()
sms = Sms("1234", "BASAL 20% 20") sms = Sms("1234", "BASAL 20% 20")
smsCommunicatorPlugin.processSms(sms) smsCommunicatorPlugin.processSms(sms)
Assert.assertEquals("BASAL 20% 20", smsCommunicatorPlugin.messages[0].text) Assert.assertEquals("BASAL 20% 20", smsCommunicatorPlugin.messages[0].text)
Assert.assertTrue(smsCommunicatorPlugin.messages[1].text.contains("To start basal 20% for 20 min reply with code")) Assert.assertEquals("TBR duration must a multiple of 30 minutes and greater than 0.", smsCommunicatorPlugin.messages[1].text)
`when`(constraintChecker.applyBasalPercentConstraints(anyObject(), anyObject())).thenReturn(Constraint<Int>(20))
//BASAL 20% 30
smsCommunicatorPlugin.messages = ArrayList()
sms = Sms("1234", "BASAL 20% 30")
smsCommunicatorPlugin.processSms(sms)
Assert.assertEquals("BASAL 20% 30", smsCommunicatorPlugin.messages[0].text)
Assert.assertTrue(smsCommunicatorPlugin.messages[1].text.contains("To start basal 20% for 30 min reply with code"))
passCode = smsCommunicatorPlugin.messageToConfirm?.confirmCode!! passCode = smsCommunicatorPlugin.messageToConfirm?.confirmCode!!
smsCommunicatorPlugin.processSms(Sms("1234", passCode)) smsCommunicatorPlugin.processSms(Sms("1234", passCode))
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text) Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text)
Assert.assertEquals("Temp basal 20% for 20 min started successfully\nVirtual Pump", smsCommunicatorPlugin.messages[3].text) Assert.assertEquals("Temp basal 20% for 30 min started successfully\nVirtual Pump", smsCommunicatorPlugin.messages[3].text)
//BASAL a //BASAL a
smsCommunicatorPlugin.messages = ArrayList() smsCommunicatorPlugin.messages = ArrayList()
@ -803,7 +813,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
sms = Sms("1234", "BASAL 1 0") sms = Sms("1234", "BASAL 1 0")
smsCommunicatorPlugin.processSms(sms) smsCommunicatorPlugin.processSms(sms)
Assert.assertEquals("BASAL 1 0", smsCommunicatorPlugin.messages[0].text) Assert.assertEquals("BASAL 1 0", smsCommunicatorPlugin.messages[0].text)
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages[1].text) Assert.assertEquals("TBR duration must a multiple of 30 minutes and greater than 0.", smsCommunicatorPlugin.messages[1].text)
`when`(constraintChecker.applyBasalConstraints(anyObject(), anyObject())).thenReturn(Constraint<Double>(1.0)) `when`(constraintChecker.applyBasalConstraints(anyObject(), anyObject())).thenReturn(Constraint<Double>(1.0))
//BASAL 1 20 //BASAL 1 20
@ -811,11 +821,19 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
sms = Sms("1234", "BASAL 1 20") sms = Sms("1234", "BASAL 1 20")
smsCommunicatorPlugin.processSms(sms) smsCommunicatorPlugin.processSms(sms)
Assert.assertEquals("BASAL 1 20", smsCommunicatorPlugin.messages[0].text) Assert.assertEquals("BASAL 1 20", smsCommunicatorPlugin.messages[0].text)
Assert.assertTrue(smsCommunicatorPlugin.messages[1].text.contains("To start basal 1.00U/h for 20 min reply with code")) Assert.assertEquals("TBR duration must a multiple of 30 minutes and greater than 0.", smsCommunicatorPlugin.messages[1].text)
`when`(constraintChecker.applyBasalConstraints(anyObject(), anyObject())).thenReturn(Constraint<Double>(1.0))
//BASAL 1 30
smsCommunicatorPlugin.messages = ArrayList()
sms = Sms("1234", "BASAL 1 30")
smsCommunicatorPlugin.processSms(sms)
Assert.assertEquals("BASAL 1 30", smsCommunicatorPlugin.messages[0].text)
Assert.assertTrue(smsCommunicatorPlugin.messages[1].text.contains("To start basal 1.00U/h for 30 min reply with code"))
passCode = smsCommunicatorPlugin.messageToConfirm?.confirmCode!! passCode = smsCommunicatorPlugin.messageToConfirm?.confirmCode!!
smsCommunicatorPlugin.processSms(Sms("1234", passCode)) smsCommunicatorPlugin.processSms(Sms("1234", passCode))
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text) Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text)
Assert.assertEquals("Temp basal 1.00U/h for 20 min started successfully\nVirtual Pump", smsCommunicatorPlugin.messages[3].text) Assert.assertEquals("Temp basal 1.00U/h for 30 min started successfully\nVirtual Pump", smsCommunicatorPlugin.messages[3].text)
} }
@Test fun processExtendedTest() { @Test fun processExtendedTest() {