* 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
This commit is contained in:
Johannes Mockenhaupt 2017-07-17 14:54:47 +02:00
parent 4dc38e447a
commit 5f43f0f147
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
4 changed files with 92 additions and 21 deletions

View file

@ -187,7 +187,7 @@ public class RuffyScripter {
if (currentMenu == null || currentMenu.getType() == MenuType.WARNING_OR_ERROR) { if (currentMenu == null || currentMenu.getType() == MenuType.WARNING_OR_ERROR) {
try { try {
PumpState pumpState = null; 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); returnable.cmdResult = new CommandResult().message("Pump is in an error state: " + currentMenu.getAttribute(MenuAttribute.MESSAGE)).state(pumpState);
return; return;
} catch (Exception e) { } catch (Exception e) {

View file

@ -35,8 +35,9 @@ public class ComboFragment extends Fragment implements View.OnClickListener {
private TextView tbrPercentage; private TextView tbrPercentage;
private TextView tbrDurationRemaining; private TextView tbrDurationRemaining;
private TextView tbrRate; private TextView tbrRate;
private TextView errorMsg; private TextView lastCmd;
private TextView lastUpdate; private TextView lastCmdTime;
private TextView lastCmdResult;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 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); tbrPercentage = (TextView) view.findViewById(R.id.combo_tbr_percentage);
tbrDurationRemaining = (TextView) view.findViewById(R.id.combo_tbr_duration_remaining); tbrDurationRemaining = (TextView) view.findViewById(R.id.combo_tbr_duration_remaining);
tbrRate = (TextView) view.findViewById(R.id.combo_tbr_rate); tbrRate = (TextView) view.findViewById(R.id.combo_tbr_rate);
errorMsg = (TextView) view.findViewById(R.id.combo_error_message); lastCmd = (TextView) view.findViewById(R.id.combo_last_command);
lastUpdate = (TextView) view.findViewById(R.id.combo_last_update); 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); refresh.setOnClickListener(this);
status.setText("Initializing");
updateGUI();
return view; return view;
} }
@ -75,7 +83,6 @@ public class ComboFragment extends Fragment implements View.OnClickListener {
updateGUI(); updateGUI();
} }
@Override @Override
public void onClick(View view) { public void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
@ -111,8 +118,15 @@ public class ComboFragment extends Fragment implements View.OnClickListener {
tbrDurationRemaining.setText(""); tbrDurationRemaining.setText("");
tbrRate.setText("" + getPlugin().getBaseBasalRate() + " U/h"); tbrRate.setText("" + getPlugin().getBaseBasalRate() + " U/h");
} }
errorMsg.setText(ps.errorMsg != null ? ps.errorMsg : ""); if (getPlugin().lastCmd != null) {
lastUpdate.setText(ps.timestamp.toLocaleString()); lastCmd.setText("" + getPlugin().lastCmd);
lastCmdTime.setText(ps.timestamp.toLocaleString());
lastCmdResult.setText(ps.errorMsg == null ? "Success" : ps.errorMsg);
} else {
lastCmd.setText("");
lastCmdTime.setText("");
lastCmdResult.setText("");
}
} }
}); });
} }

View file

