From b47559647f407b09887c42b8a3ac67e34ee63fb5 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Fri, 27 Oct 2017 00:03:01 +0200 Subject: [PATCH] wip --- Combo-Setup.md => README-Combo.md | 15 +++ .../info/nightscout/androidaps/Constants.java | 4 +- .../plugins/PumpCombo/ComboFragment.java | 57 ++++++----- .../plugins/PumpCombo/ComboPlugin.java | 57 ++++++----- .../plugins/PumpCombo/ComboPump.java | 15 +-- .../events/EventComboPumpUpdateGUI.java | 7 -- .../receivers/KeepAliveReceiver.java | 1 + .../main/res/layout/combopump_fragment.xml | 73 +++++++++++--- app/src/main/res/values/strings.xml | 2 +- .../java/de/jotomo/ruffy/spi/PumpState.java | 19 ++-- .../de/jotomo/ruffy/spi/RuffyCommands.java | 61 +++++++++++- .../history/{Error.java => PumpError.java} | 4 +- .../jotomo/ruffy/spi/history/PumpHistory.java | 9 +- .../ruffy/spi/history/PumpHistoryRequest.java | 6 +- .../jotomo/ruffyscripter/RuffyScripter.java | 70 +++++--------- .../ruffyscripter/commands/BolusCommand.java | 5 + .../commands/ReadHistoryCommand.java | 94 +++++++++++++------ .../commands/TakeOverAlarmsCommand.java | 50 ++++++++++ 18 files changed, 364 insertions(+), 185 deletions(-) rename Combo-Setup.md => README-Combo.md (89%) rename ruffy-spi/src/main/java/de/jotomo/ruffy/spi/history/{Error.java => PumpError.java} (76%) create mode 100644 ruffyscripter/src/main/java/de/jotomo/ruffyscripter/commands/TakeOverAlarmsCommand.java diff --git a/Combo-Setup.md b/README-Combo.md similarity index 89% rename from Combo-Setup.md rename to README-Combo.md index e009eb5909..e8f55d03a9 100644 --- a/Combo-Setup.md +++ b/README-Combo.md @@ -52,3 +52,18 @@ Testing: - Try to reproduce and open a ticket, add tag if any, otherwise add the hash of the commit used (right-click on the branch name select _Copy revision number_ or use _git show_ on the command-line) the branch name. Attach the log to the issue and label it as a bug. The logs can be found in _/storage/emulated/0/Android/data/info.nightscout.androidaps/_ + +v2 usage +- When a BOLUS/TBR CANCELLED alert starts on the pump during bolusing or setting a TBR, this is caused by disconnect + between pump and phone. The app will try to reconnect and confirm the alert and then retry the last action. Therefore, + such an alarm shall be ignored (cancelling it is not a big issue, but will lead to the currently active action to + have to wait till the pump's display turns off before it can reconnect to the pump). + If the pump's alarm continues, the last action might have failed, in which case the user needs to confirm the alarm + +v3 TODOs +- Reading and displaying TDDs + +Maybes: +- reading/writing basal profiles to pump +- splitted bolus/slower bolus delivery +- extended bolus support diff --git a/app/src/main/java/info/nightscout/androidaps/Constants.java b/app/src/main/java/info/nightscout/androidaps/Constants.java index e37a5b4b9f..a2a5e58761 100644 --- a/app/src/main/java/info/nightscout/androidaps/Constants.java +++ b/app/src/main/java/info/nightscout/androidaps/Constants.java @@ -1,7 +1,5 @@ package info.nightscout.androidaps; -import com.j256.ormlite.stmt.query.In; - /** * Created by mike on 07.06.2016. */ @@ -24,7 +22,7 @@ public class Constants { public static final int hoursToKeepInDatabase = 72; public static final int daysToKeepHistoryInDatabase = 30; - public static final long keepAliveMsecs = 5 * 60 * 1000L; + public static final long keepAliveMsecs = 60 * 1000L; // SMS COMMUNICATOR public static final long remoteBolusMinDistance = 15 * 60 * 1000L; 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 dcf1da68d1..3aa3930d08 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 @@ -30,7 +30,8 @@ import info.nightscout.utils.DecimalFormatter; public class ComboFragment extends SubscriberFragment implements View.OnClickListener { private static Logger log = LoggerFactory.getLogger(ComboFragment.class); - private TextView statusView; + private TextView stateView; + private TextView activityView; private TextView batteryView; private TextView reservoirView; private TextView lastConnectionView; @@ -44,7 +45,8 @@ public class ComboFragment extends SubscriberFragment implements View.OnClickLis Bundle savedInstanceState) { View view = inflater.inflate(R.layout.combopump_fragment, container, false); - statusView = (TextView) view.findViewById(R.id.combo_status); + stateView = (TextView) view.findViewById(R.id.combo_state); + activityView = (TextView) view.findViewById(R.id.combo_activity); batteryView = (TextView) view.findViewById(R.id.combo_pumpstate_battery); reservoirView = (TextView) view.findViewById(R.id.combo_insulinstate); lastConnectionView = (TextView) view.findViewById(R.id.combo_lastconnection); @@ -70,52 +72,48 @@ public class ComboFragment extends SubscriberFragment implements View.OnClickLis }); thread.start(); break; - case R.id.combo_history: - // TODO show popup with warnings/errors from the pump + case R.id.combo_error_history: + // TODO show popup with pump errors and comm problems break; case R.id.combo_stats: - // TODO show TDD stats from the pump + // TODO show TDD stats from the pump (later) break; } } @Subscribe public void onStatusEvent(final EventComboPumpUpdateGUI ev) { - if (ev.status != null) { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - statusView.setText(ev.status); - statusView.setTextColor(Color.WHITE); - } - }); - } else { - updateGUI(); - } + updateGUI(); } public void updateGUI() { - Activity activity = getActivity(); + final Activity activity = getActivity(); + log.debug("aCtI: activity available? " + (activity != null)); if (activity != null) activity.runOnUiThread(new Runnable() { @Override public void run() { ComboPlugin plugin = ComboPlugin.getPlugin(); + + // activity + String activity = plugin.getPump().activity; + activityView.setText(activity != null ? activity : "Idle"); + if (plugin.isInitialized()) { // status - statusView.setText(plugin.getPump().state.getStateSummary()); - if (plugin.getPump().state.errorMsg != null) { - statusView.setTextColor(Color.RED); + PumpState ps = plugin.getPump().state; + stateView.setText(plugin.getPump().state.getStateSummary()); + if (plugin.getPump().state.errorMsg != null + || ps.insulinState == PumpState.EMPTY + || ps.batteryState == PumpState.EMPTY) { + stateView.setTextColor(Color.RED); } else if (plugin.getPump().state.suspended) { - statusView.setTextColor(Color.YELLOW); + stateView.setTextColor(Color.YELLOW); } else { - statusView.setTextColor(Color.WHITE); + stateView.setTextColor(Color.WHITE); } // battery - PumpState ps = plugin.getPump().state; if (ps.batteryState == PumpState.EMPTY) { batteryView.setText("{fa-battery-empty}"); batteryView.setTextColor(Color.RED); @@ -143,15 +141,14 @@ public class ComboFragment extends SubscriberFragment implements View.OnClickLis if (lastCmdResult != null) { String minAgo = DateUtil.minAgo(lastCmdResult.completionTime); String time = DateUtil.timeString(lastCmdResult.completionTime); + // TODO must not be within if (lastCmdResult) so we can complain if NO command ever worked; also move from completionTime to new times + // TODO check all access to completionTime. useful anymore? if (plugin.getPump().lastSuccessfulConnection < System.currentTimeMillis() + 30 * 60 * 1000) { - lastConnectionView.setText( - "No successful connection" + - "\nwithin the last " + minAgo + " min"); + lastConnectionView.setText("No connection for " + minAgo + " min"); lastConnectionView.setTextColor(Color.RED); } if (plugin.getPump().lastConnectionAttempt > plugin.getPump().lastSuccessfulConnection) { - lastConnectionView.setText("" + minAgo + " (" + time + ")" + - "\nLast connect attempt failed"); + lastConnectionView.setText("Last connect attempt failed"); lastConnectionView.setTextColor(Color.YELLOW); } else { lastConnectionView.setText("" + minAgo + " (" + time + ")"); 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 99603853f6..d53f4055d7 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 @@ -1,13 +1,7 @@ package info.nightscout.androidaps.plugins.PumpCombo; -import android.app.NotificationManager; -import android.content.Context; -import android.graphics.Color; -import android.media.RingtoneManager; -import android.net.Uri; import android.os.SystemClock; import android.support.annotation.NonNull; -import android.support.v4.app.NotificationCompat; import org.json.JSONObject; import org.slf4j.Logger; @@ -22,7 +16,7 @@ import de.jotomo.ruffy.spi.CommandResult; import de.jotomo.ruffy.spi.PumpState; import de.jotomo.ruffy.spi.RuffyCommands; import de.jotomo.ruffy.spi.history.Bolus; -import de.jotomo.ruffy.spi.history.Error; +import de.jotomo.ruffy.spi.history.PumpError; import de.jotomo.ruffy.spi.history.PumpHistoryRequest; import de.jotomo.ruffy.spi.history.Tbr; import de.jotomo.ruffyscripter.RuffyCommandsV1Impl; @@ -44,7 +38,6 @@ import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.PumpCombo.events.EventComboPumpUpdateGUI; import info.nightscout.utils.DateUtil; -import info.nightscout.utils.SP; /** * Created by mike on 05.08.2016. @@ -205,23 +198,24 @@ public class ComboPlugin implements PluginBase, PumpInterface { @NonNull @Override public Date lastDataTime() { - CommandResult lastCmdResult = pump.lastCmdResult; - return lastCmdResult != null ? new Date(lastCmdResult.completionTime) : new Date(0); + return new Date(pump.lastSuccessfulConnection); } @Override public synchronized void refreshDataFromPump(String reason) { log.debug("RefreshDataFromPump called"); - if (pump.lastCmdResult == null) { + boolean firstRun = pump.lastCmdResult == null; + + runCommand("Refreshing", new CommandExecution() { + @Override + public CommandResult execute() { + return ruffyScripter.readHistory(new PumpHistoryRequest().reservoirLevel(true).bolusHistory(PumpHistoryRequest.LAST)); + } + }); + + if (firstRun) { initializePump(); - } else { - runCommand("Refreshing", new CommandExecution() { - @Override - public CommandResult execute() { - return ruffyScripter.readHistory(new PumpHistoryRequest().reservoirLevel(true).bolusHistory(PumpHistoryRequest.LAST)); - } - }); } } @@ -585,8 +579,10 @@ public class ComboPlugin implements PluginBase, PumpInterface { } - private CommandResult runCommand(String status, boolean checkTbrMisMatch, CommandExecution commandExecution) { - MainApp.bus().post(new EventComboPumpUpdateGUI(status)); + private CommandResult runCommand(String activity, boolean checkTbrMisMatch, CommandExecution commandExecution) { + pump.activity = activity; + MainApp.bus().post(new EventComboPumpUpdateGUI()); + CommandResult commandResult = commandExecution.execute(); if (commandResult.success) { @@ -606,17 +602,19 @@ public class ComboPlugin implements PluginBase, PumpInterface { // get the current state, then decide what makes sense to do further, if anything, // send next request. // then request state again ... ? - if (commandResult.state.errorMsg != null) { + // TODO rethink this errorMsg field; + +/* if (commandResult.state.errorMsg != null) { CommandResult takeOverAlarmResult = ruffyScripter.takeOverAlarms(); - for (Error error : takeOverAlarmResult.history.errorHistory) { + for (PumpError pumpError : takeOverAlarmResult.history.pumpErrorHistory) { MainApp.bus().post(new EventNewNotification( - new Notification(Notification.COMBO_PUMP_ERROR, - "Pump alarm: " + error.message, Notification.URGENT))); + new Notification(Notification.COMBO_PUMP_ALARM, + "Pump alarm: " + pumpError.message, Notification.URGENT))); } commandResult.state = takeOverAlarmResult.state; - } + }*/ pump.lastCmdResult = commandResult; pump.state = commandResult.state; @@ -632,20 +630,21 @@ public class ComboPlugin implements PluginBase, PumpInterface { log.error("JOE: no!"); } else { // still crashable ... + // TODO only update if command was successful? -> KeepAliveReceiver, triggering alarm on unavaiblae pump pump.lastCmdResult.completionTime = System.currentTimeMillis(); } - // TOOD + // TODO merge all new history here? if (commandResult.history != null) { if (commandResult.history.reservoirLevel != -1) { pump.reservoirLevel = commandResult.history.reservoirLevel; } pump.history = commandResult.history; - if (pump.history.bolusHistory.size() > 0) { - pump.lastBolus = pump.history.bolusHistory.get(0); - } } + // TODO in the event of an error schedule a resync + + pump.activity = null; MainApp.bus().post(new EventComboPumpUpdateGUI()); return commandResult; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPump.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPump.java index 22b4b6d8a2..202989a27a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPump.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPump.java @@ -10,16 +10,17 @@ import de.jotomo.ruffy.spi.history.Bolus; import de.jotomo.ruffy.spi.history.PumpHistory; class ComboPump { - public long lastSuccessfulConnection; - public long lastConnectionAttempt; - + // TODO actually ... this isn't about successful command execution, but whether we could connect to the pump at all + volatile long lastSuccessfulConnection; + volatile long lastConnectionAttempt; @Nullable volatile CommandResult lastCmdResult; + + public volatile String activity; @NonNull volatile PumpState state = new PumpState(); - @NonNull - volatile PumpHistory history = new PumpHistory(); - @Nullable - volatile Bolus lastBolus; volatile int reservoirLevel = -1; + @NonNull + + volatile PumpHistory history = new PumpHistory(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/events/EventComboPumpUpdateGUI.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/events/EventComboPumpUpdateGUI.java index 5b0755da97..e9bf3f8415 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/events/EventComboPumpUpdateGUI.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/events/EventComboPumpUpdateGUI.java @@ -5,11 +5,4 @@ package info.nightscout.androidaps.plugins.PumpCombo.events; */ public class EventComboPumpUpdateGUI { - public EventComboPumpUpdateGUI() {} - - public EventComboPumpUpdateGUI(String status) { - this.status = status; - } - - public String status; } diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java index 03610d3a2d..860da1aa65 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java @@ -68,6 +68,7 @@ public class KeepAliveReceiver extends BroadcastReceiver { // fixing the problem takes longer (or is not immediately possible because the pump was forgotten)? // suppress this for another 25m if the message was dismissed? // The alarm sound is played back as regular media, that means it might be muted if sound level is at 0 + // a simple 'Enable/disable alarms' button on the actions tab? Notification n = new Notification(Notification.PUMP_UNREACHABLE, "Pump unreachable", Notification.URGENT); n.soundId = R.raw.alarm; MainApp.bus().post(new EventNewNotification(n)); diff --git a/app/src/main/res/layout/combopump_fragment.xml b/app/src/main/res/layout/combopump_fragment.xml index f728f7ee96..7d208858ff 100644 --- a/app/src/main/res/layout/combopump_fragment.xml +++ b/app/src/main/res/layout/combopump_fragment.xml @@ -29,7 +29,7 @@ android:layout_weight="1.5" android:gravity="end" android:paddingRight="5dp" - android:text="Status" + android:text="State" android:textSize="14sp" /> @@ -145,6 +144,51 @@ + + + + + + + + + + + + -