- Added a lot of logging for Double Bolus problem...

- Found one small problem when calling update treatment (if called from NS)...
This commit is contained in:
Andy Rozman 2019-10-23 11:14:04 +01:00
parent 43f0fe1df9
commit 66e7d0d29a
4 changed files with 76 additions and 15 deletions

View file

@ -34,6 +34,7 @@ import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewB
import info.nightscout.androidaps.plugins.pump.common.data.PumpStatus; import info.nightscout.androidaps.plugins.pump.common.data.PumpStatus;
import info.nightscout.androidaps.plugins.pump.common.defs.PumpDriverState; import info.nightscout.androidaps.plugins.pump.common.defs.PumpDriverState;
import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; import info.nightscout.androidaps.plugins.pump.common.defs.PumpType;
import info.nightscout.androidaps.plugins.pump.medtronic.data.MedtronicHistoryData;
import info.nightscout.androidaps.plugins.treatments.Treatment; import info.nightscout.androidaps.plugins.treatments.Treatment;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DateUtil;
@ -415,6 +416,9 @@ public abstract class PumpPluginAbstract extends PluginBase implements PumpInter
// bolus needed, ask pump to deliver it // bolus needed, ask pump to deliver it
return deliverBolus(detailedBolusInfo); return deliverBolus(detailedBolusInfo);
} else { } else {
if (MedtronicHistoryData.doubleBolusDebug)
LOG.debug("DoubleBolusDebug: deliverTreatment::(carb only entry)");
// no bolus required, carb only treatment // no bolus required, carb only treatment
TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, true); TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, true);

View file

