diff --git a/app/build.gradle b/app/build.gradle index f27f8fc37f..ddc6dfbbc4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -105,14 +105,14 @@ android { multiDexEnabled true versionCode 1500 // dev_version: 2.3.1-dev - version "medtronic-0.9.4-SNAPSHOT" + version "medtronic-0.9.6-SNAPSHOT" buildConfigField "String", "VERSION", '"' + version + '"' buildConfigField "String", "BUILDVERSION", '"' + generateGitBuild() + '-' + generateDate() + '"' buildConfigField "String", "HEAD", '"' + generateGitBuild() + '"' buildConfigField "String", "REMOTE", '"' + generateGitRemote() + '"' buildConfigField "String", "DEV_VERSION", '"2.3.1-dev"' - buildConfigField "String", "DEV_DATE", '"29.4.2019"' - buildConfigField "String", "DEV_CHECKIN", '"ea60b6ceef30ccf32566bf7c6caa4f1f33b42f41"' + buildConfigField "String", "DEV_DATE", '"2.5.2019"' + buildConfigField "String", "DEV_CHECKIN", '"75d469a9db4bad6c619de8d36006e935c12b6ff1"' testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" // if you change minSdkVersion to less than 11, you need to change executeTask for wear @@ -276,6 +276,8 @@ dependencies { testImplementation "com.google.truth:truth:0.39" testImplementation 'org.robolectric:robolectric:3.8' testImplementation "org.skyscreamer:jsonassert:1.5.0" + testImplementation "org.hamcrest:hamcrest-all:1.3" + testImplementation "uk.org.lidalia:slf4j-test:1.2.0" androidTestImplementation "org.mockito:mockito-core:2.8.47" androidTestImplementation "com.google.dexmaker:dexmaker:${dexmakerVersion}" diff --git a/app/src/main/assets/logback.xml b/app/src/main/assets/logback.xml index d271d333ea..21ff368791 100644 --- a/app/src/main/assets/logback.xml +++ b/app/src/main/assets/logback.xml @@ -12,7 +12,7 @@ - 500KB + 5MB 240 diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 99a78ae8e4..592ba9e77d 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -454,6 +454,14 @@ public class MainApp extends Application { return null; } + + public static boolean isEngineeringMode() { + if (!Config.APS) + return true; + return engineeringMode; + } + + public static boolean isEngineeringModeOrRelease() { if (!Config.APS) return true; diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 393cdd13e5..690bc4a0bb 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -876,6 +876,31 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { log.debug("TEMPBASAL: Already exists from: " + Source.getString(tempBasal.source) + " " + tempBasal.toString()); return false; } + + // search by date (in case its standard record that has become pump record) + QueryBuilder queryBuilder2 = getDaoTemporaryBasal().queryBuilder(); + Where where2 = queryBuilder2.where(); + where2.eq("date", tempBasal.date); + PreparedQuery preparedQuery2 = queryBuilder2.prepare(); + List trList2 = getDaoTemporaryBasal().query(preparedQuery2); + + if (trList2.size() > 0) { + old = trList2.get(0); + + old.copyFromPump(tempBasal); + old.source = Source.PUMP; + + if (L.isEnabled(L.DATABASE)) + log.debug("TEMPBASAL: Updated record with Pump Data : " + Source.getString(tempBasal.source) + " " + tempBasal.toString()); + + getDaoTemporaryBasal().update(old); + + updateEarliestDataChange(tempBasal.date); + scheduleTemporaryBasalChange(); + + return false; + } + getDaoTemporaryBasal().create(tempBasal); if (L.isEnabled(L.DATABASE)) log.debug("TEMPBASAL: New record from: " + Source.getString(tempBasal.source) + " " + tempBasal.toString()); diff --git a/app/src/main/java/info/nightscout/androidaps/db/DbObjectBase.java b/app/src/main/java/info/nightscout/androidaps/db/DbObjectBase.java new file mode 100644 index 0000000000..a0c7f4bd7b --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/db/DbObjectBase.java @@ -0,0 +1,9 @@ +package info.nightscout.androidaps.db; + +public interface DbObjectBase { + + long getDate(); + + long getPumpId(); + +} diff --git a/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java b/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java index 336332790a..28e768946f 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java +++ b/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java @@ -27,7 +27,7 @@ import info.nightscout.androidaps.utils.SP; */ @DatabaseTable(tableName = DatabaseHelper.DATABASE_TEMPORARYBASALS) -public class TemporaryBasal implements Interval { +public class TemporaryBasal implements Interval, DbObjectBase { private static Logger log = LoggerFactory.getLogger(L.DATABASE); @DatabaseField(id = true) @@ -156,6 +156,14 @@ public class TemporaryBasal implements Interval { netExtendedRate = t.netExtendedRate; } + public void copyFromPump(TemporaryBasal t) { + durationInMinutes = t.durationInMinutes; + isAbsolute = t.isAbsolute; + percentRate = t.percentRate; + absoluteRate = t.absoluteRate; + pumpId = t.pumpId; + } + // -------- Interval interface --------- Long cuttedEnd = null; @@ -416,4 +424,13 @@ public class TemporaryBasal implements Interval { } } + @Override + public long getDate() { + return this.date; + } + + @Override + public long getPumpId() { + return this.pumpId; + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntryType.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntryType.java index fa8328982c..9fd984407c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntryType.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/PumpHistoryEntryType.java @@ -107,7 +107,7 @@ public enum PumpHistoryEntryType // implements CodeEnum // V4 // Andy58(0x58, "Unknown", 13, 5, 0), // TO DO is this one really there ??? - BolusWizardChange(0x5a, "Bolus Wizard Change", PumpHistoryEntryGroup.Configuration, 2, 5, 117), // V2: 522+[B=143] + BolusWizardChange(0x5a, "Bolus Wizard Change", PumpHistoryEntryGroup.Configuration, 2, 5, 117), // V2: 522+[B=143] // V6 124/144 BolusWizardBolusEstimate(0x5b, "Bolus Wizard Estimate", PumpHistoryEntryGroup.Configuration, 2, 5, 13), // 15 // UnabsorbedInsulin(0x5c, "Unabsorbed Insulin", PumpHistoryEntryGroup.Statistic, 5, 0, 0), // head[1] -> body // length @@ -220,7 +220,7 @@ public enum PumpHistoryEntryType // implements CodeEnum EndResultTotals.addSpecialRuleBody(new SpecialRule(MedtronicDeviceType.Medtronic_523andHigher, 3)); Bolus.addSpecialRuleHead(new SpecialRule(MedtronicDeviceType.Medtronic_523andHigher, 8)); // BolusWizardChange.addSpecialRuleBody(new SpecialRule(MedtronicDeviceType.Medtronic_522andHigher, 143)); - BolusWizardChange.addSpecialRuleBody(new SpecialRule(MedtronicDeviceType.Medtronic_523andHigher, 143)); // V5: + BolusWizardChange.addSpecialRuleBody(new SpecialRule(MedtronicDeviceType.Medtronic_523andHigher, 137)); // V5: // 522 // has // old diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java index 00c8d583dd..3787801215 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java @@ -1,6 +1,16 @@ package info.nightscout.androidaps.plugins.pump.medtronic.data; +import com.google.android.gms.common.util.CollectionUtils; +import com.google.common.base.Splitter; +import com.google.gson.Gson; + +import org.joda.time.LocalDateTime; +import org.joda.time.Minutes; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.ArrayList; +import java.util.Calendar; import java.util.Collections; import java.util.Date; import java.util.GregorianCalendar; @@ -8,20 +18,14 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.joda.time.LocalDateTime; -import org.joda.time.Minutes; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.base.Splitter; -import com.google.gson.Gson; - import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.db.DatabaseHelper; +import info.nightscout.androidaps.db.DbObjectBase; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TDD; +import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.configBuilder.DetailedBolusInfoStorage; import info.nightscout.androidaps.plugins.pump.common.utils.DateTimeUtil; @@ -34,6 +38,7 @@ import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.BasalProfile; import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.BolusDTO; import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.ClockDTO; import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.DailyTotalsDTO; +import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.TempBasalPair; import info.nightscout.androidaps.plugins.pump.medtronic.driver.MedtronicPumpStatus; import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil; import info.nightscout.androidaps.plugins.treatments.Treatment; @@ -134,7 +139,6 @@ public class MedtronicHistoryData { } } - // } } } @@ -221,7 +225,6 @@ public class MedtronicHistoryData { PumpHistoryEntryType.ChangeMaxBolus, // PumpHistoryEntryType.ChangeMaxBasal, // PumpHistoryEntryType.ChangeTempBasalType); - } @@ -283,14 +286,14 @@ public class MedtronicHistoryData { this.sort(newAndAll); List newAndAll2 = getFilteredItems(newAndAll, // - PumpHistoryEntryType.Bolus, // - PumpHistoryEntryType.TempBasalCombined, // - PumpHistoryEntryType.Prime, // - PumpHistoryEntryType.PumpSuspend, // - PumpHistoryEntryType.PumpResume, // - PumpHistoryEntryType.Rewind, // - PumpHistoryEntryType.NoDeliveryAlarm, // - PumpHistoryEntryType.BasalProfileStart); + PumpHistoryEntryType.Bolus, // + PumpHistoryEntryType.TempBasalCombined, // + PumpHistoryEntryType.Prime, // + PumpHistoryEntryType.PumpSuspend, // + PumpHistoryEntryType.PumpResume, // + PumpHistoryEntryType.Rewind, // + PumpHistoryEntryType.NoDeliveryAlarm, // + PumpHistoryEntryType.BasalProfileStart); if (!forHistory) { newAndAll2 = filterPumpSuspend(newAndAll2, 10); // just last 10 (of relevant), for history we already @@ -340,7 +343,7 @@ public class MedtronicHistoryData { LOG.debug("ProcessHistoryData: Bolus [count={}, items={}]", treatments.size(), gsonPretty.toJson(treatments)); if (treatments.size() > 0) { - processBoluses(treatments); + processEntries(treatments, ProcessHistoryRecord.Bolus); } // TBR @@ -349,7 +352,7 @@ public class MedtronicHistoryData { LOG.debug("ProcessHistoryData: TBRs [count={}, items={}]", tbrs.size(), gsonPretty.toJson(tbrs)); if (tbrs.size() > 0) { - // processTBRs(tbrs); + processEntries(tbrs, ProcessHistoryRecord.TBR); } // Suspends (for suspends/resume, fakeTBR) @@ -408,169 +411,69 @@ public class MedtronicHistoryData { } - // TODO needs to be implemented - public void processBoluses(List boluses) { + private enum ProcessHistoryRecord { + Bolus("Bolus"), + TBR("TBR"), + Suspend("Suspend"); - int dateDifference = getOldestDateDifference(boluses); + private String description; - List treatmentsFromHistory = TreatmentsPlugin.getPlugin().getTreatmentsFromHistoryXMinutesAgo( - dateDifference); + ProcessHistoryRecord(String desc) { + this.description = desc; + } - LOG.debug("Boluses (before filter): {}, FromDb={}", gsonPretty.toJson(boluses), - gsonPretty.toJson(treatmentsFromHistory)); + public String getDescription() { + return this.description; + } - filterOutAlreadyAddedEntries(boluses, treatmentsFromHistory); + } - if (boluses.isEmpty()) + + private void processEntries(List entryList, ProcessHistoryRecord processHistoryRecord) { + + int dateDifference = getOldestDateDifference(entryList); + + List entriesFromHistory = getDatabaseEntries(dateDifference, processHistoryRecord); + + LOG.debug(processHistoryRecord.getDescription() + " List (before filter): {}, FromDb={}", gsonPretty.toJson(entryList), + gsonPretty.toJson(entriesFromHistory)); + + filterOutAlreadyAddedEntries(entryList, entriesFromHistory); + + if (entryList.isEmpty()) return; - LOG.debug("Boluses (after filter): {}, FromDb={}", gsonPretty.toJson(boluses), - gsonPretty.toJson(treatmentsFromHistory)); + LOG.debug(processHistoryRecord.getDescription() + " List (after filter): {}, FromDb={}", gsonPretty.toJson(entryList), + gsonPretty.toJson(entriesFromHistory)); - if (treatmentsFromHistory.isEmpty()) { - for (PumpHistoryEntry treatment : boluses) { - LOG.debug("Add Bolus (no treatments): " + treatment); - addBolus(treatment, null); + if (isCollectionEmpty(entriesFromHistory)) { + for (PumpHistoryEntry treatment : entryList) { + LOG.debug("Add " + processHistoryRecord.getDescription() + " (no db entries): " + treatment); + addEntry(treatment, null, processHistoryRecord); } } else { - for (PumpHistoryEntry treatment : boluses) { - tryToGetByLocalTime(treatment.atechDateTime); - Treatment treatmentDb = findTreatment(treatment, treatmentsFromHistory, dateDifference); - LOG.debug("Add Bolus {} - (treatmentFromDb={}) ", treatment, treatmentDb); + for (PumpHistoryEntry treatment : entryList) { + DbObjectBase treatmentDb = findDbEntry(treatment, entriesFromHistory); + LOG.debug("Add " + processHistoryRecord.getDescription() + " {} - (entryFromDb={}) ", treatment, treatmentDb); - addBolus(treatment, treatmentDb); + addEntry(treatment, treatmentDb, processHistoryRecord); } } } - private void filterOutAlreadyAddedEntries(List boluses, List treatmentsFromHistory) { - - List removeTreatmentsFromHistory = new ArrayList<>(); - - for (Treatment treatment : treatmentsFromHistory) { - - if (treatment.pumpId != 0) { - - PumpHistoryEntry selectedBolus = null; - - for (PumpHistoryEntry bolus : boluses) { - if (bolus.getPumpId() == treatment.pumpId) { - selectedBolus = bolus; - break; - } - } - - if (selectedBolus != null) { - boluses.remove(selectedBolus); - - removeTreatmentsFromHistory.add(treatment); - } - } - } - - treatmentsFromHistory.removeAll(removeTreatmentsFromHistory); - - } - - - private Treatment findTreatmentOld(PumpHistoryEntry treatment, List treatmentsFromHistory) { + private DbObjectBase findDbEntry(PumpHistoryEntry treatment, List entriesFromHistory) { long proposedTime = DateTimeUtil.toMillisFromATD(treatment.atechDateTime); proposedTime += (this.pumpTime.timeDifference * 1000); - treatment.phoneDateTime = proposedTime; - - List outList = new ArrayList<>(); - - for (Treatment treatment1 : treatmentsFromHistory) { - if ((treatment1.date > proposedTime - (5 * 60 * 1000)) - && (treatment1.date < proposedTime + (5 * 60 * 1000))) { - outList.add(treatment1); - } - } - - if (outList.size() == 0) { + if (entriesFromHistory.size() == 0) { return null; - } else if (outList.size() == 1) { - return outList.get(0); - } else { - LOG.error("TODO. Multiple options: {}", outList); - - Map data = new HashMap<>(); - - for (Treatment treatment1 : outList) { - int diff = Math.abs((int)(treatment1.date - proposedTime)); - data.put(treatment1, diff); - } - - for (int i = 1; i < 5; i++) { - - List outList2 = new ArrayList<>(); - - for (Treatment treatment1 : treatmentsFromHistory) { - if ((treatment1.date > proposedTime - (i * 60 * 1000)) - && (treatment1.date < proposedTime + (i * 60 * 1000))) { - outList2.add(treatment1); - } - } - - LOG.error("Treatment List: (timeDiff={},count={},list={})", (i * 60 * 1000), outList2.size(), - gsonPretty.toJson(outList2)); - - if (outList2.size() == 1) { - return outList2.get(0); - } else if (outList2.size() > 1) { - - for (int j = 1; j < 6; j++) { - - List outList3 = new ArrayList<>(); - - int ttt = (i * 60 * 1000) - (10 * j * 1000); - - for (Treatment treatment1 : treatmentsFromHistory) { - - if ((treatment1.date > proposedTime - ttt) && (treatment1.date < proposedTime + ttt)) { - outList3.add(treatment1); - } - } - - LOG.error("Treatment List: (timeDiff={},count={},list={})", ttt, outList3.size(), - gsonPretty.toJson(outList3)); - - if (outList3.size() == 1) { - return outList3.get(0); - } - } // for - - } // outList2 - } - - // TODO - } // outList - - // TODO - return null; - } - - - private Treatment findTreatment(PumpHistoryEntry treatment, List treatmentsFromHistory, - int dateDifference) { - - long proposedTime = DateTimeUtil.toMillisFromATD(treatment.atechDateTime); - - proposedTime += (this.pumpTime.timeDifference * 1000); - - // treatment.phoneDateTime = proposedTime; - Date proposedTimeDD = new Date(proposedTime); - - if (treatmentsFromHistory.size() == 0) { - return null; - } else if (treatmentsFromHistory.size() == 1) { - Treatment treatment1 = treatmentsFromHistory.get(0); - LocalDateTime ldt = new LocalDateTime(treatment1.date); - return treatmentsFromHistory.get(0); + } else if (entriesFromHistory.size() == 1) { + DbObjectBase treatment1 = entriesFromHistory.get(0); + LocalDateTime ldt = new LocalDateTime(treatment1.getDate()); + return entriesFromHistory.get(0); } for (int min = 0; min < 5; min++) { @@ -578,38 +481,96 @@ public class MedtronicHistoryData { int diff = (min * 60 * 1000) + (sec * 1000); - List outList = new ArrayList<>(); + List outList = new ArrayList<>(); - for (Treatment treatment1 : treatmentsFromHistory) { + for (DbObjectBase treatment1 : entriesFromHistory) { - if ((treatment1.date > proposedTime - diff) && (treatment1.date < proposedTime + diff)) { + if ((treatment1.getDate() > proposedTime - diff) && (treatment1.getDate() < proposedTime + diff)) { outList.add(treatment1); } } - LOG.error("Treatments: (timeDiff=[min={},sec={}],count={},list={})", min, sec, outList.size(), - gsonPretty.toJson(outList)); + LOG.error("Entries: (timeDiff=[min={},sec={}],count={},list={})", min, sec, outList.size(), + gsonPretty.toJson(outList)); if (outList.size() == 1) { return outList.get(0); } if (min == 0 && sec == 10 && outList.size() > 1) { - LOG.error("Too many treatments (with too small diff): (timeDiff=[min={},sec={}],count={},list={})", - min, sec, outList.size(), gsonPretty.toJson(outList)); - + LOG.error("Too many entries (with too small diff): (timeDiff=[min={},sec={}],count={},list={})", + min, sec, outList.size(), gsonPretty.toJson(outList)); } } } return null; + } + + private void addEntry(PumpHistoryEntry treatment, DbObjectBase treatmentDb, ProcessHistoryRecord processHistoryRecord) { + + if (processHistoryRecord==ProcessHistoryRecord.Bolus) { + addBolus(treatment, (Treatment)treatmentDb); + } else if (processHistoryRecord==ProcessHistoryRecord.TBR) { + addTBR(treatment, (TemporaryBasal)treatmentDb); + } else { + addTBR(treatment, (TemporaryBasal)treatmentDb); + } + } + + + private List getDatabaseEntries(int dateDifference, ProcessHistoryRecord processHistoryRecord) { + if (processHistoryRecord==ProcessHistoryRecord.Bolus) { + List treatmentsFromHistory = TreatmentsPlugin.getPlugin().getTreatmentsFromHistoryXMinutesAgo( + dateDifference); + return treatmentsFromHistory; + } else { + + GregorianCalendar gc = new GregorianCalendar(); + gc.add(Calendar.MINUTE, (-1)*dateDifference); + + List tbrsFromHistory = databaseHelper.getTemporaryBasalsDataFromTime(gc.getTimeInMillis(), true); + return tbrsFromHistory; + } + } + + + private void filterOutAlreadyAddedEntries(List entryList, List treatmentsFromHistory) { + + if (isCollectionEmpty(treatmentsFromHistory) ) + return; + + List removeTreatmentsFromHistory = new ArrayList<>(); + + for (DbObjectBase treatment : treatmentsFromHistory) { + + if (treatment.getPumpId() != 0) { + + PumpHistoryEntry selectedBolus = null; + + for (PumpHistoryEntry bolus : entryList) { + if (bolus.getPumpId() == treatment.getPumpId()) { + selectedBolus = bolus; + break; + } + } + + if (selectedBolus != null) { + entryList.remove(selectedBolus); + + removeTreatmentsFromHistory.add(treatment); + } + } + } + + treatmentsFromHistory.removeAll(removeTreatmentsFromHistory); } private void addBolus(PumpHistoryEntry bolus, Treatment treatment) { - BolusDTO bolusDTO = (BolusDTO)bolus.getDecodedData().get("Object"); + BolusDTO bolusDTO = (BolusDTO) bolus.getDecodedData().get("Object"); if (treatment == null) { @@ -630,9 +591,9 @@ public class MedtronicHistoryData { if (L.isEnabled(L.PUMPCOMM)) LOG.debug("addBolus - [date={},pumpId={}, insulin={}, newRecord={}]", detailedBolusInfo.date, - detailedBolusInfo.pumpId, detailedBolusInfo.insulin, newRecord); + detailedBolusInfo.pumpId, detailedBolusInfo.insulin, newRecord); } - break; + break; case Audio: case Extended: { @@ -648,12 +609,12 @@ public class MedtronicHistoryData { TreatmentsPlugin.getPlugin().addToHistoryExtendedBolus(extendedBolus); - LOG.debug("addBolus - Extended [date={},pumpId={}, insulin={}, duration={}]", extendedBolus.date, - extendedBolus.pumpId, extendedBolus.insulin, extendedBolus.durationInMinutes); - + if (L.isEnabled(L.PUMP)) { + LOG.debug("addBolus - Extended [date={},pumpId={}, insulin={}, duration={}]", extendedBolus.date, + extendedBolus.pumpId, extendedBolus.insulin, extendedBolus.durationInMinutes); + } } - break; - + break; } } else { @@ -673,50 +634,51 @@ public class MedtronicHistoryData { bolus.setLinkedObject(detailedBolusInfo); - if (L.isEnabled(L.PUMPCOMM)) + if (L.isEnabled(L.PUMP)) { LOG.debug("editBolus - [date={},pumpId={}, insulin={}, newRecord={}]", detailedBolusInfo.date, - detailedBolusInfo.pumpId, detailedBolusInfo.insulin, newRecord); - // } + detailedBolusInfo.pumpId, detailedBolusInfo.insulin, newRecord); + } } + } + + + private void addTBR(PumpHistoryEntry treatment, TemporaryBasal temporaryBasalDbInput) { + + TempBasalPair tbr = (TempBasalPair)treatment.getDecodedData().get("Object"); + + TemporaryBasal temporaryBasalDb = temporaryBasalDbInput; + String operation = "editTBR"; + + if (temporaryBasalDb==null) { + temporaryBasalDb = new TemporaryBasal(); + temporaryBasalDb.date = tryToGetByLocalTime(treatment.atechDateTime); + + operation = "addTBR"; + } + + temporaryBasalDb.source = Source.PUMP; + temporaryBasalDb.pumpId = treatment.getPumpId(); + temporaryBasalDb.durationInMinutes = tbr.getDurationMinutes(); + temporaryBasalDb.absoluteRate = tbr.getInsulinRate(); + temporaryBasalDb.isAbsolute = !tbr.isPercent(); + + treatment.setLinkedObject(temporaryBasalDb); + + databaseHelper.createOrUpdate(temporaryBasalDb); + + LOG.debug(operation + " - [date={},pumpId={}, rate={} {}, duration={}]", // + temporaryBasalDb.date, // + temporaryBasalDb.pumpId, // + temporaryBasalDb.isAbsolute ? String.format("%.2f", temporaryBasalDb.absoluteRate) : + String.format("%d", temporaryBasalDb.percentRate), // + temporaryBasalDb.isAbsolute ? "U/h" : "%", // + temporaryBasalDb.durationInMinutes); } - private DetailedBolusInfo createTreatment(long date, double amount, long pumpId, Boolean isSmb, Treatment treatment) { - DetailedBolusInfo normalBolus = new DetailedBolusInfo(); - normalBolus.date = date; - - if (treatment != null) { - normalBolus.carbs = treatment.carbs; - normalBolus.date = treatment.date; - } - - normalBolus.source = Source.PUMP; - normalBolus.insulin = amount; - normalBolus.pumpId = pumpId; - normalBolus.isValid = true; - normalBolus.isSMB = isSmb == null ? false : isSmb; - - return normalBolus; - } - // TODO needs to be implemented - public void processTBRs(List treatments) { - - int dateDifference = getOldestDateDifference(treatments); - - // List treatmentsFromHistory = TreatmentsPlugin.getPlugin().getTreatmentsFromHistoryXMinutesAgo( - // dateDifference); - - for (PumpHistoryEntry treatment : treatments) { - - LOG.debug("TOE. Treatment: " + treatment); - long inLocalTime = tryToGetByLocalTime(treatment.atechDateTime); - - } - - } // TODO needs to be implemented @@ -772,9 +734,9 @@ public class MedtronicHistoryData { // LOG.debug("TOE. New Time Of Entry: " + ldt.toString("HH:mm:ss")); LOG.debug("tryToGetByLocalTime: [TimeOfEntry={}, ClockPump={}, LocalTime={}, DifferenceSec={}, " - + "NewTimeOfEntry={}, time={}", atechDateTime, pumpTime.pumpTime.toString("HH:mm:ss"), - pumpTime.localDeviceTime.toString("HH:mm:ss"), pumpTime.timeDifference, ldt.toString("HH:mm:ss"), ldt - .toDate().getTime()); + + "NewTimeOfEntry={}, time={}", atechDateTime, pumpTime.pumpTime.toString("HH:mm:ss"), + pumpTime.localDeviceTime.toString("HH:mm:ss"), pumpTime.timeDifference, ldt.toString("HH:mm:ss"), ldt + .toDate().getTime()); return ldt.toDate().getTime(); } @@ -783,25 +745,32 @@ public class MedtronicHistoryData { private int getOldestDateDifference(List treatments) { long dt = Long.MAX_VALUE; + PumpHistoryEntry currentTreatment = null; for (PumpHistoryEntry treatment : treatments) { if (treatment.atechDateTime < dt) { dt = treatment.atechDateTime; + currentTreatment = treatment; } } // LOG.debug("Oldest entry: {}, pumpTimeDifference={}", dt, this.pumpTime.timeDifference); - LocalDateTime oldestEntryTime = DateTimeUtil.toLocalDateTime(dt); - oldestEntryTime = oldestEntryTime.minusMinutes(5); + LocalDateTime oldestEntryTime = null; - if (this.pumpTime.timeDifference < 0) { - oldestEntryTime = oldestEntryTime.plusSeconds(this.pumpTime.timeDifference); + try { + + oldestEntryTime = DateTimeUtil.toLocalDateTime(dt); + oldestEntryTime = oldestEntryTime.minusMinutes(5); + + if (this.pumpTime.timeDifference < 0) { + oldestEntryTime = oldestEntryTime.plusSeconds(this.pumpTime.timeDifference); + } + } catch (Exception ex) { + LOG.error("Problem decoding date from last record: {}" + currentTreatment); } - // } else { - // d.minusSeconds(this.pumpTime.timeDifference); - // } + LocalDateTime now = new LocalDateTime(); @@ -809,7 +778,7 @@ public class MedtronicHistoryData { // returns oldest time in history, with calculated time difference between pump and phone, minus 5 minutes LOG.debug("Oldest entry: {}, pumpTimeDifference={}, newDt={}, currentTime={}, differenceMin={}", dt, - this.pumpTime.timeDifference, oldestEntryTime, now, minutes.getMinutes()); + this.pumpTime.timeDifference, oldestEntryTime, now, minutes.getMinutes()); return minutes.getMinutes(); @@ -862,26 +831,26 @@ public class MedtronicHistoryData { * entryType == PumpHistoryEntryType.Bolus || // Treatments * entryType == PumpHistoryEntryType.TempBasalRate || // * entryType == PumpHistoryEntryType.TempBasalDuration || // - * + * * entryType == PumpHistoryEntryType.Prime || // Pump Status Change * entryType == PumpHistoryEntryType.PumpSuspend || // * entryType == PumpHistoryEntryType.PumpResume || // * entryType == PumpHistoryEntryType.Rewind || // * entryType == PumpHistoryEntryType.NoDeliveryAlarm || // no delivery * entryType == PumpHistoryEntryType.BasalProfileStart || // - * + * * entryType == PumpHistoryEntryType.ChangeTime || // Time Change * entryType == PumpHistoryEntryType.NewTimeSet || // - * + * * entryType == PumpHistoryEntryType.SelectBasalProfile || // Configuration * entryType == PumpHistoryEntryType.ClearSettings || // * entryType == PumpHistoryEntryType.SaveSettings || // * entryType == PumpHistoryEntryType.ChangeMaxBolus || // * entryType == PumpHistoryEntryType.ChangeMaxBasal || // * entryType == PumpHistoryEntryType.ChangeTempBasalType || // - * + * * entryType == PumpHistoryEntryType.ChangeBasalProfile_NewProfile || // Basal profile - * + * * entryType == PumpHistoryEntryType.DailyTotals512 || // Daily Totals * entryType == PumpHistoryEntryType.DailyTotals522 || // * entryType == PumpHistoryEntryType.DailyTotals523 || // @@ -925,7 +894,7 @@ public class MedtronicHistoryData { if (newProfile != null) { LOG.debug("processLastBasalProfileChange. item found, setting new basalProfileLocally: " + newProfile); - BasalProfile basalProfile = (BasalProfile)newProfile.getDecodedData().get("Object"); + BasalProfile basalProfile = (BasalProfile) newProfile.getDecodedData().get("Object"); mdtPumpStatus.basalsByHour = basalProfile.getProfilesByHour(); } @@ -943,7 +912,7 @@ public class MedtronicHistoryData { public boolean hasPumpTimeChanged() { return getStateFromFilteredList(PumpHistoryEntryType.NewTimeSet, // - PumpHistoryEntryType.ChangeTime); + PumpHistoryEntryType.ChangeTime); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/DailyTotalsDTO.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/DailyTotalsDTO.java index 30528a5619..a6a2878df5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/DailyTotalsDTO.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/DailyTotalsDTO.java @@ -76,6 +76,7 @@ public class DailyTotalsDTO { case DailyTotals522: decodeDailyTotals522(entry.getBody()); break; + case DailyTotals523: decodeDailyTotals523(entry.getBody()); break; @@ -151,7 +152,7 @@ public class DailyTotalsDTO { // Delivery Stats: BOLUS: Food=0.00, Corr=0.00, Manual=4.20 // Delivery Stats: NUM BOLUS: Food/Corr=0,Food+Corr=0, Manual=3 - LOG.debug("515: {}", toString()); + //LOG.debug("515: {}", toString()); } @@ -205,7 +206,7 @@ public class DailyTotalsDTO { // Delivery Stats: #Corr_only=0,Food+Corr=0.000, #Food+Corr=0 // Delivery Stats: Manual = 0.95, #Manual=5 - LOG.debug("523: {}", toString()); + //LOG.debug("523: {}", toString()); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/Treatment.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/Treatment.java index ac69a6a0fc..c050348759 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/Treatment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/Treatment.java @@ -16,6 +16,7 @@ import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Iob; +import info.nightscout.androidaps.db.DbObjectBase; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.interfaces.InsulinInterface; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; @@ -27,7 +28,7 @@ import info.nightscout.androidaps.utils.DecimalFormatter; import info.nightscout.androidaps.utils.JsonHelper; @DatabaseTable(tableName = Treatment.TABLE_TREATMENTS) -public class Treatment implements DataPointWithLabelInterface { +public class Treatment implements DataPointWithLabelInterface, DbObjectBase { public static final String TABLE_TREATMENTS = "Treatments"; @DatabaseField(id = true) @@ -266,4 +267,14 @@ public class Treatment implements DataPointWithLabelInterface { InsulinInterface insulinInterface = ConfigBuilderPlugin.getPlugin().getActiveInsulin(); return insulinInterface.iobCalcForTreatment(this, time, dia); } + + @Override + public long getDate() { + return this.date; + } + + @Override + public long getPumpId() { + return this.pumpId; + } } \ No newline at end of file