From 9a1993c5f6991f546e2819be1aeabbeeb95718b8 Mon Sep 17 00:00:00 2001 From: Lexsus Date: Thu, 28 May 2020 17:33:51 +0300 Subject: [PATCH] corrections. add some test for sms commands --- .../smsCommunicator/SmsCommunicatorPlugin.kt | 47 +++++++------------ app/src/main/res/values/strings.xml | 8 ++-- .../SmsCommunicatorPluginTest.kt | 32 +++++++++++++ 3 files changed, 55 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt index 229f1209e3..8ba5c5fb35 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -227,9 +227,9 @@ class SmsCommunicatorPlugin @Inject constructor( "NSCLIENT" -> if (splitted.size == 2) processNSCLIENT(splitted, receivedSms) else sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) - "PUMP" ->//TODO remoteCommandsAllowed - if (!remoteCommandsAllowed) sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_remotecommandnotallowed))) - else if (splitted.size <=3) processPUMP(splitted,receivedSms) + "PUMP" -> + if (!remoteCommandsAllowed && splitted.size > 1) sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_remotecommandnotallowed))) + else if (splitted.size <= 3) processPUMP(splitted, receivedSms) else sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) "PROFILE" -> if (!remoteCommandsAllowed) sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_remotecommandnotallowed))) @@ -442,7 +442,7 @@ class SmsCommunicatorPlugin @Inject constructor( sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) } - private fun processPUMP(splitted: Array,receivedSms: Sms) { + private fun processPUMP(splitted: Array, receivedSms: Sms) { if (splitted.size == 1) { commandQueue.readStatus("SMS", object : Callback() { override fun run() { @@ -457,60 +457,49 @@ class SmsCommunicatorPlugin @Inject constructor( } }) receivedSms.processed = true - } - else if ((splitted.size == 2) && (splitted[1].equals("CONNECT", ignoreCase = true))) - { - + } else if ((splitted.size == 2) && (splitted[1].equals("CONNECT", ignoreCase = true))) { val passCode = generatePasscode() - val reply = String.format(resourceHelper.gs(R.string.smscommunicator_pumpconnect),passCode) + val reply = String.format(resourceHelper.gs(R.string.smscommunicator_pumpconnectwithcode), passCode) receivedSms.processed = true - messageToConfirm = AuthRequest(injector,receivedSms,reply,passCode,object : SmsAction(){ + messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction() { override fun run() { - commandQueue.cancelTempBasal(true, object : Callback() { override fun run() { if (!result.success) { sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_pumpconnectfail))) - } - else - { + } else { loopPlugin.suspendTo(0L) - sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.reconnect))) + sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_reconnect))) rxBus.send(EventRefreshOverview("SMS_PUMP_START")) - //rxBus.send(EventRefreshOverview("SMS_PUMP_CONNECT")) sp.putBoolean(R.string.key_objectiveusereconnect, true) loopPlugin.createOfflineEvent(0) - - } } }) - } }) - } - else if (splitted.size == 3) - { - if (splitted[1].equals("DISCONNECT", ignoreCase = true)){ + } else if (splitted.size == 3) { + if (splitted[1].equals("DISCONNECT", ignoreCase = true)) { val minutes = SafeParse.stringToInt(splitted[2]) val passCode = generatePasscode() - val reply = String.format(resourceHelper.gs(R.string.smscommunicator_pumpdisconnect),minutes,passCode) + val reply = String.format(resourceHelper.gs(R.string.smscommunicator_pumpdisconnectwithcode), minutes, passCode) receivedSms.processed = true - messageToConfirm = AuthRequest(injector,receivedSms,reply,passCode,object : SmsAction(){ + messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction() { override fun run() { val profile = profileFunction.getProfile() loopPlugin.disconnectPump(minutes, profile) rxBus.send(EventRefreshOverview("SMS_PUMP_DISCONNECT")) - sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.pumpsuspended))) - //rxBus.send(EventRefreshOverview("suspendmenu")) + sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_pumpdisconnected))) } }) - } - else{ + } else { sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) return } + } else { + sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) + return } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b1916d9769..b1190382b2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -300,9 +300,11 @@ Loop is enabled %1$.2f limited to %2$.2f Value %1$s is out of hard limits - To connect pump reply with code %1$s - To disconnect pump to %1d minutes reply with code %2$s - To connect pump failed + To connect pump reply with code %1$s + Connection to pump failed + To disconnect pump for %1d minutes reply with code %2$s + Pump disconnected + Pump reconnected Remote command is not allowed Remote bolus not available. Try again later. To start basal %1$.2fU/h for %2$d min reply with code %3$s diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt index 7321131e30..6af5afd171 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt @@ -226,6 +226,9 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { `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") + `when`(resourceHelper.gs(R.string.smscommunicator_pumpconnectwithcode)).thenReturn("To connect pump reply with code %1\$s") + `when`(resourceHelper.gs(R.string.smscommunicator_reconnect)).thenReturn("Pump reconnected") + `when`(resourceHelper.gs(R.string.smscommunicator_pumpconnectfail)).thenReturn("Connection to pump failed") } @Test @@ -515,6 +518,35 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { Assert.assertEquals("PUMP", smsCommunicatorPlugin.messages[0].text) Assert.assertEquals("Virtual Pump", smsCommunicatorPlugin.messages[1].text) + //PUMP CONNECT 1 2: wrong format + smsCommunicatorPlugin.messages = ArrayList() + sms = Sms("1234", "PUMP CONNECT 1 2") + smsCommunicatorPlugin.processSms(sms) + Assert.assertFalse(sms.ignored) + Assert.assertEquals("PUMP CONNECT 1 2", smsCommunicatorPlugin.messages[0].text) + Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages[1].text) + + //PUMP CONNECT BLABLA + smsCommunicatorPlugin.messages = ArrayList() + sms = Sms("1234", "PUMP BLABLA") + smsCommunicatorPlugin.processSms(sms) + Assert.assertFalse(sms.ignored) + Assert.assertEquals("PUMP BLABLA", smsCommunicatorPlugin.messages[0].text) + Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages[1].text) + + //PUMP CONNECT + PowerMockito.`when`(loopPlugin.isEnabled(PluginType.LOOP)).thenReturn(true) + smsCommunicatorPlugin.messages = ArrayList() + sms = Sms("1234", "PUMP CONNECT") + smsCommunicatorPlugin.processSms(sms) + Assert.assertFalse(sms.ignored) + Assert.assertEquals("PUMP CONNECT", smsCommunicatorPlugin.messages[0].text) + Assert.assertTrue(smsCommunicatorPlugin.messages[1].text.contains("To connect pump reply with code ")) + passCode = smsCommunicatorPlugin.messageToConfirm?.confirmCode!! + smsCommunicatorPlugin.processSms(Sms("1234", passCode)) + Assert.assertEquals(passCode, smsCommunicatorPlugin.messages[2].text) + Assert.assertEquals("Pump reconnected", smsCommunicatorPlugin.messages[3].text) + //HELP smsCommunicatorPlugin.messages = ArrayList() sms = Sms("1234", "HELP")