redesign uploading tb&eb to NS

This commit is contained in:
Milos Kozak 2017-05-29 19:43:19 +02:00
parent dec627946d
commit ec76908eca
6 changed files with 110 additions and 123 deletions

View file

@ -27,9 +27,6 @@ public class PumpEnactResult extends Object {
public Double bolusDelivered = 0d; // real value of delivered insulin
public Integer carbsDelivered = 0; // real value of delivered carbs
public Double originalExtendedAmount = null; // amount of extended bolus insulin if uploading as temp basal
public Boolean isFakedTempBasal = null; // true if canceling extended bolus but uploading as temp basal
public boolean queued = false;
public String log() {

View file

@ -61,6 +61,19 @@ public class TemporaryBasal implements Interval {
this.absoluteRate = basal + extendedBolus.absoluteRate();
}
public TemporaryBasal clone() {
TemporaryBasal t = new TemporaryBasal();
t.date = date;
t.isValid = isValid;
t.source = source;
t._id = _id;
t.durationInMinutes = durationInMinutes;
t.isAbsolute = isAbsolute;
t.percentRate = percentRate;
t.absoluteRate = absoluteRate;
return t;
}
// -------- Interval interface ---------
Long cuttedEnd = null;

View file

@ -406,7 +406,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
public PumpEnactResult deliverTreatmentFromBolusWizard(InsulinInterface insulinType, Context context, Double insulin, Integer carbs, Double glucose, String glucoseType, int carbTime, JSONObject boluscalc) {
mWakeLock.acquire();
PumpEnactResult result;
if (activePump != null) {
insulin = applyBolusConstraints(insulin);
carbs = applyCarbsConstraints(carbs);
@ -430,27 +429,12 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
t.insulin = result.bolusDelivered;
if (carbTime == 0)
t.carbs = (double) result.carbsDelivered; // with different carbTime record will come back from nightscout
t.date = new Date().getDate();
t.date = new Date().getTime();
t.mealBolus = result.carbsDelivered > 0;
addTreatmentToHistory(t);
t.carbs = (double) result.carbsDelivered;
NSUpload.uploadBolusWizardRecord(t, glucose, glucoseType, carbTime, boluscalc);
}
} else {
if (Config.logCongigBuilderActions)
log.debug("Creating treatment: " + insulin + " carbs: " + carbs);
Treatment t = new Treatment(insulinType);
t.insulin = insulin;
t.carbs = (double) carbs;
t.date = new Date().getDate();
t.mealBolus = t.carbs > 0;
addTreatmentToHistory(t);
NSUpload.uploadTreatment(t);
result = new PumpEnactResult();
result.success = true;
result.bolusDelivered = insulin;
result.carbsDelivered = carbs;
}
mWakeLock.release();
return result;
}
@ -463,7 +447,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
public PumpEnactResult deliverTreatment(InsulinInterface insulinType, Double insulin, Integer carbs, Context context, boolean createTreatment) {
mWakeLock.acquire();
PumpEnactResult result;
if (activePump != null) {
insulin = applyBolusConstraints(insulin);
carbs = applyCarbsConstraints(carbs);
@ -500,11 +483,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
addTreatmentToHistory(t);
NSUpload.uploadTreatment(t);
}
} else {
log.error("activePump==null");
result = new PumpEnactResult();
result.success = false;
}
mWakeLock.release();
return result;
}
@ -528,13 +506,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
PumpEnactResult result = activePump.setTempBasalAbsolute(rateAfterConstraints, durationInMinutes);
if (Config.logCongigBuilderActions)
log.debug("setTempBasalAbsolute rate: " + rateAfterConstraints + " durationInMinutes: " + durationInMinutes + " success: " + result.success + " enacted: " + result.enacted);
if (result.enacted && result.success) {
if (result.isPercent) {
NSUpload.uploadTempBasalStartPercent(result.percent, result.duration);
} else {
NSUpload.uploadTempBasalStartAbsolute(result.absolute, result.duration, result.originalExtendedAmount);
}
}
return result;
}
@ -551,9 +522,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
PumpEnactResult result = activePump.setTempBasalPercent(percentAfterConstraints, durationInMinutes);
if (Config.logCongigBuilderActions)
log.debug("setTempBasalPercent percent: " + percentAfterConstraints + " durationInMinutes: " + durationInMinutes + " success: " + result.success + " enacted: " + result.enacted);
if (result.enacted && result.success) {
NSUpload.uploadTempBasalStartPercent(result.percent, result.duration);
}
return result;
}
@ -563,9 +531,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
PumpEnactResult result = activePump.setExtendedBolus(rateAfterConstraints, durationInMinutes);
if (Config.logCongigBuilderActions)
log.debug("setExtendedBolus rate: " + rateAfterConstraints + " durationInMinutes: " + durationInMinutes + " success: " + result.success + " enacted: " + result.enacted);
if (result.enacted && result.success) {
NSUpload.uploadExtendedBolus(result.bolusDelivered, result.duration);
}
return result;
}
@ -574,9 +539,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
PumpEnactResult result = activePump.cancelTempBasal();
if (Config.logCongigBuilderActions)
log.debug("cancelTempBasal success: " + result.success + " enacted: " + result.enacted);
if (result.enacted && result.success) {
NSUpload.uploadTempBasalEnd(result.isFakedTempBasal);
}
return result;
}
@ -585,9 +547,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
PumpEnactResult result = activePump.cancelExtendedBolus();
if (Config.logCongigBuilderActions)
log.debug("cancelExtendedBolus success: " + result.success + " enacted: " + result.enacted);
if (result.enacted && result.success) {
NSUpload.uploadExtendedBolusEnd();
}
return result;
}
@ -898,11 +857,17 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
@Override
public void addToHistoryTempBasalStart(TemporaryBasal tempBasal) {
activeTreatments.addToHistoryTempBasalStart(tempBasal);
if (tempBasal.isAbsolute)
NSUpload.uploadTempBasalStartAbsolute(tempBasal, null);
else
NSUpload.uploadTempBasalStartPercent(tempBasal);
}
@Override
public void addToHistoryTempBasalStop(long time) {
activeTreatments.addToHistoryTempBasalStop(time);
NSUpload.uploadTempBasalEnd(time, false);
}
@Override
@ -918,11 +883,19 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
@Override
public void addToHistoryExtendedBolusStart(ExtendedBolus extendedBolus) {
activeTreatments.addToHistoryExtendedBolusStart(extendedBolus);
if (activePump.isFakingTempsByExtendedBoluses())
NSUpload.uploadTempBasalStartAbsolute(new TemporaryBasal(extendedBolus), extendedBolus.insulin);
else
NSUpload.uploadExtendedBolus(extendedBolus);
}
@Override
public void addToHistoryExtendedBolusStop(long time) {
activeTreatments.addToHistoryExtendedBolusStop(time);
if (activePump.isFakingTempsByExtendedBoluses())
NSUpload.uploadTempBasalEnd(time, true);
else
NSUpload.uploadExtendedBolusEnd(time);
}
@Override

