From 7dd678c917100db2632a4d9774bc3500e0923b04 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 21 Oct 2017 18:55:56 +0200 Subject: [PATCH] Better bolusing with different speeds (R & Rv2) --- .../androidaps/data/DetailedBolusInfo.java | 10 ++++ .../DetailedBolusInfoStorage.java | 6 +- .../plugins/PumpDanaR/SerialIOThread.java | 4 +- .../services/DanaRExecutionService.java | 43 +++++++++++--- .../plugins/PumpDanaRv2/DanaRv2Plugin.java | 7 ++- .../plugins/PumpDanaRv2/SerialIOThread.java | 4 +- .../services/DanaRv2ExecutionService.java | 56 +++++++++++++------ app/src/main/res/values/arrays.xml | 6 +- app/src/main/res/values/strings.xml | 3 + 9 files changed, 101 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java b/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java index 2d928a5541..7720b03d3e 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java +++ b/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java @@ -29,4 +29,14 @@ public class DetailedBolusInfo { public Context context = null; // context for progress dialog public long pumpId = 0; // id of record if comming from pump history (not a newly created treatment) public boolean isSMB = false; // is a Super-MicroBolus + + @Override + public String toString() { + return new Date(date).toLocaleString() + + " insulin: " + insulin + + " carbs: " + carbs + + " isValid: " + isValid + + " carbTime: " + carbTime + + " isSMB: " + isSMB; + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/DetailedBolusInfoStorage.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/DetailedBolusInfoStorage.java index d6352cdb18..abe3c75c73 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/DetailedBolusInfoStorage.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/DetailedBolusInfoStorage.java @@ -20,7 +20,7 @@ public class DetailedBolusInfoStorage { private static List store = new ArrayList<>(); public static void add(DetailedBolusInfo detailedBolusInfo) { - log.debug("Bolus info stored: " + new Date(detailedBolusInfo.date).toLocaleString()); + log.debug("Stored bolus info: " + detailedBolusInfo); store.add(detailedBolusInfo); } @@ -29,7 +29,7 @@ public class DetailedBolusInfoStorage { DetailedBolusInfo found = null; for (int i = 0; i < store.size(); i++) { long infoTime = store.get(i).date; - log.debug("Existing info: " + new Date(infoTime).toLocaleString()); + log.debug("Existing bolus info: " + store.get(i)); if (bolustime > infoTime - 60 * 1000 && bolustime < infoTime + 60 * 1000) { found = store.get(i); break; @@ -42,7 +42,7 @@ public class DetailedBolusInfoStorage { for (int i = 0; i < store.size(); i++) { long infoTime = store.get(i).date; if (bolustime > infoTime - 60 * 1000 && bolustime < infoTime + 60 * 1000) { - log.debug("Removing info: " + new Date(infoTime).toLocaleString()); + log.debug("Removing bolus info: " + store.get(i)); store.remove(i); break; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/SerialIOThread.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/SerialIOThread.java index af3b852aa6..b6a54f4ec1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/SerialIOThread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/SerialIOThread.java @@ -187,12 +187,12 @@ public class SerialIOThread extends Thread { scheduledDisconnection = null; } } - // prepare task for execution in 5 sec + // prepare task for execution in 10 sec // cancel waiting task to prevent sending multiple disconnections if (scheduledDisconnection != null) scheduledDisconnection.cancel(false); Runnable task = new DisconnectRunnable(); - final int sec = 5; + final int sec = 10; scheduledDisconnection = worker.schedule(task, sec, TimeUnit.SECONDS); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/services/DanaRExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/services/DanaRExecutionService.java index 9d94634094..1ef5349f25 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/services/DanaRExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/services/DanaRExecutionService.java @@ -34,6 +34,7 @@ import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; import info.nightscout.androidaps.plugins.PumpDanaR.SerialIOThread; @@ -410,12 +411,12 @@ public class DanaRExecutionService extends Service { public boolean bolus(double amount, int carbs, Treatment t) { bolusingTreatment = t; - int speed = SP.getInt(R.string.key_danars_bolusspeed, 0); + int preferencesSpeed = SP.getInt(R.string.key_danars_bolusspeed, 0); MessageBase start; - if (speed == 0) + if (preferencesSpeed == 0) start = new MsgBolusStart(amount); else - start = new MsgBolusStartWithSpeed(amount, speed); + start = new MsgBolusStartWithSpeed(amount, preferencesSpeed); MsgBolusStop stop = new MsgBolusStop(amount, t); connect("bolus"); @@ -426,7 +427,7 @@ public class DanaRExecutionService extends Service { } MsgBolusProgress progress = new MsgBolusProgress(amount, t); // initialize static variables - long startTime = System.currentTimeMillis(); + long bolusStart = System.currentTimeMillis(); if (!stop.stopped) { mSerialIOThread.sendMessage(start); @@ -436,23 +437,47 @@ public class DanaRExecutionService extends Service { } while (!stop.stopped && !start.failed) { waitMsec(100); - if ((System.currentTimeMillis() - progress.lastReceive) > 5 * 1000L) { // if i didn't receive status for more than 5 sec expecting broken comm + if ((System.currentTimeMillis() - progress.lastReceive) > 15 * 1000L) { // if i didn't receive status for more than 5 sec expecting broken comm stop.stopped = true; stop.forced = true; log.debug("Communication stopped"); } } waitMsec(300); + + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); + bolusingEvent.t = t; + bolusingEvent.percent = 99; + bolusingTreatment = null; + + int speed = 12; + switch (preferencesSpeed) { + case 0: + speed = 12; + break; + case 1: + speed = 30; + break; + case 2: + speed = 60; + break; + } // try to find real amount if bolusing was interrupted or comm failed if (t.insulin != amount) { disconnect("bolusingInterrupted"); - long now = System.currentTimeMillis(); - long estimatedBolusEnd = (long) (startTime + amount / 5d * 60 * 1000); // std delivery rate 5 U/min - waitMsec(Math.max(5000, estimatedBolusEnd - now + 3000)); + long bolusDurationInMSec = (long) (amount * speed * 1000); + long expectedEnd = bolusStart + bolusDurationInMSec + 3000; + + while (System.currentTimeMillis() < expectedEnd) { + long waitTime = expectedEnd - System.currentTimeMillis(); + bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.waitingforestimatedbolusend), waitTime / 1000); + MainApp.bus().post(bolusingEvent); + SystemClock.sleep(1000); + } connect("bolusingInterrupted"); getPumpStatus(); - if (danaRPump.lastBolusTime.getTime() > now - 60 * 1000L) { // last bolus max 1 min old + if (danaRPump.lastBolusTime.getTime() > System.currentTimeMillis() - 60 * 1000L) { // last bolus max 1 min old t.insulin = danaRPump.lastBolusAmount; log.debug("Used bolus amount from history: " + danaRPump.lastBolusAmount); } else { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java index ea174e4608..4a224bd57b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java @@ -293,9 +293,8 @@ public class DanaRv2Plugin implements PluginBase, PumpInterface, DanaRInterface, ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); detailedBolusInfo.insulin = configBuilderPlugin.applyBolusConstraints(detailedBolusInfo.insulin); if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { - DetailedBolusInfoStorage.add(detailedBolusInfo); // will be picked up on reading history // v2 stores end time for bolus, we need to adjust time - // default delivery speed is 12 U/min + // default delivery speed is 12 sec/U int preferencesSpeed = SP.getInt(R.string.key_danars_bolusspeed, 0); int speed = 12; switch (preferencesSpeed) { @@ -309,7 +308,7 @@ public class DanaRv2Plugin implements PluginBase, PumpInterface, DanaRInterface, speed = 60; break; } - detailedBolusInfo.date += detailedBolusInfo.insulin / speed * 60d * 1000; + detailedBolusInfo.date += speed * detailedBolusInfo.insulin * 1000; // clean carbs to prevent counting them as twice because they will picked up as another record // I don't think it's necessary to copy DetailedBolusInfo right now for carbs records double carbs = detailedBolusInfo.carbs; @@ -317,6 +316,8 @@ public class DanaRv2Plugin implements PluginBase, PumpInterface, DanaRInterface, int carbTime = detailedBolusInfo.carbTime; detailedBolusInfo.carbTime = 0; + DetailedBolusInfoStorage.add(detailedBolusInfo); // will be picked up on reading history + Treatment t = new Treatment(); boolean connectionOK = false; if (detailedBolusInfo.insulin > 0 || carbs > 0) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/SerialIOThread.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/SerialIOThread.java index 244014f3d0..da9e44f925 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/SerialIOThread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/SerialIOThread.java @@ -188,12 +188,12 @@ public class SerialIOThread extends Thread { scheduledDisconnection = null; } } - // prepare task for execution in 5 sec + // prepare task for execution in 10 sec // cancel waiting task to prevent sending multiple disconnections if (scheduledDisconnection != null) scheduledDisconnection.cancel(false); Runnable task = new DisconnectRunnable(); - final int sec = 5; + final int sec = 10; scheduledDisconnection = worker.schedule(task, sec, TimeUnit.SECONDS); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java index fc3df19296..75f9893bc9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java @@ -36,6 +36,7 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; +import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; import info.nightscout.androidaps.plugins.PumpDanaR.comm.*; import info.nightscout.androidaps.plugins.PumpDanaR.events.EventDanaRNewStatus; @@ -400,14 +401,15 @@ public class DanaRv2ExecutionService extends Service { return true; } - public boolean bolus(double amount, int carbs, long carbtime, Treatment t) { + public boolean bolus(final double amount, int carbs, long carbtime, Treatment t) { + MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.startingbolus))); bolusingTreatment = t; - int speed = SP.getInt(R.string.key_danars_bolusspeed, 0); + final int preferencesSpeed = SP.getInt(R.string.key_danars_bolusspeed, 0); MessageBase start; - if (speed == 0) + if (preferencesSpeed == 0) start = new MsgBolusStart(amount); else - start = new MsgBolusStartWithSpeed(amount, speed); + start = new MsgBolusStartWithSpeed(amount, preferencesSpeed); MsgBolusStop stop = new MsgBolusStop(amount, t); connect("bolus"); @@ -420,6 +422,8 @@ public class DanaRv2ExecutionService extends Service { mSerialIOThread.sendMessage(msgSetHistoryEntry_v2); lastHistoryFetched = carbtime - 60000; } + + final long bolusStart = System.currentTimeMillis(); if (amount > 0) { MsgBolusProgress progress = new MsgBolusProgress(amount, t); // initialize static variables @@ -431,26 +435,46 @@ public class DanaRv2ExecutionService extends Service { } while (!stop.stopped && !start.failed) { waitMsec(100); - if ((System.currentTimeMillis() - progress.lastReceive) > 5 * 1000L) { // if i didn't receive status for more than 5 sec expecting broken comm + if ((System.currentTimeMillis() - progress.lastReceive) > 15 * 1000L) { // if i didn't receive status for more than 5 sec expecting broken comm stop.stopped = true; stop.forced = true; log.debug("Communication stopped"); } } } + + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); + bolusingEvent.t = t; + bolusingEvent.percent = 99; + bolusingTreatment = null; - // run loading history in separate thread and allow bolus dialog to be closed - new Thread(new Runnable() { - @Override - public void run() { - waitMsec(4000); - if (!(isConnected())) - connect("loadEvents"); - loadEvents(); - } - }).start(); + int speed = 12; + switch (preferencesSpeed) { + case 0: + speed = 12; + break; + case 1: + speed = 30; + break; + case 2: + speed = 60; + break; + } + long bolusDurationInMSec = (long) (amount * speed * 1000); + long expectedEnd = bolusStart + bolusDurationInMSec + 2000; + while (System.currentTimeMillis() < expectedEnd) { + long waitTime = expectedEnd - System.currentTimeMillis(); + bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.waitingforestimatedbolusend), waitTime / 1000); + MainApp.bus().post(bolusingEvent); + SystemClock.sleep(1000); + } + if (!(isConnected())) + connect("loadEvents"); + loadEvents(); + bolusingEvent.percent = 100; + MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.disconnecting))); return true; - } +} public void bolusStop() { if (Config.logDanaBTComm) diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index efa2d31c4d..c8bdc47ec9 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -49,9 +49,9 @@ - 12s 1U - 30s 1U - 60s 1U + 12 s/U + 30 s/U + 60 s/U 0 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3b25c78dd2..0c0e9060d4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -747,5 +747,8 @@ Set Temp-Targets and enter Treatments from the watch. Connection timed out ]]> + Waiting for estimated bolus end. Remaining %d sec. + Processing event + Starting bolus delivery