From 22bac709fb5b73532155264886d72954c235fd1a Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Wed, 25 Oct 2017 18:15:59 +0200 Subject: [PATCH] Cleanups around ComboPlugin.refreshDataFromPump. --- .../plugins/PumpCombo/ComboPlugin.java | 53 +++++++------------ .../ruffyscripter/RuffyCommandsV1Impl.java | 2 + 2 files changed, 20 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java index 5ca8efefda..99603853f6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java @@ -57,7 +57,8 @@ public class ComboPlugin implements PluginBase, PumpInterface { private PumpDescription pumpDescription = new PumpDescription(); - private RuffyCommands ruffyScripter; + @NonNull + private final RuffyCommands ruffyScripter; // TODO access to pump (and its members) is chaotic and needs an update private ComboPump pump = new ComboPump(); @@ -80,7 +81,6 @@ public class ComboPlugin implements PluginBase, PumpInterface { private ComboPlugin() { definePumpCapabilities(); - MainApp.bus().register(this); ruffyScripter = RuffyCommandsV1Impl.getInstance(MainApp.instance()); } @@ -209,6 +209,22 @@ public class ComboPlugin implements PluginBase, PumpInterface { return lastCmdResult != null ? new Date(lastCmdResult.completionTime) : new Date(0); } + @Override + public synchronized void refreshDataFromPump(String reason) { + log.debug("RefreshDataFromPump called"); + + if (pump.lastCmdResult == null) { + initializePump(); + } else { + runCommand("Refreshing", new CommandExecution() { + @Override + public CommandResult execute() { + return ruffyScripter.readHistory(new PumpHistoryRequest().reservoirLevel(true).bolusHistory(PumpHistoryRequest.LAST)); + } + }); + } + } + private void initializePump() { CommandResult commandResult = runCommand("Checking pump history", false, new CommandExecution() { @Override @@ -285,39 +301,6 @@ public class ComboPlugin implements PluginBase, PumpInterface { } - // this method is regularly called from info.nightscout.androidaps.receivers.KeepAliveReceiver - // TODO check this is eithor called regularly even with other commansd being fired; if not, - // request this periodically - @Override - public synchronized void refreshDataFromPump(String reason) { - log.debug("RefreshDataFromPump called"); - - // if Android is sluggish this might get called before ruffy is bound - if (!ruffyScripter.isPumpAvailable()) { - log.error("Rejecting call to RefreshDataFromPump: scripter not ready yet."); - return; - } - - // TODO -// boolean notAUserRequest = !reason.toLowerCase().contains("user"); -// boolean wasRunAtLeastOnce = pump.lastCmdResult != null; -// boolean ranWithinTheLastMinute = wasRunAtLeastOnce && System.currentTimeMillis() < pump.lastCmdResult.completionTime + 60 * 1000; -// if (notAUserRequest && wasRunAtLeastOnce && ranWithinTheLastMinute) { -// log.debug("Not fetching state from pump, since we did already within the last 60 seconds"); -// } else { - - if (pump.lastCmdResult == null) { - initializePump(); - } else { - runCommand("Refreshing", new CommandExecution() { - @Override - public CommandResult execute() { - return ruffyScripter.readHistory(new PumpHistoryRequest().reservoirLevel(true).bolusHistory(PumpHistoryRequest.LAST)); - } - }); - } - } - // TODO uses profile values for the time being // this get's called multiple times a minute, must absolutely be cached @Override diff --git a/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyCommandsV1Impl.java b/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyCommandsV1Impl.java index 8d4d9b9db6..421e9dd4f0 100644 --- a/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyCommandsV1Impl.java +++ b/ruffyscripter/src/main/java/de/jotomo/ruffyscripter/RuffyCommandsV1Impl.java @@ -1,6 +1,7 @@ package de.jotomo.ruffyscripter; import android.content.Context; +import android.support.annotation.NonNull; import java.util.Date; @@ -14,6 +15,7 @@ import de.jotomo.ruffy.spi.history.PumpHistoryRequest; public class RuffyCommandsV1Impl implements RuffyCommands { private static RuffyCommands delegate; + @NonNull public static RuffyCommands getInstance(Context context) { if (delegate == null) delegate = new RuffyScripter(context); return delegate;