View file

@ -472,7 +472,6 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Extended bolus set ok");
result.absolute = result.absolute + getBaseBasalRate();
result.originalExtendedAmount = extendedAmount;
return result;
}
// We should never end here
@ -578,7 +577,6 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf
return cancelRealTempBasal();
if (MainApp.getConfigBuilder().isInHistoryExtendedBoluslInProgress() && useExtendedBoluses) {
PumpEnactResult cancelEx = cancelExtendedBolus();
cancelEx.isFakedTempBasal = true;
return cancelEx;
}
PumpEnactResult result = new PumpEnactResult();

View file

@ -475,7 +475,6 @@ public class DanaRKoreanPlugin implements PluginBase, PumpInterface, Constraints
if (Config.logPumpActions)
log.debug("setTempBasalAbsolute: Extended bolus set ok");
result.absolute = result.absolute + getBaseBasalRate();
result.originalExtendedAmount = extendedAmount;
return result;
}
// We should never end here
@ -581,7 +580,6 @@ public class DanaRKoreanPlugin implements PluginBase, PumpInterface, Constraints
return cancelRealTempBasal();
if (MainApp.getConfigBuilder().isInHistoryExtendedBoluslInProgress() && useExtendedBoluses) {
PumpEnactResult cancelEx = cancelExtendedBolus();
cancelEx.isFakedTempBasal = true;
return cancelEx;
}
PumpEnactResult result = new PumpEnactResult();

View file

