SmsCommunicator detect commands

This commit is contained in:
Milos Kozak 2019-03-22 22:18:51 +01:00
parent 52482d112d
commit ca3c6b4093
3 changed files with 43 additions and 11 deletions

View file

@ -11,7 +11,7 @@ import info.nightscout.androidaps.utils.DateUtil;
class AuthRequest {
private static Logger log = LoggerFactory.getLogger(L.SMS);
private Sms requester;
Sms requester;
String confirmCode;
private Runnable action;

View file

@ -115,6 +115,25 @@ public class SmsCommunicatorPlugin extends PluginBase {
}
}
boolean isCommand(String command, String number) {
switch(command.toUpperCase()) {
case "BG":
case "LOOP":
case "TREATMENTS":
case "NSCLIENT":
case "PUMP":
case "BASAL":
case "BOLUS":
case "EXTENDED":
case "CAL":
case "PROFILE":
return true;
}
if (messageToConfirm != null && messageToConfirm.requester.phoneNumber.equals(number))
return true;
return false;
}
boolean isAllowedNumber(String number) {
for (String num : allowedNumbers) {
if (num.equals(number)) return true;
@ -155,7 +174,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
String[] splitted = receivedSms.text.split("\\s+");
boolean remoteCommandsAllowed = SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false);
if (splitted.length > 0) {
if (splitted.length > 0 && isCommand(splitted[0].toUpperCase(), receivedSms.phoneNumber)) {
switch (splitted[0].toUpperCase()) {
case "BG":
processBG(splitted, receivedSms);
@ -228,10 +247,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
break;
default: // expect passCode here
//noinspection StatementWithEmptyBody
if (!Character.isLetter(splitted[0].charAt(0))) {
// user text .... ignore
} else if (messageToConfirm != null) {
if (messageToConfirm != null && messageToConfirm.requester.phoneNumber.equals(receivedSms.phoneNumber)) {
messageToConfirm.action(splitted[0]);
messageToConfirm = null;
} else

View file

@ -74,6 +74,21 @@ public class SmsCommunicatorPluginTest {
Assert.assertEquals(2, smsCommunicatorPlugin.allowedNumbers.size());
}
@Test
public void isCommandTest() {
Assert.assertTrue(smsCommunicatorPlugin.isCommand("BOLUS", ""));
smsCommunicatorPlugin.messageToConfirm = null;
Assert.assertFalse(smsCommunicatorPlugin.isCommand("BLB", ""));
smsCommunicatorPlugin.messageToConfirm = new AuthRequest(smsCommunicatorPlugin, new Sms("1234", "ddd"), "RequestText", "ccode", new SmsAction() {
@Override
public void run() {
}
});
Assert.assertTrue(smsCommunicatorPlugin.isCommand("BLB", "1234"));
Assert.assertFalse(smsCommunicatorPlugin.isCommand("BLB", "2345"));
smsCommunicatorPlugin.messageToConfirm = null;
}
@Test
public void isAllowedNumberTest() {
Assert.assertTrue(smsCommunicatorPlugin.isAllowedNumber("5678"));
@ -96,7 +111,6 @@ public class SmsCommunicatorPluginTest {
sms = new Sms("1234", "UNKNOWN");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("UNKNOWN", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertEquals("Unknown command or wrong reply", smsCommunicatorPlugin.messages.get(1).text);
//BG
smsCommunicatorPlugin.messages = new ArrayList<>();
@ -245,13 +259,15 @@ public class SmsCommunicatorPluginTest {
Assert.assertEquals("LOOP SUSPEND 200", smsCommunicatorPlugin.messages.get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.messages.get(1).text.contains("To suspend loop for 180 minutes reply with code "));
passCode = smsCommunicatorPlugin.messageToConfirm.confirmCode;
// ignore from other number
smsCommunicatorPlugin.processSms(new Sms("5678", passCode));
smsCommunicatorPlugin.processSms(new Sms("1234", "XXXX"));
Assert.assertEquals("XXXX", smsCommunicatorPlugin.messages.get(2).text);
Assert.assertEquals("Wrong code. Command cancelled.", smsCommunicatorPlugin.messages.get(3).text);
Assert.assertEquals("XXXX", smsCommunicatorPlugin.messages.get(3).text);
Assert.assertEquals("Wrong code. Command cancelled.", smsCommunicatorPlugin.messages.get(4).text);
//then correct code should not work
smsCommunicatorPlugin.processSms(new Sms("1234", passCode));
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages.get(4).text);
Assert.assertEquals("Unknown command or wrong reply", smsCommunicatorPlugin.messages.get(5).text);
Assert.assertEquals(passCode, smsCommunicatorPlugin.messages.get(5).text);
Assert.assertEquals(6, smsCommunicatorPlugin.messages.size()); // processed as common message
//LOOP BLABLA
smsCommunicatorPlugin.messages = new ArrayList<>();