Better bolusing with different speeds (R & Rv2)
This commit is contained in:
parent
8e82e341fd
commit
7dd678c917
9 changed files with 101 additions and 38 deletions
|
@ -29,4 +29,14 @@ public class DetailedBolusInfo {
|
||||||
public Context context = null; // context for progress dialog
|
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 long pumpId = 0; // id of record if comming from pump history (not a newly created treatment)
|
||||||
public boolean isSMB = false; // is a Super-MicroBolus
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ public class DetailedBolusInfoStorage {
|
||||||
private static List<DetailedBolusInfo> store = new ArrayList<>();
|
private static List<DetailedBolusInfo> store = new ArrayList<>();
|
||||||
|
|
||||||
public static void add(DetailedBolusInfo detailedBolusInfo) {
|
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);
|
store.add(detailedBolusInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ public class DetailedBolusInfoStorage {
|
||||||
DetailedBolusInfo found = null;
|
DetailedBolusInfo found = null;
|
||||||
for (int i = 0; i < store.size(); i++) {
|
for (int i = 0; i < store.size(); i++) {
|
||||||
long infoTime = store.get(i).date;
|
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) {
|
if (bolustime > infoTime - 60 * 1000 && bolustime < infoTime + 60 * 1000) {
|
||||||
found = store.get(i);
|
found = store.get(i);
|
||||||
break;
|
break;
|
||||||
|
@ -42,7 +42,7 @@ public class DetailedBolusInfoStorage {
|
||||||
for (int i = 0; i < store.size(); i++) {
|
for (int i = 0; i < store.size(); i++) {
|
||||||
long infoTime = store.get(i).date;
|
long infoTime = store.get(i).date;
|
||||||
if (bolustime > infoTime - 60 * 1000 && bolustime < infoTime + 60 * 1000) {
|
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);
|
store.remove(i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -187,12 +187,12 @@ public class SerialIOThread extends Thread {
|
||||||
scheduledDisconnection = null;
|
scheduledDisconnection = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// prepare task for execution in 5 sec
|
// prepare task for execution in 10 sec
|
||||||
// cancel waiting task to prevent sending multiple disconnections
|
// cancel waiting task to prevent sending multiple disconnections
|
||||||
if (scheduledDisconnection != null)
|
if (scheduledDisconnection != null)
|
||||||
scheduledDisconnection.cancel(false);
|
scheduledDisconnection.cancel(false);
|
||||||
Runnable task = new DisconnectRunnable();
|
Runnable task = new DisconnectRunnable();
|
||||||
final int sec = 5;
|
final int sec = 10;
|
||||||
scheduledDisconnection = worker.schedule(task, sec, TimeUnit.SECONDS);
|
scheduledDisconnection = worker.schedule(task, sec, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ import info.nightscout.androidaps.events.EventPreferenceChange;
|
||||||
import info.nightscout.androidaps.events.EventPumpStatusChanged;
|
import info.nightscout.androidaps.events.EventPumpStatusChanged;
|
||||||
import info.nightscout.androidaps.interfaces.PluginBase;
|
import info.nightscout.androidaps.interfaces.PluginBase;
|
||||||
import info.nightscout.androidaps.data.Profile;
|
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.DanaRPlugin;
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
|
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaR.SerialIOThread;
|
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) {
|
public boolean bolus(double amount, int carbs, Treatment t) {
|
||||||
bolusingTreatment = 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;
|
MessageBase start;
|
||||||
if (speed == 0)
|
if (preferencesSpeed == 0)
|
||||||
start = new MsgBolusStart(amount);
|
start = new MsgBolusStart(amount);
|
||||||
else
|
else
|
||||||
start = new MsgBolusStartWithSpeed(amount, speed);
|
start = new MsgBolusStartWithSpeed(amount, preferencesSpeed);
|
||||||
MsgBolusStop stop = new MsgBolusStop(amount, t);
|
MsgBolusStop stop = new MsgBolusStop(amount, t);
|
||||||
|
|
||||||
connect("bolus");
|
connect("bolus");
|
||||||
|
@ -426,7 +427,7 @@ public class DanaRExecutionService extends Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
MsgBolusProgress progress = new MsgBolusProgress(amount, t); // initialize static variables
|
MsgBolusProgress progress = new MsgBolusProgress(amount, t); // initialize static variables
|
||||||
long startTime = System.currentTimeMillis();
|
long bolusStart = System.currentTimeMillis();
|
||||||
|
|
||||||
if (!stop.stopped) {
|
if (!stop.stopped) {
|
||||||
mSerialIOThread.sendMessage(start);
|
mSerialIOThread.sendMessage(start);
|
||||||
|
@ -436,23 +437,47 @@ public class DanaRExecutionService extends Service {
|
||||||
}
|
}
|
||||||
while (!stop.stopped && !start.failed) {
|
while (!stop.stopped && !start.failed) {
|
||||||
waitMsec(100);
|
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.stopped = true;
|
||||||
stop.forced = true;
|
stop.forced = true;
|
||||||
log.debug("Communication stopped");
|
log.debug("Communication stopped");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
waitMsec(300);
|
waitMsec(300);
|
||||||
|
|
||||||
|
EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
|
||||||
|
bolusingEvent.t = t;
|
||||||
|
bolusingEvent.percent = 99;
|
||||||
|
|
||||||
bolusingTreatment = null;
|
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
|
// try to find real amount if bolusing was interrupted or comm failed
|
||||||
if (t.insulin != amount) {
|
if (t.insulin != amount) {
|
||||||
disconnect("bolusingInterrupted");
|
disconnect("bolusingInterrupted");
|
||||||
long now = System.currentTimeMillis();
|
long bolusDurationInMSec = (long) (amount * speed * 1000);
|
||||||
long estimatedBolusEnd = (long) (startTime + amount / 5d * 60 * 1000); // std delivery rate 5 U/min
|
long expectedEnd = bolusStart + bolusDurationInMSec + 3000;
|
||||||
waitMsec(Math.max(5000, estimatedBolusEnd - now + 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");
|
connect("bolusingInterrupted");
|
||||||
getPumpStatus();
|
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;
|
t.insulin = danaRPump.lastBolusAmount;
|
||||||
log.debug("Used bolus amount from history: " + danaRPump.lastBolusAmount);
|
log.debug("Used bolus amount from history: " + danaRPump.lastBolusAmount);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -293,9 +293,8 @@ public class DanaRv2Plugin implements PluginBase, PumpInterface, DanaRInterface,
|
||||||
ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder();
|
ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder();
|
||||||
detailedBolusInfo.insulin = configBuilderPlugin.applyBolusConstraints(detailedBolusInfo.insulin);
|
detailedBolusInfo.insulin = configBuilderPlugin.applyBolusConstraints(detailedBolusInfo.insulin);
|
||||||
if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) {
|
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
|
// 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 preferencesSpeed = SP.getInt(R.string.key_danars_bolusspeed, 0);
|
||||||
int speed = 12;
|
int speed = 12;
|
||||||
switch (preferencesSpeed) {
|
switch (preferencesSpeed) {
|
||||||
|
@ -309,7 +308,7 @@ public class DanaRv2Plugin implements PluginBase, PumpInterface, DanaRInterface,
|
||||||
speed = 60;
|
speed = 60;
|
||||||
break;
|
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
|
// 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
|
// I don't think it's necessary to copy DetailedBolusInfo right now for carbs records
|
||||||
double carbs = detailedBolusInfo.carbs;
|
double carbs = detailedBolusInfo.carbs;
|
||||||
|
@ -317,6 +316,8 @@ public class DanaRv2Plugin implements PluginBase, PumpInterface, DanaRInterface,
|
||||||
int carbTime = detailedBolusInfo.carbTime;
|
int carbTime = detailedBolusInfo.carbTime;
|
||||||
detailedBolusInfo.carbTime = 0;
|
detailedBolusInfo.carbTime = 0;
|
||||||
|
|
||||||
|
DetailedBolusInfoStorage.add(detailedBolusInfo); // will be picked up on reading history
|
||||||
|
|
||||||
Treatment t = new Treatment();
|
Treatment t = new Treatment();
|
||||||
boolean connectionOK = false;
|
boolean connectionOK = false;
|
||||||
if (detailedBolusInfo.insulin > 0 || carbs > 0)
|
if (detailedBolusInfo.insulin > 0 || carbs > 0)
|
||||||
|
|
|
@ -188,12 +188,12 @@ public class SerialIOThread extends Thread {
|
||||||
scheduledDisconnection = null;
|
scheduledDisconnection = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// prepare task for execution in 5 sec
|
// prepare task for execution in 10 sec
|
||||||
// cancel waiting task to prevent sending multiple disconnections
|
// cancel waiting task to prevent sending multiple disconnections
|
||||||
if (scheduledDisconnection != null)
|
if (scheduledDisconnection != null)
|
||||||
scheduledDisconnection.cancel(false);
|
scheduledDisconnection.cancel(false);
|
||||||
Runnable task = new DisconnectRunnable();
|
Runnable task = new DisconnectRunnable();
|
||||||
final int sec = 5;
|
final int sec = 10;
|
||||||
scheduledDisconnection = worker.schedule(task, sec, TimeUnit.SECONDS);
|
scheduledDisconnection = worker.schedule(task, sec, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,7 @@ import info.nightscout.androidaps.interfaces.PluginBase;
|
||||||
import info.nightscout.androidaps.data.Profile;
|
import info.nightscout.androidaps.data.Profile;
|
||||||
import info.nightscout.androidaps.plugins.Overview.Notification;
|
import info.nightscout.androidaps.plugins.Overview.Notification;
|
||||||
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
|
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.DanaRPump;
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaR.comm.*;
|
import info.nightscout.androidaps.plugins.PumpDanaR.comm.*;
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaR.events.EventDanaRNewStatus;
|
import info.nightscout.androidaps.plugins.PumpDanaR.events.EventDanaRNewStatus;
|
||||||
|
@ -400,14 +401,15 @@ public class DanaRv2ExecutionService extends Service {
|
||||||
return true;
|
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;
|
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;
|
MessageBase start;
|
||||||
if (speed == 0)
|
if (preferencesSpeed == 0)
|
||||||
start = new MsgBolusStart(amount);
|
start = new MsgBolusStart(amount);
|
||||||
else
|
else
|
||||||
start = new MsgBolusStartWithSpeed(amount, speed);
|
start = new MsgBolusStartWithSpeed(amount, preferencesSpeed);
|
||||||
MsgBolusStop stop = new MsgBolusStop(amount, t);
|
MsgBolusStop stop = new MsgBolusStop(amount, t);
|
||||||
|
|
||||||
connect("bolus");
|
connect("bolus");
|
||||||
|
@ -420,6 +422,8 @@ public class DanaRv2ExecutionService extends Service {
|
||||||
mSerialIOThread.sendMessage(msgSetHistoryEntry_v2);
|
mSerialIOThread.sendMessage(msgSetHistoryEntry_v2);
|
||||||
lastHistoryFetched = carbtime - 60000;
|
lastHistoryFetched = carbtime - 60000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final long bolusStart = System.currentTimeMillis();
|
||||||
if (amount > 0) {
|
if (amount > 0) {
|
||||||
MsgBolusProgress progress = new MsgBolusProgress(amount, t); // initialize static variables
|
MsgBolusProgress progress = new MsgBolusProgress(amount, t); // initialize static variables
|
||||||
|
|
||||||
|
@ -431,26 +435,46 @@ public class DanaRv2ExecutionService extends Service {
|
||||||
}
|
}
|
||||||
while (!stop.stopped && !start.failed) {
|
while (!stop.stopped && !start.failed) {
|
||||||
waitMsec(100);
|
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.stopped = true;
|
||||||
stop.forced = true;
|
stop.forced = true;
|
||||||
log.debug("Communication stopped");
|
log.debug("Communication stopped");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
|
||||||
|
bolusingEvent.t = t;
|
||||||
|
bolusingEvent.percent = 99;
|
||||||
|
|
||||||
bolusingTreatment = null;
|
bolusingTreatment = null;
|
||||||
// run loading history in separate thread and allow bolus dialog to be closed
|
int speed = 12;
|
||||||
new Thread(new Runnable() {
|
switch (preferencesSpeed) {
|
||||||
@Override
|
case 0:
|
||||||
public void run() {
|
speed = 12;
|
||||||
waitMsec(4000);
|
break;
|
||||||
if (!(isConnected()))
|
case 1:
|
||||||
connect("loadEvents");
|
speed = 30;
|
||||||
loadEvents();
|
break;
|
||||||
}
|
case 2:
|
||||||
}).start();
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void bolusStop() {
|
public void bolusStop() {
|
||||||
if (Config.logDanaBTComm)
|
if (Config.logDanaBTComm)
|
||||||
|
|
|
@ -49,9 +49,9 @@
|
||||||
</string-array>
|
</string-array>
|
||||||
|
|
||||||
<string-array name="danaSpeedArray">
|
<string-array name="danaSpeedArray">
|
||||||
<item>12s 1U</item>
|
<item>12 s/U</item>
|
||||||
<item>30s 1U</item>
|
<item>30 s/U</item>
|
||||||
<item>60s 1U</item>
|
<item>60 s/U</item>
|
||||||
</string-array>
|
</string-array>
|
||||||
<string-array name="danaSpeedValues">
|
<string-array name="danaSpeedValues">
|
||||||
<item>0</item>
|
<item>0</item>
|
||||||
|
|
|
@ -747,5 +747,8 @@
|
||||||
<string name="wearcontrol_summary">Set Temp-Targets and enter Treatments from the watch.</string>
|
<string name="wearcontrol_summary">Set Temp-Targets and enter Treatments from the watch.</string>
|
||||||
<string name="connectiontimedout">Connection timed out</string>
|
<string name="connectiontimedout">Connection timed out</string>
|
||||||
<string name="active"><![CDATA[<Active>]]></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>
|
</resources>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue