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 { class AuthRequest {
private static Logger log = LoggerFactory.getLogger(L.SMS); private static Logger log = LoggerFactory.getLogger(L.SMS);
private Sms requester; Sms requester;
String confirmCode; String confirmCode;
private Runnable action; 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) { boolean isAllowedNumber(String number) {
for (String num : allowedNumbers) { for (String num : allowedNumbers) {
if (num.equals(number)) return true; if (num.equals(number)) return true;
@ -155,7 +174,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
String[] splitted = receivedSms.text.split("\\s+"); String[] splitted = receivedSms.text.split("\\s+");
boolean remoteCommandsAllowed = SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false); 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()) { switch (splitted[0].toUpperCase()) {
case "BG": case "BG":
processBG(splitted, receivedSms); processBG(splitted, receivedSms);
@ -228,10 +247,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
break; break;
default: // expect passCode here default: // expect passCode here
//noinspection StatementWithEmptyBody if (messageToConfirm != null && messageToConfirm.requester.phoneNumber.equals(receivedSms.phoneNumber)) {
if (!Character.isLetter(splitted[0].charAt(0))) {
// user text .... ignore
} else if (messageToConfirm != null) {
messageToConfirm.action(splitted[0]); messageToConfirm.action(splitted[0]);
messageToConfirm = null; messageToConfirm = null;
} else } else

View file

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