@ -11,6 +11,7 @@ import android.net.Uri;
import android.os.IBinder; import android.os.IBinder;
import android.os.SystemClock; import android.os.SystemClock;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
@ -60,9 +61,12 @@ public class ComboPlugin implements PluginBase, PumpInterface {
private PumpDescription pumpDescription = new PumpDescription(); private PumpDescription pumpDescription = new PumpDescription();
private RuffyScripter ruffyScripter; private RuffyScripter ruffyScripter;
private Date lastCmdTime = new Date(0);
private ServiceConnection mRuffyServiceConnection; private ServiceConnection mRuffyServiceConnection;
@Nullable
volatile Command lastCmd;
@NonNull
private Date lastCmdTime = new Date(0);
@NonNull @NonNull
volatile PumpState pumpState = new PumpState(); volatile PumpState pumpState = new PumpState();
@ -79,8 +83,8 @@ public class ComboPlugin implements PluginBase, PumpInterface {
public ComboPlugin() { public ComboPlugin() {
definePumpCapabilities(); definePumpCapabilities();
bindRuffyService();
MainApp.bus().register(this); MainApp.bus().register(this);
bindRuffyService();
} }
private void bindRuffyService() { private void bindRuffyService() {
@ -137,6 +141,8 @@ public class ComboPlugin implements PluginBase, PumpInterface {
if (errorMsg != null) if (errorMsg != null)
if (now > sixMinutesSinceLastAlarm) { if (now > sixMinutesSinceLastAlarm) {
log.warn("Pump is in error state, raising alert: " + errorMsg); 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}; long[] vibratePattern = new long[]{1000, 2000, 1000, 2000, 1000, 2000, 1000, 2000, 1000, 2000};
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
NotificationCompat.Builder notificationBuilder = NotificationCompat.Builder notificationBuilder =
@ -144,7 +150,9 @@ public class ComboPlugin implements PluginBase, PumpInterface {
.setSmallIcon(R.drawable.notif_icon) .setSmallIcon(R.drawable.notif_icon)
.setSmallIcon(R.drawable.icon_bolus) .setSmallIcon(R.drawable.icon_bolus)
.setContentTitle("Combo communication error") .setContentTitle("Combo communication error")
.setContentText(errorMsg) .setContentText("Error: " + errorMsg +
"\nCommand: " + lastCmd +
"\nTime: " + lastCmdTime)
.setPriority(NotificationCompat.PRIORITY_MAX) .setPriority(NotificationCompat.PRIORITY_MAX)
.setLights(Color.BLUE, 1000, 0) .setLights(Color.BLUE, 1000, 0)
.setSound(uri) .setSound(uri)
@ -283,15 +291,17 @@ public class ComboPlugin implements PluginBase, PumpInterface {
return lastCmdTime; return lastCmdTime;
} }
// TODO // this method is regularly called from info.nightscout.androidaps.receivers.KeepAliveReceiver
@Override @Override
public void refreshDataFromPump(String reason) { public void refreshDataFromPump(String reason) {
log.debug("RefreshDataFromPump called"); log.debug("RefreshDataFromPump called");
// this is called regulary from keepalive 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()); runCommand(new ReadPumpStateCommand());
} }
}
// TODO uses profile values for the time being // TODO uses profile values for the time being
// this get's called mulitple times a minute, must absolutely be cached // this get's called mulitple times a minute, must absolutely be cached
@ -355,12 +365,14 @@ public class ComboPlugin implements PluginBase, PumpInterface {
CommandResult commandResult = ruffyScripter.runCommand(command); CommandResult commandResult = ruffyScripter.runCommand(command);
log.debug("RuffyScripter returned from command invocation, result: " + commandResult); log.debug("RuffyScripter returned from command invocation, result: " + commandResult);
lastCmd = command;
lastCmdTime = new Date(); lastCmdTime = new Date();
if (commandResult.success) { if (commandResult.success) {
statusSummary = "Idle"; statusSummary = "Idle";
pumpState = commandResult.state; pumpState = commandResult.state;
} else { } else {
statusSummary = "Command failed: " + command; statusSummary = "Command failed: " + command;
pumpState = new PumpState();
pumpState.errorMsg = commandResult.message != null pumpState.errorMsg = commandResult.message != null
? commandResult.message ? commandResult.message
: "Unknown error"; : "Unknown error";

View file

@ -212,7 +212,7 @@
android:layout_weight="2" android:layout_weight="2"
android:gravity="end" android:gravity="end"
android:paddingRight="5dp" android:paddingRight="5dp"
android:text="Error" android:text="Last command"
android:textSize="14sp" /> android:textSize="14sp" />
<TextView <TextView
@ -226,7 +226,7 @@
android:textSize="14sp" /> android:textSize="14sp" />
<TextView <TextView
android:id="@+id/combo_error_message" android:id="@+id/combo_last_command"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
@ -257,7 +257,7 @@
android:layout_weight="2" android:layout_weight="2"
android:gravity="end" android:gravity="end"
android:paddingRight="5dp" android:paddingRight="5dp"
android:text="Last update" android:text="Command time"
android:textSize="14sp" /> android:textSize="14sp" />
<TextView <TextView
@ -271,7 +271,52 @@
android:textSize="14sp" /> android:textSize="14sp" />
<TextView <TextView
android:id="@+id/combo_last_update" android:id="@+id/combo_last_command_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:paddingLeft="5dp"
android:textColor="@android:color/white"
android:textSize="14sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:background="@color/listdelimiter" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="end"
android:paddingRight="5dp"
android:text="Command result"
android:textSize="14sp" />
<TextView
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:paddingEnd="2dp"
android:paddingStart="2dp"
android:text=":"
android:textSize="14sp" />
<TextView
android:id="@+id/combo_last_command_result"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"