From bda1866c6d12760d20127e424b9412ed6a20cf9f Mon Sep 17 00:00:00 2001 From: Jamorham Date: Wed, 7 Feb 2018 13:36:06 +0000 Subject: [PATCH] Do our own history merging for TBRs --- .../history/HistoryIntentAdapter.java | 10 +++--- .../history/HistoryLogAdapter.java | 35 +++++++++++++++++-- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryIntentAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryIntentAdapter.java index 8da9d22dd9..5375d88b7b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryIntentAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryIntentAdapter.java @@ -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) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryLogAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryLogAdapter.java index 73fafd6e6e..ce4f7522a7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryLogAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/history/HistoryLogAdapter.java @@ -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;