SMSPlugin refactor & cleanup

This commit is contained in:
Milos Kozak 2018-10-14 10:55:11 +02:00
parent 94ef8f4a76
commit 903504d72d
3 changed files with 143 additions and 188 deletions

View file

@ -2,20 +2,16 @@ package info.nightscout.androidaps.plugins.SmsCommunicator;
import android.telephony.SmsMessage; import android.telephony.SmsMessage;
import info.nightscout.utils.DateUtil;
class Sms { class Sms {
String phoneNumber; String phoneNumber;
String confirmCode; // move
String text; String text;
long date; //move long date;
boolean received = false; boolean received = false;
boolean sent = false; boolean sent = false;
boolean processed = false; boolean processed = false;
double bolusRequested = 0d;
double tempBasal = 0d;
double calibrationRequested = 0d;
int duration = 0;
Sms(SmsMessage message) { Sms(SmsMessage message) {
phoneNumber = message.getOriginatingAddress(); phoneNumber = message.getOriginatingAddress();
text = message.getMessageBody(); text = message.getMessageBody();
@ -26,21 +22,7 @@ class Sms {
Sms(String phoneNumber, String text) { Sms(String phoneNumber, String text) {
this.phoneNumber = phoneNumber; this.phoneNumber = phoneNumber;
this.text = text; this.text = text;
sent = true; this.date = DateUtil.now();
}
Sms(String phoneNumber, String text, long date) {
this.phoneNumber = phoneNumber;
this.text = text;
this.date = date;
sent = true;
}
Sms(String phoneNumber, String text, long date, String confirmCode) {
this.phoneNumber = phoneNumber;
this.text = text;
this.date = date;
this.confirmCode = confirmCode;
sent = true; sent = true;
} }

View file

@ -1,16 +1,16 @@
package info.nightscout.androidaps.plugins.SmsCommunicator; package info.nightscout.androidaps.plugins.SmsCommunicator;
abstract class SmsAction implements Runnable { abstract class SmsAction implements Runnable {
Double d; Double aDouble;
Integer i; Integer anInteger;
SmsAction() {} SmsAction() {}
SmsAction(Double d) { SmsAction(Double aDouble) {
this.d = d; this.aDouble = aDouble;
} }
SmsAction(Integer i) { SmsAction(Integer anInteger) {
this.i = i; this.anInteger = anInteger;
} }
} }

View file

@ -73,11 +73,6 @@ public class SmsCommunicatorPlugin extends PluginBase {
private AuthRequest messageToConfirm = null; private AuthRequest messageToConfirm = null;
private Sms cancelTempBasalWaitingForConfirmation = null;
private Sms tempBasalWaitingForConfirmation = null;
private Sms bolusWaitingForConfirmation = null;
private Sms calibrationWaitingForConfirmation = null;
private Sms suspendWaitingForConfirmation = null;
private Date lastRemoteBolusTime = new Date(0); private Date lastRemoteBolusTime = new Date(0);
ArrayList<Sms> messages = new ArrayList<>(); ArrayList<Sms> messages = new ArrayList<>();
@ -158,9 +153,6 @@ public class SmsCommunicatorPlugin extends PluginBase {
log.debug(receivedSms.toString()); log.debug(receivedSms.toString());
String[] splited = receivedSms.text.split("\\s+"); String[] splited = receivedSms.text.split("\\s+");
Double amount;
Double tempBasal;
int duration = 0;
String passCode; String passCode;
boolean remoteCommandsAllowed = SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false); boolean remoteCommandsAllowed = SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false);
@ -192,9 +184,8 @@ public class SmsCommunicatorPlugin extends PluginBase {
+ MainApp.gs(R.string.sms_bolus) + " " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U " + MainApp.gs(R.string.sms_bolus) + " " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U "
+ MainApp.gs(R.string.sms_basal) + " " + DecimalFormatter.to2Decimal(basalIob.basaliob) + "U)"; + MainApp.gs(R.string.sms_basal) + " " + DecimalFormatter.to2Decimal(basalIob.basaliob) + "U)";
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
receivedSms.processed = true; receivedSms.processed = true;
FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Bg"));
break; break;
case "LOOP": case "LOOP":
if (splited.length > 1) if (splited.length > 1)
@ -210,7 +201,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_STOP")); MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_STOP"));
String reply = MainApp.gs(R.string.smscommunicator_loophasbeendisabled) + " " + String reply = MainApp.gs(R.string.smscommunicator_loophasbeendisabled) + " " +
MainApp.gs(result.success ? R.string.smscommunicator_tempbasalcanceled : R.string.smscommunicator_tempbasalcancelfailed); MainApp.gs(result.success ? R.string.smscommunicator_tempbasalcanceled : R.string.smscommunicator_tempbasalcancelfailed);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
} }
}); });
} }
@ -223,7 +214,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
if (loopPlugin != null && !loopPlugin.isEnabled(PluginType.LOOP)) { if (loopPlugin != null && !loopPlugin.isEnabled(PluginType.LOOP)) {
loopPlugin.setPluginEnabled(PluginType.LOOP, true); loopPlugin.setPluginEnabled(PluginType.LOOP, true);
reply = MainApp.gs(R.string.smscommunicator_loophasbeenenabled); reply = MainApp.gs(R.string.smscommunicator_loophasbeenenabled);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_START")); MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_START"));
} }
receivedSms.processed = true; receivedSms.processed = true;
@ -240,7 +231,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
} else { } else {
reply = MainApp.gs(R.string.smscommunicator_loopisdisabled); reply = MainApp.gs(R.string.smscommunicator_loopisdisabled);
} }
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
} }
receivedSms.processed = true; receivedSms.processed = true;
FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Loop_Status")); FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Loop_Status"));
@ -250,28 +241,48 @@ public class SmsCommunicatorPlugin extends PluginBase {
MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_RESUME")); MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_RESUME"));
NSUpload.uploadOpenAPSOffline(0); NSUpload.uploadOpenAPSOffline(0);
reply = MainApp.gs(R.string.smscommunicator_loopresumed); reply = MainApp.gs(R.string.smscommunicator_loopresumed);
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply));
FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Loop_Resume")); FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Loop_Resume"));
break; break;
case "SUSPEND": case "SUSPEND":
int duration = 0;
if (splited.length >= 3) if (splited.length >= 3)
duration = SafeParse.stringToInt(splited[2]); duration = SafeParse.stringToInt(splited[2]);
duration = Math.max(0, duration); duration = Math.max(0, duration);
duration = Math.min(180, duration); duration = Math.min(180, duration);
if (duration == 0) { if (duration == 0) {
reply = MainApp.gs(R.string.smscommunicator_wrongduration); reply = MainApp.gs(R.string.smscommunicator_wrongduration);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
} else if (remoteCommandsAllowed) { } else if (remoteCommandsAllowed) {
passCode = generatePasscode(); passCode = generatePasscode();
reply = String.format(MainApp.gs(R.string.smscommunicator_suspendreplywithcode), duration, passCode); reply = String.format(MainApp.gs(R.string.smscommunicator_suspendreplywithcode), duration, passCode);
receivedSms.processed = true; receivedSms.processed = true;
resetWaitingMessages(); messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(duration) {
sendSMS(suspendWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis(), passCode)); @Override
suspendWaitingForConfirmation.duration = duration; public void run() {
FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Loop_Suspend")); ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelTempBasal(true, new Callback() {
@Override
public void run() {
if (result.success) {
LoopPlugin.getPlugin().suspendTo(System.currentTimeMillis() + anInteger * 60L * 1000);
NSUpload.uploadOpenAPSOffline(anInteger * 60);
MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_SUSPENDED"));
String reply = MainApp.gs(R.string.smscommunicator_loopsuspended) + " " +
MainApp.gs(result.success ? R.string.smscommunicator_tempbasalcanceled : R.string.smscommunicator_tempbasalcancelfailed);
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply));
} else {
String reply = MainApp.gs(R.string.smscommunicator_tempbasalcancelfailed);
reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
}
}
});
}
});
} else { } else {
reply = MainApp.gs(R.string.smscommunicator_remotecommandnotallowed); reply = MainApp.gs(R.string.smscommunicator_remotecommandnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
} }
break; break;
} }
@ -285,7 +296,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient); MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient);
List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0); List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0);
reply = "TERATMENTS REFRESH " + q.size() + " receivers"; reply = "TERATMENTS REFRESH " + q.size() + " receivers";
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
receivedSms.processed = true; receivedSms.processed = true;
FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Treatments_Refresh")); FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Treatments_Refresh"));
break; break;
@ -299,7 +310,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient); MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient);
List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0); List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0);
reply = "NSCLIENT RESTART " + q.size() + " receivers"; reply = "NSCLIENT RESTART " + q.size() + " receivers";
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
receivedSms.processed = true; receivedSms.processed = true;
FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Nsclient_Restart")); FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Nsclient_Restart"));
break; break;
@ -314,11 +325,11 @@ public class SmsCommunicatorPlugin extends PluginBase {
if (result.success) { if (result.success) {
if (pump != null) { if (pump != null) {
String reply = pump.shortStatus(true); String reply = pump.shortStatus(true);
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply));
} }
} else { } else {
String reply = MainApp.gs(R.string.readstatusfailed); String reply = MainApp.gs(R.string.readstatusfailed);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
} }
} }
}); });
@ -332,32 +343,65 @@ public class SmsCommunicatorPlugin extends PluginBase {
passCode = generatePasscode(); passCode = generatePasscode();
reply = String.format(MainApp.gs(R.string.smscommunicator_basalstopreplywithcode), passCode); reply = String.format(MainApp.gs(R.string.smscommunicator_basalstopreplywithcode), passCode);
receivedSms.processed = true; receivedSms.processed = true;
resetWaitingMessages(); messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction() {
sendSMS(cancelTempBasalWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis(), passCode)); @Override
FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Basal")); public void run() {
ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelTempBasal(true, new Callback() {
@Override
public void run() {
if (result.success) {
String reply = MainApp.gs(R.string.smscommunicator_tempbasalcanceled);
reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true);
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply));
} else {
String reply = MainApp.gs(R.string.smscommunicator_tempbasalcancelfailed);
reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
}
}
});
}
});
} else { } else {
reply = MainApp.gs(R.string.smscommunicator_remotebasalnotallowed); reply = MainApp.gs(R.string.smscommunicator_remotebasalnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
} }
} else { } else {
tempBasal = SafeParse.stringToDouble(splited[1]); Double tempBasal = SafeParse.stringToDouble(splited[1]);
Profile profile = ProfileFunctions.getInstance().getProfile(); Profile profile = ProfileFunctions.getInstance().getProfile();
if (profile == null) { if (profile == null) {
reply = MainApp.gs(R.string.noprofile); reply = MainApp.gs(R.string.noprofile);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
} else { } else {
tempBasal = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(tempBasal), profile).value(); tempBasal = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(tempBasal), profile).value();
if (remoteCommandsAllowed) { if (remoteCommandsAllowed) {
passCode = generatePasscode(); passCode = generatePasscode();
reply = String.format(MainApp.gs(R.string.smscommunicator_basalreplywithcode), tempBasal, passCode); reply = String.format(MainApp.gs(R.string.smscommunicator_basalreplywithcode), tempBasal, passCode);
receivedSms.processed = true; receivedSms.processed = true;
resetWaitingMessages(); messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(tempBasal) {
sendSMS(tempBasalWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis(), passCode)); @Override
tempBasalWaitingForConfirmation.tempBasal = tempBasal; public void run() {
FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Basal")); Profile profile = ProfileFunctions.getInstance().getProfile();
if (profile != null)
ConfigBuilderPlugin.getPlugin().getCommandQueue().tempBasalAbsolute(aDouble, 30, true, profile, new Callback() {
@Override
public void run() {
if (result.success) {
String reply = String.format(MainApp.gs(R.string.smscommunicator_tempbasalset), result.absolute, result.duration);
reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true);
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply));
} else {
String reply = MainApp.gs(R.string.smscommunicator_tempbasalfailed);
reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
}
}
});
}
});
} else { } else {
reply = MainApp.gs(R.string.smscommunicator_remotebasalnotallowed); reply = MainApp.gs(R.string.smscommunicator_remotebasalnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
} }
} }
} }
@ -366,41 +410,74 @@ public class SmsCommunicatorPlugin extends PluginBase {
case "BOLUS": case "BOLUS":
if (System.currentTimeMillis() - lastRemoteBolusTime.getTime() < Constants.remoteBolusMinDistance) { if (System.currentTimeMillis() - lastRemoteBolusTime.getTime() < Constants.remoteBolusMinDistance) {
reply = MainApp.gs(R.string.smscommunicator_remotebolusnotallowed); reply = MainApp.gs(R.string.smscommunicator_remotebolusnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
} else if (ConfigBuilderPlugin.getPlugin().getActivePump().isSuspended()) { } else if (ConfigBuilderPlugin.getPlugin().getActivePump().isSuspended()) {
reply = MainApp.gs(R.string.pumpsuspended); reply = MainApp.gs(R.string.pumpsuspended);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
} else if (splited.length > 1) { } else if (splited.length > 1) {
amount = SafeParse.stringToDouble(splited[1]); Double bolus = SafeParse.stringToDouble(splited[1]);
amount = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(amount)).value(); bolus = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(bolus)).value();
if (amount > 0d && remoteCommandsAllowed) { if (bolus > 0d && remoteCommandsAllowed) {
passCode = generatePasscode(); passCode = generatePasscode();
reply = String.format(MainApp.gs(R.string.smscommunicator_bolusreplywithcode), amount, passCode); reply = String.format(MainApp.gs(R.string.smscommunicator_bolusreplywithcode), bolus, passCode);
receivedSms.processed = true; receivedSms.processed = true;
resetWaitingMessages(); messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(bolus) {
sendSMS(bolusWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis(), passCode)); @Override
bolusWaitingForConfirmation.bolusRequested = amount; public void run() {
FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Bolus")); DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo();
detailedBolusInfo.insulin = aDouble;
detailedBolusInfo.source = Source.USER;
ConfigBuilderPlugin.getPlugin().getCommandQueue().bolus(detailedBolusInfo, new Callback() {
@Override
public void run() {
PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
if (result.success) {
SystemClock.sleep(T.secs(15).msecs()); // wait some time to get history
String reply = String.format(MainApp.gs(R.string.smscommunicator_bolusdelivered), result.bolusDelivered);
if (pump != null)
reply += "\n" + pump.shortStatus(true);
lastRemoteBolusTime = new Date();
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply));
} else {
SystemClock.sleep(T.secs(60).msecs()); // wait some time to get history
String reply = MainApp.gs(R.string.smscommunicator_bolusfailed);
if (pump != null)
reply += "\n" + pump.shortStatus(true);
sendSMS(new Sms(receivedSms.phoneNumber, reply));
}
}
});
}
});
} else { } else {
reply = MainApp.gs(R.string.smscommunicator_remotebolusnotallowed); reply = MainApp.gs(R.string.smscommunicator_remotebolusnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
} }
} }
break; break;
case "CAL": case "CAL":
if (splited.length > 1) { if (splited.length > 1) {
amount = SafeParse.stringToDouble(splited[1]); Double cal = SafeParse.stringToDouble(splited[1]);
if (amount > 0d && remoteCommandsAllowed) { if (cal > 0d && remoteCommandsAllowed) {
passCode = generatePasscode(); passCode = generatePasscode();
reply = String.format(MainApp.gs(R.string.smscommunicator_calibrationreplywithcode), amount, passCode); reply = String.format(MainApp.gs(R.string.smscommunicator_calibrationreplywithcode), cal, passCode);
receivedSms.processed = true; receivedSms.processed = true;
resetWaitingMessages(); messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction() {
sendSMS(calibrationWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis(), passCode)); @Override
calibrationWaitingForConfirmation.calibrationRequested = amount; public void run() {
FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Cal")); 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));
}
}
});
} else { } else {
reply = MainApp.gs(R.string.smscommunicator_remotecalibrationnotallowed); reply = MainApp.gs(R.string.smscommunicator_remotecalibrationnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, reply));
} }
} }
break; break;
@ -408,105 +485,9 @@ public class SmsCommunicatorPlugin extends PluginBase {
if (messageToConfirm != null) { if (messageToConfirm != null) {
messageToConfirm.action(splited[0]); messageToConfirm.action(splited[0]);
messageToConfirm = null; messageToConfirm = null;
}
if (bolusWaitingForConfirmation != null && !bolusWaitingForConfirmation.processed &&
bolusWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - bolusWaitingForConfirmation.date < Constants.SMS_CONFIRM_TIMEOUT) {
bolusWaitingForConfirmation.processed = true;
DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo();
detailedBolusInfo.insulin = bolusWaitingForConfirmation.bolusRequested;
detailedBolusInfo.source = Source.USER;
ConfigBuilderPlugin.getPlugin().getCommandQueue().bolus(detailedBolusInfo, new Callback() {
@Override
public void run() {
PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
if (result.success) {
SystemClock.sleep(T.secs(15).msecs()); // wait some time to get history
String reply = String.format(MainApp.gs(R.string.smscommunicator_bolusdelivered), result.bolusDelivered);
if (pump != null)
reply += "\n" + pump.shortStatus(true);
lastRemoteBolusTime = new Date();
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis()));
} else {
SystemClock.sleep(T.secs(60).msecs()); // wait some time to get history
String reply = MainApp.gs(R.string.smscommunicator_bolusfailed);
if (pump != null)
reply += "\n" + pump.shortStatus(true);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis()));
}
}
});
} else if (tempBasalWaitingForConfirmation != null && !tempBasalWaitingForConfirmation.processed &&
tempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - tempBasalWaitingForConfirmation.date < Constants.SMS_CONFIRM_TIMEOUT) {
tempBasalWaitingForConfirmation.processed = true;
Profile profile = ProfileFunctions.getInstance().getProfile();
if (profile != null)
ConfigBuilderPlugin.getPlugin().getCommandQueue().tempBasalAbsolute(tempBasalWaitingForConfirmation.tempBasal, 30, true, profile, new Callback() {
@Override
public void run() {
if (result.success) {
String reply = String.format(MainApp.gs(R.string.smscommunicator_tempbasalset), result.absolute, result.duration);
reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true);
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis()));
} else {
String reply = MainApp.gs(R.string.smscommunicator_tempbasalfailed);
reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis()));
}
}
});
} else if (cancelTempBasalWaitingForConfirmation != null && !cancelTempBasalWaitingForConfirmation.processed &&
cancelTempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - cancelTempBasalWaitingForConfirmation.date < Constants.SMS_CONFIRM_TIMEOUT) {
cancelTempBasalWaitingForConfirmation.processed = true;
ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelTempBasal(true, new Callback() {
@Override
public void run() {
if (result.success) {
String reply = MainApp.gs(R.string.smscommunicator_tempbasalcanceled);
reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true);
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis()));
} else {
String reply = MainApp.gs(R.string.smscommunicator_tempbasalcancelfailed);
reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis()));
}
}
});
} else if (calibrationWaitingForConfirmation != null && !calibrationWaitingForConfirmation.processed &&
calibrationWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - calibrationWaitingForConfirmation.date < Constants.SMS_CONFIRM_TIMEOUT) {
calibrationWaitingForConfirmation.processed = true;
boolean result = XdripCalibrations.sendIntent(calibrationWaitingForConfirmation.calibrationRequested);
if (result) {
reply = MainApp.gs(R.string.smscommunicator_calibrationsent);
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis()));
} else {
reply = MainApp.gs(R.string.smscommunicator_calibrationfailed);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis()));
}
} else if (suspendWaitingForConfirmation != null && !suspendWaitingForConfirmation.processed &&
suspendWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - suspendWaitingForConfirmation.date < Constants.SMS_CONFIRM_TIMEOUT) {
suspendWaitingForConfirmation.processed = true;
final int dur = suspendWaitingForConfirmation.duration;
ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelTempBasal(true, new Callback() {
@Override
public void run() {
if (result.success) {
LoopPlugin.getPlugin().suspendTo(System.currentTimeMillis() + dur * 60L * 1000);
NSUpload.uploadOpenAPSOffline(dur * 60);
MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_SUSPENDED"));
String reply = MainApp.gs(R.string.smscommunicator_loopsuspended) + " " +
MainApp.gs(result.success ? R.string.smscommunicator_tempbasalcanceled : R.string.smscommunicator_tempbasalcancelfailed);
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis()));
} else {
String reply = MainApp.gs(R.string.smscommunicator_tempbasalcancelfailed);
reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true);
sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis()));
}
}
});
} else { } else {
sendSMS(new Sms(receivedSms.phoneNumber, MainApp.gs(R.string.smscommunicator_unknowncommand), System.currentTimeMillis())); sendSMS(new Sms(receivedSms.phoneNumber, MainApp.gs(R.string.smscommunicator_unknowncommand)));
} }
resetWaitingMessages();
break; break;
} }
} }
@ -516,7 +497,7 @@ public class SmsCommunicatorPlugin extends PluginBase {
public void sendNotificationToAllNumbers(String text) { public void sendNotificationToAllNumbers(String text) {
for (int i = 0; i < allowedNumbers.size(); i++) { for (int i = 0; i < allowedNumbers.size(); i++) {
Sms sms = new Sms(allowedNumbers.get(i), text, System.currentTimeMillis()); Sms sms = new Sms(allowedNumbers.get(i), text);
sendSMS(sms); sendSMS(sms);
} }
} }
@ -556,14 +537,6 @@ public class SmsCommunicatorPlugin extends PluginBase {
return passCode; return passCode;
} }
private void resetWaitingMessages() {
tempBasalWaitingForConfirmation = null;
cancelTempBasalWaitingForConfirmation = null;
bolusWaitingForConfirmation = null;
calibrationWaitingForConfirmation = null;
suspendWaitingForConfirmation = null;
}
private static String stripAccents(String s) { private static String stripAccents(String s) {
s = Normalizer.normalize(s, Normalizer.Form.NFD); s = Normalizer.normalize(s, Normalizer.Form.NFD);
s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}]", ""); s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");