From 5f43f0f1477e3c2040e134a4f8daf4d6b0d8cb33 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Mon, 17 Jul 2017 14:54:47 +0200 Subject: [PATCH] * Fix stale data in Combo tab after error * Add info about last command ran to the Combo tab * Don't refresh data more than once a minute. * Specify not only error, but also command that raised the error in alert notification --- .../jotomo/ruffyscripter/RuffyScripter.java | 2 +- .../plugins/PumpCombo/ComboFragment.java | 32 +++++++---- .../plugins/PumpCombo/ComboPlugin.java | 26 ++++++--- .../main/res/layout/combopump_fragment.xml | 53 +++++++++++++++++-- 4 files changed, 92 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index b6909e7434..8f3ddf4773 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -187,7 +187,7 @@ public class RuffyScripter { if (currentMenu == null || currentMenu.getType() == MenuType.WARNING_OR_ERROR) { try { PumpState pumpState = null; - try { pumpState = readPumpState(); } catch (Exception e) {} + try { pumpState = readPumpState(); } catch (Exception e) { /* We tried ... */ } returnable.cmdResult = new CommandResult().message("Pump is in an error state: " + currentMenu.getAttribute(MenuAttribute.MESSAGE)).state(pumpState); return; } catch (Exception e) { 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 c060c4cd1e..83cc32a9e0 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 @@ -35,8 +35,9 @@ public class ComboFragment extends Fragment implements View.OnClickListener { private TextView tbrPercentage; private TextView tbrDurationRemaining; private TextView tbrRate; - private TextView errorMsg; - private TextView lastUpdate; + private TextView lastCmd; + private TextView lastCmdTime; + private TextView lastCmdResult; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -48,13 +49,20 @@ public class ComboFragment extends Fragment implements View.OnClickListener { tbrPercentage = (TextView) view.findViewById(R.id.combo_tbr_percentage); tbrDurationRemaining = (TextView) view.findViewById(R.id.combo_tbr_duration_remaining); tbrRate = (TextView) view.findViewById(R.id.combo_tbr_rate); - errorMsg = (TextView) view.findViewById(R.id.combo_error_message); - lastUpdate = (TextView) view.findViewById(R.id.combo_last_update); + 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); + + status.setText("Initializing"); + tbrPercentage.setText(""); + tbrDurationRemaining.setText(""); + tbrRate.setText(""); + lastCmd.setText(""); + lastCmdTime.setText(""); + lastCmdResult.setText(""); refresh.setOnClickListener(this); - status.setText("Initializing"); - updateGUI(); return view; } @@ -75,7 +83,6 @@ public class ComboFragment extends Fragment implements View.OnClickListener { updateGUI(); } - @Override public void onClick(View view) { switch (view.getId()) { @@ -111,8 +118,15 @@ public class ComboFragment extends Fragment implements View.OnClickListener { tbrDurationRemaining.setText(""); tbrRate.setText("" + getPlugin().getBaseBasalRate() + " U/h"); } - errorMsg.setText(ps.errorMsg != null ? ps.errorMsg : ""); - lastUpdate.setText(ps.timestamp.toLocaleString()); + if (getPlugin().lastCmd != null) { + lastCmd.setText("" + getPlugin().lastCmd); + lastCmdTime.setText(ps.timestamp.toLocaleString()); + lastCmdResult.setText(ps.errorMsg == null ? "Success" : ps.errorMsg); + } else { + lastCmd.setText(""); + lastCmdTime.setText(""); + lastCmdResult.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 1bdb0409ab..166f3918a3 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 @@ -11,6 +11,7 @@ import android.net.Uri; import android.os.IBinder; import android.os.SystemClock; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.app.NotificationCompat; import com.squareup.otto.Subscribe; @@ -60,9 +61,12 @@ public class ComboPlugin implements PluginBase, PumpInterface { private PumpDescription pumpDescription = new PumpDescription(); private RuffyScripter ruffyScripter; - private Date lastCmdTime = new Date(0); private ServiceConnection mRuffyServiceConnection; + @Nullable + volatile Command lastCmd; + @NonNull + private Date lastCmdTime = new Date(0); @NonNull volatile PumpState pumpState = new PumpState(); @@ -79,8 +83,8 @@ public class ComboPlugin implements PluginBase, PumpInterface { public ComboPlugin() { definePumpCapabilities(); - bindRuffyService(); MainApp.bus().register(this); + bindRuffyService(); } private void bindRuffyService() { @@ -137,6 +141,8 @@ public class ComboPlugin implements PluginBase, PumpInterface { if (errorMsg != null) if (now > sixMinutesSinceLastAlarm) { log.warn("Pump is in error state, raising alert: " + errorMsg); + log.warn(" LastCmd: " + lastCmd); + log.warn(" LastCmdTime: " + lastCmdTime); long[] vibratePattern = new long[]{1000, 2000, 1000, 2000, 1000, 2000, 1000, 2000, 1000, 2000}; Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); NotificationCompat.Builder notificationBuilder = @@ -144,7 +150,9 @@ public class ComboPlugin implements PluginBase, PumpInterface { .setSmallIcon(R.drawable.notif_icon) .setSmallIcon(R.drawable.icon_bolus) .setContentTitle("Combo communication error") - .setContentText(errorMsg) + .setContentText("Error: " + errorMsg + + "\nCommand: " + lastCmd + + "\nTime: " + lastCmdTime) .setPriority(NotificationCompat.PRIORITY_MAX) .setLights(Color.BLUE, 1000, 0) .setSound(uri) @@ -283,14 +291,16 @@ public class ComboPlugin implements PluginBase, PumpInterface { return lastCmdTime; } - // TODO + // this method is regularly called from info.nightscout.androidaps.receivers.KeepAliveReceiver @Override public void refreshDataFromPump(String reason) { log.debug("RefreshDataFromPump called"); - // this is called regulary from keepalive - - runCommand(new ReadPumpStateCommand()); + if (lastCmdTime.getTime() > 0 && System.currentTimeMillis() > lastCmdTime.getTime() + 60 * 1000) { + log.debug("Not fetching state from pump, since we did already within the last 60 seconds"); + } else { + runCommand(new ReadPumpStateCommand()); + } } // TODO uses profile values for the time being @@ -355,12 +365,14 @@ public class ComboPlugin implements PluginBase, PumpInterface { CommandResult commandResult = ruffyScripter.runCommand(command); log.debug("RuffyScripter returned from command invocation, result: " + commandResult); + lastCmd = command; lastCmdTime = new Date(); if (commandResult.success) { statusSummary = "Idle"; pumpState = commandResult.state; } else { statusSummary = "Command failed: " + command; + pumpState = new PumpState(); pumpState.errorMsg = commandResult.message != null ? commandResult.message : "Unknown error"; diff --git a/app/src/main/res/layout/combopump_fragment.xml b/app/src/main/res/layout/combopump_fragment.xml index 634c7be6fd..8df7c83121 100644 --- a/app/src/main/res/layout/combopump_fragment.xml +++ b/app/src/main/res/layout/combopump_fragment.xml @@ -212,7 +212,7 @@ android:layout_weight="2" android:gravity="end" android:paddingRight="5dp" - android:text="Error" + android:text="Last command" android:textSize="14sp" /> + + + + + + + + + + + +