@ -77,6 +77,8 @@ public class MedtronicHistoryData {
private long lastIdUsed = 0; private long lastIdUsed = 0;
public static boolean doubleBolusDebug = true;
public MedtronicHistoryData() { public MedtronicHistoryData() {
this.allHistory = new ArrayList<>(); this.allHistory = new ArrayList<>();
@ -517,18 +519,23 @@ public class MedtronicHistoryData {
long oldestTimestamp = getOldestTimestamp(entryList); long oldestTimestamp = getOldestTimestamp(entryList);
Gson gson = MedtronicUtil.getGsonInstance();
List<? extends DbObjectBase> entriesFromHistory = getDatabaseEntriesByLastTimestamp(oldestTimestamp, ProcessHistoryRecord.Bolus); List<? extends DbObjectBase> entriesFromHistory = getDatabaseEntriesByLastTimestamp(oldestTimestamp, ProcessHistoryRecord.Bolus);
// LOG.debug(processHistoryRecord.getDescription() + " List (before filter): {}, FromDb={}", gsonPretty.toJson(entryList), if (doubleBolusDebug)
// gsonPretty.toJson(entriesFromHistory)); LOG.debug("DoubleBolusDebug: List (before filter): {}, FromDb={}", gson.toJson(entryList),
gson.toJson(entriesFromHistory));
filterOutAlreadyAddedEntries(entryList, entriesFromHistory); filterOutAlreadyAddedEntries(entryList, entriesFromHistory);
if (entryList.isEmpty()) if (entryList.isEmpty()) {
return; return;
}
// LOG.debug(processHistoryRecord.getDescription() + " List (after filter): {}, FromDb={}", gsonPretty.toJson(entryList), if (doubleBolusDebug)
// gsonPretty.toJson(entriesFromHistory)); LOG.debug("DoubleBolusDebug: List (after filter): {}, FromDb={}", gson.toJson(entryList),
gson.toJson(entriesFromHistory));
if (isCollectionEmpty(entriesFromHistory)) { if (isCollectionEmpty(entriesFromHistory)) {
for (PumpHistoryEntry treatment : entryList) { for (PumpHistoryEntry treatment : entryList) {
@ -686,7 +693,6 @@ public class MedtronicHistoryData {
* *
* @param treatment Pump Entry * @param treatment Pump Entry
* @param entriesFromHistory entries from history * @param entriesFromHistory entries from history
*
* @return DbObject from AAPS (if found) * @return DbObject from AAPS (if found)
*/ */
private DbObjectBase findDbEntry(PumpHistoryEntry treatment, List<? extends DbObjectBase> entriesFromHistory) { private DbObjectBase findDbEntry(PumpHistoryEntry treatment, List<? extends DbObjectBase> entriesFromHistory) {
@ -784,7 +790,10 @@ public class MedtronicHistoryData {
BolusDTO bolusDTO = (BolusDTO) bolus.getDecodedData().get("Object"); BolusDTO bolusDTO = (BolusDTO) bolus.getDecodedData().get("Object");
if (treatment == null) { if (treatment == null) {
if (doubleBolusDebug)
LOG.debug("DoubleBolusDebug: addBolus(tretament==null): Bolus={}", bolusDTO);
switch (bolusDTO.getBolusType()) { switch (bolusDTO.getBolusType()) {
case Normal: { case Normal: {
@ -797,6 +806,9 @@ public class MedtronicHistoryData {
addCarbsFromEstimate(detailedBolusInfo, bolus); addCarbsFromEstimate(detailedBolusInfo, bolus);
if (doubleBolusDebug)
LOG.debug("DoubleBolusDebug: addBolus(tretament==null): DetailedBolusInfo={}", detailedBolusInfo);
boolean newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); boolean newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false);
bolus.setLinkedObject(detailedBolusInfo); bolus.setLinkedObject(detailedBolusInfo);
@ -862,6 +874,9 @@ public class MedtronicHistoryData {
BolusWizardDTO bolusWizard = (BolusWizardDTO) bolus.getDecodedData().get("Estimate"); BolusWizardDTO bolusWizard = (BolusWizardDTO) bolus.getDecodedData().get("Estimate");
if (doubleBolusDebug)
LOG.debug("DoubleBolusDebug: addCarbsFromEstimate: Bolus={}, BolusWizardDTO={}", bolus, bolusWizard);
detailedBolusInfo.carbs = bolusWizard.carbs; detailedBolusInfo.carbs = bolusWizard.carbs;
} }
} }
@ -1212,22 +1227,26 @@ public class MedtronicHistoryData {
//LocalDateTime oldestEntryTime = null; //LocalDateTime oldestEntryTime = null;
if (doubleBolusDebug)
LOG.debug("DoubleBolusDebug: getOldestTimestamp. Oldest entry found: time={}, object={}", dt, currentTreatment);
try { try {
GregorianCalendar oldestEntryTime = DateTimeUtil.toGregorianCalendar(dt); GregorianCalendar oldestEntryTime = DateTimeUtil.toGregorianCalendar(dt);
if (doubleBolusDebug)
LOG.debug("DoubleBolusDebug: getOldestTimestamp. oldestEntryTime: {}", DateTimeUtil.toString(oldestEntryTime));
oldestEntryTime.add(Calendar.MINUTE, -2); oldestEntryTime.add(Calendar.MINUTE, -2);
if (doubleBolusDebug)
LOG.debug("DoubleBolusDebug: getOldestTimestamp. oldestEntryTime (-2m): {}, timeInMillis={}", DateTimeUtil.toString(oldestEntryTime), oldestEntryTime.getTimeInMillis());
return oldestEntryTime.getTimeInMillis(); return oldestEntryTime.getTimeInMillis();
// if (this.pumpTime.timeDifference < 0) {
// oldestEntryTime = oldestEntryTime.plusSeconds(this.pumpTime.timeDifference);
// }
} catch (Exception ex) { } catch (Exception ex) {
LOG.error("Problem decoding date from last record: {}" + currentTreatment); LOG.error("Problem decoding date from last record: {}", currentTreatment);
return 8; // default return of 6 minutes return 8; // default return of 6 minutes
} }
} }

View file

@ -39,6 +39,7 @@ import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventNewHistoryData; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventNewHistoryData;
import info.nightscout.androidaps.plugins.pump.medtronic.data.MedtronicHistoryData;
import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil; import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil;
import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.FabricPrivacy;
import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.JsonHelper;
@ -249,10 +250,14 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
try { try {
Treatment treatment = Treatment.createFromJson(json); Treatment treatment = Treatment.createFromJson(json);
if (treatment != null) { if (treatment != null) {
if (MedtronicHistoryData.doubleBolusDebug)
log.debug("DoubleBolusDebug: createTreatmentFromJsonIfNotExists:: medtronicPump={}", MedtronicUtil.isMedtronicPump());
if (!MedtronicUtil.isMedtronicPump()) if (!MedtronicUtil.isMedtronicPump())
createOrUpdate(treatment); createOrUpdate(treatment);
else else
createOrUpdateMedtronic(treatment, false); createOrUpdateMedtronic(treatment, true);
} else } else
log.error("Date is null: " + treatment.toString()); log.error("Date is null: " + treatment.toString());
} catch (JSONException e) { } catch (JSONException e) {
@ -392,11 +397,18 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
public UpdateReturn createOrUpdateMedtronic(Treatment treatment, boolean fromNightScout) { public UpdateReturn createOrUpdateMedtronic(Treatment treatment, boolean fromNightScout) {
if (MedtronicHistoryData.doubleBolusDebug)
log.debug("DoubleBolusDebug: createOrUpdateMedtronic:: originalTreatment={}, fromNightScout={}", treatment, fromNightScout);
try { try {
treatment.date = DatabaseHelper.roundDateToSec(treatment.date); treatment.date = DatabaseHelper.roundDateToSec(treatment.date);
Treatment existingTreatment = getRecord(treatment.pumpId, treatment.date); Treatment existingTreatment = getRecord(treatment.pumpId, treatment.date);
if (MedtronicHistoryData.doubleBolusDebug)
log.debug("DoubleBolusDebug: createOrUpdateMedtronic:: existingTreatment={}", treatment);
if (existingTreatment == null) { if (existingTreatment == null) {
getDao().create(treatment); getDao().create(treatment);
if (L.isEnabled(L.DATATREATMENTS)) if (L.isEnabled(L.DATATREATMENTS))
@ -407,6 +419,9 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
} else { } else {
if (existingTreatment.date == treatment.date) { if (existingTreatment.date == treatment.date) {
if (MedtronicHistoryData.doubleBolusDebug)
log.debug("DoubleBolusDebug: createOrUpdateMedtronic::(existingTreatment.date==treatment.date)");
// we will do update only, if entry changed // we will do update only, if entry changed
if (!optionalTreatmentCopy(existingTreatment, treatment, fromNightScout)) { if (!optionalTreatmentCopy(existingTreatment, treatment, fromNightScout)) {
return new UpdateReturn(true, false); return new UpdateReturn(true, false);
@ -416,6 +431,9 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
scheduleTreatmentChange(treatment); scheduleTreatmentChange(treatment);
return new UpdateReturn(true, false); return new UpdateReturn(true, false);
} else { } else {
if (MedtronicHistoryData.doubleBolusDebug)
log.debug("DoubleBolusDebug: createOrUpdateMedtronic::(existingTreatment.date != treatment.date)");
// date is different, we need to remove entry // date is different, we need to remove entry
getDao().delete(existingTreatment); getDao().delete(existingTreatment);
optionalTreatmentCopy(existingTreatment, treatment, fromNightScout); optionalTreatmentCopy(existingTreatment, treatment, fromNightScout);

View file

@ -51,6 +51,7 @@ import info.nightscout.androidaps.plugins.general.overview.notifications.Notific
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensResult;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.plugins.pump.medtronic.data.MedtronicHistoryData;
import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil; import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil;
import info.nightscout.androidaps.plugins.sensitivity.SensitivityAAPSPlugin; import info.nightscout.androidaps.plugins.sensitivity.SensitivityAAPSPlugin;
import info.nightscout.androidaps.plugins.sensitivity.SensitivityWeightedAveragePlugin; import info.nightscout.androidaps.plugins.sensitivity.SensitivityWeightedAveragePlugin;
@ -344,12 +345,20 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
@Override @Override
public List<Treatment> getTreatmentsFromHistoryAfterTimestamp(long fromTimestamp) { public List<Treatment> getTreatmentsFromHistoryAfterTimestamp(long fromTimestamp) {
List<Treatment> in5minback = new ArrayList<>(); List<Treatment> in5minback = new ArrayList<>();
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
synchronized (treatments) { synchronized (treatments) {
if (MedtronicHistoryData.doubleBolusDebug)
log.debug("DoubleBolusDebug: AllTreatmentsInDb: {}", MedtronicUtil.getGsonInstance().toJson(treatments));
for (Treatment t : treatments) { for (Treatment t : treatments) {
if (t.date <= time && t.date >= fromTimestamp) if (t.date <= time && t.date >= fromTimestamp)
in5minback.add(t); in5minback.add(t);
} }
if (MedtronicHistoryData.doubleBolusDebug)
log.debug("DoubleBolusDebug: FilteredTreatments: After={}, Items={}", fromTimestamp, MedtronicUtil.getGsonInstance().toJson(in5minback));
return in5minback; return in5minback;
} }
} }
@ -623,6 +632,9 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
public boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo, boolean allowUpdate) { public boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo, boolean allowUpdate) {
boolean medtronicPump = MedtronicUtil.isMedtronicPump(); boolean medtronicPump = MedtronicUtil.isMedtronicPump();
if (MedtronicHistoryData.doubleBolusDebug)
log.debug("DoubleBolusDebug: addToHistoryTreatment::isMedtronicPump={}", medtronicPump);
Treatment treatment = new Treatment(); Treatment treatment = new Treatment();
treatment.date = detailedBolusInfo.date; treatment.date = detailedBolusInfo.date;
treatment.source = detailedBolusInfo.source; treatment.source = detailedBolusInfo.source;
@ -637,6 +649,9 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
treatment.boluscalc = detailedBolusInfo.boluscalc != null ? detailedBolusInfo.boluscalc.toString() : null; treatment.boluscalc = detailedBolusInfo.boluscalc != null ? detailedBolusInfo.boluscalc.toString() : null;
TreatmentService.UpdateReturn creatOrUpdateResult; TreatmentService.UpdateReturn creatOrUpdateResult;
if (medtronicPump && MedtronicHistoryData.doubleBolusDebug)
log.debug("DoubleBolusDebug: addToHistoryTreatment::treatment={}", treatment);
if (!medtronicPump) if (!medtronicPump)
creatOrUpdateResult = getService().createOrUpdate(treatment); creatOrUpdateResult = getService().createOrUpdate(treatment);
else else
@ -645,12 +660,17 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
boolean newRecordCreated = creatOrUpdateResult.newRecord; boolean newRecordCreated = creatOrUpdateResult.newRecord;
//log.debug("Adding new Treatment record" + treatment.toString()); //log.debug("Adding new Treatment record" + treatment.toString());
if (detailedBolusInfo.carbTime != 0) { if (detailedBolusInfo.carbTime != 0) {
Treatment carbsTreatment = new Treatment(); Treatment carbsTreatment = new Treatment();
carbsTreatment.source = detailedBolusInfo.source; carbsTreatment.source = detailedBolusInfo.source;
carbsTreatment.pumpId = detailedBolusInfo.pumpId; // but this should never happen carbsTreatment.pumpId = detailedBolusInfo.pumpId; // but this should never happen
carbsTreatment.date = detailedBolusInfo.date + detailedBolusInfo.carbTime * 60 * 1000L + 1000L; // add 1 sec to make them different records carbsTreatment.date = detailedBolusInfo.date + detailedBolusInfo.carbTime * 60 * 1000L + 1000L; // add 1 sec to make them different records
carbsTreatment.carbs = detailedBolusInfo.carbs; carbsTreatment.carbs = detailedBolusInfo.carbs;
carbsTreatment.source = detailedBolusInfo.source; carbsTreatment.source = detailedBolusInfo.source;
if (medtronicPump && MedtronicHistoryData.doubleBolusDebug)
log.debug("DoubleBolusDebug: carbTime!=0, creating second treatment. CarbsTreatment={}", carbsTreatment);
if (!medtronicPump) if (!medtronicPump)
getService().createOrUpdate(carbsTreatment); getService().createOrUpdate(carbsTreatment);
else else