refactor all deliverTreatment

This commit is contained in:
Milos Kozak 2017-11-12 00:08:25 +01:00
parent 620a54a31b
commit 7374545046
4 changed files with 67 additions and 84 deletions

View file

@ -2,9 +2,8 @@ package info.nightscout.androidaps.plugins.Actions.dialogs;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -27,11 +26,10 @@ import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.queue.Callback;
import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.NumberPicker; import info.nightscout.utils.NumberPicker;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
@ -48,13 +46,7 @@ public class FillDialog extends DialogFragment implements OnClickListener {
NumberPicker editInsulin; NumberPicker editInsulin;
Handler mHandler;
public static HandlerThread mHandlerThread;
public FillDialog() { public FillDialog() {
mHandlerThread = new HandlerThread(FillDialog.class.getSimpleName());
mHandlerThread.start();
this.mHandler = new Handler(mHandlerThread.getLooper());
} }
@Override @Override
@ -158,28 +150,21 @@ public class FillDialog extends DialogFragment implements OnClickListener {
builder.setPositiveButton(getString(R.string.primefill), new DialogInterface.OnClickListener() { builder.setPositiveButton(getString(R.string.primefill), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
if (finalInsulinAfterConstraints > 0) { if (finalInsulinAfterConstraints > 0) {
final ConfigBuilderPlugin pump = MainApp.getConfigBuilder(); DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo();
mHandler.post(new Runnable() { detailedBolusInfo.insulin = finalInsulinAfterConstraints;
detailedBolusInfo.context = context;
detailedBolusInfo.source = Source.USER;
detailedBolusInfo.isValid = false; // do not count it in IOB (for pump history)
ConfigBuilderPlugin.getCommandQueue().bolus(detailedBolusInfo, new Callback() {
@Override @Override
public void run() { public void run() {
DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo();
detailedBolusInfo.insulin = finalInsulinAfterConstraints;
detailedBolusInfo.context = context;
detailedBolusInfo.source = Source.USER;
detailedBolusInfo.isValid = false; // do not count it in IOB (for pump history)
PumpEnactResult result = pump.deliverTreatment(detailedBolusInfo);
if (!result.success) { if (!result.success) {
try { Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class);
AlertDialog.Builder builder = new AlertDialog.Builder(context); i.putExtra("soundid", R.raw.boluserror);
builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror)); i.putExtra("status", result.comment);
builder.setMessage(result.comment); i.putExtra("title", MainApp.sResources.getString(R.string.treatmentdeliveryerror));
builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), null); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
builder.show(); MainApp.instance().startActivity(i);
} catch (WindowManager.BadTokenException | NullPointerException e) {
// window has been destroyed
Notification notification = new Notification(Notification.BOLUS_DELIVERY_ERROR, MainApp.sResources.getString(R.string.treatmentdeliveryerror), Notification.URGENT);
MainApp.bus().post(new EventNewNotification(notification));
}
} }
} }
}); });

View file

@ -5,6 +5,7 @@ import android.app.Activity;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.DashPathEffect; import android.graphics.DashPathEffect;
import android.graphics.Paint; import android.graphics.Paint;
@ -26,7 +27,6 @@ import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.CompoundButton; import android.widget.CompoundButton;
@ -109,10 +109,10 @@ import info.nightscout.androidaps.plugins.NSClientInternal.data.NSDeviceStatus;
import info.nightscout.androidaps.plugins.OpenAPSAMA.DetermineBasalResultAMA; import info.nightscout.androidaps.plugins.OpenAPSAMA.DetermineBasalResultAMA;
import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin; import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin;
import info.nightscout.androidaps.plugins.Overview.Dialogs.CalibrationDialog; import info.nightscout.androidaps.plugins.Overview.Dialogs.CalibrationDialog;
import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity;
import info.nightscout.androidaps.plugins.Overview.Dialogs.NewTreatmentDialog; import info.nightscout.androidaps.plugins.Overview.Dialogs.NewTreatmentDialog;
import info.nightscout.androidaps.plugins.Overview.Dialogs.WizardDialog; import info.nightscout.androidaps.plugins.Overview.Dialogs.WizardDialog;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventSetWakeLock; import info.nightscout.androidaps.plugins.Overview.events.EventSetWakeLock;
import info.nightscout.androidaps.plugins.Overview.graphExtensions.AreaGraphSeries; import info.nightscout.androidaps.plugins.Overview.graphExtensions.AreaGraphSeries;
import info.nightscout.androidaps.plugins.Overview.graphExtensions.DataPointWithLabelInterface; import info.nightscout.androidaps.plugins.Overview.graphExtensions.DataPointWithLabelInterface;
@ -122,6 +122,7 @@ import info.nightscout.androidaps.plugins.Overview.graphExtensions.PointsWithLab
import info.nightscout.androidaps.plugins.Overview.graphExtensions.TimeAsXAxisLabelFormatter; import info.nightscout.androidaps.plugins.Overview.graphExtensions.TimeAsXAxisLabelFormatter;
import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin; import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin;
import info.nightscout.androidaps.plugins.Treatments.fragments.ProfileViewerDialog; import info.nightscout.androidaps.plugins.Treatments.fragments.ProfileViewerDialog;
import info.nightscout.androidaps.queue.Callback;
import info.nightscout.utils.BolusWizard; import info.nightscout.utils.BolusWizard;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.DecimalFormatter;
@ -707,30 +708,23 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) { if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) {
final ConfigBuilderPlugin pump = MainApp.getConfigBuilder(); DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo();
sHandler.post(new Runnable() { detailedBolusInfo.eventType = CareportalEvent.BOLUSWIZARD;
detailedBolusInfo.insulin = finalInsulinAfterConstraints;
detailedBolusInfo.carbs = finalCarbsAfterConstraints;
detailedBolusInfo.context = context;
detailedBolusInfo.boluscalc = boluscalcJSON;
detailedBolusInfo.source = Source.USER;
ConfigBuilderPlugin.getCommandQueue().bolus(detailedBolusInfo, new Callback() {
@Override @Override
public void run() { public void run() {
DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo();
detailedBolusInfo.eventType = CareportalEvent.BOLUSWIZARD;
detailedBolusInfo.insulin = finalInsulinAfterConstraints;
detailedBolusInfo.carbs = finalCarbsAfterConstraints;
detailedBolusInfo.context = context;
detailedBolusInfo.boluscalc = boluscalcJSON;
detailedBolusInfo.source = Source.USER;
PumpEnactResult result = pump.deliverTreatment(detailedBolusInfo);
if (!result.success) { if (!result.success) {
try { Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); i.putExtra("soundid", R.raw.boluserror);
builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror)); i.putExtra("status", result.comment);
builder.setMessage(result.comment); i.putExtra("title", MainApp.sResources.getString(R.string.treatmentdeliveryerror));
builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), null); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
builder.show(); MainApp.instance().startActivity(i);
} catch (WindowManager.BadTokenException | NullPointerException e) {
// window has been destroyed
Notification notification = new Notification(Notification.BOLUS_DELIVERY_ERROR, MainApp.sResources.getString(R.string.treatmentdeliveryerror), Notification.URGENT);
MainApp.bus().post(new EventNewNotification(notification));
}
} }
} }
}); });

