Better bolusing with different speeds (R & Rv2)

This commit is contained in:
Milos Kozak 2017-10-21 18:55:56 +02:00
parent 8e82e341fd
commit 7dd678c917
9 changed files with 101 additions and 38 deletions

View file

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

View file

@ -20,7 +20,7 @@ public class DetailedBolusInfoStorage {
private static List<DetailedBolusInfo> 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;
}

View file

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

View file

@ -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 {

View file

@ -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)

View file

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

View file

@ -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)

View file

@ -49,9 +49,9 @@
</string-array>
<string-array name="danaSpeedArray">
<item>12s 1U</item>
<item>30s 1U</item>
<item>60s 1U</item>
<item>12 s/U</item>
<item>30 s/U</item>
<item>60 s/U</item>
</string-array>
<string-array name="danaSpeedValues">
<item>0</item>

View file

@ -747,5 +747,8 @@
<string name="wearcontrol_summary">Set Temp-Targets and enter Treatments from the watch.</string>
<string name="connectiontimedout">Connection timed out</string>
<string name="active"><![CDATA[<Active>]]></string>
<string name="waitingforestimatedbolusend" formatted="false">Waiting for estimated bolus end. Remaining %d sec.</string>
<string name="processinghistory">Processing event</string>
<string name="startingbolus">Starting bolus delivery</string>
</resources>