From 485b99e260418e8dc52086e32947c0c99a8acfe8 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Wed, 19 Jul 2017 01:33:19 +0200 Subject: [PATCH] Show last command and pump state better in the Combo tab. Gets rid of unhelpful "Unknown error" messaegs. --- .../de/jotomo/ruffyscripter/PumpState.java | 6 ++ .../plugins/PumpCombo/ComboFragment.java | 68 +++++++++++-------- .../plugins/PumpCombo/ComboPlugin.java | 40 ++++++----- 3 files changed, 68 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/de/jotomo/ruffyscripter/PumpState.java b/app/src/main/java/de/jotomo/ruffyscripter/PumpState.java index 910f421a14..25d6eea7e8 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/PumpState.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/PumpState.java @@ -11,6 +11,12 @@ public class PumpState { public int tbrPercent = -1; public double tbrRate = -1; public int tbrRemainingDuration = -1; + /** This is the error message (if any) displayed by the pump if there is an alarm, + e.g. if a "TBR cancelled alarm" is active, the value will be "TBR CANCELLED". + Generally, an error code is also displayed, but it flashes and it might take + longer to read that and the pump connection gets interrupted if we're not + reacting quickly. + */ public String errorMsg; public boolean suspended; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java index dd2fe52b6d..0e84a9ada2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboFragment.java @@ -17,6 +17,8 @@ import org.slf4j.LoggerFactory; import de.jotomo.ruffyscripter.PumpState; +import de.jotomo.ruffyscripter.commands.Command; +import de.jotomo.ruffyscripter.commands.CommandResult; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.PumpCombo.events.EventComboPumpUpdateGUI; @@ -31,13 +33,13 @@ public class ComboFragment extends Fragment implements View.OnClickListener { } private Button refresh; - private TextView status; - private TextView tbrPercentage; - private TextView tbrDuration; - private TextView tbrRate; - private TextView lastCmd; - private TextView lastCmdTime; - private TextView lastCmdResult; + private TextView statusText; + private TextView tbrPercentageText; + private TextView tbrDurationText; + private TextView tbrRateText; + private TextView lastCmdText; + private TextView lastCmdTimeText; + private TextView lastCmdResultText; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -45,13 +47,13 @@ public class ComboFragment extends Fragment implements View.OnClickListener { View view = inflater.inflate(R.layout.combopump_fragment, container, false); refresh = (Button) view.findViewById(R.id.combo_refresh); - status = (TextView) view.findViewById(R.id.combo_status); - tbrPercentage = (TextView) view.findViewById(R.id.combo_tbr_percentage); - tbrDuration = (TextView) view.findViewById(R.id.combo_tbr_duration); - tbrRate = (TextView) view.findViewById(R.id.combo_tbr_rate); - lastCmd = (TextView) view.findViewById(R.id.combo_last_command); - lastCmdTime = (TextView) view.findViewById(R.id.combo_last_command_time); - lastCmdResult = (TextView) view.findViewById(R.id.combo_last_command_result); + statusText = (TextView) view.findViewById(R.id.combo_status); + tbrPercentageText = (TextView) view.findViewById(R.id.combo_tbr_percentage); + tbrDurationText = (TextView) view.findViewById(R.id.combo_tbr_duration); + tbrRateText = (TextView) view.findViewById(R.id.combo_tbr_rate); + lastCmdText = (TextView) view.findViewById(R.id.combo_last_command); + lastCmdTimeText = (TextView) view.findViewById(R.id.combo_last_command_time); + lastCmdResultText = (TextView) view.findViewById(R.id.combo_last_command_result); refresh.setOnClickListener(this); @@ -98,27 +100,37 @@ public class ComboFragment extends Fragment implements View.OnClickListener { activity.runOnUiThread(new Runnable() { @Override public void run() { - status.setText(getPlugin().statusSummary); + statusText.setText(getPlugin().statusSummary); if (getPlugin().isInitialized()) { PumpState ps = getPlugin().pumpState; boolean tbrActive = ps.tbrPercent != -1 && ps.tbrPercent != 100; if (tbrActive) { - tbrPercentage.setText("" + ps.tbrPercent + "%"); - tbrDuration.setText("" + ps.tbrRemainingDuration + " min"); - tbrRate.setText("" + ps.tbrRate + " U/h"); + tbrPercentageText.setText("" + ps.tbrPercent + "%"); + tbrDurationText.setText("" + ps.tbrRemainingDuration + " min"); + tbrRateText.setText("" + ps.tbrRate + " U/h"); } else { - tbrPercentage.setText("Default basal rate running"); - tbrDuration.setText(""); - tbrRate.setText("" + getPlugin().getBaseBasalRate() + " U/h"); + tbrPercentageText.setText("Default basal rate running"); + tbrDurationText.setText(""); + tbrRateText.setText("" + getPlugin().getBaseBasalRate() + " U/h"); } - if (getPlugin().lastCmd != null) { - lastCmd.setText("" + getPlugin().lastCmd); - lastCmdTime.setText(ps.timestamp.toLocaleString()); - lastCmdResult.setText(ps.errorMsg == null ? "Success" : ps.errorMsg); + Command lastCmd = getPlugin().lastCmd; + if (lastCmd != null) { + lastCmdText.setText("" + lastCmd); + lastCmdTimeText.setText(getPlugin().lastCmdTime.toLocaleString()); + CommandResult lastCmdResult = getPlugin().lastCmdResult; + if (lastCmdResult != null) { + String message = lastCmdResult.message; + if (ps.errorMsg != null) { + message = message + "\nPump is in error state:\n" + ps.errorMsg; + } + lastCmdResultText.setText(message); + } else { + lastCmdResultText.setText(""); + } } else { - lastCmd.setText(""); - lastCmdTime.setText(""); - lastCmdResult.setText(""); + ComboFragment.this.lastCmdText.setText(""); + lastCmdTimeText.setText(""); + lastCmdResultText.setText(""); } } } 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 c4257c5d3a..ceb38165bb 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 @@ -63,15 +63,17 @@ public class ComboPlugin implements PluginBase, PumpInterface { private RuffyScripter ruffyScripter; private ServiceConnection mRuffyServiceConnection; - @Nullable - volatile Command lastCmd; - @NonNull - private Date lastCmdTime = new Date(0); - @NonNull - volatile PumpState pumpState = new PumpState(); - + // TODO is volatile really needed here? @NonNull volatile String statusSummary = "Initializing"; + @Nullable + volatile Command lastCmd; + @Nullable + volatile CommandResult lastCmdResult; + @NonNull + volatile Date lastCmdTime = new Date(0); + @NonNull + volatile PumpState pumpState = new PumpState(); private static PumpEnactResult OPERATION_NOT_SUPPORTED = new PumpEnactResult(); @@ -260,7 +262,8 @@ public class ComboPlugin implements PluginBase, PumpInterface { @Override public boolean isInitialized() { - // consider initialized when the pump's state was initially fetched + // consider initialized when the pump's state was initially fetched, + // after that lastCmd* variables will have values return lastCmdTime.getTime() > 0; } @@ -372,22 +375,23 @@ public class ComboPlugin implements PluginBase, PumpInterface { CommandResult commandResult = ruffyScripter.runCommand(command); log.debug("RuffyScripter returned from command invocation, result: " + commandResult); + if (commandResult.exception != null) { + log.error("Exception received from pump", commandResult.exception); + } lastCmd = command; lastCmdTime = new Date(); - if (commandResult.success) { - statusSummary = commandResult.state.suspended ? "Suspended" : "Idle"; - pumpState = commandResult.state; + lastCmdResult = commandResult; + pumpState = commandResult.state; + + if (commandResult.success && commandResult.state.suspended) { + statusSummary = "Suspended"; + } else if (commandResult.success) { + statusSummary = "Idle"; } else { statusSummary = "Error"; - pumpState = new PumpState(); - pumpState.errorMsg = commandResult.message != null - ? commandResult.message - : "Unknown error"; - if (commandResult.exception != null) { - log.error("Exception received from pump", commandResult.exception); - } } + MainApp.bus().post(new EventComboPumpUpdateGUI()); return commandResult; }