Insight EB handling
This commit is contained in:
parent
af089b4dcf
commit
57e7d63794
8 changed files with 113 additions and 53 deletions
|
@ -961,13 +961,13 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
public void createTempBasalFromJsonIfNotExists(JSONObject trJson) {
|
||||
try {
|
||||
if (trJson.has("originalExtendedAmount")) { // extended bolus uploaded as temp basal
|
||||
ExtendedBolus extendedBolus = new ExtendedBolus();
|
||||
extendedBolus.source = Source.NIGHTSCOUT;
|
||||
extendedBolus.date = trJson.getLong("mills");
|
||||
extendedBolus.pumpId = trJson.has("pumpId") ? trJson.getLong("pumpId") : 0;
|
||||
extendedBolus.durationInMinutes = trJson.getInt("duration");
|
||||
extendedBolus.insulin = trJson.getDouble("originalExtendedAmount");
|
||||
extendedBolus._id = trJson.getString("_id");
|
||||
ExtendedBolus extendedBolus = new ExtendedBolus()
|
||||
.source(Source.NIGHTSCOUT)
|
||||
.date(trJson.getLong("mills"))
|
||||
.pumpId(trJson.has("pumpId") ? trJson.getLong("pumpId") : 0)
|
||||
.durationInMinutes(trJson.getInt("duration"))
|
||||
.insulin(trJson.getDouble("originalExtendedAmount"))
|
||||
._id(trJson.getString("_id"));
|
||||
// if faking found in NS, adapt AAPS to use it too
|
||||
if (!VirtualPumpPlugin.getPlugin().getFakingStatus()) {
|
||||
VirtualPumpPlugin.getPlugin().setFakingStatus(true);
|
||||
|
|
|
@ -70,6 +70,36 @@ public class ExtendedBolus implements Interval, DataPointWithLabelInterface {
|
|||
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) {
|
||||
if (date != other.date) {
|
||||
return false;
|
||||
|
@ -94,13 +124,13 @@ public class ExtendedBolus implements Interval, DataPointWithLabelInterface {
|
|||
}
|
||||
|
||||
public static ExtendedBolus createFromJson(JSONObject json) {
|
||||
ExtendedBolus extendedBolus = new ExtendedBolus();
|
||||
extendedBolus.source = Source.NIGHTSCOUT;
|
||||
extendedBolus.date = JsonHelper.safeGetLong(json, "mills");
|
||||
extendedBolus.durationInMinutes = JsonHelper.safeGetInt(json, "duration");
|
||||
extendedBolus.insulin = JsonHelper.safeGetDouble(json, "relative") / 60 * extendedBolus.durationInMinutes;
|
||||
extendedBolus._id = JsonHelper.safeGetString(json, "_id");
|
||||
extendedBolus.pumpId = JsonHelper.safeGetLong(json, "pumpId");
|
||||
ExtendedBolus extendedBolus = new ExtendedBolus()
|
||||
.source(Source.NIGHTSCOUT)
|
||||
.date(JsonHelper.safeGetLong(json, "mills"))
|
||||
.durationInMinutes(JsonHelper.safeGetInt(json, "duration"))
|
||||
.insulin(JsonHelper.safeGetDouble(json, "relative") / 60 * JsonHelper.safeGetInt(json, "duration"))
|
||||
._id(JsonHelper.safeGetString(json, "_id"))
|
||||
.pumpId(JsonHelper.safeGetLong(json, "pumpId"));
|
||||
return extendedBolus;
|
||||
}
|
||||
// -------- Interval interface ---------
|
||||
|
|
|
@ -79,27 +79,27 @@ public class MsgStatusBolusExtended extends MessageBase {
|
|||
exStop.source = Source.USER;
|
||||
treatmentsInterface.addToHistoryExtendedBolus(exStop);
|
||||
// Create new
|
||||
ExtendedBolus newExtended = new ExtendedBolus();
|
||||
newExtended.date = pump.extendedBolusStart;
|
||||
newExtended.insulin = pump.extendedBolusAmount;
|
||||
newExtended.durationInMinutes = pump.extendedBolusMinutes;
|
||||
newExtended.source = Source.USER;
|
||||
ExtendedBolus newExtended = new ExtendedBolus()
|
||||
.date(pump.extendedBolusStart)
|
||||
.insulin(pump.extendedBolusAmount)
|
||||
.durationInMinutes(pump.extendedBolusMinutes)
|
||||
.source(Source.USER);
|
||||
treatmentsInterface.addToHistoryExtendedBolus(newExtended);
|
||||
}
|
||||
} else {
|
||||
// Close curent temp basal
|
||||
ExtendedBolus exStop = new ExtendedBolus(now);
|
||||
exStop.source = Source.USER;
|
||||
ExtendedBolus exStop = new ExtendedBolus(now)
|
||||
.source(Source.USER);
|
||||
treatmentsInterface.addToHistoryExtendedBolus(exStop);
|
||||
}
|
||||
} else {
|
||||
if (pump.isExtendedInProgress) {
|
||||
// Create new
|
||||
ExtendedBolus newExtended = new ExtendedBolus();
|
||||
newExtended.date = pump.extendedBolusStart;
|
||||
newExtended.insulin = pump.extendedBolusAmount;
|
||||
newExtended.durationInMinutes = pump.extendedBolusMinutes;
|
||||
newExtended.source = Source.USER;
|
||||
ExtendedBolus newExtended = new ExtendedBolus()
|
||||
.date(pump.extendedBolusStart)
|
||||
.insulin(pump.extendedBolusAmount)
|
||||
.durationInMinutes(pump.extendedBolusMinutes)
|
||||
.source(Source.USER);
|
||||
treatmentsInterface.addToHistoryExtendedBolus(newExtended);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
ExtendedBolus extendedBolus = new ExtendedBolus();
|
||||
extendedBolus.date = datetime;
|
||||
extendedBolus.source = Source.PUMP;
|
||||
extendedBolus.pumpId = datetime;
|
||||
ExtendedBolus extendedBolus = new ExtendedBolus().date(datetime).source(Source.PUMP).pumpId(datetime);
|
||||
|
||||
String status;
|
||||
|
||||
|
|
|
@ -67,10 +67,10 @@ public class MsgHistoryEvents_v2 extends MessageBase {
|
|||
.source(Source.PUMP)
|
||||
.pumpId(datetime);
|
||||
|
||||
ExtendedBolus extendedBolus = new ExtendedBolus();
|
||||
extendedBolus.date = datetime;
|
||||
extendedBolus.source = Source.PUMP;
|
||||
extendedBolus.pumpId = datetime;
|
||||
ExtendedBolus extendedBolus = new ExtendedBolus()
|
||||
.date(datetime)
|
||||
.source(Source.PUMP)
|
||||
.pumpId(datetime);
|
||||
|
||||
String status = "";
|
||||
|
||||
|
|
|
@ -609,12 +609,12 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai
|
|||
extendedBolusMessage.setAmount(insulin);
|
||||
extendedBolusMessage.setDuration(durationInMinutes);
|
||||
BolusMessage bolusMessage = fetchSingleMessage(extendedBolusMessage, BolusMessage.class);
|
||||
final ExtendedBolus extendedBolus = new ExtendedBolus();
|
||||
extendedBolus.date = System.currentTimeMillis();
|
||||
extendedBolus.insulin = insulin;
|
||||
extendedBolus.durationInMinutes = durationInMinutes;
|
||||
extendedBolus.source = Source.USER;
|
||||
extendedBolus.pumpId = getRecordUniqueID(bolusMessage.getBolusId());
|
||||
final ExtendedBolus extendedBolus = new ExtendedBolus()
|
||||
.date(System.currentTimeMillis())
|
||||
.insulin(insulin)
|
||||
.durationInMinutes(durationInMinutes)
|
||||
.source(Source.USER)
|
||||
.pumpId(getRecordUniqueID(bolusMessage.getBolusId()));
|
||||
TreatmentsPlugin.getPlugin().addToHistoryExtendedBolus(extendedBolus);
|
||||
updateGui();
|
||||
connector.requestHistorySync(30000);
|
||||
|
|
|
@ -15,6 +15,8 @@ import info.nightscout.androidaps.plugins.ConfigBuilder.ProfileFunctions;
|
|||
import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload;
|
||||
import info.nightscout.androidaps.plugins.NSClientInternal.UploadQueue;
|
||||
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
|
||||
import info.nightscout.utils.DateUtil;
|
||||
import info.nightscout.utils.T;
|
||||
|
||||
/**
|
||||
* Created by jamorham on 27/01/2018.
|
||||
|
@ -25,7 +27,7 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
|
|||
class HistoryLogAdapter {
|
||||
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) {
|
||||
|
||||
|
@ -62,7 +64,7 @@ class HistoryLogAdapter {
|
|||
}
|
||||
} else {
|
||||
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) {
|
||||
|
||||
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
|
||||
|
||||
final ExtendedBolus extendedBolus = new ExtendedBolus();
|
||||
extendedBolus.date = eventDate.getTime();
|
||||
extendedBolus.insulin = insulin;
|
||||
extendedBolus.durationInMinutes = durationInMinutes;
|
||||
extendedBolus.source = Source.PUMP;
|
||||
extendedBolus.pumpId = record_id;
|
||||
// TODO (mike) find and remove ending record with Source.USER
|
||||
|
||||
ExtendedBolus extendedBolus = new ExtendedBolus()
|
||||
.date(eventDate.getTime())
|
||||
.insulin(insulin)
|
||||
.durationInMinutes(durationInMinutes)
|
||||
.source(Source.PUMP)
|
||||
.pumpId(record_id);
|
||||
|
||||
if (ProfileFunctions.getInstance().getProfile(extendedBolus.date) != null) // actual basal rate is needed for absolute rate calculation
|
||||
TreatmentsPlugin.getPlugin().addToHistoryExtendedBolus(extendedBolus);
|
||||
|
|
|
@ -315,11 +315,11 @@ public class VirtualPumpPlugin extends PluginBase implements PumpInterface {
|
|||
if (!result.success)
|
||||
return result;
|
||||
|
||||
ExtendedBolus extendedBolus = new ExtendedBolus();
|
||||
extendedBolus.date = System.currentTimeMillis();
|
||||
extendedBolus.insulin = insulin;
|
||||
extendedBolus.durationInMinutes = durationInMinutes;
|
||||
extendedBolus.source = Source.USER;
|
||||
ExtendedBolus extendedBolus = new ExtendedBolus()
|
||||
.date(System.currentTimeMillis())
|
||||
.insulin(insulin)
|
||||
.durationInMinutes(durationInMinutes)
|
||||
.source(Source.USER);
|
||||
result.success = true;
|
||||
result.enacted = true;
|
||||
result.bolusDelivered = insulin;
|
||||
|
|
Loading…
Reference in a new issue