@ -17,11 +17,14 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.Intents;
import info.nightscout.androidaps.db.CareportalEvent;
import info.nightscout.androidaps.db.ExtendedBolus;
import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.plugins.Loop.APSResult;
import info.nightscout.androidaps.plugins.Loop.DeviceStatus;
import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
import info.nightscout.androidaps.plugins.NSClientInternal.data.DbLogger;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
import info.nightscout.androidaps.plugins.OpenAPSAMA.DetermineBasalResultAMA;
import info.nightscout.androidaps.plugins.OpenAPSMA.DetermineBasalResultMA;
@ -32,16 +35,16 @@ import info.nightscout.androidaps.plugins.OpenAPSMA.DetermineBasalResultMA;
public class NSUpload {
private static Logger log = LoggerFactory.getLogger(NSUpload.class);
public static void uploadTempBasalStartAbsolute(Double absolute, double durationInMinutes, Double originalExtendedAmount) {
public static void uploadTempBasalStartAbsolute(TemporaryBasal temporaryBasal, Double originalExtendedAmount) {
try {
Context context = MainApp.instance().getApplicationContext();
JSONObject data = new JSONObject();
data.put("eventType", CareportalEvent.TEMPBASAL);
data.put("duration", durationInMinutes);
data.put("absolute", absolute);
data.put("created_at", DateUtil.toISOString(new Date()));
data.put("duration", temporaryBasal.durationInMinutes);
data.put("absolute", temporaryBasal.absoluteRate);
data.put("created_at", DateUtil.toISOString(temporaryBasal.date));
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalstartnote) + " " + absolute + "u/h " + durationInMinutes + " min"); // ECOR
data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalstartnote) + " " + temporaryBasal.absoluteRate + "u/h " + temporaryBasal.durationInMinutes + " min"); // ECOR
if (originalExtendedAmount != null)
data.put("originalExtendedAmount", originalExtendedAmount); // for back synchronization
Bundle bundle = new Bundle();
@ -58,22 +61,27 @@ public class NSUpload {
}
}
public static void uploadTempBasalStartPercent(Integer percent, double durationInMinutes) {
public static void uploadTempBasalStartPercent(TemporaryBasal temporaryBasal) {
try {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
boolean useAbsolute = SP.getBoolean("ns_sync_use_absolute", false);
if (useAbsolute) {
double absolute = MainApp.getConfigBuilder().getBaseBasalRate() * percent / 100d;
uploadTempBasalStartAbsolute(absolute, durationInMinutes, null);
TemporaryBasal t = temporaryBasal.clone();
t.isAbsolute = true;
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile != null) {
t.absoluteRate = profile.getBasal(NSProfile.secondsFromMidnight(temporaryBasal.date)) * temporaryBasal.percentRate / 100d;
uploadTempBasalStartAbsolute(t, null);
}
} else {
Context context = MainApp.instance().getApplicationContext();
JSONObject data = new JSONObject();
data.put("eventType", CareportalEvent.TEMPBASAL);
data.put("duration", durationInMinutes);
data.put("percent", percent - 100);
data.put("created_at", DateUtil.toISOString(new Date()));
data.put("duration", temporaryBasal.durationInMinutes);
data.put("percent", temporaryBasal.percentRate - 100);
data.put("created_at", DateUtil.toISOString(temporaryBasal.date));
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalstartnote) + " " + percent + "% " + durationInMinutes + " min"); // ECOR
data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalstartnote) + " " + temporaryBasal.percentRate + "% " + temporaryBasal.durationInMinutes + " min"); // ECOR
Bundle bundle = new Bundle();
bundle.putString("action", "dbAdd");
bundle.putString("collection", "treatments");
@ -89,15 +97,15 @@ public class NSUpload {
}
}
public static void uploadTempBasalEnd(Boolean isFakedTempBasal) {
public static void uploadTempBasalEnd(long time, boolean isFakedTempBasal) {
try {
Context context = MainApp.instance().getApplicationContext();
JSONObject data = new JSONObject();
data.put("eventType", CareportalEvent.TEMPBASAL);
data.put("created_at", DateUtil.toISOString(new Date()));
data.put("created_at", DateUtil.toISOString(time));
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalendnote)); // ECOR
if (isFakedTempBasal != null)
if (isFakedTempBasal)
data.put("isFakedTempBasal", isFakedTempBasal);
Bundle bundle = new Bundle();
bundle.putString("action", "dbAdd");
@ -113,17 +121,17 @@ public class NSUpload {
}
}
public static void uploadExtendedBolus(Double insulin, double durationInMinutes) {
public static void uploadExtendedBolus(ExtendedBolus extendedBolus) {
try {
Context context = MainApp.instance().getApplicationContext();
JSONObject data = new JSONObject();
data.put("eventType", CareportalEvent.COMBOBOLUS);
data.put("duration", durationInMinutes);
data.put("duration", extendedBolus.durationInMinutes);
data.put("splitNow", 0);
data.put("splitExt", 100);
data.put("enteredinsulin", insulin);
data.put("relative", insulin);
data.put("created_at", DateUtil.toISOString(new Date()));
data.put("enteredinsulin", extendedBolus.insulin);
data.put("relative", extendedBolus.insulin);
data.put("created_at", DateUtil.toISOString(extendedBolus.date));
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
Bundle bundle = new Bundle();
bundle.putString("action", "dbAdd");
@ -139,7 +147,7 @@ public class NSUpload {
}
}
public static void uploadExtendedBolusEnd() {
public static void uploadExtendedBolusEnd(long time) {
try {
Context context = MainApp.instance().getApplicationContext();
JSONObject data = new JSONObject();
@ -149,7 +157,7 @@ public class NSUpload {
data.put("splitExt", 100);
data.put("enteredinsulin", 0);
data.put("relative", 0);
data.put("created_at", DateUtil.toISOString(new Date()));
data.put("created_at", DateUtil.toISOString(time));
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
Bundle bundle = new Bundle();
bundle.putString("action", "dbAdd");