From 52c6d209af63363f169b91db1d53a813ca262776 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Wed, 28 Mar 2018 19:33:41 +0200 Subject: [PATCH] Clean up initial profile push to pump. --- .../Dialogs/NewNSTreatmentDialog.java | 32 ------------------- .../ConfigBuilder/ConfigBuilderPlugin.java | 28 ++++++++++++++++ .../androidaps/queue/CommandQueue.java | 17 +--------- .../receivers/KeepAliveReceiver.java | 7 ++-- .../androidaps/queue/CommandQueueTest.java | 9 +++--- 5 files changed, 37 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java index 7aea403941..9c2f5e5e22 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java @@ -741,22 +741,6 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick profileSwitch.timeshift = timeshift; profileSwitch.percentage = percentage; MainApp.getConfigBuilder().addToHistoryProfileSwitch(profileSwitch); - - ConfigBuilderPlugin.getCommandQueue().setProfile(profileSwitch.getProfileObject(), new Callback() { - @Override - public void run() { - if (!result.success) { - Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); - i.putExtra("soundid", R.raw.boluserror); - i.putExtra("status", result.comment); - i.putExtra("title", MainApp.sResources.getString(R.string.failedupdatebasalprofile)); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - MainApp.instance().startActivity(i); - } - MainApp.bus().post(new EventNewBasalProfile()); - } - }); - FabricPrivacy.getInstance().logCustom(new CustomEvent("ProfileSwitch")); } public static void doProfileSwitch(final int duration, final int percentage, final int timeshift) { @@ -773,22 +757,6 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick profileSwitch.timeshift = timeshift; profileSwitch.percentage = percentage; MainApp.getConfigBuilder().addToHistoryProfileSwitch(profileSwitch); - - ConfigBuilderPlugin.getCommandQueue().setProfile(profileSwitch.getProfileObject(), new Callback() { - @Override - public void run() { - if (!result.success) { - Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); - i.putExtra("soundid", R.raw.boluserror); - i.putExtra("status", result.comment); - i.putExtra("title", MainApp.sResources.getString(R.string.failedupdatebasalprofile)); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - MainApp.instance().startActivity(i); - } - MainApp.bus().post(new EventNewBasalProfile()); - } - }); - FabricPrivacy.getInstance().logCustom(new CustomEvent("ProfileSwitch")); } else { log.error("No profile switch existing"); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 51d91732ca..5bba016848 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -1,10 +1,12 @@ package info.nightscout.androidaps.plugins.ConfigBuilder; +import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.support.annotation.Nullable; import com.crashlytics.android.answers.CustomEvent; +import com.squareup.otto.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,6 +35,8 @@ import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventAppInitialized; +import info.nightscout.androidaps.events.EventNewBasalProfile; +import info.nightscout.androidaps.events.EventProfileSwitchChange; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.Constraint; @@ -44,11 +48,13 @@ import info.nightscout.androidaps.interfaces.SensitivityInterface; import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.plugins.Loop.APSResult; 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.notifications.Notification; import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.CommandQueue; +import info.nightscout.androidaps.queue.commands.Command; import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.NSUpload; import info.nightscout.utils.ToastUtils; @@ -643,6 +649,28 @@ public class ConfigBuilderPlugin implements PluginBase, TreatmentsInterface { NSUpload.uploadProfileSwitch(profileSwitch); } + @Subscribe + public void onProfileSwitch(EventProfileSwitchChange ignored) { + if (getCommandQueue().isRunning(Command.CommandType.BASALPROFILE)) + return; + + getCommandQueue().setProfile(getProfile(), new Callback() { + @Override + public void run() { + if (!result.success) { + Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); + i.putExtra("soundid", R.raw.boluserror); + i.putExtra("status", result.comment); + i.putExtra("title", MainApp.sResources.getString(R.string.failedupdatebasalprofile)); + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + MainApp.instance().startActivity(i); + } + MainApp.bus().post(new EventNewBasalProfile()); + } + }); + FabricPrivacy.getInstance().logCustom(new CustomEvent("ProfileSwitch")); + } + @Override public long oldestDataAvailable() { return activeTreatments.oldestDataAvailable(); diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java index d8d4102828..b343cbde3e 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java @@ -84,7 +84,7 @@ public class CommandQueue { return new PumpEnactResult().success(false).enacted(false).comment(MainApp.sResources.getString(R.string.executingrightnow)); } - private boolean isRunning(Command.CommandType type) { + public boolean isRunning(Command.CommandType type) { if (performing != null && performing.commandType == type) return true; return false; @@ -296,21 +296,6 @@ public class CommandQueue { return false; } - // Check that there is a valid profileSwitch NOW - if (MainApp.getConfigBuilder().getProfileSwitchFromHistory(System.currentTimeMillis())==null) { - // wait for DatabaseHelper.scheduleProfiSwitch() to do the profile switch // TODO clean this crap up - SystemClock.sleep(5000); - if (MainApp.getConfigBuilder().getProfileSwitchFromHistory(System.currentTimeMillis())==null) { - Notification noProfileSwitchNotif = new Notification(Notification.PROFILE_SWITCH_MISSING, MainApp.gs(R.string.profileswitch_ismissing), Notification.NORMAL); - MainApp.bus().post(new EventNewNotification(noProfileSwitchNotif)); - if (callback != null) { - PumpEnactResult result = new PumpEnactResult().success(false).enacted(false).comment("Refuse to send profile to pump! No ProfileSwitch!"); - callback.result(result).run(); - } - return false; - } - } - if (!MainApp.isEngineeringModeOrRelease()) { Notification notification = new Notification(Notification.NOT_ENG_MODE_OR_RELEASE, MainApp.sResources.getString(R.string.not_eng_mode_or_release), Notification.URGENT); MainApp.bus().post(new EventNewNotification(notification)); diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java index 810bf124c9..87f1f5a7b0 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java @@ -18,6 +18,7 @@ import java.util.Date; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.events.EventProfileSwitchChange; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; @@ -60,11 +61,11 @@ public class KeepAliveReceiver extends BroadcastReceiver { LocalAlertUtils.checkPumpUnreachableAlarm(lastConnection, isStatusOutdated); if (!pump.isThisProfileSet(profile)) { - MainApp.getConfigBuilder().getCommandQueue().setProfile(profile, null); + MainApp.bus().post(new EventProfileSwitchChange()); } else if (isStatusOutdated && !pump.isBusy()) { - MainApp.getConfigBuilder().getCommandQueue().readStatus("KeepAlive. Status outdated.", null); + ConfigBuilderPlugin.getCommandQueue().readStatus("KeepAlive. Status outdated.", null); } else if (isBasalOutdated && !pump.isBusy()) { - MainApp.getConfigBuilder().getCommandQueue().readStatus("KeepAlive. Basal outdated.", null); + ConfigBuilderPlugin.getCommandQueue().readStatus("KeepAlive. Basal outdated.", null); } } } diff --git a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java index 9f89fe1ec0..c4a73ccd04 100644 --- a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java +++ b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java @@ -88,18 +88,17 @@ public class CommandQueueTest extends CommandQueue { extendedBolus(1, 30, null); Assert.assertEquals(2, size()); - // add setProfile (command is not queued before unless a ProfileSwitch exists) - // TODO test with profile switch set + // add setProfile setProfile(profile, null); - Assert.assertEquals(2, size()); + Assert.assertEquals(3, size()); // add loadHistory loadHistory((byte) 0, null); - Assert.assertEquals(3, size()); + Assert.assertEquals(4, size()); // add loadEvents loadEvents(null); - Assert.assertEquals(4, size()); + Assert.assertEquals(5, size()); clear(); tempBasalAbsolute(0, 30, true, profile, null);