Insight EB handling

This commit is contained in:
Milos Kozak 2018-09-13 18:53:16 +02:00
parent af089b4dcf
commit 57e7d63794
8 changed files with 113 additions and 53 deletions

View file

@ -961,13 +961,13 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
public void createTempBasalFromJsonIfNotExists(JSONObject trJson) { public void createTempBasalFromJsonIfNotExists(JSONObject trJson) {
try { try {
if (trJson.has("originalExtendedAmount")) { // extended bolus uploaded as temp basal if (trJson.has("originalExtendedAmount")) { // extended bolus uploaded as temp basal
ExtendedBolus extendedBolus = new ExtendedBolus(); ExtendedBolus extendedBolus = new ExtendedBolus()
extendedBolus.source = Source.NIGHTSCOUT; .source(Source.NIGHTSCOUT)
extendedBolus.date = trJson.getLong("mills"); .date(trJson.getLong("mills"))
extendedBolus.pumpId = trJson.has("pumpId") ? trJson.getLong("pumpId") : 0; .pumpId(trJson.has("pumpId") ? trJson.getLong("pumpId") : 0)
extendedBolus.durationInMinutes = trJson.getInt("duration"); .durationInMinutes(trJson.getInt("duration"))
extendedBolus.insulin = trJson.getDouble("originalExtendedAmount"); .insulin(trJson.getDouble("originalExtendedAmount"))
extendedBolus._id = trJson.getString("_id"); ._id(trJson.getString("_id"));
// if faking found in NS, adapt AAPS to use it too // if faking found in NS, adapt AAPS to use it too
if (!VirtualPumpPlugin.getPlugin().getFakingStatus()) { if (!VirtualPumpPlugin.getPlugin().getFakingStatus()) {
VirtualPumpPlugin.getPlugin().setFakingStatus(true); VirtualPumpPlugin.getPlugin().setFakingStatus(true);

View file

@ -70,6 +70,36 @@ public class ExtendedBolus implements Interval, DataPointWithLabelInterface {
this.date = date; this.date = date;
} }
public ExtendedBolus date(long date) {
this.date = date;
return this;
}
public ExtendedBolus insulin(double insulin) {
this.insulin = insulin;
return this;
}
public ExtendedBolus pumpId(long pumpId) {
this.pumpId = pumpId;
return this;
}
public ExtendedBolus source(int source) {
this.source = source;
return this;
}
public ExtendedBolus durationInMinutes(int durationInMinutes) {
this.durationInMinutes = durationInMinutes;
return this;
}
public ExtendedBolus _id(String _id) {
this._id = _id;
return this;
}
public boolean isEqual(ExtendedBolus other) { public boolean isEqual(ExtendedBolus other) {
if (date != other.date) { if (date != other.date) {
return false; return false;
@ -94,13 +124,13 @@ public class ExtendedBolus implements Interval, DataPointWithLabelInterface {
} }
public static ExtendedBolus createFromJson(JSONObject json) { public static ExtendedBolus createFromJson(JSONObject json) {
ExtendedBolus extendedBolus = new ExtendedBolus(); ExtendedBolus extendedBolus = new ExtendedBolus()
extendedBolus.source = Source.NIGHTSCOUT; .source(Source.NIGHTSCOUT)
extendedBolus.date = JsonHelper.safeGetLong(json, "mills"); .date(JsonHelper.safeGetLong(json, "mills"))
extendedBolus.durationInMinutes = JsonHelper.safeGetInt(json, "duration"); .durationInMinutes(JsonHelper.safeGetInt(json, "duration"))
extendedBolus.insulin = JsonHelper.safeGetDouble(json, "relative") / 60 * extendedBolus.durationInMinutes; .insulin(JsonHelper.safeGetDouble(json, "relative") / 60 * JsonHelper.safeGetInt(json, "duration"))
extendedBolus._id = JsonHelper.safeGetString(json, "_id"); ._id(JsonHelper.safeGetString(json, "_id"))
extendedBolus.pumpId = JsonHelper.safeGetLong(json, "pumpId"); .pumpId(JsonHelper.safeGetLong(json, "pumpId"));
return extendedBolus; return extendedBolus;
} }
// -------- Interval interface --------- // -------- Interval interface ---------

View file

@ -79,27 +79,27 @@ public class MsgStatusBolusExtended extends MessageBase {
exStop.source = Source.USER; exStop.source = Source.USER;
treatmentsInterface.addToHistoryExtendedBolus(exStop); treatmentsInterface.addToHistoryExtendedBolus(exStop);
// Create new // Create new
ExtendedBolus newExtended = new ExtendedBolus(); ExtendedBolus newExtended = new ExtendedBolus()
newExtended.date = pump.extendedBolusStart; .date(pump.extendedBolusStart)
newExtended.insulin = pump.extendedBolusAmount; .insulin(pump.extendedBolusAmount)
newExtended.durationInMinutes = pump.extendedBolusMinutes; .durationInMinutes(pump.extendedBolusMinutes)
newExtended.source = Source.USER; .source(Source.USER);
treatmentsInterface.addToHistoryExtendedBolus(newExtended); treatmentsInterface.addToHistoryExtendedBolus(newExtended);
} }
} else { } else {
// Close curent temp basal // Close curent temp basal
ExtendedBolus exStop = new ExtendedBolus(now); ExtendedBolus exStop = new ExtendedBolus(now)
exStop.source = Source.USER; .source(Source.USER);
treatmentsInterface.addToHistoryExtendedBolus(exStop); treatmentsInterface.addToHistoryExtendedBolus(exStop);
} }
} else { } else {
if (pump.isExtendedInProgress) { if (pump.isExtendedInProgress) {
// Create new // Create new
ExtendedBolus newExtended = new ExtendedBolus(); ExtendedBolus newExtended = new ExtendedBolus()
newExtended.date = pump.extendedBolusStart; .date(pump.extendedBolusStart)
newExtended.insulin = pump.extendedBolusAmount; .insulin(pump.extendedBolusAmount)
newExtended.durationInMinutes = pump.extendedBolusMinutes; .durationInMinutes(pump.extendedBolusMinutes)
newExtended.source = Source.USER; .source(Source.USER);
treatmentsInterface.addToHistoryExtendedBolus(newExtended); treatmentsInterface.addToHistoryExtendedBolus(newExtended);
} }
} }

View file

@ -89,10 +89,7 @@ public class DanaRS_Packet_APS_History_Events extends DanaRS_Packet {
TemporaryBasal temporaryBasal = new TemporaryBasal().date(datetime).source(Source.PUMP).pumpId(datetime); TemporaryBasal temporaryBasal = new TemporaryBasal().date(datetime).source(Source.PUMP).pumpId(datetime);
ExtendedBolus extendedBolus = new ExtendedBolus(); ExtendedBolus extendedBolus = new ExtendedBolus().date(datetime).source(Source.PUMP).pumpId(datetime);
extendedBolus.date = datetime;
extendedBolus.source = Source.PUMP;
extendedBolus.pumpId = datetime;
String status; String status;

View file

@ -67,10 +67,10 @@ public class MsgHistoryEvents_v2 extends MessageBase {
.source(Source.PUMP) .source(Source.PUMP)
.pumpId(datetime); .pumpId(datetime);
ExtendedBolus extendedBolus = new ExtendedBolus(); ExtendedBolus extendedBolus = new ExtendedBolus()
extendedBolus.date = datetime; .date(datetime)
extendedBolus.source = Source.PUMP; .source(Source.PUMP)
extendedBolus.pumpId = datetime; .pumpId(datetime);
String status = ""; String status = "";

View file

@ -609,12 +609,12 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
extendedBolusMessage.setAmount(insulin); extendedBolusMessage.setAmount(insulin);
extendedBolusMessage.setDuration(durationInMinutes); extendedBolusMessage.setDuration(durationInMinutes);
BolusMessage bolusMessage = fetchSingleMessage(extendedBolusMessage, BolusMessage.class); BolusMessage bolusMessage = fetchSingleMessage(extendedBolusMessage, BolusMessage.class);
final ExtendedBolus extendedBolus = new ExtendedBolus(); final ExtendedBolus extendedBolus = new ExtendedBolus()
extendedBolus.date = System.currentTimeMillis(); .date(System.currentTimeMillis())
extendedBolus.insulin = insulin; .insulin(insulin)
extendedBolus.durationInMinutes = durationInMinutes; .durationInMinutes(durationInMinutes)
extendedBolus.source = Source.USER; .source(Source.USER)
extendedBolus.pumpId = getRecordUniqueID(bolusMessage.getBolusId()); .pumpId(getRecordUniqueID(bolusMessage.getBolusId()));
TreatmentsPlugin.getPlugin().addToHistoryExtendedBolus(extendedBolus); TreatmentsPlugin.getPlugin().addToHistoryExtendedBolus(extendedBolus);
updateGui(); updateGui();
connector.requestHistorySync(30000); connector.requestHistorySync(30000);

View file

@ -15,6 +15,8 @@ import info.nightscout.androidaps.plugins.ConfigBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload; import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload;
import info.nightscout.androidaps.plugins.NSClientInternal.UploadQueue; import info.nightscout.androidaps.plugins.NSClientInternal.UploadQueue;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.T;
/** /**
* Created by jamorham on 27/01/2018. * Created by jamorham on 27/01/2018.
@ -25,7 +27,7 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
class HistoryLogAdapter { class HistoryLogAdapter {
private Logger log = LoggerFactory.getLogger(L.PUMP); private Logger log = LoggerFactory.getLogger(L.PUMP);
private static final long MAX_TIME_DIFFERENCE = 61000; private static final long MAX_TIME_DIFFERENCE = T.secs(61).msecs();
void createTBRrecord(Date eventDate, int percent, int duration, long record_id) { void createTBRrecord(Date eventDate, int percent, int duration, long record_id) {
@ -62,7 +64,7 @@ class HistoryLogAdapter {
} }
} else { } else {
if (L.isEnabled(L.PUMP)) if (L.isEnabled(L.PUMP))
log.debug("Time difference too great! : " + (eventDate.getTime() - temporaryBasalFromHistory.date)); log.debug("Time difference too big! : " + (eventDate.getTime() - temporaryBasalFromHistory.date));
} }
} }
@ -76,14 +78,45 @@ class HistoryLogAdapter {
void createExtendedBolusRecord(Date eventDate, double insulin, int durationInMinutes, long record_id) { void createExtendedBolusRecord(Date eventDate, double insulin, int durationInMinutes, long record_id) {
final ExtendedBolus extendedBolusFromHistory = TreatmentsPlugin.getPlugin().getExtendedBolusFromHistory(eventDate.getTime());
if (extendedBolusFromHistory == null) {
if (L.isEnabled(L.PUMP))
log.debug("Create new EB: " + eventDate + " " + insulin + " " + durationInMinutes);
} else {
if (L.isEnabled(L.PUMP))
log.debug("Loaded existing EB record: " + extendedBolusFromHistory.toString());
if (Math.abs(eventDate.getTime() - extendedBolusFromHistory.date) < MAX_TIME_DIFFERENCE) {
if (extendedBolusFromHistory.source != Source.PUMP) {
if (L.isEnabled(L.PUMP))
log.debug("Date seem to match: " + DateUtil.dateAndTimeFullString(eventDate.getTime()));
String _id = extendedBolusFromHistory._id;
if (NSUpload.isIdValid(_id)) {
NSUpload.removeCareportalEntryFromNS(_id);
} else {
UploadQueue.removeID("dbAdd", _id);
}
MainApp.getDbHelper().delete(extendedBolusFromHistory);
} else {
if (L.isEnabled(L.PUMP))
log.debug("This record is already a pump record!");
}
} else {
if (L.isEnabled(L.PUMP))
log.debug("Time difference too big! : " + (eventDate.getTime() - extendedBolusFromHistory.date));
}
}
// TODO trap items below minimum period // TODO trap items below minimum period
final ExtendedBolus extendedBolus = new ExtendedBolus(); // TODO (mike) find and remove ending record with Source.USER
extendedBolus.date = eventDate.getTime();
extendedBolus.insulin = insulin; ExtendedBolus extendedBolus = new ExtendedBolus()
extendedBolus.durationInMinutes = durationInMinutes; .date(eventDate.getTime())
extendedBolus.source = Source.PUMP; .insulin(insulin)
extendedBolus.pumpId = record_id; .durationInMinutes(durationInMinutes)
.source(Source.PUMP)
.pumpId(record_id);
if (ProfileFunctions.getInstance().getProfile(extendedBolus.date) != null) // actual basal rate is needed for absolute rate calculation if (ProfileFunctions.getInstance().getProfile(extendedBolus.date) != null) // actual basal rate is needed for absolute rate calculation
TreatmentsPlugin.getPlugin().addToHistoryExtendedBolus(extendedBolus); TreatmentsPlugin.getPlugin().addToHistoryExtendedBolus(extendedBolus);

View file

@ -315,11 +315,11 @@ public class VirtualPumpPlugin extends PluginBase implements PumpInterface {
if (!result.success) if (!result.success)
return result; return result;
ExtendedBolus extendedBolus = new ExtendedBolus(); ExtendedBolus extendedBolus = new ExtendedBolus()
extendedBolus.date = System.currentTimeMillis(); .date(System.currentTimeMillis())
extendedBolus.insulin = insulin; .insulin(insulin)
extendedBolus.durationInMinutes = durationInMinutes; .durationInMinutes(durationInMinutes)
extendedBolus.source = Source.USER; .source(Source.USER);
result.success = true; result.success = true;
result.enacted = true; result.enacted = true;
result.bolusDelivered = insulin; result.bolusDelivered = insulin;