send important confirmations to all phone numbers

This commit is contained in:
Milos Kozak 2016-12-25 11:29:13 +01:00
parent 273993f995
commit 02b3da97e0

View file

@ -71,6 +71,14 @@ public class SmsCommunicatorPlugin implements PluginBase {
sent = true; sent = true;
} }
public Sms(String phoneNumber, String text, Date date, String confirmCode) {
this.phoneNumber = phoneNumber;
this.text = text;
this.date = date;
this.confirmCode = confirmCode;
sent = true;
}
public String toString() { public String toString() {
return "SMS from " + phoneNumber + ": " + text; return "SMS from " + phoneNumber + ": " + text;
} }
@ -183,7 +191,6 @@ public class SmsCommunicatorPlugin implements PluginBase {
Double amount = 0d; Double amount = 0d;
Double tempBasal = 0d; Double tempBasal = 0d;
String passCode = ""; String passCode = "";
Sms newSms = null;
if (splited.length > 0) { if (splited.length > 0) {
switch (splited[0].toUpperCase()) { switch (splited[0].toUpperCase()) {
@ -194,7 +201,7 @@ public class SmsCommunicatorPlugin implements PluginBase {
if (loopPlugin != null && loopPlugin.isEnabled(PluginBase.LOOP)) { if (loopPlugin != null && loopPlugin.isEnabled(PluginBase.LOOP)) {
loopPlugin.setFragmentEnabled(PluginBase.LOOP, false); loopPlugin.setFragmentEnabled(PluginBase.LOOP, false);
reply = MainApp.sResources.getString(R.string.smscommunicator_loophasbeendisabled); reply = MainApp.sResources.getString(R.string.smscommunicator_loophasbeendisabled);
newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
} }
receivedSms.processed = true; receivedSms.processed = true;
break; break;
@ -203,7 +210,7 @@ public class SmsCommunicatorPlugin implements PluginBase {
if (loopPlugin != null && !loopPlugin.isEnabled(PluginBase.LOOP)) { if (loopPlugin != null && !loopPlugin.isEnabled(PluginBase.LOOP)) {
loopPlugin.setFragmentEnabled(PluginBase.LOOP, true); loopPlugin.setFragmentEnabled(PluginBase.LOOP, true);
reply = MainApp.sResources.getString(R.string.smscommunicator_loophasbeenenabled); reply = MainApp.sResources.getString(R.string.smscommunicator_loophasbeenenabled);
newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
} }
receivedSms.processed = true; receivedSms.processed = true;
break; break;
@ -215,7 +222,7 @@ public class SmsCommunicatorPlugin implements PluginBase {
} else { } else {
reply = MainApp.sResources.getString(R.string.smscommunicator_loopisdisabled); reply = MainApp.sResources.getString(R.string.smscommunicator_loopisdisabled);
} }
newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
} }
receivedSms.processed = true; receivedSms.processed = true;
break; break;
@ -229,7 +236,7 @@ public class SmsCommunicatorPlugin implements 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";
newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
receivedSms.processed = true; receivedSms.processed = true;
break; break;
} }
@ -241,7 +248,7 @@ public class SmsCommunicatorPlugin implements 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";
newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
receivedSms.processed = true; receivedSms.processed = true;
break; break;
} }
@ -250,7 +257,7 @@ public class SmsCommunicatorPlugin implements PluginBase {
DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class); DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class);
if (danaRPlugin != null && danaRPlugin.isEnabled(PluginBase.PUMP)) if (danaRPlugin != null && danaRPlugin.isEnabled(PluginBase.PUMP))
reply = danaRPlugin.shortStatus(); reply = danaRPlugin.shortStatus();
newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
receivedSms.processed = true; receivedSms.processed = true;
break; break;
case "BASAL": case "BASAL":
@ -261,13 +268,11 @@ public class SmsCommunicatorPlugin implements PluginBase {
passCode = generatePasscode(); passCode = generatePasscode();
reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_basalstopreplywithcode), passCode); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_basalstopreplywithcode), passCode);
receivedSms.processed = true; receivedSms.processed = true;
newSms = new Sms(receivedSms.phoneNumber, reply, new Date());
newSms.confirmCode = passCode;
resetWaitingMessages(); resetWaitingMessages();
cancelTempBasalWaitingForConfirmation = newSms; sendSMS(cancelTempBasalWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, new Date(), passCode));
} else { } else {
reply = MainApp.sResources.getString(R.string.remotebasalnotallowed); reply = MainApp.sResources.getString(R.string.remotebasalnotallowed);
newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
} }
} else { } else {
tempBasal = SafeParse.stringToDouble(splited[1]); tempBasal = SafeParse.stringToDouble(splited[1]);
@ -276,14 +281,12 @@ public class SmsCommunicatorPlugin implements PluginBase {
passCode = generatePasscode(); passCode = generatePasscode();
reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_basalreplywithcode), tempBasal, passCode); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_basalreplywithcode), tempBasal, passCode);
receivedSms.processed = true; receivedSms.processed = true;
newSms = new Sms(receivedSms.phoneNumber, reply, new Date());
newSms.tempBasal = tempBasal;
newSms.confirmCode = passCode;
resetWaitingMessages(); resetWaitingMessages();
tempBasalWaitingForConfirmation = newSms; sendSMS(tempBasalWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, new Date(), passCode));
tempBasalWaitingForConfirmation.tempBasal = tempBasal;
} else { } else {
reply = MainApp.sResources.getString(R.string.remotebasalnotallowed); reply = MainApp.sResources.getString(R.string.remotebasalnotallowed);
newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
} }
} }
} }
@ -291,7 +294,7 @@ public class SmsCommunicatorPlugin implements PluginBase {
case "BOLUS": case "BOLUS":
if (new Date().getTime() - lastRemoteBolusTime.getTime() < Constants.remoteBolusMinDistance) { if (new Date().getTime() - lastRemoteBolusTime.getTime() < Constants.remoteBolusMinDistance) {
reply = MainApp.sResources.getString(R.string.remotebolusnotallowed); reply = MainApp.sResources.getString(R.string.remotebolusnotallowed);
newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
} else if (splited.length > 1) { } else if (splited.length > 1) {
amount = SafeParse.stringToDouble(splited[1]); amount = SafeParse.stringToDouble(splited[1]);
amount = MainApp.getConfigBuilder().applyBolusConstraints(amount); amount = MainApp.getConfigBuilder().applyBolusConstraints(amount);
@ -300,14 +303,12 @@ public class SmsCommunicatorPlugin implements PluginBase {
passCode = generatePasscode(); passCode = generatePasscode();
reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_bolusreplywithcode), amount, passCode); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_bolusreplywithcode), amount, passCode);
receivedSms.processed = true; receivedSms.processed = true;
newSms = new Sms(receivedSms.phoneNumber, reply, new Date());
newSms.bolusRequested = amount;
newSms.confirmCode = passCode;
resetWaitingMessages(); resetWaitingMessages();
bolusWaitingForConfirmation = newSms; sendSMS(bolusWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, new Date(), passCode));
bolusWaitingForConfirmation.bolusRequested = amount;
} else { } else {
reply = MainApp.sResources.getString(R.string.remotebolusnotallowed); reply = MainApp.sResources.getString(R.string.remotebolusnotallowed);
newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
} }
} }
break; break;
@ -323,11 +324,12 @@ public class SmsCommunicatorPlugin implements PluginBase {
reply = String.format(MainApp.sResources.getString(R.string.bolusdelivered), result.bolusDelivered); reply = String.format(MainApp.sResources.getString(R.string.bolusdelivered), result.bolusDelivered);
if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus();
lastRemoteBolusTime = new Date(); lastRemoteBolusTime = new Date();
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, new Date()));
} else { } else {
reply = MainApp.sResources.getString(R.string.bolusfailed); reply = MainApp.sResources.getString(R.string.bolusfailed);
if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus();
sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
} }
newSms = new Sms(receivedSms.phoneNumber, reply, new Date());
} }
} else if (tempBasalWaitingForConfirmation != null && !tempBasalWaitingForConfirmation.processed && } else if (tempBasalWaitingForConfirmation != null && !tempBasalWaitingForConfirmation.processed &&
tempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && new Date().getTime() - tempBasalWaitingForConfirmation.date.getTime() < CONFIRM_TIMEOUT) { tempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && new Date().getTime() - tempBasalWaitingForConfirmation.date.getTime() < CONFIRM_TIMEOUT) {
@ -339,11 +341,12 @@ public class SmsCommunicatorPlugin implements PluginBase {
if (result.success) { if (result.success) {
reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_tempbasalset), result.absolute, result.duration); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_tempbasalset), result.absolute, result.duration);
if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus();
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, new Date()));
} else { } else {
reply = MainApp.sResources.getString(R.string.smscommunicator_tempbasalfailed); reply = MainApp.sResources.getString(R.string.smscommunicator_tempbasalfailed);
if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus();
sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
} }
newSms = new Sms(receivedSms.phoneNumber, reply, new Date());
} }
} else if (cancelTempBasalWaitingForConfirmation != null && !cancelTempBasalWaitingForConfirmation.processed && } else if (cancelTempBasalWaitingForConfirmation != null && !cancelTempBasalWaitingForConfirmation.processed &&
cancelTempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && new Date().getTime() - cancelTempBasalWaitingForConfirmation.date.getTime() < CONFIRM_TIMEOUT) { cancelTempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && new Date().getTime() - cancelTempBasalWaitingForConfirmation.date.getTime() < CONFIRM_TIMEOUT) {
@ -355,30 +358,39 @@ public class SmsCommunicatorPlugin implements PluginBase {
if (result.success) { if (result.success) {
reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_tempbasalcanceled)); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_tempbasalcanceled));
if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus();
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, new Date()));
} else { } else {
reply = MainApp.sResources.getString(R.string.smscommunicator_tempbasalcancelfailed); reply = MainApp.sResources.getString(R.string.smscommunicator_tempbasalcancelfailed);
if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus();
sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
} }
newSms = new Sms(receivedSms.phoneNumber, reply, new Date());
} }
} else { } else {
newSms = new Sms(receivedSms.phoneNumber, MainApp.sResources.getString(R.string.smscommunicator_unknowncommand), new Date()); sendSMS(new Sms(receivedSms.phoneNumber, MainApp.sResources.getString(R.string.smscommunicator_unknowncommand), new Date()));
} }
resetWaitingMessages(); resetWaitingMessages();
break; break;
} }
} }
if (newSms != null) {
SmsManager smsManager = SmsManager.getDefault();
newSms.text = stripAccents(newSms.text);
if (newSms.text.length() > 140) newSms.text = newSms.text.substring(0, 139);
smsManager.sendTextMessage(newSms.phoneNumber, null, newSms.text, null, null);
messages.add(newSms);
}
MainApp.bus().post(new EventSmsCommunicatorUpdateGui()); MainApp.bus().post(new EventSmsCommunicatorUpdateGui());
} }
public void sendSMSToAllNumbers(Sms sms) {
for (int i = 0; i < allowedNumbers.size(); i++) {
sms.phoneNumber = allowedNumbers.get(i);
sendSMS(sms);
}
}
public void sendSMS(Sms sms) {
SmsManager smsManager = SmsManager.getDefault();
sms.text = stripAccents(sms.text);
if (sms.text.length() > 140) sms.text = sms.text.substring(0, 139);
smsManager.sendTextMessage(sms.phoneNumber, null, sms.text, null, null);
messages.add(sms);
}
private String generatePasscode() { private String generatePasscode() {
int startChar1 = 'A'; // on iphone 1st char is uppercase :) int startChar1 = 'A'; // on iphone 1st char is uppercase :)
String passCode = Character.toString((char) (startChar1 + Math.random() * ('z' - 'a' + 1))); String passCode = Character.toString((char) (startChar1 + Math.random() * ('z' - 'a' + 1)));