fix exception in sms comm

This commit is contained in:
Milos Kozak 2017-05-02 22:49:17 +02:00
parent 1b69771893
commit b80c382512

View file

@ -262,102 +262,105 @@ public class SmsCommunicatorPlugin implements PluginBase {
Answers.getInstance().logCustom(new CustomEvent("SMS_Bg")); Answers.getInstance().logCustom(new CustomEvent("SMS_Bg"));
break; break;
case "LOOP": case "LOOP":
switch (splited[1].toUpperCase()) { if (splited.length > 1)
case "DISABLE": switch (splited[1].toUpperCase()) {
case "STOP": case "DISABLE":
LoopPlugin loopPlugin = (LoopPlugin) MainApp.getSpecificPlugin(LoopPlugin.class); case "STOP":
if (loopPlugin != null && loopPlugin.isEnabled(PluginBase.LOOP)) { LoopPlugin loopPlugin = (LoopPlugin) MainApp.getSpecificPlugin(LoopPlugin.class);
loopPlugin.setFragmentEnabled(PluginBase.LOOP, false); if (loopPlugin != null && loopPlugin.isEnabled(PluginBase.LOOP)) {
reply = MainApp.sResources.getString(R.string.smscommunicator_loophasbeendisabled); loopPlugin.setFragmentEnabled(PluginBase.LOOP, false);
sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); reply = MainApp.sResources.getString(R.string.smscommunicator_loophasbeendisabled);
} sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
receivedSms.processed = true;
Answers.getInstance().logCustom(new CustomEvent("SMS_Loop_Stop"));
break;
case "ENABLE":
case "START":
loopPlugin = (LoopPlugin) MainApp.getSpecificPlugin(LoopPlugin.class);
if (loopPlugin != null && !loopPlugin.isEnabled(PluginBase.LOOP)) {
loopPlugin.setFragmentEnabled(PluginBase.LOOP, true);
reply = MainApp.sResources.getString(R.string.smscommunicator_loophasbeenenabled);
sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
}
receivedSms.processed = true;
Answers.getInstance().logCustom(new CustomEvent("SMS_Loop_Start"));
break;
case "STATUS":
loopPlugin = (LoopPlugin) MainApp.getSpecificPlugin(LoopPlugin.class);
if (loopPlugin != null) {
if (loopPlugin.isEnabled(PluginBase.LOOP)) {
if (loopPlugin.isSuspended())
reply = String.format(MainApp.sResources.getString(R.string.loopsuspendedfor), loopPlugin.minutesToEndOfSuspend());
else
reply = MainApp.sResources.getString(R.string.smscommunicator_loopisenabled);
} else {
reply = MainApp.sResources.getString(R.string.smscommunicator_loopisdisabled);
} }
sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
}
receivedSms.processed = true;
Answers.getInstance().logCustom(new CustomEvent("SMS_Loop_Status"));
break;
case "RESUME":
final LoopPlugin activeloop = MainApp.getConfigBuilder().getActiveLoop();
activeloop.suspendTo(0);
MainApp.bus().post(new EventRefreshGui(false));
reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_loopresumed));
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, new Date()));
Answers.getInstance().logCustom(new CustomEvent("SMS_Loop_Resume"));
break;
case "SUSPEND":
if (splited.length >= 3)
duration = SafeParse.stringToInt(splited[2]);
duration = Math.max(0, duration);
duration = Math.min(180, duration);
if (duration == 0) {
reply = MainApp.sResources.getString(R.string.smscommunicator_wrongduration);
sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
} else if (remoteCommandsAllowed) {
passCode = generatePasscode();
reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_suspendreplywithcode), duration, passCode);
receivedSms.processed = true; receivedSms.processed = true;
resetWaitingMessages(); Answers.getInstance().logCustom(new CustomEvent("SMS_Loop_Stop"));
sendSMS(suspendWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, new Date(), passCode)); break;
suspendWaitingForConfirmation.duration = duration; case "ENABLE":
Answers.getInstance().logCustom(new CustomEvent("SMS_Loop_Suspend")); case "START":
} else { loopPlugin = (LoopPlugin) MainApp.getSpecificPlugin(LoopPlugin.class);
reply = MainApp.sResources.getString(R.string.smscommunicator_remotecommandnotallowed); if (loopPlugin != null && !loopPlugin.isEnabled(PluginBase.LOOP)) {
sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); loopPlugin.setFragmentEnabled(PluginBase.LOOP, true);
} reply = MainApp.sResources.getString(R.string.smscommunicator_loophasbeenenabled);
break; sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
} }
receivedSms.processed = true;
Answers.getInstance().logCustom(new CustomEvent("SMS_Loop_Start"));
break;
case "STATUS":
loopPlugin = (LoopPlugin) MainApp.getSpecificPlugin(LoopPlugin.class);
if (loopPlugin != null) {
if (loopPlugin.isEnabled(PluginBase.LOOP)) {
if (loopPlugin.isSuspended())
reply = String.format(MainApp.sResources.getString(R.string.loopsuspendedfor), loopPlugin.minutesToEndOfSuspend());
else
reply = MainApp.sResources.getString(R.string.smscommunicator_loopisenabled);
} else {
reply = MainApp.sResources.getString(R.string.smscommunicator_loopisdisabled);
}
sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
}
receivedSms.processed = true;
Answers.getInstance().logCustom(new CustomEvent("SMS_Loop_Status"));
break;
case "RESUME":
final LoopPlugin activeloop = MainApp.getConfigBuilder().getActiveLoop();
activeloop.suspendTo(0);
MainApp.bus().post(new EventRefreshGui(false));
reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_loopresumed));
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, new Date()));
Answers.getInstance().logCustom(new CustomEvent("SMS_Loop_Resume"));
break;
case "SUSPEND":
if (splited.length >= 3)
duration = SafeParse.stringToInt(splited[2]);
duration = Math.max(0, duration);
duration = Math.min(180, duration);
if (duration == 0) {
reply = MainApp.sResources.getString(R.string.smscommunicator_wrongduration);
sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
} else if (remoteCommandsAllowed) {
passCode = generatePasscode();
reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_suspendreplywithcode), duration, passCode);
receivedSms.processed = true;
resetWaitingMessages();
sendSMS(suspendWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, new Date(), passCode));
suspendWaitingForConfirmation.duration = duration;
Answers.getInstance().logCustom(new CustomEvent("SMS_Loop_Suspend"));
} else {
reply = MainApp.sResources.getString(R.string.smscommunicator_remotecommandnotallowed);
sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
}
break;
}
break; break;
case "TREATMENTS": case "TREATMENTS":
switch (splited[1].toUpperCase()) { if (splited.length > 1)
case "REFRESH": switch (splited[1].toUpperCase()) {
Intent restartNSClient = new Intent(Intents.ACTION_RESTART); case "REFRESH":
MainApp.getDbHelper().resetTreatments(); Intent restartNSClient = new Intent(Intents.ACTION_RESTART);
MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient); MainApp.getDbHelper().resetTreatments();
List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0); MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient);
reply = "TERATMENTS REFRESH " + q.size() + " receivers"; List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0);
sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); reply = "TERATMENTS REFRESH " + q.size() + " receivers";
receivedSms.processed = true; sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
Answers.getInstance().logCustom(new CustomEvent("SMS_Treatments_Refresh")); receivedSms.processed = true;
break; Answers.getInstance().logCustom(new CustomEvent("SMS_Treatments_Refresh"));
} break;
}
break; break;
case "NSCLIENT": case "NSCLIENT":
switch (splited[1].toUpperCase()) { if (splited.length > 1)
case "RESTART": switch (splited[1].toUpperCase()) {
Intent restartNSClient = new Intent(Intents.ACTION_RESTART); case "RESTART":
MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient); Intent restartNSClient = new Intent(Intents.ACTION_RESTART);
List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0); MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient);
reply = "NSCLIENT RESTART " + q.size() + " receivers"; List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0);
sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); reply = "NSCLIENT RESTART " + q.size() + " receivers";
receivedSms.processed = true; sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
Answers.getInstance().logCustom(new CustomEvent("SMS_Nsclient_Restart")); receivedSms.processed = true;
break; Answers.getInstance().logCustom(new CustomEvent("SMS_Nsclient_Restart"));
} break;
}
break; break;
case "DANAR": case "DANAR":
DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class); DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class);
@ -450,7 +453,7 @@ public class SmsCommunicatorPlugin implements PluginBase {
PumpInterface pumpInterface = MainApp.getConfigBuilder(); PumpInterface pumpInterface = MainApp.getConfigBuilder();
if (pumpInterface != null) { if (pumpInterface != null) {
danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class); danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class);
PumpEnactResult result = pumpInterface.deliverTreatment(MainApp.getConfigBuilder().getActiveInsulin() ,bolusWaitingForConfirmation.bolusRequested, 0, null); PumpEnactResult result = pumpInterface.deliverTreatment(MainApp.getConfigBuilder().getActiveInsulin(), bolusWaitingForConfirmation.bolusRequested, 0, null);
if (result.success) { if (result.success) {
reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_bolusdelivered), result.bolusDelivered); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_bolusdelivered), result.bolusDelivered);
if (danaRPlugin != null) if (danaRPlugin != null)