diff --git a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java index 8faedd6950..6c760c5297 100644 --- a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java +++ b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java @@ -8,6 +8,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; +import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.NonNull; @@ -19,6 +20,7 @@ import dagger.android.support.DaggerFragment; import info.nightscout.androidaps.combo.R; import info.nightscout.androidaps.interfaces.CommandQueue; import info.nightscout.androidaps.plugins.bus.RxBus; +import info.nightscout.androidaps.plugins.pump.combo.data.ComboErrorUtil; import info.nightscout.androidaps.plugins.pump.combo.events.EventComboPumpUpdateGUI; import info.nightscout.androidaps.plugins.pump.combo.ruffyscripter.PumpState; import info.nightscout.androidaps.plugins.pump.combo.ruffyscripter.history.Bolus; @@ -53,6 +55,12 @@ public class ComboFragment extends DaggerFragment { private TextView bolusCount; private TextView tbrCount; + private View errorCountDelimiter; + private LinearLayout errorCountLayout; + private TextView errorCountLabel; + private TextView errorCountDots; + private TextView errorCountValue; + @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -69,6 +77,12 @@ public class ComboFragment extends DaggerFragment { bolusCount = view.findViewById(R.id.combo_bolus_count); tbrCount = view.findViewById(R.id.combo_tbr_count); + errorCountDelimiter = view.findViewById(R.id.combo_connection_error_delimiter); + errorCountLayout = view.findViewById(R.id.combo_connection_error_layout); + errorCountLabel = view.findViewById(R.id.combo_connection_error_label); + errorCountDots = view.findViewById(R.id.combo_connection_error_dots); + errorCountValue = view.findViewById(R.id.combo_connection_error_value); + refreshButton = view.findViewById(R.id.combo_refresh_button); refreshButton.setOnClickListener(v -> { refreshButton.setEnabled(false); @@ -240,6 +254,47 @@ public class ComboFragment extends DaggerFragment { // stats bolusCount.setText(String.valueOf(comboPlugin.getBolusesDelivered())); tbrCount.setText(String.valueOf(comboPlugin.getTbrsSet())); + + updateErrorDisplay(false); + } else { + updateErrorDisplay(true); } } -} \ No newline at end of file + + private void updateErrorDisplay(boolean forceHide) { + int errorCount = -1; + + ComboErrorUtil errorUtil = ComboErrorUtil.getInstance(); + + if (!forceHide) { + ComboErrorUtil.DisplayType displayType = errorUtil.getDisplayType(); + + if (displayType== ComboErrorUtil.DisplayType.ON_ERROR || displayType== ComboErrorUtil.DisplayType.ALWAYS) { + int errorCountInternal = errorUtil.getErrorCount(); + + if (errorCountInternal>0) { + errorCount = errorCountInternal; + } else if (displayType== ComboErrorUtil.DisplayType.ALWAYS) { + errorCount = 0; + } + } + } + + if (errorCount >=0) { + errorCountDelimiter.setVisibility(View.VISIBLE); + errorCountLayout.setVisibility(View.VISIBLE); + errorCountLabel.setVisibility(View.VISIBLE); + errorCountDots.setVisibility(View.VISIBLE); + errorCountValue.setVisibility(View.VISIBLE); + errorCountValue.setText(errorCount==0 ? + "-" : + ""+errorCount); + } else { + errorCountDelimiter.setVisibility(View.GONE); + errorCountLayout.setVisibility(View.GONE); + errorCountLabel.setVisibility(View.GONE); + errorCountDots.setVisibility(View.GONE); + errorCountValue.setVisibility(View.GONE); + } + } +} diff --git a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java index 72f9040087..29d38d54f3 100644 --- a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java +++ b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java @@ -38,6 +38,7 @@ import info.nightscout.androidaps.interfaces.Pump; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpPluginBase; import info.nightscout.androidaps.interfaces.PumpSync; +import info.nightscout.androidaps.plugins.pump.combo.data.ComboErrorUtil; import info.nightscout.shared.logging.AAPSLogger; import info.nightscout.shared.logging.LTag; import info.nightscout.androidaps.plugins.bus.RxBus; @@ -161,6 +162,7 @@ public class ComboPlugin extends PumpPluginBase implements Pump, Constraints { .pluginIcon(R.drawable.ic_combo_128) .pluginName(R.string.combopump) .shortName(R.string.combopump_shortname) + .preferencesId(R.xml.pref_combo) .description(R.string.description_pump_combo), injector, aapsLogger, rh, commandQueue ); @@ -172,6 +174,9 @@ public class ComboPlugin extends PumpPluginBase implements Pump, Constraints { this.pumpSync = pumpSync; this.dateUtil = dateUtil; + ComboErrorUtil.getInstance().setSP(sp); + ComboErrorUtil.getInstance().clearErrors(); + pumpDescription.fillFor(PumpType.ACCU_CHEK_COMBO); } diff --git a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/data/ComboErrorUtil.java b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/data/ComboErrorUtil.java new file mode 100755 index 0000000000..79ce7b71dc --- /dev/null +++ b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/data/ComboErrorUtil.java @@ -0,0 +1,106 @@ +package info.nightscout.androidaps.plugins.pump.combo.data; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import info.nightscout.androidaps.combo.R; +import info.nightscout.shared.sharedPreferences.SP; + +/** + * Created by andy on 3/17/18. + */ +public class ComboErrorUtil { + + private SP sp; + + Map> errorMap = new HashMap<>(); + + private static final ComboErrorUtil comboDataUtil = new ComboErrorUtil(); + + private ComboErrorUtil() { + } + + public static ComboErrorUtil getInstance() { + return comboDataUtil; + } + + public void setSP(SP sp) { + this.sp = sp; + } + + public void addError(Exception exception) { + String exceptionMsg = exception.getMessage(); + + if (!errorMap.containsKey(exceptionMsg)) { + List list = new ArrayList<>(); + list.add(createErrorState(exception)); + + errorMap.put(exceptionMsg, list); + } else { + errorMap.get(exceptionMsg).add(createErrorState(exception)); + } + + updateErrorCount(); + } + + + private void updateErrorCount() { + int errorCount = 0; + + if (!isErrorPresent()) { + for (List errorStates : errorMap.values()) { + errorCount += errorStates.size(); + } + } + + if (errorCount==0) { + if (sp.contains(R.string.key_combo_error_count)) { + sp.remove(R.string.key_combo_error_count); + } + } else { + sp.putInt(R.string.key_combo_error_count, errorCount); + } + } + + private ErrorState createErrorState(Exception exception) { + ErrorState errorState = new ErrorState(); + errorState.setException(exception); + errorState.setTimeInMillis(System.currentTimeMillis()); + + return errorState; + } + + public void clearErrors() { + if (errorMap != null) + this.errorMap.clear(); + else + this.errorMap = new HashMap<>(); + + if (sp.contains(R.string.key_combo_error_count)) { + sp.remove(R.string.key_combo_error_count); + } + } + + public boolean isErrorPresent() { + return !this.errorMap.isEmpty(); + } + + public int getErrorCount() { + return sp.contains(R.string.key_combo_error_count) ? + sp.getInt(R.string.key_combo_error_count, -1) : -1; + } + + public DisplayType getDisplayType() { + String displayTypeString = sp.getString(R.string.key_show_comm_error_count, "ON_ERROR"); + return DisplayType.valueOf(displayTypeString); + } + + public enum DisplayType { + NEVER, + ON_ERROR, + ALWAYS + } + +} diff --git a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/data/ErrorState.java b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/data/ErrorState.java new file mode 100755 index 0000000000..ec00441309 --- /dev/null +++ b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/data/ErrorState.java @@ -0,0 +1,17 @@ +package info.nightscout.androidaps.plugins.pump.combo.data; + +public class ErrorState { + + private Exception exception; + private long timeInMillis; + + + public void setException(Exception exception) { + this.exception = exception; + } + + + public void setTimeInMillis(long timeInMillis) { + this.timeInMillis = timeInMillis; + } +} diff --git a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ruffyscripter/RuffyScripter.java b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ruffyscripter/RuffyScripter.java index f5aedff356..155c9f45ae 100644 --- a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ruffyscripter/RuffyScripter.java +++ b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ruffyscripter/RuffyScripter.java @@ -25,6 +25,7 @@ import java.util.Date; import java.util.List; import java.util.Objects; +import info.nightscout.androidaps.plugins.pump.combo.data.ComboErrorUtil; import info.nightscout.shared.logging.StacktraceLoggerWrapper; import info.nightscout.androidaps.plugins.pump.combo.ruffyscripter.commands.ReadQuickInfoCommand; import info.nightscout.androidaps.plugins.pump.combo.ruffyscripter.history.PumpHistoryRequest; @@ -188,6 +189,14 @@ public class RuffyScripter implements RuffyCommands { } } + private void addError(Exception e) { + try { + ComboErrorUtil.getInstance().addError(e); + } catch (Exception ex) { + log.error("Combo data util problem." + ex.getMessage(), ex); + } + } + @Override public synchronized void disconnect() { if (ruffyService == null) { @@ -196,10 +205,16 @@ public class RuffyScripter implements RuffyCommands { try { log.debug("Disconnecting"); ruffyService.doRTDisconnect(); + try { + ComboErrorUtil.getInstance().clearErrors(); + } catch (Exception ex) { + log.error("Combo data util problem." + ex.getMessage(), ex); + } } catch (RemoteException e) { // ignore } catch (Exception e) { log.warn("Disconnect not happy", e); + addError(e); } } @@ -262,9 +277,11 @@ public class RuffyScripter implements RuffyCommands { log.debug("Executing " + cmd + " took " + (cmdEndTime - cmdStartTime) + "ms"); } catch (CommandException e) { log.info("CommandException running command", e); + addError(e); cmd.getResult().success = false; } catch (Exception e) { log.error("Unexpected exception running cmd", e); + addError(e); cmd.getResult().success = false; } }, cmd.getClass().getSimpleName()); @@ -329,10 +346,12 @@ public class RuffyScripter implements RuffyCommands { } catch (CommandException e) { log.error("CommandException while executing command", e); PumpState pumpState = recoverFromCommandFailure(); + addError(e); return activeCmd.getResult().success(false).state(pumpState); } catch (Exception e) { log.error("Unexpected exception communication with ruffy", e); PumpState pumpState = recoverFromCommandFailure(); + addError(e); return activeCmd.getResult().success(false).state(pumpState); } finally { Menu menu = this.currentMenu; diff --git a/combo/src/main/res/layout/combopump_fragment.xml b/combo/src/main/res/layout/combopump_fragment.xml index 0ea1d9c0cb..5ca2fe8be9 100644 --- a/combo/src/main/res/layout/combopump_fragment.xml +++ b/combo/src/main/res/layout/combopump_fragment.xml @@ -472,6 +472,55 @@ + + + + + + + + + + + + + + + @string/combo_error_display_never + @string/combo_error_display_error + @string/combo_error_display_always + + + + @string/key_combo_error_display_never + @string/key_combo_error_display_error + @string/key_combo_error_display_always + + \ No newline at end of file diff --git a/combo/src/main/res/values/strings.xml b/combo/src/main/res/values/strings.xml index dd844055ee..314a3e468c 100644 --- a/combo/src/main/res/values/strings.xml +++ b/combo/src/main/res/values/strings.xml @@ -57,4 +57,19 @@ combo_pump_serial combo_tbrs_set combo_boluses_delivered - \ No newline at end of file + Comm. Error count + combo_error_count + combo_name_settings + show_error_count + Show comm. error count + Shows count of errors, when communicating with Ruffy. In most cases number higher than 0 denotes, Ruffy communication problems (restart might be needed). + + Never + When Error + Always + + NEVER + ON_ERROR + ALWAYS + + diff --git a/combo/src/main/res/xml/pref_combo.xml b/combo/src/main/res/xml/pref_combo.xml new file mode 100755 index 0000000000..601bc4f62f --- /dev/null +++ b/combo/src/main/res/xml/pref_combo.xml @@ -0,0 +1,22 @@ + + + + + + + + + + \ No newline at end of file