* 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) {
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) {

View file

@ -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("");
}
}
});
}

View file

@ -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";

View file

@ -212,7 +212,7 @@
android:layout_weight="2"
android:gravity="end"
android:paddingRight="5dp"
android:text="Error"
android:text="Last command"
android:textSize="14sp" />
<TextView
@ -226,7 +226,7 @@
android:textSize="14sp" />
<TextView
android:id="@+id/combo_error_message"
android:id="@+id/combo_last_command"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
@ -257,7 +257,7 @@
android:layout_weight="2"
android:gravity="end"
android:paddingRight="5dp"
android:text="Last update"
android:text="Command time"
android:textSize="14sp" />
<TextView
@ -271,7 +271,52 @@
android:textSize="14sp" />
<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_height="wrap_content"
android:layout_weight="1"