SMS code cleanup

This commit is contained in:
Milos Kozak 2019-03-09 18:46:22 +01:00
parent 53721d5d2c
commit f30f4cbee2
4 changed files with 466 additions and 432 deletions

View file

@ -4,6 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.utils.DateUtil;
@ -17,11 +18,13 @@ class AuthRequest {
private long date;
private boolean processed;
private SmsCommunicatorPlugin plugin;
AuthRequest(SmsCommunicatorPlugin plugin, Sms requester, String requestText, String confirmCode, SmsAction action) {
this.requester = requester;
this.confirmCode = confirmCode;
this.action = action;
this.plugin = plugin;
this.date = DateUtil.now();
@ -37,6 +40,7 @@ class AuthRequest {
if (!confirmCode.equals(codeReceived)) {
if (L.isEnabled(L.SMS))
log.debug("Wrong code");
plugin.sendSMS(new Sms(requester.phoneNumber, R.string.sms_wrongcode));
return;
}
if (DateUtil.now() - date < Constants.SMS_CONFIRM_TIMEOUT) {

View file

@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.general.smsCommunicator;
import android.telephony.SmsMessage;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.utils.DateUtil;
class Sms {
@ -27,6 +28,13 @@ class Sms {
sent = true;
}
Sms(String phoneNumber, int textId) {
this.phoneNumber = phoneNumber;
this.text = MainApp.gs(textId);
this.date = DateUtil.now();
sent = true;
}
public String toString() {
return "SMS from " + phoneNumber + ": " + text;
}

View file

@ -46,6 +46,7 @@ import info.nightscout.androidaps.plugins.general.smsCommunicator.events.EventSm
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.queue.Callback;
import info.nightscout.androidaps.services.Intents;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.DecimalFormatter;
import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.SafeParse;
@ -149,21 +150,99 @@ public class SmsCommunicatorPlugin extends PluginBase {
return;
}
String reply = "";
messages.add(receivedSms);
log.debug(receivedSms.toString());
String[] splited = receivedSms.text.split("\\s+");
String passCode;
String[] splitted = receivedSms.text.split("\\s+");
boolean remoteCommandsAllowed = SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false);
if (splited.length > 0) {
switch (splited[0].toUpperCase()) {
if (splitted.length > 0) {
switch (splitted[0].toUpperCase()) {
case "BG":
processBG(splitted, receivedSms);
break;
case "LOOP":
if (!remoteCommandsAllowed)
sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed));
else if (splitted.length == 2)
processLOOP(splitted, receivedSms);
else
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
break;
case "TREATMENTS":
if (splitted.length == 2)
processTREATMENTS(splitted, receivedSms);
else
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
break;
case "NSCLIENT":
if (splitted.length == 2)
processNSCLIENT(splitted, receivedSms);
else
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
break;
case "PUMP":
processPUMP(splitted, receivedSms);
break;
case "BASAL":
if (!remoteCommandsAllowed)
sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed));
else if (splitted.length == 2 || splitted.length == 3)
processBASAL(splitted, receivedSms);
else
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
break;
case "EXTENDED":
if (!remoteCommandsAllowed)
sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed));
else if (splitted.length == 3)
processEXTENDED(splitted, receivedSms);
else
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
break;
case "BOLUS":
if (!remoteCommandsAllowed)
sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed));
else if (DateUtil.now() - lastRemoteBolusTime.getTime() < Constants.remoteBolusMinDistance)
sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotebolusnotallowed));
else if (ConfigBuilderPlugin.getPlugin().getActivePump().isSuspended())
sendSMS(new Sms(receivedSms.phoneNumber, R.string.pumpsuspended));
else if (splitted.length == 2)
processBOLUS(splitted, receivedSms);
else
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
break;
case "CAL":
if (!remoteCommandsAllowed)
sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed));
else if (splitted.length == 2)
processCAL(splitted, receivedSms);
else
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) {
messageToConfirm.action(splitted[0]);
messageToConfirm = null;
} else
sendSMS(new Sms(receivedSms.phoneNumber, MainApp.gs(R.string.smscommunicator_unknowncommand)));
break;
}
}
MainApp.bus().post(new EventSmsCommunicatorUpdateGui());
}
@SuppressWarnings("unused")
private void processBG(String[] splitted, Sms receivedSms) {
BgReading actualBG = DatabaseHelper.actualBg();
BgReading lastBG = DatabaseHelper.lastBg();
String reply = "";
String units = ProfileFunctions.getInstance().getProfileUnits();
if (actualBG != null) {
@ -188,10 +267,11 @@ public class SmsCommunicatorPlugin extends PluginBase {
sendSMS(new Sms(receivedSms.phoneNumber, reply));
receivedSms.processed = true;
break;
case "LOOP":
if (splited.length > 1)
switch (splited[1].toUpperCase()) {
}
private void processLOOP(String[] splitted, Sms receivedSms) {
String reply;
switch (splitted[1].toUpperCase()) {
case "DISABLE":
case "STOP":
LoopPlugin loopPlugin = MainApp.getSpecificPlugin(LoopPlugin.class);
@ -214,8 +294,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
loopPlugin = MainApp.getSpecificPlugin(LoopPlugin.class);
if (loopPlugin != null && !loopPlugin.isEnabled(PluginType.LOOP)) {
loopPlugin.setPluginEnabled(PluginType.LOOP, true);
reply = MainApp.gs(R.string.smscommunicator_loophasbeenenabled);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_loophasbeenenabled));
MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_START"));
}
receivedSms.processed = true;
@ -244,15 +323,15 @@ public class SmsCommunicatorPlugin extends PluginBase {
break;
case "SUSPEND":
int duration = 0;
if (splited.length >= 3)
duration = SafeParse.stringToInt(splited[2]);
if (splitted.length >= 3)
duration = SafeParse.stringToInt(splitted[2]);
duration = Math.max(0, duration);
duration = Math.min(180, duration);
if (duration == 0) {
reply = MainApp.gs(R.string.smscommunicator_wrongduration);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
} else if (remoteCommandsAllowed) {
passCode = generatePasscode();
} else {
String passCode = generatePasscode();
reply = String.format(MainApp.gs(R.string.smscommunicator_suspendreplywithcode), duration, passCode);
receivedSms.processed = true;
messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(duration) {
@ -278,42 +357,40 @@ public class SmsCommunicatorPlugin extends PluginBase {
}
});
} else {
reply = MainApp.gs(R.string.smscommunicator_remotecommandnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
}
default:
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
break;
}
break;
case "TREATMENTS":
if (splited.length > 1)
switch (splited[1].toUpperCase()) {
case "REFRESH":
}
private void processTREATMENTS(String[] splitted, Sms receivedSms) {
if (splitted[1].toUpperCase().equals("REFRESH")) {
Intent restartNSClient = new Intent(Intents.ACTION_RESTART);
TreatmentsPlugin.getPlugin().getService().resetTreatments();
MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient);
List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0);
reply = "TERATMENTS REFRESH " + q.size() + " receivers";
String reply = "TREATMENTS REFRESH " + q.size() + " receivers";
sendSMS(new Sms(receivedSms.phoneNumber, reply));
receivedSms.processed = true;
break;
} else
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
}
break;
case "NSCLIENT":
if (splited.length > 1)
switch (splited[1].toUpperCase()) {
case "RESTART":
private void processNSCLIENT(String[] splitted, Sms receivedSms) {
if (splitted[2].toUpperCase().equals("RESTART")) {
Intent restartNSClient = new Intent(Intents.ACTION_RESTART);
MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient);
List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0);
reply = "NSCLIENT RESTART " + q.size() + " receivers";
String reply = "NSCLIENT RESTART " + q.size() + " receivers";
sendSMS(new Sms(receivedSms.phoneNumber, reply));
receivedSms.processed = true;
break;
} else
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
}
break;
case "PUMP":
case "DANAR":
@SuppressWarnings("unused")
private void processPUMP(String[] splitted, Sms receivedSms) {
ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("SMS", new Callback() {
@Override
public void run() {
@ -330,13 +407,12 @@ public class SmsCommunicatorPlugin extends PluginBase {
}
});
receivedSms.processed = true;
break;
case "BASAL":
if (splited.length > 1) {
if (splited[1].toUpperCase().equals("CANCEL") || splited[1].toUpperCase().equals("STOP")) {
if (remoteCommandsAllowed) {
passCode = generatePasscode();
reply = String.format(MainApp.gs(R.string.smscommunicator_basalstopreplywithcode), passCode);
}
private void processBASAL(String[] splitted, Sms receivedSms) {
if (splitted[1].toUpperCase().equals("CANCEL") || splitted[1].toUpperCase().equals("STOP")) {
String passCode = generatePasscode();
String reply = String.format(MainApp.gs(R.string.smscommunicator_basalstopreplywithcode), passCode);
receivedSms.processed = true;
messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction() {
@Override
@ -357,33 +433,27 @@ public class SmsCommunicatorPlugin extends PluginBase {
});
}
});
} else {
reply = MainApp.gs(R.string.smscommunicator_remotecommandnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
}
} else if (splited[1].endsWith("%")) {
int tempBasalPct = SafeParse.stringToInt(StringUtils.removeEnd(splited[1], "%"));
} else if (splitted[1].endsWith("%")) {
int tempBasalPct = SafeParse.stringToInt(StringUtils.removeEnd(splitted[1], "%"));
int duration = 30;
if (splited.length > 2)
duration = SafeParse.stringToInt(splited[2]);
Profile profile = ProfileFunctions.getInstance().getProfile();
if (profile == null) {
reply = MainApp.gs(R.string.noprofile);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
} else if (tempBasalPct == 0 && !splited[1].equals("0%")) {
reply = MainApp.gs(R.string.wrongformat);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
} else {
if (splitted.length > 2)
duration = SafeParse.stringToInt(splitted[2]);
final Profile profile = ProfileFunctions.getInstance().getProfile();
if (profile == null)
sendSMS(new Sms(receivedSms.phoneNumber, R.string.noprofile));
else if (tempBasalPct == 0 && !splitted[1].equals("0%"))
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
else if (duration == 0)
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
else {
tempBasalPct = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(tempBasalPct), profile).value();
if (remoteCommandsAllowed) {
passCode = generatePasscode();
reply = String.format(MainApp.gs(R.string.smscommunicator_basalpctreplywithcode), tempBasalPct, passCode);
String passCode = generatePasscode();
String reply = String.format(MainApp.gs(R.string.smscommunicator_basalpctreplywithcode), tempBasalPct, passCode);
receivedSms.processed = true;
messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(tempBasalPct, duration) {
@Override
public void run() {
Profile profile = ProfileFunctions.getInstance().getProfile();
if (profile != null)
ConfigBuilderPlugin.getPlugin().getCommandQueue().tempBasalPercent(anInteger, secondInteger, true, profile, new Callback() {
@Override
public void run() {
@ -404,31 +474,27 @@ public class SmsCommunicatorPlugin extends PluginBase {
});
}
});
} else {
reply = MainApp.gs(R.string.smscommunicator_remotecommandnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
}
}
} else {
Double tempBasal = SafeParse.stringToDouble(splited[1]);
Double tempBasal = SafeParse.stringToDouble(splitted[1]);
int duration = 30;
if (splited.length > 2)
duration = SafeParse.stringToInt(splited[2]);
Profile profile = ProfileFunctions.getInstance().getProfile();
if (profile == null) {
reply = MainApp.gs(R.string.noprofile);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
} else {
if (splitted.length > 2)
duration = SafeParse.stringToInt(splitted[2]);
final Profile profile = ProfileFunctions.getInstance().getProfile();
if (profile == null)
sendSMS(new Sms(receivedSms.phoneNumber, R.string.noprofile));
else if (tempBasal == 0 && !splitted[1].equals("0"))
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
else if (duration == 0)
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
else {
tempBasal = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(tempBasal), profile).value();
if (remoteCommandsAllowed) {
passCode = generatePasscode();
reply = String.format(MainApp.gs(R.string.smscommunicator_basalreplywithcode), tempBasal, passCode);
String passCode = generatePasscode();
String reply = String.format(MainApp.gs(R.string.smscommunicator_basalreplywithcode), tempBasal, passCode);
receivedSms.processed = true;
messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(tempBasal, duration) {
@Override
public void run() {
Profile profile = ProfileFunctions.getInstance().getProfile();
if (profile != null)
ConfigBuilderPlugin.getPlugin().getCommandQueue().tempBasalAbsolute(aDouble, secondInteger, true, profile, new Callback() {
@Override
public void run() {
@ -449,20 +515,14 @@ public class SmsCommunicatorPlugin extends PluginBase {
});
}
});
} else {
reply = MainApp.gs(R.string.smscommunicator_remotecommandnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
}
}
}
}
break;
case "EXTENDED":
if (splited.length > 1) {
if (splited[1].toUpperCase().equals("CANCEL") || splited[1].toUpperCase().equals("STOP")) {
if (remoteCommandsAllowed) {
passCode = generatePasscode();
reply = String.format(MainApp.gs(R.string.smscommunicator_extendedstopreplywithcode), passCode);
private void processEXTENDED(String[] splitted, Sms receivedSms) {
if (splitted[1].toUpperCase().equals("CANCEL") || splitted[1].toUpperCase().equals("STOP")) {
String passCode = generatePasscode();
String reply = String.format(MainApp.gs(R.string.smscommunicator_extendedstopreplywithcode), passCode);
receivedSms.processed = true;
messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction() {
@Override
@ -484,20 +544,14 @@ public class SmsCommunicatorPlugin extends PluginBase {
}
});
} else {
reply = MainApp.gs(R.string.smscommunicator_remotecommandnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
}
} else {
if (splited.length < 3) {
reply = MainApp.gs(R.string.wrongformat);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
} else {
Double extended = SafeParse.stringToDouble(splited[1]);
int duration = SafeParse.stringToInt(splited[2]);
Double extended = SafeParse.stringToDouble(splitted[1]);
int duration = SafeParse.stringToInt(splitted[2]);
extended = MainApp.getConstraintChecker().applyExtendedBolusConstraints(new Constraint<>(extended)).value();
if (remoteCommandsAllowed) {
passCode = generatePasscode();
reply = String.format(MainApp.gs(R.string.smscommunicator_extendedreplywithcode), extended, duration, passCode);
if (extended == 0 || duration == 0)
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
else {
String passCode = generatePasscode();
String reply = String.format(MainApp.gs(R.string.smscommunicator_extendedreplywithcode), extended, duration, passCode);
receivedSms.processed = true;
messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(extended, duration) {
@Override
@ -518,27 +572,17 @@ public class SmsCommunicatorPlugin extends PluginBase {
});
}
});
} else {
reply = MainApp.gs(R.string.smscommunicator_remotecommandnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
}
}
}
}
break;
case "BOLUS":
if (System.currentTimeMillis() - lastRemoteBolusTime.getTime() < Constants.remoteBolusMinDistance) {
reply = MainApp.gs(R.string.smscommunicator_remotecommandnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
} else if (ConfigBuilderPlugin.getPlugin().getActivePump().isSuspended()) {
reply = MainApp.gs(R.string.pumpsuspended);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
} else if (splited.length > 1) {
Double bolus = SafeParse.stringToDouble(splited[1]);
private void processBOLUS(String[] splitted, Sms receivedSms) {
Double bolus = SafeParse.stringToDouble(splitted[1]);
bolus = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(bolus)).value();
if (bolus > 0d && remoteCommandsAllowed) {
passCode = generatePasscode();
reply = String.format(MainApp.gs(R.string.smscommunicator_bolusreplywithcode), bolus, passCode);
if (bolus > 0d) {
String passCode = generatePasscode();
String reply = String.format(MainApp.gs(R.string.smscommunicator_bolusreplywithcode), bolus, passCode);
receivedSms.processed = true;
messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(bolus) {
@Override
@ -568,52 +612,28 @@ public class SmsCommunicatorPlugin extends PluginBase {
});
}
});
} else {
reply = MainApp.gs(R.string.smscommunicator_remotecommandnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
} else
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
}
}
break;
case "CAL":
if (splited.length > 1) {
Double cal = SafeParse.stringToDouble(splited[1]);
if (cal > 0d && remoteCommandsAllowed) {
passCode = generatePasscode();
reply = String.format(MainApp.gs(R.string.smscommunicator_calibrationreplywithcode), cal, passCode);
private void processCAL(String[] splitted, Sms receivedSms) {
Double cal = SafeParse.stringToDouble(splitted[1]);
if (cal > 0d) {
String passCode = generatePasscode();
String reply = String.format(MainApp.gs(R.string.smscommunicator_calibrationreplywithcode), cal, passCode);
receivedSms.processed = true;
messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction() {
@Override
public void run() {
boolean result = XdripCalibrations.sendIntent(aDouble);
if (result) {
String reply = MainApp.gs(R.string.smscommunicator_calibrationsent);
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply));
} else {
String reply = MainApp.gs(R.string.smscommunicator_calibrationfailed);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
}
if (result)
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_calibrationsent));
else
sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_calibrationfailed));
}
});
} else {
reply = MainApp.gs(R.string.smscommunicator_remotecommandnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
}
}
break;
default: // expect passCode here
if (!Character.isLetter(splited[0].charAt(0))) {
// user text .... ignore
} else if (messageToConfirm != null) {
messageToConfirm.action(splited[0]);
messageToConfirm = null;
} else {
sendSMS(new Sms(receivedSms.phoneNumber, MainApp.gs(R.string.smscommunicator_unknowncommand)));
}
break;
}
}
MainApp.bus().post(new EventSmsCommunicatorUpdateGui());
} else
sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat));
}
public void sendNotificationToAllNumbers(String text) {

View file

@ -366,6 +366,7 @@
<string name="valuelimitedto">%1$.2f limited to %2$.2f</string>
<string name="valueoutofrange" formatted="false">Value %s is out of hard limits</string>
<string name="smscommunicator_remotecommandnotallowed">Remote command is not allowed</string>
<string name="smscommunicator_remotebolusnotallowed">Remote bolus not available. Try again later.</string>
<string name="smscommunicator_basalreplywithcode">To start basal %1$.2fU/h reply with code %2$s</string>
<string name="smscommunicator_extendedreplywithcode">To start extended bolus %1$.2fU for %2$d min reply with code %3$s</string>
<string name="smscommunicator_basalpctreplywithcode">To start basal %1$d%% reply with code %2$s</string>
@ -1320,6 +1321,7 @@
<string name="storage">internal storage constraint</string>
<string name="diskfull">Free at least %1$d MB from internal storage! Loop disabled!</string>
<string name="wrongformat">Wrong format</string>
<string name="sms_wrongcode">Wrong code. Command cancelled.</string>
<plurals name="objective_days">
<item quantity="one">%1$d day</item>
<item quantity="other">%1$d days</item>