View file

@ -35,12 +35,14 @@ import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity;
import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.Notification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanPlugin; import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanPlugin;
import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventNewSMS; import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventNewSMS;
import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventSmsCommunicatorUpdateGui; import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventSmsCommunicatorUpdateGui;
import info.nightscout.androidaps.queue.Callback;
import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.NSUpload; import info.nightscout.utils.NSUpload;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
@ -225,7 +227,7 @@ public class SmsCommunicatorPlugin implements PluginBase {
} }
} }
private void processSms(Sms receivedSms) { private void processSms(final Sms receivedSms) {
if (!isEnabled(PluginBase.GENERAL)) { if (!isEnabled(PluginBase.GENERAL)) {
log.debug("Ignoring SMS. Plugin disabled."); log.debug("Ignoring SMS. Plugin disabled.");
return; return;
@ -477,23 +479,27 @@ public class SmsCommunicatorPlugin implements PluginBase {
bolusWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - bolusWaitingForConfirmation.date.getTime() < CONFIRM_TIMEOUT) { bolusWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - bolusWaitingForConfirmation.date.getTime() < CONFIRM_TIMEOUT) {
bolusWaitingForConfirmation.processed = true; bolusWaitingForConfirmation.processed = true;
PumpInterface pumpInterface = MainApp.getConfigBuilder(); PumpInterface pumpInterface = MainApp.getConfigBuilder();
danaRPlugin = MainApp.getSpecificPlugin(DanaRPlugin.class);
DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo();
detailedBolusInfo.insulin = bolusWaitingForConfirmation.bolusRequested; detailedBolusInfo.insulin = bolusWaitingForConfirmation.bolusRequested;
detailedBolusInfo.source = Source.USER; detailedBolusInfo.source = Source.USER;
PumpEnactResult result = pumpInterface.deliverTreatment(detailedBolusInfo); ConfigBuilderPlugin.getCommandQueue().bolus(detailedBolusInfo, new Callback() {
if (result.success) { @Override
reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_bolusdelivered), result.bolusDelivered); public void run() {
if (danaRPlugin != null) DanaRPlugin danaRPlugin = MainApp.getSpecificPlugin(DanaRPlugin.class);
reply += "\n" + danaRPlugin.shortStatus(true); if (result.success) {
lastRemoteBolusTime = new Date(); String reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_bolusdelivered), result.bolusDelivered);
sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, new Date())); if (danaRPlugin != null)
} else { reply += "\n" + danaRPlugin.shortStatus(true);
reply = MainApp.sResources.getString(R.string.smscommunicator_bolusfailed); lastRemoteBolusTime = new Date();
if (danaRPlugin != null) sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, new Date()));
reply += "\n" + danaRPlugin.shortStatus(true); } else {
sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); String reply = MainApp.sResources.getString(R.string.smscommunicator_bolusfailed);
} if (danaRPlugin != null)
reply += "\n" + danaRPlugin.shortStatus(true);
sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date()));
}
}
});
} else if (tempBasalWaitingForConfirmation != null && !tempBasalWaitingForConfirmation.processed && } else if (tempBasalWaitingForConfirmation != null && !tempBasalWaitingForConfirmation.processed &&
tempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - tempBasalWaitingForConfirmation.date.getTime() < CONFIRM_TIMEOUT) { tempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - tempBasalWaitingForConfirmation.date.getTime() < CONFIRM_TIMEOUT) {
tempBasalWaitingForConfirmation.processed = true; tempBasalWaitingForConfirmation.processed = true;

View file

@ -1,5 +1,6 @@
package info.nightscout.androidaps.plugins.Wear; package info.nightscout.androidaps.plugins.Wear;
import android.content.Intent;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -33,14 +34,17 @@ import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.Actions.dialogs.FillDialog; import info.nightscout.androidaps.plugins.Actions.dialogs.FillDialog;
import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.APSResult;
import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
import info.nightscout.androidaps.plugins.PumpDanaR.comm.RecordTypes; import info.nightscout.androidaps.plugins.PumpDanaR.comm.RecordTypes;
import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanPlugin; import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanPlugin;
import info.nightscout.androidaps.plugins.PumpDanaRv2.DanaRv2Plugin; import info.nightscout.androidaps.plugins.PumpDanaRv2.DanaRv2Plugin;
import info.nightscout.androidaps.queue.Callback;
import info.nightscout.utils.BolusWizard; import info.nightscout.utils.BolusWizard;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.DecimalFormatter;
@ -625,16 +629,13 @@ public class ActionStringHandler {
} }
private static void doFillBolus(final Double amount) { private static void doFillBolus(final Double amount) {
//if(1==1)return; DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo();
Handler handler = new Handler(handlerThread.getLooper()); detailedBolusInfo.insulin = amount;
handler.post(new Runnable() { detailedBolusInfo.isValid = false;
detailedBolusInfo.source = Source.USER;
ConfigBuilderPlugin.getCommandQueue().bolus(detailedBolusInfo, new Callback() {
@Override @Override
public void run() { public void run() {
DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo();
detailedBolusInfo.insulin = amount;
detailedBolusInfo.isValid = false;
detailedBolusInfo.source = Source.USER;
PumpEnactResult result = MainApp.getConfigBuilder().deliverTreatment(detailedBolusInfo);
if (!result.success) { if (!result.success) {
sendError(MainApp.sResources.getString(R.string.treatmentdeliveryerror) + sendError(MainApp.sResources.getString(R.string.treatmentdeliveryerror) +
"\n" + "\n" +
@ -645,16 +646,13 @@ public class ActionStringHandler {
} }
private static void doBolus(final Double amount, final Integer carbs) { private static void doBolus(final Double amount, final Integer carbs) {
//if(1==1)return; DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo();
Handler handler = new Handler(handlerThread.getLooper()); detailedBolusInfo.insulin = amount;
handler.post(new Runnable() { detailedBolusInfo.carbs = carbs;
detailedBolusInfo.source = Source.USER;
ConfigBuilderPlugin.getCommandQueue().bolus(detailedBolusInfo, new Callback() {
@Override @Override
public void run() { public void run() {
DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo();
detailedBolusInfo.insulin = amount;
detailedBolusInfo.carbs = carbs;
detailedBolusInfo.source = Source.USER;
PumpEnactResult result = MainApp.getConfigBuilder().deliverTreatment(detailedBolusInfo);
if (!result.success) { if (!result.success) {
sendError(MainApp.sResources.getString(R.string.treatmentdeliveryerror) + sendError(MainApp.sResources.getString(R.string.treatmentdeliveryerror) +
"\n" + "\n" +