SMSCommunicator LOOP command auth

This commit is contained in:
Milos Kozak 2020-05-24 23:40:51 +02:00
parent c1c87b2e30
commit 8a731a9187
3 changed files with 55 additions and 16 deletions

View file

@ -308,13 +308,20 @@ class SmsCommunicatorPlugin @Inject constructor(
when (splitted[1].toUpperCase(Locale.getDefault())) {
"DISABLE", "STOP" -> {
if (loopPlugin.isEnabled(PluginType.LOOP)) {
loopPlugin.setPluginEnabled(PluginType.LOOP, false)
commandQueue.cancelTempBasal(true, object : Callback() {
val passCode = generatePasscode()
val reply = String.format(resourceHelper.gs(R.string.smscommunicator_loopdisablereplywithcode), passCode)
receivedSms.processed = true
messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction() {
override fun run() {
rxBus.send(EventRefreshOverview("SMS_LOOP_STOP"))
val replyText = resourceHelper.gs(R.string.smscommunicator_loophasbeendisabled) + " " +
resourceHelper.gs(if (result.success) R.string.smscommunicator_tempbasalcanceled else R.string.smscommunicator_tempbasalcancelfailed)
sendSMS(Sms(receivedSms.phoneNumber, replyText))
loopPlugin.setPluginEnabled(PluginType.LOOP, false)
commandQueue.cancelTempBasal(true, object : Callback() {
override fun run() {
rxBus.send(EventRefreshOverview("SMS_LOOP_STOP"))
val replyText = resourceHelper.gs(R.string.smscommunicator_loophasbeendisabled) + " " +
resourceHelper.gs(if (result.success) R.string.smscommunicator_tempbasalcanceled else R.string.smscommunicator_tempbasalcancelfailed)
sendSMS(Sms(receivedSms.phoneNumber, replyText))
}
})
}
})
} else
@ -324,9 +331,16 @@ class SmsCommunicatorPlugin @Inject constructor(
"ENABLE", "START" -> {
if (!loopPlugin.isEnabled(PluginType.LOOP)) {
loopPlugin.setPluginEnabled(PluginType.LOOP, true)
sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_loophasbeenenabled)))
rxBus.send(EventRefreshOverview("SMS_LOOP_START"))
val passCode = generatePasscode()
val reply = String.format(resourceHelper.gs(R.string.smscommunicator_loopenablereplywithcode), passCode)
receivedSms.processed = true
messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction() {
override fun run() {
loopPlugin.setPluginEnabled(PluginType.LOOP, true)
sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_loophasbeenenabled)))
rxBus.send(EventRefreshOverview("SMS_LOOP_START"))
}
})
} else
sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_loopisenabled)))
receivedSms.processed = true
@ -343,9 +357,16 @@ class SmsCommunicatorPlugin @Inject constructor(
}
"RESUME" -> {
rxBus.send(EventRefreshOverview("SMS_LOOP_RESUME"))
loopPlugin.createOfflineEvent(0)
sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_loopresumed)))
val passCode = generatePasscode()
val reply = String.format(resourceHelper.gs(R.string.smscommunicator_loopresumereplywithcode), passCode)
receivedSms.processed = true
messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction() {
override fun run() {
rxBus.send(EventRefreshOverview("SMS_LOOP_RESUME"))
loopPlugin.createOfflineEvent(0)
sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_loopresumed)))
}
})
}
"SUSPEND" -> {

View file

@ -308,6 +308,9 @@
<string name="smscommunicator_carbsreplywithcode">To enter %1$dg at %2$s reply with code %3$s</string>
<string name="smscommunicator_basalpctreplywithcode">To start basal %1$d%% for %2$d min reply with code %3$s</string>
<string name="smscommunicator_suspendreplywithcode">To suspend loop for %1$d minutes reply with code %2$s</string>
<string name="smscommunicator_loopresumereplywithcode">To resume loop reply with code %1$s</string>
<string name="smscommunicator_loopenablereplywithcode">To enable loop reply with code %1$s</string>
<string name="smscommunicator_loopdisablereplywithcode">To disable loop reply with code %1$s</string>
<string name="smscommunicator_tempbasalset">Temp basal %1$.2fU/h for %2$d min started successfully</string>
<string name="smscommunicator_extendedset">Extended bolus %1$.2fU for %2$d min started successfully</string>
<string name="smscommunicator_carbsset">Carbs %1$dg entered successfully</string>

View file

@ -223,6 +223,9 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
`when`(resourceHelper.gs(R.string.smscommunicator_mealbolusdelivered_tt)).thenReturn("Target %1\$s for %2\$d minutes")
`when`(resourceHelper.gs(R.string.sms_actualbg)).thenReturn("BG:")
`when`(resourceHelper.gs(R.string.sms_lastbg)).thenReturn("Last BG:")
`when`(resourceHelper.gs(R.string.smscommunicator_loopdisablereplywithcode)).thenReturn("To disable loop reply with code %1\$s")
`when`(resourceHelper.gs(R.string.smscommunicator_loopenablereplywithcode)).thenReturn("To enable loop reply with code %1\$s")
`when`(resourceHelper.gs(R.string.smscommunicator_loopresumereplywithcode)).thenReturn("To resume loop reply with code %1\$s")
}
@Test
@ -344,7 +347,11 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
smsCommunicatorPlugin.processSms(sms)
Assert.assertFalse(sms.ignored)
Assert.assertEquals("LOOP DISABLE", smsCommunicatorPlugin.messages[0].text)
Assert.assertEquals("Loop has been disabled Temp basal canceled", smsCommunicatorPlugin.messages[1].text)
Assert.assertTrue(smsCommunicatorPlugin.messages[1].text.contains("To disable loop reply with code "))
var passCode: String = smsCommunicatorPlugin.messageToConfirm?.confirmCode!!
smsCommunicatorPlugin.processSms(Sms("1234", passCode))
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text)
Assert.assertEquals("Loop has been disabled Temp basal canceled", smsCommunicatorPlugin.messages[3].text)
Assert.assertTrue(hasBeenRun)
//LOOP ENABLE : already enabled
@ -368,7 +375,11 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
smsCommunicatorPlugin.processSms(sms)
Assert.assertFalse(sms.ignored)
Assert.assertEquals("LOOP ENABLE", smsCommunicatorPlugin.messages[0].text)
Assert.assertEquals("Loop has been enabled", smsCommunicatorPlugin.messages[1].text)
Assert.assertTrue(smsCommunicatorPlugin.messages[1].text.contains("To enable loop reply with code "))
passCode = smsCommunicatorPlugin.messageToConfirm?.confirmCode!!
smsCommunicatorPlugin.processSms(Sms("1234", passCode))
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text)
Assert.assertEquals("Loop has been enabled", smsCommunicatorPlugin.messages[3].text)
Assert.assertTrue(hasBeenRun)
//LOOP RESUME : already enabled
@ -377,7 +388,11 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
smsCommunicatorPlugin.processSms(sms)
Assert.assertFalse(sms.ignored)
Assert.assertEquals("LOOP RESUME", smsCommunicatorPlugin.messages[0].text)
Assert.assertEquals("Loop resumed", smsCommunicatorPlugin.messages[1].text)
Assert.assertTrue(smsCommunicatorPlugin.messages[1].text.contains("To resume loop reply with code "))
passCode = smsCommunicatorPlugin.messageToConfirm?.confirmCode!!
smsCommunicatorPlugin.processSms(Sms("1234", passCode))
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text)
Assert.assertEquals("Loop resumed", smsCommunicatorPlugin.messages[3].text)
//LOOP SUSPEND 1 2: wrong format
smsCommunicatorPlugin.messages = ArrayList()
@ -402,7 +417,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
Assert.assertFalse(sms.ignored)
Assert.assertEquals("LOOP SUSPEND 100", smsCommunicatorPlugin.messages[0].text)
Assert.assertTrue(smsCommunicatorPlugin.messages[1].text.contains("To suspend loop for 100 minutes reply with code "))
var passCode: String = smsCommunicatorPlugin.messageToConfirm?.confirmCode!!
passCode = smsCommunicatorPlugin.messageToConfirm?.confirmCode!!
smsCommunicatorPlugin.processSms(Sms("1234", passCode))
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text)
Assert.assertEquals("Loop suspended Temp basal canceled", smsCommunicatorPlugin.messages[3].text)