Do our own history merging for TBRs

This commit is contained in:
Jamorham 2018-02-07 13:36:06 +00:00
parent 693c3ef8ad
commit bda1866c6d
No known key found for this signature in database
GPG key ID: 0BC5C3E0AAD64DF9
2 changed files with 39 additions and 6 deletions

View file

@ -4,7 +4,6 @@ import android.content.Intent;
import java.util.Date;
import info.nightscout.utils.SP;
import sugar.free.sightparser.handling.HistoryBroadcast;
import static info.nightscout.androidaps.plugins.PumpInsight.history.PumpIdCache.updatePumpSerialNumber;
@ -50,9 +49,12 @@ class HistoryIntentAdapter {
final long record_unique_id = getRecordUniqueID(pump_serial_number, pump_record_id);
// other sanity checks
log("Creating TBR record: " + pump_tbr_percent + "% " + pump_tbr_duration + "m" + " id:" + record_unique_id);
logAdapter.createTBRrecord(start_time, pump_tbr_percent, pump_tbr_duration, record_unique_id);
if ((pump_tbr_percent == 90) && (pump_tbr_duration <= 1)) {
log("Not creating TBR record for faux cancel");
} else {
log("Creating TBR record: " + pump_tbr_percent + "% " + pump_tbr_duration + "m" + " id:" + record_unique_id);
logAdapter.createTBRrecord(start_time, pump_tbr_percent, pump_tbr_duration, record_unique_id);
}
}
void processDeliveredBolusIntent(Intent intent) {

View file

@ -17,10 +17,39 @@ import info.nightscout.androidaps.db.TemporaryBasal;
class HistoryLogAdapter {
private static final long MAX_TIME_DIFFERENCE = 5000;
private static void log(String msg) {
android.util.Log.e("HISTORYLOG", msg);
}
void createTBRrecord(Date eventDate, int percent, int duration, long record_id) {
final TemporaryBasal temporaryBasal = new TemporaryBasal();
temporaryBasal.date = eventDate.getTime();
TemporaryBasal temporaryBasal = new TemporaryBasal(eventDate.getTime());
final TemporaryBasal temporaryBasalFromHistory = MainApp.getConfigBuilder().getTempBasalFromHistory(eventDate.getTime());
if (temporaryBasalFromHistory == null) {
log("Create new TBR: " + eventDate + " " + percent + " " + duration);
} else {
log("Loaded existing TBR record: " + temporaryBasalFromHistory.toString());
if (Math.abs(eventDate.getTime() - temporaryBasalFromHistory.date) < MAX_TIME_DIFFERENCE) {
if (temporaryBasalFromHistory.source != Source.PUMP) {
if (temporaryBasalFromHistory.percentRate == percent) {
log("Things seem to match: %" + percent);
temporaryBasal = temporaryBasalFromHistory;
MainApp.getDbHelper().delete(temporaryBasalFromHistory);
} else {
log("This record has different percent rates: " + temporaryBasalFromHistory.percentRate + " vs us: " + percent);
}
} else {
log("This record is already a pump record!");
}
} else {
log("Time difference too great! : " + (eventDate.getTime() - temporaryBasalFromHistory.date));
}
}
temporaryBasal.source = Source.PUMP;
temporaryBasal.pumpId = record_id;
temporaryBasal.percentRate = percent;
@ -47,6 +76,8 @@ class HistoryLogAdapter {
//DetailedBolusInfo detailedBolusInfo = DetailedBolusInfoStorage.findDetailedBolusInfo(eventDate.getTime());
// TODO do we need to do the same delete + insert that we are doing for temporary basals here too?
final DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo();
detailedBolusInfo.date = eventDate.getTime();
detailedBolusInfo.source = Source.PUMP;