TBR emulation
This commit is contained in:
parent
96fb4323f8
commit
f151cbb0bf
Binary file not shown.
|
@ -226,6 +226,10 @@ public class Profile {
|
||||||
basal_v.setValueAt(i, description.basalMinimumRate);
|
basal_v.setValueAt(i, description.basalMinimumRate);
|
||||||
if (notify)
|
if (notify)
|
||||||
sendBelowMinimumNotification(from);
|
sendBelowMinimumNotification(from);
|
||||||
|
} else if (basal_v.valueAt(i) > description.basalMaximumRate) {
|
||||||
|
basal_v.setValueAt(i, description.basalMaximumRate);
|
||||||
|
if (notify)
|
||||||
|
sendAboveMaximumNotification(from);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -243,6 +247,10 @@ public class Profile {
|
||||||
MainApp.bus().post(new EventNewNotification(new Notification(Notification.MINIMAL_BASAL_VALUE_REPLACED, String.format(MainApp.gs(R.string.minimalbasalvaluereplaced), from), Notification.NORMAL)));
|
MainApp.bus().post(new EventNewNotification(new Notification(Notification.MINIMAL_BASAL_VALUE_REPLACED, String.format(MainApp.gs(R.string.minimalbasalvaluereplaced), from), Notification.NORMAL)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void sendAboveMaximumNotification(String from) {
|
||||||
|
MainApp.bus().post(new EventNewNotification(new Notification(Notification.MINIMAL_BASAL_VALUE_REPLACED, String.format(MainApp.gs(R.string.maximumbasalvaluereplaced), from), Notification.NORMAL)));
|
||||||
|
}
|
||||||
|
|
||||||
private void validate(LongSparseArray array) {
|
private void validate(LongSparseArray array) {
|
||||||
if (array.size() == 0) {
|
if (array.size() == 0) {
|
||||||
isValid = false;
|
isValid = false;
|
||||||
|
|
|
@ -35,6 +35,7 @@ public class PumpDescription {
|
||||||
public boolean isSetBasalProfileCapable = true;
|
public boolean isSetBasalProfileCapable = true;
|
||||||
public double basalStep = 0.01d;
|
public double basalStep = 0.01d;
|
||||||
public double basalMinimumRate = 0.04d;
|
public double basalMinimumRate = 0.04d;
|
||||||
|
public double basalMaximumRate = 25d;
|
||||||
|
|
||||||
public boolean isRefillingCapable = false;
|
public boolean isRefillingCapable = false;
|
||||||
|
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
package info.nightscout.androidaps.plugins.PumpInsight;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by jamorham on 25/01/2018.
|
|
||||||
*
|
|
||||||
* Async command status
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
enum Cstatus {
|
|
||||||
UNKNOWN,
|
|
||||||
PENDING,
|
|
||||||
SUCCESS,
|
|
||||||
FAILURE,
|
|
||||||
TIMEOUT;
|
|
||||||
|
|
||||||
boolean success() {
|
|
||||||
return this == SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,95 +0,0 @@
|
||||||
package info.nightscout.androidaps.plugins.PumpInsight;
|
|
||||||
|
|
||||||
import android.os.PowerManager;
|
|
||||||
|
|
||||||
import com.squareup.otto.Subscribe;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
import info.nightscout.androidaps.MainApp;
|
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightCallback;
|
|
||||||
|
|
||||||
import static info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers.getWakeLock;
|
|
||||||
import static info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers.msSince;
|
|
||||||
import static info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers.releaseWakeLock;
|
|
||||||
import static info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers.tsl;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by jamorham on 25/01/2018.
|
|
||||||
*
|
|
||||||
* Asynchronous adapter
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class InsightAsyncAdapter {
|
|
||||||
|
|
||||||
private final ConcurrentHashMap<UUID, EventInsightCallback> commandResults = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
InsightAsyncAdapter() {
|
|
||||||
MainApp.bus().register(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
// just log during debugging
|
|
||||||
private static void log(String msg) {
|
|
||||||
android.util.Log.e("INSIGHTPUMPASYNC", msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Subscribe
|
|
||||||
public void onStatusEvent(final EventInsightCallback ev) {
|
|
||||||
log("Received callback event: " + ev.toString());
|
|
||||||
commandResults.put(ev.request_uuid, ev);
|
|
||||||
}
|
|
||||||
|
|
||||||
// poll command result
|
|
||||||
private Cstatus checkCommandResult(UUID uuid) {
|
|
||||||
if (uuid == null) return Cstatus.FAILURE;
|
|
||||||
if (commandResults.containsKey(uuid)) {
|
|
||||||
if (commandResults.get(uuid).success) {
|
|
||||||
return Cstatus.SUCCESS;
|
|
||||||
} else {
|
|
||||||
return Cstatus.FAILURE;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Cstatus.PENDING;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// blocking call to wait for result callback
|
|
||||||
private Cstatus busyWaitForCommandInternal(final UUID uuid, long wait_time) {
|
|
||||||
final PowerManager.WakeLock wl = getWakeLock("insight-wait-cmd", 60000);
|
|
||||||
try {
|
|
||||||
log("busy wait for command " + uuid);
|
|
||||||
if (uuid == null) return Cstatus.FAILURE;
|
|
||||||
final long start_time = tsl();
|
|
||||||
Cstatus status = checkCommandResult(uuid);
|
|
||||||
while ((status == Cstatus.PENDING) && msSince(start_time) < wait_time) {
|
|
||||||
//log("command result waiting");
|
|
||||||
try {
|
|
||||||
Thread.sleep(200);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
log("Got interrupted exception! " + e);
|
|
||||||
}
|
|
||||||
status = checkCommandResult(uuid);
|
|
||||||
}
|
|
||||||
if (status == Cstatus.PENDING) {
|
|
||||||
return Cstatus.TIMEOUT;
|
|
||||||
} else {
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
releaseWakeLock(wl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// wait for and then package result, cleanup and return
|
|
||||||
Mstatus busyWaitForCommandResult(final UUID uuid, long wait_time) {
|
|
||||||
final Mstatus mstatus = new Mstatus();
|
|
||||||
mstatus.cstatus = busyWaitForCommandInternal(uuid, wait_time);
|
|
||||||
mstatus.event = commandResults.get(uuid);
|
|
||||||
commandResults.remove(uuid);
|
|
||||||
return mstatus;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -9,7 +9,6 @@ import java.text.DecimalFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import info.nightscout.androidaps.BuildConfig;
|
import info.nightscout.androidaps.BuildConfig;
|
||||||
import info.nightscout.androidaps.Config;
|
import info.nightscout.androidaps.Config;
|
||||||
|
@ -21,7 +20,6 @@ import info.nightscout.androidaps.data.PumpEnactResult;
|
||||||
import info.nightscout.androidaps.db.ExtendedBolus;
|
import info.nightscout.androidaps.db.ExtendedBolus;
|
||||||
import info.nightscout.androidaps.db.Source;
|
import info.nightscout.androidaps.db.Source;
|
||||||
import info.nightscout.androidaps.db.TemporaryBasal;
|
import info.nightscout.androidaps.db.TemporaryBasal;
|
||||||
import info.nightscout.androidaps.plugins.Treatments.Treatment;
|
|
||||||
import info.nightscout.androidaps.interfaces.Constraint;
|
import info.nightscout.androidaps.interfaces.Constraint;
|
||||||
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
|
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
|
||||||
import info.nightscout.androidaps.interfaces.PluginBase;
|
import info.nightscout.androidaps.interfaces.PluginBase;
|
||||||
|
@ -33,7 +31,7 @@ import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotificati
|
||||||
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.Overview.events.EventOverviewBolusProgress;
|
||||||
import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
|
import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.connector.CancelBolusTaskRunner;
|
import info.nightscout.androidaps.plugins.PumpInsight.connector.CancelBolusSilentlyTaskRunner;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.connector.Connector;
|
import info.nightscout.androidaps.plugins.PumpInsight.connector.Connector;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.connector.SetTBRTaskRunner;
|
import info.nightscout.androidaps.plugins.PumpInsight.connector.SetTBRTaskRunner;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.connector.StatusTaskRunner;
|
import info.nightscout.androidaps.plugins.PumpInsight.connector.StatusTaskRunner;
|
||||||
|
@ -44,12 +42,14 @@ import info.nightscout.androidaps.plugins.PumpInsight.history.HistoryReceiver;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.history.LiveHistory;
|
import info.nightscout.androidaps.plugins.PumpInsight.history.LiveHistory;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers;
|
import info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.utils.StatusItem;
|
import info.nightscout.androidaps.plugins.PumpInsight.utils.StatusItem;
|
||||||
|
import info.nightscout.androidaps.plugins.Treatments.Treatment;
|
||||||
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
|
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
|
||||||
import info.nightscout.utils.DateUtil;
|
import info.nightscout.utils.DateUtil;
|
||||||
import info.nightscout.utils.NSUpload;
|
import info.nightscout.utils.NSUpload;
|
||||||
import info.nightscout.utils.SP;
|
import info.nightscout.utils.SP;
|
||||||
import sugar.free.sightparser.applayer.descriptors.ActiveBolus;
|
import sugar.free.sightparser.applayer.descriptors.ActiveBolus;
|
||||||
import sugar.free.sightparser.applayer.descriptors.ActiveBolusType;
|
import sugar.free.sightparser.applayer.descriptors.ActiveBolusType;
|
||||||
|
import sugar.free.sightparser.applayer.descriptors.MessagePriority;
|
||||||
import sugar.free.sightparser.applayer.descriptors.PumpStatus;
|
import sugar.free.sightparser.applayer.descriptors.PumpStatus;
|
||||||
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfileBlock;
|
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfileBlock;
|
||||||
import sugar.free.sightparser.applayer.messages.AppLayerMessage;
|
import sugar.free.sightparser.applayer.messages.AppLayerMessage;
|
||||||
|
@ -94,7 +94,6 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
private static boolean initialized = false;
|
private static boolean initialized = false;
|
||||||
private static volatile boolean update_pending = false;
|
private static volatile boolean update_pending = false;
|
||||||
private static Logger log = LoggerFactory.getLogger(InsightPlugin.class);
|
private static Logger log = LoggerFactory.getLogger(InsightPlugin.class);
|
||||||
private final InsightAsyncAdapter async = new InsightAsyncAdapter();
|
|
||||||
private StatusTaskRunner.Result statusResult;
|
private StatusTaskRunner.Result statusResult;
|
||||||
private long statusResultTime = -1;
|
private long statusResultTime = -1;
|
||||||
private Date lastDataTime = new Date(0);
|
private Date lastDataTime = new Date(0);
|
||||||
|
@ -115,10 +114,10 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
);
|
);
|
||||||
log("InsightPlugin instantiated");
|
log("InsightPlugin instantiated");
|
||||||
pumpDescription.isBolusCapable = true;
|
pumpDescription.isBolusCapable = true;
|
||||||
pumpDescription.bolusStep = 0.05d; // specification says 0.05U up to 2U then 0.1U @ 2-5U 0.2U @ 10-20U 0.5U 10-20U (are these just UI restrictions?)
|
pumpDescription.bolusStep = 0.01d; // specification says 0.05U up to 2U then 0.1U @ 2-5U 0.2U @ 10-20U 0.5U 10-20U (are these just UI restrictions? Yes, they are!)
|
||||||
|
|
||||||
pumpDescription.isExtendedBolusCapable = true;
|
pumpDescription.isExtendedBolusCapable = true;
|
||||||
pumpDescription.extendedBolusStep = 0.05d; // specification probably same as above
|
pumpDescription.extendedBolusStep = 0.01d; // specification probably same as above
|
||||||
pumpDescription.extendedBolusDurationStep = 15; // 15 minutes up to 24 hours
|
pumpDescription.extendedBolusDurationStep = 15; // 15 minutes up to 24 hours
|
||||||
pumpDescription.extendedBolusMaxDuration = 24 * 60;
|
pumpDescription.extendedBolusMaxDuration = 24 * 60;
|
||||||
|
|
||||||
|
@ -138,6 +137,7 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
pumpDescription.is30minBasalRatesCapable = true;
|
pumpDescription.is30minBasalRatesCapable = true;
|
||||||
pumpDescription.basalStep = 0.01d;
|
pumpDescription.basalStep = 0.01d;
|
||||||
pumpDescription.basalMinimumRate = 0.02d;
|
pumpDescription.basalMinimumRate = 0.02d;
|
||||||
|
pumpDescription.basalMaximumRate = 25d;
|
||||||
|
|
||||||
pumpDescription.isRefillingCapable = true;
|
pumpDescription.isRefillingCapable = true;
|
||||||
|
|
||||||
|
@ -191,7 +191,7 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isFakingTempsByExtendedBoluses() {
|
public boolean isFakingTempsByExtendedBoluses() {
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -287,17 +287,15 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
log("getPumpStatus");
|
log("getPumpStatus");
|
||||||
if (Connector.get().isPumpConnected()) {
|
if (Connector.get().isPumpConnected()) {
|
||||||
log("is connected.. requesting status");
|
log("is connected.. requesting status");
|
||||||
final UUID uuid = aSyncTaskRunner(new StatusTaskRunner(connector.getServiceConnector()), "Status");
|
try {
|
||||||
Mstatus mstatus = async.busyWaitForCommandResult(uuid, BUSY_WAIT_TIME);
|
setStatusResult(fetchTaskRunner(new StatusTaskRunner(connector.getServiceConnector()), StatusTaskRunner.Result.class));
|
||||||
if (mstatus.success()) {
|
|
||||||
log("GOT STATUS RESULT!!! PARTY WOOHOO!!!");
|
log("GOT STATUS RESULT!!! PARTY WOOHOO!!!");
|
||||||
setStatusResult((StatusTaskRunner.Result) mstatus.getResponseObject());
|
|
||||||
statusResultTime = Helpers.tsl();
|
statusResultTime = Helpers.tsl();
|
||||||
processStatusResult();
|
processStatusResult();
|
||||||
updateGui();
|
updateGui();
|
||||||
connector.requestHistoryReSync();
|
connector.requestHistoryReSync();
|
||||||
connector.requestHistorySync();
|
connector.requestHistorySync();
|
||||||
} else {
|
} catch (Exception e) {
|
||||||
log("StatusTaskRunner wasn't successful.");
|
log("StatusTaskRunner wasn't successful.");
|
||||||
if (connector.getServiceConnector().isConnectedToService() && connector.getServiceConnector().getStatus() != Status.CONNECTED) {
|
if (connector.getServiceConnector().isConnectedToService() && connector.getServiceConnector().getStatus() != Status.CONNECTED) {
|
||||||
if (Helpers.ratelimit("insight-reconnect", 2)) {
|
if (Helpers.ratelimit("insight-reconnect", 2)) {
|
||||||
|
@ -313,6 +311,8 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
|
|
||||||
public void setStatusResult(StatusTaskRunner.Result result) {
|
public void setStatusResult(StatusTaskRunner.Result result) {
|
||||||
this.statusResult = result;
|
this.statusResult = result;
|
||||||
|
this.pumpDescription.basalMinimumRate = result.minimumBasalAmount;
|
||||||
|
this.pumpDescription.basalMaximumRate = result.maximumBasalAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -335,9 +335,8 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
profileBlocks.add(new BRProfileBlock.ProfileBlock((((nextValue != null ? nextValue.timeAsSeconds : 24 * 60 * 60) - basalValue.timeAsSeconds) / 60), Helpers.roundDouble(basalValue.value, 2)));
|
profileBlocks.add(new BRProfileBlock.ProfileBlock((((nextValue != null ? nextValue.timeAsSeconds : 24 * 60 * 60) - basalValue.timeAsSeconds) / 60), Helpers.roundDouble(basalValue.value, 2)));
|
||||||
log("setNewBasalProfile: " + basalValue.value + " for " + Integer.toString(((nextValue != null ? nextValue.timeAsSeconds : 24 * 60 * 60) - basalValue.timeAsSeconds) / 60));
|
log("setNewBasalProfile: " + basalValue.value + " for " + Integer.toString(((nextValue != null ? nextValue.timeAsSeconds : 24 * 60 * 60) - basalValue.timeAsSeconds) / 60));
|
||||||
}
|
}
|
||||||
final UUID uuid = aSyncTaskRunner(new WriteBasalProfileTaskRunner(connector.getServiceConnector(), profileBlocks), "Write basal profile");
|
try {
|
||||||
final Mstatus ms = async.busyWaitForCommandResult(uuid, BUSY_WAIT_TIME);
|
fetchTaskRunner(new WriteBasalProfileTaskRunner(connector.getServiceConnector(), profileBlocks));
|
||||||
if (ms.success()) {
|
|
||||||
MainApp.bus().post(new EventDismissNotification(Notification.FAILED_UDPATE_PROFILE));
|
MainApp.bus().post(new EventDismissNotification(Notification.FAILED_UDPATE_PROFILE));
|
||||||
Notification notification = new Notification(Notification.PROFILE_SET_OK, MainApp.gs(R.string.profile_set_ok), Notification.INFO, 60);
|
Notification notification = new Notification(Notification.PROFILE_SET_OK, MainApp.gs(R.string.profile_set_ok), Notification.INFO, 60);
|
||||||
MainApp.bus().post(new EventNewNotification(notification));
|
MainApp.bus().post(new EventNewNotification(notification));
|
||||||
|
@ -345,7 +344,7 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
result.enacted = true;
|
result.enacted = true;
|
||||||
result.comment = "OK";
|
result.comment = "OK";
|
||||||
this.profileBlocks = profileBlocks;
|
this.profileBlocks = profileBlocks;
|
||||||
} else {
|
} catch (Exception e) {
|
||||||
Notification notification = new Notification(Notification.FAILED_UDPATE_PROFILE, MainApp.gs(R.string.failedupdatebasalprofile), Notification.URGENT);
|
Notification notification = new Notification(Notification.FAILED_UDPATE_PROFILE, MainApp.gs(R.string.failedupdatebasalprofile), Notification.URGENT);
|
||||||
MainApp.bus().post(new EventNewNotification(notification));
|
MainApp.bus().post(new EventNewNotification(notification));
|
||||||
result.comment = MainApp.gs(R.string.failedupdatebasalprofile);
|
result.comment = MainApp.gs(R.string.failedupdatebasalprofile);
|
||||||
|
@ -403,17 +402,13 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
|
|
||||||
// is there an insulin component to the treatment?
|
// is there an insulin component to the treatment?
|
||||||
if (detailedBolusInfo.insulin > 0) {
|
if (detailedBolusInfo.insulin > 0) {
|
||||||
final UUID cmd = deliverBolus(detailedBolusInfo.insulin); // actually request delivery
|
try {
|
||||||
if (cmd == null) {
|
bolusId = deliverBolus(detailedBolusInfo.insulin);
|
||||||
|
result.success = true;
|
||||||
|
detailedBolusInfo.pumpId = getRecordUniqueID(bolusId);
|
||||||
|
} catch (Exception e) {
|
||||||
return pumpEnactFailure();
|
return pumpEnactFailure();
|
||||||
}
|
}
|
||||||
final Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME);
|
|
||||||
|
|
||||||
result.success = ms.success();
|
|
||||||
if (ms.success()) {
|
|
||||||
detailedBolusInfo.pumpId = getRecordUniqueID(ms.getResponseID());
|
|
||||||
bolusId = ms.getResponseID();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
result.success = true; // always true with carb only treatments
|
result.success = true; // always true with carb only treatments
|
||||||
}
|
}
|
||||||
|
@ -440,20 +435,11 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
updateGui();
|
updateGui();
|
||||||
connector.tryToGetPumpStatusAgain();
|
connector.tryToGetPumpStatusAgain();
|
||||||
|
|
||||||
connector.requestHistorySync(30000);
|
|
||||||
|
|
||||||
if (result.success) while (true) {
|
if (result.success) while (true) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(200);
|
Thread.sleep(500);
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
final UUID uuid = aSyncSingleCommand(new ActiveBolusesMessage(), "Active boluses");
|
|
||||||
Mstatus mstatus = async.busyWaitForCommandResult(uuid, BUSY_WAIT_TIME);
|
|
||||||
if (mstatus.success()) {
|
|
||||||
final EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
|
final EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance();
|
||||||
ActiveBolusesMessage activeBolusesMessage = (ActiveBolusesMessage) mstatus.getResponseObject();
|
ActiveBolusesMessage activeBolusesMessage = fetchSingleMessage(new ActiveBolusesMessage(), ActiveBolusesMessage.class);
|
||||||
ActiveBolus activeBolus = null;
|
ActiveBolus activeBolus = null;
|
||||||
if (activeBolusesMessage.getBolus1() != null && activeBolusesMessage.getBolus1().getBolusID() == bolusingEvent.bolusId)
|
if (activeBolusesMessage.getBolus1() != null && activeBolusesMessage.getBolus1().getBolusID() == bolusingEvent.bolusId)
|
||||||
activeBolus = activeBolusesMessage.getBolus1();
|
activeBolus = activeBolusesMessage.getBolus1();
|
||||||
|
@ -463,87 +449,60 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
activeBolus = activeBolusesMessage.getBolus3();
|
activeBolus = activeBolusesMessage.getBolus3();
|
||||||
if (activeBolus == null) break;
|
if (activeBolus == null) break;
|
||||||
else {
|
else {
|
||||||
|
int percentBefore = bolusingEvent.percent;
|
||||||
bolusingEvent.percent = (int) (100D / activeBolus.getInitialAmount() * (activeBolus.getInitialAmount() - activeBolus.getLeftoverAmount()));
|
bolusingEvent.percent = (int) (100D / activeBolus.getInitialAmount() * (activeBolus.getInitialAmount() - activeBolus.getLeftoverAmount()));
|
||||||
bolusingEvent.status = String.format(MainApp.gs(R.string.bolusdelivering), activeBolus.getInitialAmount() - activeBolus.getLeftoverAmount());
|
bolusingEvent.status = String.format(MainApp.gs(R.string.bolusdelivering), activeBolus.getInitialAmount() - activeBolus.getLeftoverAmount());
|
||||||
MainApp.bus().post(bolusingEvent);
|
if (percentBefore != bolusingEvent.percent) MainApp.bus().post(bolusingEvent);
|
||||||
}
|
}
|
||||||
} else break;
|
} catch (Exception e) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
connector.requestHistorySync(2000);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stopBolusDelivering() {
|
public void stopBolusDelivering() {
|
||||||
CancelBolusMessage cancelBolusMessage = new CancelBolusMessage();
|
CancelBolusMessage cancelBolusMessage = new CancelBolusMessage();
|
||||||
|
cancelBolusMessage.setMessagePriority(MessagePriority.HIGHEST);
|
||||||
cancelBolusMessage.setBolusId(EventOverviewBolusProgress.getInstance().bolusId);
|
cancelBolusMessage.setBolusId(EventOverviewBolusProgress.getInstance().bolusId);
|
||||||
final UUID cmd = aSyncSingleCommand(cancelBolusMessage, "Cancel standard bolus");
|
try {
|
||||||
|
fetchSingleMessage(cancelBolusMessage);
|
||||||
if (cmd == null) {
|
} catch (Exception e) {
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final Mstatus cs = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME);
|
|
||||||
log("Got command status: " + cs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Temporary Basals
|
// Temporary Basals
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, Profile profile, boolean enforceNew) {
|
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, Profile profile, boolean enforceNew) {
|
||||||
absoluteRate = Helpers.roundDouble(absoluteRate, 3);
|
|
||||||
log("Set TBR absolute: " + absoluteRate);
|
log("Set TBR absolute: " + absoluteRate);
|
||||||
final double base_basal = getBaseBasalRate();
|
if (getBaseBasalRate() == 0) {
|
||||||
if (base_basal == 0) {
|
|
||||||
log("Base basal rate appears to be zero!");
|
log("Base basal rate appears to be zero!");
|
||||||
return pumpEnactFailure();
|
return pumpEnactFailure();
|
||||||
}
|
}
|
||||||
int percent_amount = (int) Math.round(100d / base_basal * absoluteRate);
|
double percent = 100D / getBaseBasalRate() * absoluteRate;
|
||||||
log("Calculated requested rate: " + absoluteRate + " base rate: " + base_basal + " percentage: " + percent_amount + "%");
|
log("Calculated requested rate: " + absoluteRate + " base rate: " + getBaseBasalRate() + " percentage: " + percent + "%");
|
||||||
percent_amount = (int) Math.round(((double) percent_amount) / 10d) * 10;
|
try {
|
||||||
log("Calculated final rate: " + percent_amount + "%");
|
if (percent > 250) {
|
||||||
|
log ("Calculated rate is above 250%, switching to emulation using extended boluses");
|
||||||
if (percent_amount == 100) {
|
cancelTempBasal(true);
|
||||||
return cancelTempBasal(false);
|
if (!setExtendedBolus((absoluteRate - getBaseBasalRate()) / 60D * ((double) durationInMinutes), durationInMinutes).success) {
|
||||||
}
|
//Fallback to TBR if setting an extended bolus didn't work
|
||||||
|
log ("Setting an extended bolus didn't work, falling back to normal TBR");
|
||||||
if (percent_amount > 250) percent_amount = 250;
|
return setTempBasalPercent((int) percent, durationInMinutes, profile, true);
|
||||||
|
}
|
||||||
|
return new PumpEnactResult().success(true).enacted(true).absolute(absoluteRate).duration(durationInMinutes);
|
||||||
final SetTBRTaskRunner task = new SetTBRTaskRunner(connector.getServiceConnector(), percent_amount, durationInMinutes);
|
} else {
|
||||||
final UUID cmd = aSyncTaskRunner(task, "Set TBR abs: " + absoluteRate + " " + durationInMinutes + "m");
|
log ("Calculated rate is below or equal to 250%, using normal TBRs");
|
||||||
|
cancelExtendedBolus();
|
||||||
if (cmd == null) {
|
return setTempBasalPercent((int) percent, durationInMinutes, profile, true);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
return pumpEnactFailure();
|
return pumpEnactFailure();
|
||||||
}
|
}
|
||||||
|
|
||||||
Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME);
|
|
||||||
log("Got command status: " + ms);
|
|
||||||
|
|
||||||
PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).isPercent(true).duration(durationInMinutes);
|
|
||||||
pumpEnactResult.percent = percent_amount;
|
|
||||||
pumpEnactResult.success = ms.success();
|
|
||||||
pumpEnactResult.comment = ms.getCommandComment();
|
|
||||||
|
|
||||||
|
|
||||||
if (pumpEnactResult.success) {
|
|
||||||
// create log entry
|
|
||||||
final TemporaryBasal tempBasal = new TemporaryBasal()
|
|
||||||
.date(System.currentTimeMillis())
|
|
||||||
.percent(percent_amount)
|
|
||||||
.duration(durationInMinutes)
|
|
||||||
.source(Source.USER);
|
|
||||||
TreatmentsPlugin.getPlugin().addToHistoryTempBasal(tempBasal);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Config.logPumpComm)
|
|
||||||
log.debug("Setting temp basal absolute: " + pumpEnactResult.success);
|
|
||||||
|
|
||||||
updateGui();
|
|
||||||
|
|
||||||
connector.requestHistorySync(5000);
|
|
||||||
connector.tryToGetPumpStatusAgain();
|
|
||||||
|
|
||||||
return pumpEnactResult;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -555,42 +514,24 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
if (percent == 100) {
|
if (percent == 100) {
|
||||||
// This would cause a cancel if a tbr is in progress so treat as a cancel
|
// This would cause a cancel if a tbr is in progress so treat as a cancel
|
||||||
return cancelTempBasal(false);
|
return cancelTempBasal(false);
|
||||||
}
|
} else if (percent > 250) percent = 250;
|
||||||
|
|
||||||
|
try {
|
||||||
final UUID cmd = aSyncTaskRunner(new SetTBRTaskRunner(connector.getServiceConnector(), percent, durationInMinutes), "Set TBR " + percent + "%" + " " + durationInMinutes + "m");
|
fetchTaskRunner(new SetTBRTaskRunner(connector.getServiceConnector(), percent, durationInMinutes));
|
||||||
|
|
||||||
if (cmd == null) {
|
|
||||||
return pumpEnactFailure();
|
|
||||||
}
|
|
||||||
|
|
||||||
final Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME);
|
|
||||||
log("Got command status: " + ms);
|
|
||||||
|
|
||||||
PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).isPercent(true).duration(durationInMinutes);
|
|
||||||
pumpEnactResult.percent = percent;
|
|
||||||
pumpEnactResult.success = ms.success();
|
|
||||||
pumpEnactResult.comment = ms.getCommandComment();
|
|
||||||
|
|
||||||
if (pumpEnactResult.success) {
|
|
||||||
// create log entry
|
|
||||||
final TemporaryBasal tempBasal = new TemporaryBasal()
|
final TemporaryBasal tempBasal = new TemporaryBasal()
|
||||||
.date(System.currentTimeMillis())
|
.date(System.currentTimeMillis())
|
||||||
.percent(percent)
|
.percent(percent)
|
||||||
.duration(durationInMinutes)
|
.duration(durationInMinutes)
|
||||||
.source(Source.USER); // TODO check this is correct
|
.source(Source.USER);
|
||||||
TreatmentsPlugin.getPlugin().addToHistoryTempBasal(tempBasal);
|
TreatmentsPlugin.getPlugin().addToHistoryTempBasal(tempBasal);
|
||||||
|
updateGui();
|
||||||
|
if (Config.logPumpComm) log.debug("Set temp basal " + percent + "% for " + durationInMinutes + "m");
|
||||||
|
connector.requestHistorySync(5000);
|
||||||
|
connector.tryToGetPumpStatusAgain();
|
||||||
|
return new PumpEnactResult().success(true).enacted(true).percent(percent);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return pumpEnactFailure();
|
||||||
}
|
}
|
||||||
|
|
||||||
updateGui();
|
|
||||||
|
|
||||||
if (Config.logPumpComm)
|
|
||||||
log.debug("Set temp basal " + percent + "% for " + durationInMinutes + "m");
|
|
||||||
|
|
||||||
connector.requestHistorySync(5000);
|
|
||||||
connector.tryToGetPumpStatusAgain();
|
|
||||||
|
|
||||||
return pumpEnactResult;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -598,35 +539,27 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
public PumpEnactResult cancelTempBasal(boolean enforceNew) {
|
public PumpEnactResult cancelTempBasal(boolean enforceNew) {
|
||||||
log("Cancel TBR");
|
log("Cancel TBR");
|
||||||
|
|
||||||
|
try {
|
||||||
fauxTBRcancel = !SP.getBoolean("insight_real_tbr_cancel", false);
|
cancelExtendedBolus();
|
||||||
|
realTBRCancel();
|
||||||
final UUID cmd;
|
updateGui();
|
||||||
|
if (Config.logPumpComm) log.debug("Canceling temp basal");
|
||||||
if (fauxTBRcancel) {
|
connector.requestHistorySync(5000);
|
||||||
cmd = aSyncTaskRunner(new SetTBRTaskRunner(connector.getServiceConnector(), 100, 1), "Faux Cancel TBR - setting " + "90%" + " 1m");
|
connector.tryToGetPumpStatusAgain();
|
||||||
} else {
|
return new PumpEnactResult().success(true).enacted(true).isTempCancel(true);
|
||||||
cmd = aSyncSingleCommand(new CancelTBRMessage(), "Cancel Temp Basal");
|
} catch (Exception e) {
|
||||||
}
|
|
||||||
if (cmd == null) {
|
|
||||||
return pumpEnactFailure();
|
return pumpEnactFailure();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO isn't conditional on one apparently being in progress only the history change
|
private void realTBRCancel() throws Exception {
|
||||||
final Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME);
|
fauxTBRcancel = !SP.getBoolean("insight_real_tbr_cancel", false);
|
||||||
|
if (fauxTBRcancel) fetchTaskRunner(new SetTBRTaskRunner(connector.getServiceConnector(), 100, 1));
|
||||||
|
else fetchSingleMessage(new CancelTBRMessage());
|
||||||
if (TreatmentsPlugin.getPlugin().isTempBasalInProgress()) {
|
if (TreatmentsPlugin.getPlugin().isTempBasalInProgress()) {
|
||||||
TemporaryBasal tempStop = new TemporaryBasal().date(System.currentTimeMillis()).source(Source.USER);
|
TemporaryBasal tempStop = new TemporaryBasal().date(System.currentTimeMillis()).source(Source.USER);
|
||||||
TreatmentsPlugin.getPlugin().addToHistoryTempBasal(tempStop);
|
TreatmentsPlugin.getPlugin().addToHistoryTempBasal(tempStop);
|
||||||
}
|
}
|
||||||
updateGui();
|
|
||||||
if (Config.logPumpComm)
|
|
||||||
log.debug("Canceling temp basal: "); // TODO get more info
|
|
||||||
|
|
||||||
connector.requestHistorySync(5000);
|
|
||||||
connector.tryToGetPumpStatusAgain();
|
|
||||||
|
|
||||||
return new PumpEnactResult().success(ms.success()).enacted(true).isTempCancel(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -635,88 +568,60 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
@Override
|
@Override
|
||||||
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
|
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
|
||||||
log("Set Extended bolus " + insulin + " " + durationInMinutes);
|
log("Set Extended bolus " + insulin + " " + durationInMinutes);
|
||||||
ExtendedBolusMessage extendedBolusMessage = new ExtendedBolusMessage();
|
try {
|
||||||
extendedBolusMessage.setAmount(insulin);
|
ExtendedBolusMessage extendedBolusMessage = new ExtendedBolusMessage();
|
||||||
extendedBolusMessage.setDuration(durationInMinutes);
|
extendedBolusMessage.setAmount(insulin);
|
||||||
final UUID cmd = aSyncSingleCommand(extendedBolusMessage, "Extended bolus U" + insulin + " mins:" + durationInMinutes);
|
extendedBolusMessage.setDuration(durationInMinutes);
|
||||||
if (cmd == null) {
|
BolusMessage bolusMessage = fetchSingleMessage(extendedBolusMessage, BolusMessage.class);
|
||||||
return pumpEnactFailure();
|
|
||||||
}
|
|
||||||
|
|
||||||
final Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME);
|
|
||||||
log("Got command status: " + ms);
|
|
||||||
|
|
||||||
PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).bolusDelivered(insulin).duration(durationInMinutes);
|
|
||||||
pumpEnactResult.success = ms.success();
|
|
||||||
pumpEnactResult.comment = ms.getCommandComment();
|
|
||||||
|
|
||||||
if (pumpEnactResult.success) {
|
|
||||||
// create log entry
|
|
||||||
final ExtendedBolus extendedBolus = new ExtendedBolus();
|
final ExtendedBolus extendedBolus = new ExtendedBolus();
|
||||||
extendedBolus.date = System.currentTimeMillis();
|
extendedBolus.date = System.currentTimeMillis();
|
||||||
extendedBolus.insulin = insulin;
|
extendedBolus.insulin = insulin;
|
||||||
extendedBolus.durationInMinutes = durationInMinutes;
|
extendedBolus.durationInMinutes = durationInMinutes;
|
||||||
extendedBolus.source = Source.USER;
|
extendedBolus.source = Source.USER;
|
||||||
extendedBolus.pumpId = getRecordUniqueID(ms.getResponseID());
|
extendedBolus.pumpId = getRecordUniqueID(bolusMessage.getBolusId());
|
||||||
TreatmentsPlugin.getPlugin().addToHistoryExtendedBolus(extendedBolus);
|
TreatmentsPlugin.getPlugin().addToHistoryExtendedBolus(extendedBolus);
|
||||||
|
updateGui();
|
||||||
|
connector.requestHistorySync(30000);
|
||||||
|
connector.tryToGetPumpStatusAgain();
|
||||||
|
if (Config.logPumpComm)
|
||||||
|
log.debug("Setting extended bolus: " + insulin + " mins:" + durationInMinutes);
|
||||||
|
return new PumpEnactResult().success(true).enacted(true).duration(durationInMinutes).bolusDelivered(insulin);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return pumpEnactFailure();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Config.logPumpComm)
|
|
||||||
log.debug("Setting extended bolus: " + insulin + " mins:" + durationInMinutes + " " + pumpEnactResult.comment);
|
|
||||||
|
|
||||||
updateGui();
|
|
||||||
|
|
||||||
connector.requestHistorySync(30000);
|
|
||||||
connector.tryToGetPumpStatusAgain();
|
|
||||||
|
|
||||||
return pumpEnactResult;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PumpEnactResult cancelExtendedBolus() {
|
public PumpEnactResult cancelExtendedBolus() {
|
||||||
|
|
||||||
log("Cancel Extended bolus");
|
log("Cancel Extended bolus");
|
||||||
|
|
||||||
// TODO note always sends cancel to pump but only changes history if present
|
Integer bolusId = null;
|
||||||
|
|
||||||
final UUID cmd = aSyncTaskRunner(new CancelBolusTaskRunner(connector.getServiceConnector(), ActiveBolusType.EXTENDED), "Cancel extended bolus");
|
try {
|
||||||
|
bolusId = fetchTaskRunner(new CancelBolusSilentlyTaskRunner(connector.getServiceConnector(), ActiveBolusType.EXTENDED), Integer.class);
|
||||||
if (cmd == null) {
|
if (TreatmentsPlugin.getPlugin().isInHistoryExtendedBoluslInProgress()) {
|
||||||
|
ExtendedBolus exStop = new ExtendedBolus(System.currentTimeMillis());
|
||||||
|
exStop.source = Source.USER;
|
||||||
|
TreatmentsPlugin.getPlugin().addToHistoryExtendedBolus(exStop);
|
||||||
|
}
|
||||||
|
if (Config.logPumpComm) log.debug("Cancel extended bolus:");
|
||||||
|
if (bolusId != null) connector.requestHistorySync(5000);
|
||||||
|
connector.tryToGetPumpStatusAgain();
|
||||||
|
updateGui();
|
||||||
|
return new PumpEnactResult().success(true).enacted(bolusId != null);
|
||||||
|
} catch (Exception e) {
|
||||||
return pumpEnactFailure();
|
return pumpEnactFailure();
|
||||||
}
|
}
|
||||||
|
|
||||||
final Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME);
|
|
||||||
|
|
||||||
if (TreatmentsPlugin.getPlugin().isInHistoryExtendedBoluslInProgress()) {
|
|
||||||
ExtendedBolus exStop = new ExtendedBolus(System.currentTimeMillis());
|
|
||||||
exStop.source = Source.USER;
|
|
||||||
TreatmentsPlugin.getPlugin().addToHistoryExtendedBolus(exStop);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Config.logPumpComm)
|
|
||||||
log.debug("Cancel extended bolus:");
|
|
||||||
|
|
||||||
updateGui();
|
|
||||||
|
|
||||||
connector.requestHistorySync(5000);
|
|
||||||
connector.tryToGetPumpStatusAgain();
|
|
||||||
|
|
||||||
return new PumpEnactResult().success(ms.success()).enacted(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private synchronized UUID deliverBolus(double bolusValue) {
|
private int deliverBolus(double bolusValue) throws Exception {
|
||||||
log("DeliverBolus: " + bolusValue);
|
log("DeliverBolus: " + bolusValue);
|
||||||
|
|
||||||
if (bolusValue == 0) return null;
|
|
||||||
if (bolusValue < 0) return null;
|
|
||||||
|
|
||||||
// TODO check limits here or they already occur via a previous constraint interface?
|
|
||||||
|
|
||||||
final StandardBolusMessage message = new StandardBolusMessage();
|
final StandardBolusMessage message = new StandardBolusMessage();
|
||||||
message.setAmount(bolusValue);
|
message.setAmount(bolusValue);
|
||||||
|
|
||||||
return aSyncSingleCommand(message, "Deliver Bolus " + bolusValue);
|
return fetchSingleMessage(message, BolusMessage.class).getBolusId();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -931,82 +836,34 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Utility
|
private void fetchTaskRunner(TaskRunner taskRunner) throws Exception {
|
||||||
|
fetchTaskRunner(taskRunner, Object.class);
|
||||||
private synchronized UUID aSyncSingleCommand(final AppLayerMessage msg, final String name) {
|
|
||||||
// if (!isConnected()) return false;
|
|
||||||
//if (isBusy()) return false;
|
|
||||||
log("asyncSinglecommand called: " + name);
|
|
||||||
final EventInsightCallback event = new EventInsightCallback();
|
|
||||||
new Thread() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
log("asyncSingleCommand thread");
|
|
||||||
final SingleMessageTaskRunner singleMessageTaskRunner = new SingleMessageTaskRunner(connector.getServiceConnector(), msg);
|
|
||||||
try {
|
|
||||||
singleMessageTaskRunner.fetch(new TaskRunner.ResultCallback() {
|
|
||||||
@Override
|
|
||||||
public void onResult(Object o) {
|
|
||||||
lastDataTime = new Date();
|
|
||||||
log(name + " success");
|
|
||||||
event.response_object = o;
|
|
||||||
if (o instanceof BolusMessage) {
|
|
||||||
event.response_id = ((BolusMessage) o).getBolusId();
|
|
||||||
}
|
|
||||||
event.success = true;
|
|
||||||
pushCallbackEvent(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(Exception e) {
|
|
||||||
log(name + " error");
|
|
||||||
event.message = e.getMessage();
|
|
||||||
pushCallbackEvent(event);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
log("EXCEPTION" + e.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.start();
|
|
||||||
return event.request_uuid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized UUID aSyncTaskRunner(final TaskRunner task, final String name) {
|
private void fetchSingleMessage(AppLayerMessage message) throws Exception {
|
||||||
// if (!isConnected()) return false;
|
fetchSingleMessage(message, AppLayerMessage.class);
|
||||||
//if (isBusy()) return false;
|
}
|
||||||
log("asyncTaskRunner called: " + name);
|
|
||||||
final EventInsightCallback event = new EventInsightCallback();
|
|
||||||
new Thread() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
log("asyncTaskRunner thread");
|
|
||||||
try {
|
|
||||||
task.fetch(new TaskRunner.ResultCallback() {
|
|
||||||
@Override
|
|
||||||
public void onResult(Object o) {
|
|
||||||
lastDataTime = new Date();
|
|
||||||
log(name + " success");
|
|
||||||
event.response_object = o;
|
|
||||||
event.success = true;
|
|
||||||
pushCallbackEvent(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
private <T> T fetchTaskRunner(TaskRunner taskRunner, Class<T> resultType) throws Exception {
|
||||||
public void onError(Exception e) {
|
try {
|
||||||
log(name + " error");
|
T result = (T) taskRunner.fetchAndWaitUsingLatch(BUSY_WAIT_TIME);
|
||||||
event.message = e.getMessage();
|
lastDataTime = new Date();
|
||||||
pushCallbackEvent(event);
|
return result;
|
||||||
}
|
} catch (Exception e) {
|
||||||
});
|
log("Error while fetching " + taskRunner.getClass().getSimpleName() + ": " + e.getClass().getSimpleName());
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
private <T extends AppLayerMessage> T fetchSingleMessage(AppLayerMessage message, Class<T> resultType) throws Exception {
|
||||||
log("EXCEPTION" + e.toString());
|
try {
|
||||||
}
|
T result = (T) new SingleMessageTaskRunner(connector.getServiceConnector(), message).fetchAndWaitUsingLatch(BUSY_WAIT_TIME);
|
||||||
}
|
lastDataTime = new Date();
|
||||||
}.start();
|
return result;
|
||||||
return event.request_uuid;
|
} catch (Exception e) {
|
||||||
|
log("Error while fetching " + message.getClass().getSimpleName() + ": " + e.getClass().getSimpleName());
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1016,14 +873,6 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
|
|
||||||
// Constraints
|
// Constraints
|
||||||
|
|
||||||
@Override
|
|
||||||
public Constraint<Double> applyBasalConstraints(Constraint<Double> absoluteRate, Profile profile) {
|
|
||||||
if (statusResult != null) {
|
|
||||||
absoluteRate.setIfSmaller(statusResult.maximumBasalAmount, String.format(MainApp.gs(R.string.limitingbasalratio), statusResult.maximumBasalAmount, MainApp.gs(R.string.pumplimit)), this);
|
|
||||||
}
|
|
||||||
return absoluteRate;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Constraint<Integer> applyBasalPercentConstraints(Constraint<Integer> percentRate, Profile profile) {
|
public Constraint<Integer> applyBasalPercentConstraints(Constraint<Integer> percentRate, Profile profile) {
|
||||||
percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.itmustbepositivevalue)), this);
|
percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.itmustbepositivevalue)), this);
|
||||||
|
@ -1034,8 +883,10 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Constraint<Double> applyBolusConstraints(Constraint<Double> insulin) {
|
public Constraint<Double> applyBolusConstraints(Constraint<Double> insulin) {
|
||||||
if (statusResult != null)
|
if (statusResult != null) {
|
||||||
insulin.setIfSmaller(statusResult.maximumBolusAmount, String.format(MainApp.gs(R.string.limitingbolus), statusResult.maximumBolusAmount, MainApp.gs(R.string.pumplimit)), this);
|
insulin.setIfSmaller(statusResult.maximumBolusAmount, String.format(MainApp.gs(R.string.limitingbolus), statusResult.maximumBolusAmount, MainApp.gs(R.string.pumplimit)), this);
|
||||||
|
insulin.setIfGreater(statusResult.minimumBolusAmount, String.format(MainApp.gs(R.string.limitingbolus), statusResult.maximumBolusAmount, MainApp.gs(R.string.pumplimit)), this);
|
||||||
|
}
|
||||||
return insulin;
|
return insulin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,50 +0,0 @@
|
||||||
package info.nightscout.androidaps.plugins.PumpInsight;
|
|
||||||
|
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightCallback;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by jamorham on 01/02/2018.
|
|
||||||
*
|
|
||||||
* Encapsulates results from commands
|
|
||||||
*/
|
|
||||||
|
|
||||||
class Mstatus {
|
|
||||||
|
|
||||||
Cstatus cstatus = Cstatus.UNKNOWN;
|
|
||||||
EventInsightCallback event;
|
|
||||||
|
|
||||||
// comment field preparation for results
|
|
||||||
String getCommandComment() {
|
|
||||||
if (success()) {
|
|
||||||
return "OK";
|
|
||||||
} else {
|
|
||||||
return (event == null) ? "EVENT DATA IS NULL - ERROR OR FIREWALL ENABLED?" : event.message;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean success() {
|
|
||||||
return cstatus.success();
|
|
||||||
}
|
|
||||||
|
|
||||||
int getResponseID() {
|
|
||||||
if (success()) {
|
|
||||||
return event.response_id;
|
|
||||||
} else {
|
|
||||||
return -2; // invalid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Object getResponseObject() {
|
|
||||||
if (success()) {
|
|
||||||
return event.response_object;
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return cstatus + " " + event;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,66 @@
|
||||||
|
package info.nightscout.androidaps.plugins.PumpInsight.connector;
|
||||||
|
|
||||||
|
import sugar.free.sightparser.applayer.descriptors.ActiveBolusType;
|
||||||
|
import sugar.free.sightparser.applayer.descriptors.MessagePriority;
|
||||||
|
import sugar.free.sightparser.applayer.descriptors.alerts.Warning38BolusCancelled;
|
||||||
|
import sugar.free.sightparser.applayer.messages.AppLayerMessage;
|
||||||
|
import sugar.free.sightparser.applayer.messages.remote_control.CancelBolusMessage;
|
||||||
|
import sugar.free.sightparser.applayer.messages.remote_control.DismissAlertMessage;
|
||||||
|
import sugar.free.sightparser.applayer.messages.status.ActiveAlertMessage;
|
||||||
|
import sugar.free.sightparser.applayer.messages.status.ActiveBolusesMessage;
|
||||||
|
import sugar.free.sightparser.handling.SightServiceConnector;
|
||||||
|
import sugar.free.sightparser.handling.TaskRunner;
|
||||||
|
|
||||||
|
// by Tebbe Ubben
|
||||||
|
|
||||||
|
public class CancelBolusSilentlyTaskRunner extends TaskRunner {
|
||||||
|
|
||||||
|
private ActiveBolusType bolusType;
|
||||||
|
private long cancelledAt;
|
||||||
|
private int bolusId;
|
||||||
|
|
||||||
|
public CancelBolusSilentlyTaskRunner(SightServiceConnector serviceConnector, ActiveBolusType bolusType) {
|
||||||
|
super(serviceConnector);
|
||||||
|
this.bolusType = bolusType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected AppLayerMessage run(AppLayerMessage message) throws Exception {
|
||||||
|
if (message == null) return new ActiveBolusesMessage();
|
||||||
|
else if (message instanceof ActiveBolusesMessage) {
|
||||||
|
ActiveBolusesMessage bolusesMessage = (ActiveBolusesMessage) message;
|
||||||
|
CancelBolusMessage cancelBolusMessage = new CancelBolusMessage();
|
||||||
|
if (bolusesMessage.getBolus1().getBolusType() == bolusType)
|
||||||
|
bolusId = bolusesMessage.getBolus1().getBolusID();
|
||||||
|
else if (bolusesMessage.getBolus2().getBolusType() == bolusType)
|
||||||
|
bolusId = bolusesMessage.getBolus2().getBolusID();
|
||||||
|
else if (bolusesMessage.getBolus3().getBolusType() == bolusType)
|
||||||
|
bolusId = bolusesMessage.getBolus3().getBolusID();
|
||||||
|
else finish(null);
|
||||||
|
cancelBolusMessage.setBolusId(bolusId);
|
||||||
|
return cancelBolusMessage;
|
||||||
|
} else if (message instanceof CancelBolusMessage) {
|
||||||
|
cancelledAt = System.currentTimeMillis();
|
||||||
|
ActiveAlertMessage activeAlertMessage = new ActiveAlertMessage();
|
||||||
|
activeAlertMessage.setMessagePriority(MessagePriority.HIGHER);
|
||||||
|
return activeAlertMessage;
|
||||||
|
} else if (message instanceof ActiveAlertMessage) {
|
||||||
|
ActiveAlertMessage activeAlertMessage = (ActiveAlertMessage) message;
|
||||||
|
if (activeAlertMessage.getAlert() == null) {
|
||||||
|
if (System.currentTimeMillis() - cancelledAt >= 10000) finish(null);
|
||||||
|
else {
|
||||||
|
ActiveAlertMessage activeAlertMessage2 = new ActiveAlertMessage();
|
||||||
|
activeAlertMessage2.setMessagePriority(MessagePriority.HIGHER);
|
||||||
|
return activeAlertMessage2;
|
||||||
|
}
|
||||||
|
} else if (!(activeAlertMessage.getAlert() instanceof Warning38BolusCancelled)) finish(null);
|
||||||
|
else {
|
||||||
|
DismissAlertMessage dismissAlertMessage = new DismissAlertMessage();
|
||||||
|
dismissAlertMessage.setAlertID(activeAlertMessage.getAlertID());
|
||||||
|
dismissAlertMessage.setMessagePriority(MessagePriority.HIGHER);
|
||||||
|
return dismissAlertMessage;
|
||||||
|
}
|
||||||
|
} else if (message instanceof DismissAlertMessage) finish(bolusId);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,6 +12,8 @@ import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfil
|
||||||
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfile5Block;
|
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfile5Block;
|
||||||
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfileBlock;
|
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfileBlock;
|
||||||
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.ConfigurationBlock;
|
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.ConfigurationBlock;
|
||||||
|
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.FactoryMinBRAmountBlock;
|
||||||
|
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.FactoryMinBolusAmountBlock;
|
||||||
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.MaxBRAmountBlock;
|
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.MaxBRAmountBlock;
|
||||||
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.MaxBolusAmountBlock;
|
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.MaxBolusAmountBlock;
|
||||||
import sugar.free.sightparser.applayer.messages.AppLayerMessage;
|
import sugar.free.sightparser.applayer.messages.AppLayerMessage;
|
||||||
|
@ -102,6 +104,16 @@ public class StatusTaskRunner extends TaskRunner {
|
||||||
return readMessage;
|
return readMessage;
|
||||||
} else if (configurationBlock instanceof MaxBRAmountBlock) {
|
} else if (configurationBlock instanceof MaxBRAmountBlock) {
|
||||||
result.maximumBasalAmount = ((MaxBRAmountBlock) configurationBlock).getMaximumAmount();
|
result.maximumBasalAmount = ((MaxBRAmountBlock) configurationBlock).getMaximumAmount();
|
||||||
|
ReadConfigurationBlockMessage readMessage = new ReadConfigurationBlockMessage();
|
||||||
|
readMessage.setConfigurationBlockID(FactoryMinBRAmountBlock.ID);
|
||||||
|
return readMessage;
|
||||||
|
} else if (configurationBlock instanceof FactoryMinBRAmountBlock) {
|
||||||
|
result.minimumBasalAmount = ((FactoryMinBRAmountBlock) configurationBlock).getMinimumAmount();
|
||||||
|
ReadConfigurationBlockMessage readMessage = new ReadConfigurationBlockMessage();
|
||||||
|
readMessage.setConfigurationBlockID(FactoryMinBolusAmountBlock.ID);
|
||||||
|
return readMessage;
|
||||||
|
} else if (configurationBlock instanceof FactoryMinBolusAmountBlock) {
|
||||||
|
result.minimumBolusAmount = ((FactoryMinBolusAmountBlock) configurationBlock).getMinimumAmount();
|
||||||
finish(result);
|
finish(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -122,5 +134,7 @@ public class StatusTaskRunner extends TaskRunner {
|
||||||
public List<BRProfileBlock.ProfileBlock> basalProfile;
|
public List<BRProfileBlock.ProfileBlock> basalProfile;
|
||||||
public double maximumBolusAmount;
|
public double maximumBolusAmount;
|
||||||
public double maximumBasalAmount;
|
public double maximumBasalAmount;
|
||||||
|
public double minimumBolusAmount;
|
||||||
|
public double minimumBasalAmount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -766,6 +766,7 @@
|
||||||
<string name="dexcomg5_xdripupload_summary">In xDrip+ select 640g/Eversense data source</string>
|
<string name="dexcomg5_xdripupload_summary">In xDrip+ select 640g/Eversense data source</string>
|
||||||
<string name="nsclientbg">NSClient BG</string>
|
<string name="nsclientbg">NSClient BG</string>
|
||||||
<string name="minimalbasalvaluereplaced">Basal value replaced by minimal supported value</string>
|
<string name="minimalbasalvaluereplaced">Basal value replaced by minimal supported value</string>
|
||||||
|
<string name="maximumbasalvaluereplaced">Basal value replaced by maximum supported value</string>
|
||||||
<string name="overview_editquickwizard_usebg">BG calculation</string>
|
<string name="overview_editquickwizard_usebg">BG calculation</string>
|
||||||
<string name="overview_editquickwizard_usebolusiob">Bolus IOB calculation</string>
|
<string name="overview_editquickwizard_usebolusiob">Bolus IOB calculation</string>
|
||||||
<string name="overview_editquickwizard_usebasaliob">Basal IOB calculation</string>
|
<string name="overview_editquickwizard_usebasaliob">Basal IOB calculation</string>
|
||||||
|
|
Loading…
Reference in a new issue