From 780a4dda365e6d33754e7f2e951401a613cc9219 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sun, 1 Jul 2018 16:56:07 +0200 Subject: [PATCH 01/24] fix #1173,newcarbsdialog: activate hypo tt if bg < 72mgdl/4mmol/l. --- .../plugins/Overview/Dialogs/NewCarbsDialog.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java index 3951b8415f..3cbc3d1d72 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java @@ -32,7 +32,9 @@ import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.db.CareportalEvent; +import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.interfaces.Constraint; @@ -153,6 +155,13 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, C notesLayout.setVisibility(SP.getBoolean(R.string.key_show_notes_entry_dialogs, false) ? View.VISIBLE : View.GONE); notesEdit = view.findViewById(R.id.newcarbs_notes); + BgReading bgReading = DatabaseHelper.actualBg(); + if (bgReading != null && bgReading.value < 72) { + startHypoTTCheckbox.setOnCheckedChangeListener(null); + startHypoTTCheckbox.setChecked(true); + startHypoTTCheckbox.setOnClickListener(this); + } + setCancelable(true); getDialog().setCanceledOnTouchOutside(false); return view; From f12db81f96e9c3764b2e684ed3f83241ff611e2f Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sun, 1 Jul 2018 17:18:42 +0200 Subject: [PATCH 02/24] treatmentservice: add method to check if record with pump id exists. --- .../plugins/Treatments/TreatmentService.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java index 2a5b798018..b82bff18d6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java @@ -248,12 +248,8 @@ public class TreatmentService extends OrmLiteBaseService { if (treatment.source == Source.PUMP) { // check for changed from pump change in NS - QueryBuilder queryBuilder = getDao().queryBuilder(); - Where where = queryBuilder.where(); - where.eq("pumpId", treatment.pumpId); - PreparedQuery preparedQuery = queryBuilder.prepare(); - List trList = getDao().query(preparedQuery); - if (trList.size() > 0) { + Treatment existingTreatment = getPumpRecordById(treatment.pumpId); + if (existingTreatment != null) { // do nothing, pump history record cannot be changed log.debug("TREATMENT: Pump record already found in database: " + treatment.toString()); return false; @@ -322,6 +318,26 @@ public class TreatmentService extends OrmLiteBaseService { return false; } + /** Returns the record for the given id, null if none, throws RuntimeException + * if multiple records with the same pump id exist. */ + @Nullable + public Treatment getPumpRecordById(long pumpId) { + try { + QueryBuilder queryBuilder = getDao().queryBuilder(); + Where where = queryBuilder.where(); + where.eq("pumpId", pumpId); + PreparedQuery preparedQuery = queryBuilder.prepare(); + List result = getDao().query(preparedQuery); + switch (result.size()) { + case 0: return null; + case 1: return result.get(1); + default: throw new RuntimeException("Multiple records with the same pump id found: " + result.toString()); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public void deleteNS(JSONObject json) { String _id = JsonHelper.safeGetString(json, "_id"); if (_id != null && !_id.isEmpty()) From 658af4bcfd54835978e41a64f69f539d824b9909 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sun, 1 Jul 2018 17:19:53 +0200 Subject: [PATCH 03/24] combo: don't attempt to add a bolus record if it already exists in db. --- .../androidaps/plugins/PumpCombo/ComboPlugin.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java index 016fbf6732..8567a0f64f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java @@ -60,10 +60,7 @@ import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.history.Bolus; import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.history.PumpHistory; import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.history.PumpHistoryRequest; import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.history.Tdd; -import info.nightscout.androidaps.plugins.Treatments.Treatment; import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; -import info.nightscout.androidaps.queue.Callback; -import info.nightscout.androidaps.queue.CommandQueue; import info.nightscout.utils.DateUtil; import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.SP; @@ -498,7 +495,7 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint return deliverBolus(detailedBolusInfo); } else { // no bolus required, carb only treatment - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); bolusingEvent.t = new Treatment(); @@ -689,7 +686,7 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint dbi.source = Source.PUMP; dbi.insulin = lastPumpBolus.amount; try { - boolean treatmentCreated = TreatmentsPlugin.getPlugin().addToHistoryTreatment(dbi); + boolean treatmentCreated = TreatmentsPlugin.getPlugin().addToHistoryTreatment(dbi, false); if (!treatmentCreated) { log.error("Adding treatment record overrode an existing record: " + dbi); if (dbi.isSMB) { @@ -1188,7 +1185,8 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint dbi.source = Source.PUMP; dbi.insulin = pumpBolus.amount; dbi.eventType = CareportalEvent.CORRECTIONBOLUS; - if (TreatmentsPlugin.getPlugin().addToHistoryTreatment(dbi)) { + if (TreatmentsPlugin.getPlugin().getService().getPumpRecordById(dbi.pumpId) != null) { + TreatmentsPlugin.getPlugin().addToHistoryTreatment(dbi, false); updated = true; } } From 6ebb13d28d478e13b12b24cfa44bd93742eb5c6c Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sun, 1 Jul 2018 17:20:28 +0200 Subject: [PATCH 04/24] alarm on failure to add treatment to db. --- .../androidaps/interfaces/TreatmentsInterface.java | 2 +- .../plugins/Overview/Dialogs/NewInsulinDialog.java | 2 +- .../Overview/Dialogs/NewTreatmentDialog.java | 3 +-- .../plugins/Overview/Dialogs/WizardDialog.java | 2 +- .../plugins/Overview/OverviewFragment.java | 2 +- .../androidaps/plugins/PumpDanaR/DanaRPlugin.java | 2 +- .../plugins/PumpDanaRKorean/DanaRKoreanPlugin.java | 2 +- .../comm/DanaRS_Packet_APS_History_Events.java | 6 +++--- .../PumpDanaRv2/comm/MsgHistoryEvents_v2.java | 6 +++--- .../plugins/PumpInsight/InsightPlugin.java | 3 +-- .../PumpInsight/history/HistoryLogAdapter.java | 2 +- .../androidaps/plugins/PumpMDI/MDIPlugin.java | 2 +- .../plugins/PumpVirtual/VirtualPumpPlugin.java | 2 +- .../plugins/Treatments/CarbsGenerator.java | 2 +- .../plugins/Treatments/TreatmentsPlugin.java | 14 +++++++++++++- .../plugins/Wear/ActionStringHandler.java | 2 +- app/src/main/res/values/strings.xml | 2 ++ 17 files changed, 34 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java index 27a6a0846b..dc437e04ab 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java @@ -50,7 +50,7 @@ public interface TreatmentsInterface { boolean addToHistoryExtendedBolus(ExtendedBolus extendedBolus); - boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo); + boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo, boolean allowUpdate); TempTarget getTempTargetFromHistory(); TempTarget getTempTargetFromHistory(long time); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java index c4aba7004a..e88fc66de0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java @@ -270,7 +270,7 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener detailedBolusInfo.notes = notes; if (recordOnlyCheckbox.isChecked()) { detailedBolusInfo.date = time; - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); } else { detailedBolusInfo.date = now(); ConfigBuilderPlugin.getCommandQueue().bolus(detailedBolusInfo, new Callback() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java index a315310dc8..5fb698d1c5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java @@ -25,7 +25,6 @@ import org.slf4j.LoggerFactory; import java.text.DecimalFormat; import java.util.Objects; -import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.DetailedBolusInfo; @@ -187,7 +186,7 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene } }); } else { - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); } FabricPrivacy.getInstance().logCustom(new CustomEvent("Bolus")); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java index 303ca10949..032104e103 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java @@ -389,7 +389,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com } }); } else { - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); } FabricPrivacy.getInstance().logCustom(new CustomEvent("Wizard")); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index de7c20d45b..c8e087c525 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -878,7 +878,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, } }); } else { - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); } FabricPrivacy.getInstance().logCustom(new CustomEvent("QuickWizard")); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java index 77146738d9..144107779d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java @@ -199,7 +199,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin { log.debug("deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.bolusDelivered); detailedBolusInfo.insulin = t.insulin; detailedBolusInfo.date = System.currentTimeMillis(); - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); return result; } else { PumpEnactResult result = new PumpEnactResult(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java index 9ce1a6409a..49edb6b096 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java @@ -202,7 +202,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { log.debug("deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.bolusDelivered); detailedBolusInfo.insulin = t.insulin; detailedBolusInfo.date = System.currentTimeMillis(); - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); return result; } else { PumpEnactResult result = new PumpEnactResult(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_History_Events.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_History_Events.java index 181584df8e..b8c8aaf2b4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_History_Events.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_APS_History_Events.java @@ -133,14 +133,14 @@ public class DanaRS_Packet_APS_History_Events extends DanaRS_Packet { break; case DanaRPump.BOLUS: detailedBolusInfo.insulin = param1 / 100d; - boolean newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + boolean newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); log.debug((newRecord ? "**NEW** " : "") + "EVENT BOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min"); DetailedBolusInfoStorage.remove(detailedBolusInfo.date); status = "BOLUS " + DateUtil.timeString(datetime); break; case DanaRPump.DUALBOLUS: detailedBolusInfo.insulin = param1 / 100d; - newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); log.debug((newRecord ? "**NEW** " : "") + "EVENT DUALBOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min"); DetailedBolusInfoStorage.remove(detailedBolusInfo.date); status = "DUALBOLUS " + DateUtil.timeString(datetime); @@ -183,7 +183,7 @@ public class DanaRS_Packet_APS_History_Events extends DanaRS_Packet { emptyCarbsInfo.date = datetime.getTime(); emptyCarbsInfo.source = Source.PUMP; emptyCarbsInfo.pumpId = datetime.getTime(); - newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(emptyCarbsInfo); + newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(emptyCarbsInfo, false); log.debug((newRecord ? "**NEW** " : "") + "EVENT CARBS (" + recordCode + ") " + datetime.toLocaleString() + " Carbs: " + param1 + "g"); status = "CARBS " + DateUtil.timeString(datetime); break; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java index 05c4087dcb..91c01ba38c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java @@ -107,14 +107,14 @@ public class MsgHistoryEvents_v2 extends MessageBase { break; case DanaRPump.BOLUS: detailedBolusInfo.insulin = param1 / 100d; - boolean newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + boolean newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); log.debug((newRecord ? "**NEW** " : "") + "EVENT BOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min"); DetailedBolusInfoStorage.remove(detailedBolusInfo.date); status = "BOLUS " + DateUtil.timeString(datetime); break; case DanaRPump.DUALBOLUS: detailedBolusInfo.insulin = param1 / 100d; - newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); log.debug((newRecord ? "**NEW** " : "") + "EVENT DUALBOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min"); DetailedBolusInfoStorage.remove(detailedBolusInfo.date); status = "DUALBOLUS " + DateUtil.timeString(datetime); @@ -157,7 +157,7 @@ public class MsgHistoryEvents_v2 extends MessageBase { emptyCarbsInfo.date = datetime.getTime(); emptyCarbsInfo.source = Source.PUMP; emptyCarbsInfo.pumpId = datetime.getTime(); - newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(emptyCarbsInfo); + newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(emptyCarbsInfo, false); log.debug((newRecord ? "**NEW** " : "") + "EVENT CARBS (" + recordCode + ") " + datetime.toLocaleString() + " Carbs: " + param1 + "g"); status = "CARBS " + DateUtil.timeString(datetime); break; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java index bf2c9ec09b..3df6a776d5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java @@ -61,7 +61,6 @@ import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfil import sugar.free.sightparser.applayer.messages.AppLayerMessage; import sugar.free.sightparser.applayer.messages.remote_control.BolusMessage; import sugar.free.sightparser.applayer.messages.remote_control.CancelBolusMessage; -import sugar.free.sightparser.applayer.messages.remote_control.CancelTBRMessage; import sugar.free.sightparser.applayer.messages.remote_control.ExtendedBolusMessage; import sugar.free.sightparser.applayer.messages.remote_control.StandardBolusMessage; import sugar.free.sightparser.applayer.messages.status.ActiveBolusesMessage; @@ -456,7 +455,7 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai bolusingEvent.bolusId = bolusId; bolusingEvent.percent = 0; MainApp.bus().post(bolusingEvent); - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); } else { log.debug("Failure to deliver treatment"); } 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 689eb9e981..fcb2ee536c 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 @@ -83,6 +83,6 @@ class HistoryLogAdapter { detailedBolusInfo.source = Source.PUMP; detailedBolusInfo.pumpId = record_id; detailedBolusInfo.insulin = insulin; - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java index 09cff7d97e..fb41a72e88 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java @@ -136,7 +136,7 @@ public class MDIPlugin extends PluginBase implements PumpInterface { result.bolusDelivered = detailedBolusInfo.insulin; result.carbsDelivered = detailedBolusInfo.carbs; result.comment = MainApp.gs(R.string.virtualpump_resultok); - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); return result; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java index 653ab66c21..12977b89bf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java @@ -226,7 +226,7 @@ public class VirtualPumpPlugin extends PluginBase implements PumpInterface { log.debug("Delivering treatment insulin: " + detailedBolusInfo.insulin + "U carbs: " + detailedBolusInfo.carbs + "g " + result); MainApp.bus().post(new EventVirtualPumpUpdateGui()); lastDataTime = new Date(); - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); return result; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/CarbsGenerator.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/CarbsGenerator.java index 99168a2776..01eeee3662 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/CarbsGenerator.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/CarbsGenerator.java @@ -50,7 +50,7 @@ public class CarbsGenerator { } }); } else { - TreatmentsPlugin.getPlugin().addToHistoryTreatment(carbInfo); + TreatmentsPlugin.getPlugin().addToHistoryTreatment(carbInfo, false); } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index 461f204f84..b6b53eed86 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.Treatments; +import android.content.Intent; import android.support.annotation.Nullable; import com.squareup.otto.Subscribe; @@ -38,6 +39,7 @@ import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; +import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.plugins.Sensitivity.SensitivityAAPSPlugin; @@ -465,7 +467,7 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface // return true if new record is created @Override - public boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo) { + public boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo, boolean allowUpdate) { Treatment treatment = new Treatment(); treatment.date = detailedBolusInfo.date; treatment.source = detailedBolusInfo.source; @@ -491,6 +493,16 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface } if (newRecordCreated && detailedBolusInfo.isValid) NSUpload.uploadTreatmentRecord(detailedBolusInfo); + + if (!allowUpdate && !newRecordCreated) { + Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); + i.putExtra("soundid", R.raw.error); + i.putExtra("status", MainApp.gs(R.string.error_adding_treatment_message)); + i.putExtra("title", MainApp.gs(R.string.error_adding_treatment_title)); + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + MainApp.instance().startActivity(i); + } + return newRecordCreated; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java index 7cebe4a72d..fc4728c53c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java @@ -719,7 +719,7 @@ public class ActionStringHandler { } }); } else { - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo); + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a55e0ed1be..9f8225c3cc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1179,6 +1179,8 @@ Clean AndroidAPS started Stored settings found Attention: If you activate and connect to a hardware pump, AndroidAPS will copy the basal settings from the profile to the pump, overwriting the existing basal rate stored on the pump. Make sure you have the correct basal setting in AndroidAPS. If you are not sure or don\'t want to overwrite the basal settings on your pump, press cancel and repeat switching to the pump at a later time. + Treatment data incomplete + A treatment (insulin:%0.2f, carbs: %2) couldn\'t not be added to treatments. Please check and manually add a record as appropriate. %d day From 9b7d36ffede6e06eab5a18d1d8a2e339a707aea6 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sun, 1 Jul 2018 18:25:32 +0200 Subject: [PATCH 05/24] bugfix --- .../androidaps/plugins/Treatments/TreatmentService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java index b82bff18d6..b5c036d1fd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java @@ -330,7 +330,7 @@ public class TreatmentService extends OrmLiteBaseService { List result = getDao().query(preparedQuery); switch (result.size()) { case 0: return null; - case 1: return result.get(1); + case 1: return result.get(0); default: throw new RuntimeException("Multiple records with the same pump id found: " + result.toString()); } } catch (SQLException e) { From 5df054a9995f4d21b280f16506eb50513162cbf7 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sun, 1 Jul 2018 18:32:52 +0200 Subject: [PATCH 06/24] more fixes --- .../nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java | 2 +- .../androidaps/plugins/Treatments/TreatmentsPlugin.java | 2 +- app/src/main/res/values/strings.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java index 8567a0f64f..a16ffbda05 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java @@ -1185,7 +1185,7 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint dbi.source = Source.PUMP; dbi.insulin = pumpBolus.amount; dbi.eventType = CareportalEvent.CORRECTIONBOLUS; - if (TreatmentsPlugin.getPlugin().getService().getPumpRecordById(dbi.pumpId) != null) { + if (TreatmentsPlugin.getPlugin().getService().getPumpRecordById(dbi.pumpId) == null) { TreatmentsPlugin.getPlugin().addToHistoryTreatment(dbi, false); updated = true; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index b6b53eed86..410af5b2c5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -498,7 +498,7 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); i.putExtra("soundid", R.raw.error); i.putExtra("status", MainApp.gs(R.string.error_adding_treatment_message)); - i.putExtra("title", MainApp.gs(R.string.error_adding_treatment_title)); + i.putExtra("title", String.format(MainApp.gs(R.string.error_adding_treatment_title), treatment.insulin, treatment.carbs)); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); MainApp.instance().startActivity(i); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9f8225c3cc..27b55e0b1f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1180,7 +1180,7 @@ Stored settings found Attention: If you activate and connect to a hardware pump, AndroidAPS will copy the basal settings from the profile to the pump, overwriting the existing basal rate stored on the pump. Make sure you have the correct basal setting in AndroidAPS. If you are not sure or don\'t want to overwrite the basal settings on your pump, press cancel and repeat switching to the pump at a later time. Treatment data incomplete - A treatment (insulin:%0.2f, carbs: %2) couldn\'t not be added to treatments. Please check and manually add a record as appropriate. + A treatment (insulin:%0.2f, carbs: %d) could not be added to treatments. Please check and manually add a record as appropriate. %d day From 290effcd7cc29bbfafc392230edd5478c53edd33 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sun, 1 Jul 2018 18:51:56 +0200 Subject: [PATCH 07/24] simplyify defaulting to hypo tt. --- .../Overview/Dialogs/NewCarbsDialog.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java index 3cbc3d1d72..8d63f0a551 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java @@ -126,6 +126,13 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, C startEatingSoonTTCheckbox = view.findViewById(R.id.newcarbs_eating_soon_tt); startEatingSoonTTCheckbox.setOnCheckedChangeListener(this); startHypoTTCheckbox = view.findViewById(R.id.newcarbs_hypo_tt); + BgReading bgReading = DatabaseHelper.actualBg(); + if (bgReading != null && bgReading.value < 72) { + startHypoTTCheckbox.setChecked(true); + // see #onCheckedChanged + startHypoTTCheckbox.setOnClickListener(this); + } + startHypoTTCheckbox.setOnCheckedChangeListener(this); editTime = view.findViewById(R.id.newcarbs_time); @@ -155,13 +162,6 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, C notesLayout.setVisibility(SP.getBoolean(R.string.key_show_notes_entry_dialogs, false) ? View.VISIBLE : View.GONE); notesEdit = view.findViewById(R.id.newcarbs_notes); - BgReading bgReading = DatabaseHelper.actualBg(); - if (bgReading != null && bgReading.value < 72) { - startHypoTTCheckbox.setOnCheckedChangeListener(null); - startHypoTTCheckbox.setChecked(true); - startHypoTTCheckbox.setOnClickListener(this); - } - setCancelable(true); getDialog().setCanceledOnTouchOutside(false); return view; @@ -230,13 +230,13 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, C @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - // Logic to disable a selected radio when pressed. When a checked radio - // is pressed, no CheckChanged event is trigger, so register a Click event + // Logic to disable a selected radio when pressed: when a checked radio + // is pressed, no CheckChanged event is triggered, so register a Click event // when checking a radio. Since Click events come after CheckChanged events, - // the Click event is triggered immediately after this. Thus, set toggingTT + // the Click event is triggered immediately after this. Thus, set togglingTT // var to true, so that the first Click event fired after this is ignored. // Radios remove themselves from Click events once unchecked. - // Since radios are not in a group, manually update their state. + // Since radios are not in a group, their state is manually updated here. switch (buttonView.getId()) { case R.id.newcarbs_activity_tt: togglingTT = true; From 21313bd418da1e52e6af5c5c8aa78834c2fbe52f Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sun, 1 Jul 2018 19:10:58 +0200 Subject: [PATCH 08/24] obligatory format specifier fixes --- .../androidaps/plugins/Treatments/TreatmentsPlugin.java | 4 ++-- app/src/main/res/values/strings.xml | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index 410af5b2c5..1a3b36e45f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -497,8 +497,8 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface if (!allowUpdate && !newRecordCreated) { Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); i.putExtra("soundid", R.raw.error); - i.putExtra("status", MainApp.gs(R.string.error_adding_treatment_message)); - i.putExtra("title", String.format(MainApp.gs(R.string.error_adding_treatment_title), treatment.insulin, treatment.carbs)); + i.putExtra("title", MainApp.gs(R.string.error_adding_treatment_title)); + i.putExtra("status", String.format(MainApp.gs(R.string.error_adding_treatment_message), treatment.insulin, (int) treatment.carbs)); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); MainApp.instance().startActivity(i); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 27b55e0b1f..3faab4c3e8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1179,8 +1179,9 @@ Clean AndroidAPS started Stored settings found Attention: If you activate and connect to a hardware pump, AndroidAPS will copy the basal settings from the profile to the pump, overwriting the existing basal rate stored on the pump. Make sure you have the correct basal setting in AndroidAPS. If you are not sure or don\'t want to overwrite the basal settings on your pump, press cancel and repeat switching to the pump at a later time. - Treatment data incomplete - A treatment (insulin:%0.2f, carbs: %d) could not be added to treatments. Please check and manually add a record as appropriate. + Treatment data incomplete + + A treatment (insulin: %.2f, carbs: %d) could not be added to treatments. Please check and manually add a record as appropriate. %d day From bf3b4502bf89f59a148a9731b060f1ef72f83a0a Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sun, 1 Jul 2018 19:15:27 +0200 Subject: [PATCH 09/24] log origin of offending call --- .../androidaps/plugins/Treatments/TreatmentsPlugin.java | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index 1a3b36e45f..5de75f0690 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -495,6 +495,7 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface NSUpload.uploadTreatmentRecord(detailedBolusInfo); if (!allowUpdate && !newRecordCreated) { + log.error("Treatment could not be added to DB", new Exception()); Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); i.putExtra("soundid", R.raw.error); i.putExtra("title", MainApp.gs(R.string.error_adding_treatment_title)); From b39fa514c2d6fee381a6e63978c8fd02713b1182 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Mon, 2 Jul 2018 02:11:25 +0200 Subject: [PATCH 10/24] alarm on clashes, not on identical treatments --- .../plugins/Treatments/Treatment.java | 16 ++++++++++ .../plugins/Treatments/TreatmentService.java | 29 +++++++++++++------ .../plugins/Treatments/TreatmentsPlugin.java | 5 ++-- 3 files changed, 39 insertions(+), 11 deletions(-) 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 4dcced443a..54802a9081 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 @@ -132,6 +132,22 @@ public class Treatment implements DataPointWithLabelInterface { return true; } + + /* + * mealBolus, _id and isSMB cannot be known coming from pump. Only compare rest + */ + public boolean equalsRePumpHistory(Treatment other) { + if (date != other.date) + return false; + if (insulin != other.insulin) + return false; + if (carbs != other.carbs) + return false; + if (pumpId != other.pumpId) + return false; + return true; + } + public void copyFrom(Treatment t) { date = t.date; _id = t._id; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java index b5c036d1fd..7db14ab037 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java @@ -241,7 +241,7 @@ public class TreatmentService extends OrmLiteBaseService { } // return true if new record is created - public boolean createOrUpdate(Treatment treatment) { + public UpdateReturn createOrUpdate(Treatment treatment) { try { Treatment old; treatment.date = DatabaseHelper.roundDateToSec(treatment.date); @@ -252,13 +252,14 @@ public class TreatmentService extends OrmLiteBaseService { if (existingTreatment != null) { // do nothing, pump history record cannot be changed log.debug("TREATMENT: Pump record already found in database: " + treatment.toString()); - return false; + //return new UpdateReturn(true, false); + return new UpdateReturn(existingTreatment.equalsRePumpHistory(treatment), false); } getDao().create(treatment); log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); DatabaseHelper.updateEarliestDataChange(treatment.date); scheduleTreatmentChange(treatment); - return true; + return new UpdateReturn(true, true); } if (treatment.source == Source.NIGHTSCOUT) { old = getDao().queryForId(treatment.date); @@ -275,9 +276,9 @@ public class TreatmentService extends OrmLiteBaseService { DatabaseHelper.updateEarliestDataChange(old.date); } scheduleTreatmentChange(treatment); - return true; + return new UpdateReturn(true, true); } - return false; + return new UpdateReturn(true, false); } // find by NS _id if (treatment._id != null) { @@ -295,7 +296,7 @@ public class TreatmentService extends OrmLiteBaseService { DatabaseHelper.updateEarliestDataChange(old.date); } scheduleTreatmentChange(treatment); - return true; + return new UpdateReturn(true, true); } } } @@ -303,19 +304,19 @@ public class TreatmentService extends OrmLiteBaseService { log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); DatabaseHelper.updateEarliestDataChange(treatment.date); scheduleTreatmentChange(treatment); - return true; + return new UpdateReturn(true, true); } if (treatment.source == Source.USER) { getDao().create(treatment); log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); DatabaseHelper.updateEarliestDataChange(treatment.date); scheduleTreatmentChange(treatment); - return true; + return new UpdateReturn(true, true); } } catch (SQLException e) { log.error("Unhandled exception", e); } - return false; + return new UpdateReturn(false, false); } /** Returns the record for the given id, null if none, throws RuntimeException @@ -439,4 +440,14 @@ public class TreatmentService extends OrmLiteBaseService { public IBinder onBind(Intent intent) { return null; } + + public class UpdateReturn { + public UpdateReturn(boolean success, boolean newRecord){ + this.success = success; + this.newRecord = newRecord; + } + boolean newRecord; + boolean success; + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index 5de75f0690..1abbd52e94 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -479,7 +479,8 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface treatment.carbs = detailedBolusInfo.carbs; treatment.source = detailedBolusInfo.source; treatment.mealBolus = treatment.carbs > 0; - boolean newRecordCreated = getService().createOrUpdate(treatment); + TreatmentService.UpdateReturn creatOrUpdateResult = getService().createOrUpdate(treatment); + boolean newRecordCreated = creatOrUpdateResult.newRecord; //log.debug("Adding new Treatment record" + treatment.toString()); if (detailedBolusInfo.carbTime != 0) { Treatment carbsTreatment = new Treatment(); @@ -494,7 +495,7 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface if (newRecordCreated && detailedBolusInfo.isValid) NSUpload.uploadTreatmentRecord(detailedBolusInfo); - if (!allowUpdate && !newRecordCreated) { + if (!allowUpdate && !creatOrUpdateResult.success) { log.error("Treatment could not be added to DB", new Exception()); Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); i.putExtra("soundid", R.raw.error); From 297fe487f75de9a4adcf064b93a1946172ee2d9f Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Mon, 2 Jul 2018 06:54:02 +0200 Subject: [PATCH 11/24] update with data from pump --- .../plugins/Treatments/Treatment.java | 23 ++++++++++---- .../plugins/Treatments/TreatmentService.java | 30 ++++++++++++++++--- .../plugins/Treatments/TreatmentsPlugin.java | 5 +++- app/src/main/res/values/strings.xml | 2 +- 4 files changed, 49 insertions(+), 11 deletions(-) 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 54802a9081..6ad09a4aa2 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 @@ -26,6 +26,7 @@ import info.nightscout.androidaps.plugins.Overview.graphExtensions.PointsWithLab import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.JsonHelper; +import info.nightscout.utils.ToastUtils; @DatabaseTable(tableName = Treatment.TABLE_TREATMENTS) public class Treatment implements DataPointWithLabelInterface { @@ -135,16 +136,21 @@ public class Treatment implements DataPointWithLabelInterface { /* * mealBolus, _id and isSMB cannot be known coming from pump. Only compare rest + * TODO: remove debug toasts */ public boolean equalsRePumpHistory(Treatment other) { - if (date != other.date) + if (date != other.date) { + ToastUtils.showToastInUiThread(MainApp.instance(), "date: " + date + " vs " + other.date); return false; - if (insulin != other.insulin) + } + if (insulin != other.insulin) { + ToastUtils.showToastInUiThread(MainApp.instance(), "insulin: " + insulin + " vs " + other.insulin); return false; - if (carbs != other.carbs) - return false; - if (pumpId != other.pumpId) + } + if (carbs != other.carbs) { + ToastUtils.showToastInUiThread(MainApp.instance(), "carbs: " + carbs + " vs " + other.carbs); return false; + } return true; } @@ -158,6 +164,13 @@ public class Treatment implements DataPointWithLabelInterface { isSMB = t.isSMB; } + public void copyBasics(Treatment t) { + date = t.date; + insulin = t.insulin; + carbs = t.carbs; + pumpId = t.pumpId; + } + // ----------------- DataPointInterface -------------------- @Override public double getX() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java index 7db14ab037..2fcfe9be55 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java @@ -250,10 +250,32 @@ public class TreatmentService extends OrmLiteBaseService { // check for changed from pump change in NS Treatment existingTreatment = getPumpRecordById(treatment.pumpId); if (existingTreatment != null) { - // do nothing, pump history record cannot be changed - log.debug("TREATMENT: Pump record already found in database: " + treatment.toString()); - //return new UpdateReturn(true, false); - return new UpdateReturn(existingTreatment.equalsRePumpHistory(treatment), false); + boolean equalRePumpHistory = existingTreatment.equalsRePumpHistory(treatment); + if(!equalRePumpHistory) { + // another treatment exists. Update it with the treatment coming from the pump + log.debug("TREATMENT: Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString()); + long oldDate = existingTreatment.date; + getDao().delete(existingTreatment); // need to delete/create because date may change too + existingTreatment.copyBasics(treatment); + getDao().create(existingTreatment); + DatabaseHelper.updateEarliestDataChange(oldDate); + DatabaseHelper.updateEarliestDataChange(existingTreatment.date); + scheduleTreatmentChange(treatment); } + return new UpdateReturn(equalRePumpHistory, false); + } + existingTreatment = getDao().queryForId(treatment.date); + if (existingTreatment != null) { + // another treatment exists with different pumpID. Update it with the treatment coming from the pump + boolean equalRePumpHistory = existingTreatment.equalsRePumpHistory(treatment); + long oldDate = existingTreatment.date; + log.debug("TREATMENT: Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString()); + getDao().delete(existingTreatment); // need to delete/create because date may change too + existingTreatment.copyFrom(treatment); + getDao().create(existingTreatment); + DatabaseHelper.updateEarliestDataChange(oldDate); + DatabaseHelper.updateEarliestDataChange(existingTreatment.date); + scheduleTreatmentChange(treatment); + return new UpdateReturn(equalRePumpHistory, false); } getDao().create(treatment); log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index 1abbd52e94..a99591a781 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -497,10 +497,13 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface if (!allowUpdate && !creatOrUpdateResult.success) { log.error("Treatment could not be added to DB", new Exception()); + + String status = String.format(MainApp.gs(R.string.error_adding_treatment_message), treatment.insulin, (int) treatment.carbs, DateUtil.dateAndTimeString(treatment.date)); + Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); i.putExtra("soundid", R.raw.error); i.putExtra("title", MainApp.gs(R.string.error_adding_treatment_title)); - i.putExtra("status", String.format(MainApp.gs(R.string.error_adding_treatment_message), treatment.insulin, (int) treatment.carbs)); + i.putExtra("status", status); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); MainApp.instance().startActivity(i); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3faab4c3e8..9123990b61 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1181,7 +1181,7 @@ Attention: If you activate and connect to a hardware pump, AndroidAPS will copy the basal settings from the profile to the pump, overwriting the existing basal rate stored on the pump. Make sure you have the correct basal setting in AndroidAPS. If you are not sure or don\'t want to overwrite the basal settings on your pump, press cancel and repeat switching to the pump at a later time. Treatment data incomplete - A treatment (insulin: %.2f, carbs: %d) could not be added to treatments. Please check and manually add a record as appropriate. + A treatment (insulin: %.2f, carbs: %d, at: %s) could not be added to treatments. Please check and manually add a record as appropriate. %d day From 006528ae4c0c485f4cf751946a52f304b48f6e94 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Tue, 3 Jul 2018 19:24:44 +0200 Subject: [PATCH 12/24] Update TreatmentService.java --- .../androidaps/plugins/Treatments/TreatmentService.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java index 2fcfe9be55..4cfaa28cc8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java @@ -251,6 +251,7 @@ public class TreatmentService extends OrmLiteBaseService { Treatment existingTreatment = getPumpRecordById(treatment.pumpId); if (existingTreatment != null) { boolean equalRePumpHistory = existingTreatment.equalsRePumpHistory(treatment); + boolean sameSource = existingTreatment.source == treatment.source; if(!equalRePumpHistory) { // another treatment exists. Update it with the treatment coming from the pump log.debug("TREATMENT: Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString()); @@ -260,13 +261,16 @@ public class TreatmentService extends OrmLiteBaseService { getDao().create(existingTreatment); DatabaseHelper.updateEarliestDataChange(oldDate); DatabaseHelper.updateEarliestDataChange(existingTreatment.date); - scheduleTreatmentChange(treatment); } + scheduleTreatmentChange(treatment); + return new UpdateReturn(sameSource, false); //updating a pump treatment with another one from the pump is not counted as clash + } return new UpdateReturn(equalRePumpHistory, false); } existingTreatment = getDao().queryForId(treatment.date); if (existingTreatment != null) { // another treatment exists with different pumpID. Update it with the treatment coming from the pump boolean equalRePumpHistory = existingTreatment.equalsRePumpHistory(treatment); + boolean sameSource = existingTreatment.source == treatment.source; long oldDate = existingTreatment.date; log.debug("TREATMENT: Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString()); getDao().delete(existingTreatment); // need to delete/create because date may change too @@ -275,7 +279,7 @@ public class TreatmentService extends OrmLiteBaseService { DatabaseHelper.updateEarliestDataChange(oldDate); DatabaseHelper.updateEarliestDataChange(existingTreatment.date); scheduleTreatmentChange(treatment); - return new UpdateReturn(equalRePumpHistory, false); + return new UpdateReturn(equalRePumpHistory || sameSource, false); } getDao().create(treatment); log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); From e5d8eb163b5dd1c195d0a60b002701a93af60a0e Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Tue, 3 Jul 2018 19:25:23 +0200 Subject: [PATCH 13/24] Update Treatment.java --- .../info/nightscout/androidaps/plugins/Treatments/Treatment.java | 1 + 1 file changed, 1 insertion(+) 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 6ad09a4aa2..f8ec7563a1 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 @@ -169,6 +169,7 @@ public class Treatment implements DataPointWithLabelInterface { insulin = t.insulin; carbs = t.carbs; pumpId = t.pumpId; + source = t.source; } // ----------------- DataPointInterface -------------------- From 5f4db31e808b43b8d0867e3ad16735c80f4106fe Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 17 Jul 2018 19:42:18 +0200 Subject: [PATCH 14/24] fix multiple code --- .../plugins/Overview/Dialogs/NewCarbsDialog.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java index f50f8e224e..26f46081ea 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java @@ -127,14 +127,6 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, C startEatingSoonTTCheckbox = view.findViewById(R.id.newcarbs_eating_soon_tt); startEatingSoonTTCheckbox.setOnCheckedChangeListener(this); startHypoTTCheckbox = view.findViewById(R.id.newcarbs_hypo_tt); - BgReading bgReading = DatabaseHelper.actualBg(); - if (bgReading != null && bgReading.value < 72) { - startHypoTTCheckbox.setChecked(true); - // see #onCheckedChanged - startHypoTTCheckbox.setOnClickListener(this); - } - - startHypoTTCheckbox.setOnCheckedChangeListener(this); editTime = view.findViewById(R.id.newcarbs_time); editTime.setParams(0d, -12 * 60d, 12 * 60d, 5d, new DecimalFormat("0"), false, textWatcher); @@ -167,8 +159,8 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, C if (bgReading != null && bgReading.value < 72) { startHypoTTCheckbox.setOnCheckedChangeListener(null); startHypoTTCheckbox.setChecked(true); - startHypoTTCheckbox.setOnClickListener(this); } + startHypoTTCheckbox.setOnClickListener(this); setCancelable(true); getDialog().setCanceledOnTouchOutside(false); From dd687fbb405da091015a981d83bee2f122245054 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 18 Jul 2018 17:02:28 +0200 Subject: [PATCH 15/24] RS,Rv2: load only recent history --- .../plugins/PumpDanaRS/services/DanaRSService.java | 4 ++-- .../PumpDanaRv2/services/DanaRv2ExecutionService.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/DanaRSService.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/DanaRSService.java index 4c0c60461f..fae0430adc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/DanaRSService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/services/DanaRSService.java @@ -240,7 +240,7 @@ public class DanaRSService extends Service { SystemClock.sleep(100); } if (DanaRS_Packet_APS_History_Events.lastEventTimeLoaded != 0) - lastHistoryFetched = DanaRS_Packet_APS_History_Events.lastEventTimeLoaded - 45 * 60 * 1000L; // always load last 45 min + lastHistoryFetched = DanaRS_Packet_APS_History_Events.lastEventTimeLoaded - T.mins(1).msecs(); else lastHistoryFetched = 0; log.debug("Events loaded"); @@ -272,7 +272,7 @@ public class DanaRSService extends Service { // bleComm.sendMessage(msg); DanaRS_Packet_APS_Set_Event_History msgSetHistoryEntry_v2 = new DanaRS_Packet_APS_Set_Event_History(DanaRPump.CARBS, carbtime, carbs, 0); bleComm.sendMessage(msgSetHistoryEntry_v2); - lastHistoryFetched = carbtime - 60000; + lastHistoryFetched = Math.min(lastHistoryFetched, carbtime - T.mins(1).msecs()); } final long bolusStart = System.currentTimeMillis(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java index f33f14b314..1229f23f4e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/services/DanaRv2ExecutionService.java @@ -371,7 +371,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { mSerialIOThread.sendMessage(msg); MsgSetHistoryEntry_v2 msgSetHistoryEntry_v2 = new MsgSetHistoryEntry_v2(DanaRPump.CARBS, carbtime, carbs, 0); mSerialIOThread.sendMessage(msgSetHistoryEntry_v2); - lastHistoryFetched = carbtime - 60000; + lastHistoryFetched = Math.min(lastHistoryFetched, carbtime - T.mins(1).msecs()); } final long bolusStart = System.currentTimeMillis(); @@ -455,7 +455,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { mSerialIOThread.sendMessage(msg); MsgSetHistoryEntry_v2 msgSetHistoryEntry_v2 = new MsgSetHistoryEntry_v2(DanaRPump.CARBS, time, amount, 0); mSerialIOThread.sendMessage(msgSetHistoryEntry_v2); - lastHistoryFetched = time - 1; + lastHistoryFetched = Math.min(lastHistoryFetched, time - T.mins(1).msecs()); return true; } @@ -485,7 +485,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { } SystemClock.sleep(200); if (MsgHistoryEvents_v2.lastEventTimeLoaded != 0) - lastHistoryFetched = MsgHistoryEvents_v2.lastEventTimeLoaded - 45 * 60 * 1000L; //always load last 45 min; + lastHistoryFetched = MsgHistoryEvents_v2.lastEventTimeLoaded - T.mins(1).msecs(); else lastHistoryFetched = 0; mDanaRPump.lastConnection = System.currentTimeMillis(); From 8f8e91f2def154b6ff8d91edb196afe4a5b723a5 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 18 Jul 2018 17:10:21 +0200 Subject: [PATCH 16/24] report TreamtentClash to Fabric --- .../androidaps/plugins/Treatments/TreatmentsPlugin.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index a99591a781..b701f66afc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.Treatments; import android.content.Intent; import android.support.annotation.Nullable; +import com.crashlytics.android.answers.CustomEvent; import com.squareup.otto.Subscribe; import org.slf4j.Logger; @@ -45,6 +46,7 @@ import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.plugins.Sensitivity.SensitivityAAPSPlugin; import info.nightscout.androidaps.plugins.Sensitivity.SensitivityWeightedAveragePlugin; import info.nightscout.utils.DateUtil; +import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.NSUpload; import info.nightscout.utils.SP; import info.nightscout.utils.T; @@ -506,6 +508,10 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface i.putExtra("status", status); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); MainApp.instance().startActivity(i); + + CustomEvent customEvent = new CustomEvent("TreatmentClash"); + customEvent.putCustomAttribute("status", status); + FabricPrivacy.getInstance().logCustom(customEvent); } return newRecordCreated; From d17ffc334d1ad0cc8a9bcecafa149f25ddfa94b6 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 20 Jul 2018 00:19:13 +0200 Subject: [PATCH 17/24] autosens call refactor --- .../androidaps/plugins/IobCobCalculator/AutosensData.java | 4 ++-- .../plugins/IobCobCalculator/IobCobCalculatorPlugin.java | 8 ++++++++ .../plugins/IobCobCalculator/IobCobOref1Thread.java | 2 +- .../androidaps/plugins/IobCobCalculator/IobCobThread.java | 2 +- .../androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java | 2 +- .../androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 2 +- .../androidaps/plugins/Overview/graphData/GraphData.java | 6 +++--- 7 files changed, 17 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java index c826b70437..da2092689a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java @@ -70,7 +70,7 @@ public class AutosensData implements DataPointWithLabelInterface { public double avgDelta = 0d; public double avgDeviation = 0d; - public double autosensRatio = 1d; + public AutosensResult autosensResult = new AutosensResult(); public double slopeFromMaxDeviation = 0; public double slopeFromMinDeviation = 999; public double usedMinCarbsImpact = 0d; @@ -86,7 +86,7 @@ public class AutosensData implements DataPointWithLabelInterface { @Override public String toString() { - return "AutosensData: " + new Date(time).toLocaleString() + " " + pastSensitivity + " Delta=" + delta + " avgDelta=" + avgDelta + " Bgi=" + bgi + " Deviation=" + deviation + " avgDeviation=" + avgDeviation + " Absorbed=" + absorbed + " CarbsFromBolus=" + carbsFromBolus + " COB=" + cob + " autosensRatio=" + autosensRatio + " slopeFromMaxDeviation=" + slopeFromMaxDeviation + " slopeFromMinDeviation =" + slopeFromMinDeviation; + return "AutosensData: " + new Date(time).toLocaleString() + " " + pastSensitivity + " Delta=" + delta + " avgDelta=" + avgDelta + " Bgi=" + bgi + " Deviation=" + deviation + " avgDeviation=" + avgDeviation + " Absorbed=" + absorbed + " CarbsFromBolus=" + carbsFromBolus + " COB=" + cob + " autosensRatio=" + autosensResult.ratio + " slopeFromMaxDeviation=" + slopeFromMaxDeviation + " slopeFromMinDeviation =" + slopeFromMinDeviation; } public int minOld() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java index a34ae6a308..a81bd81fcf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java @@ -137,6 +137,10 @@ public class IobCobCalculatorPlugin extends PluginBase { } void loadBgData(long start) { + if (start < oldestDataAvailable()) { + start = oldestDataAvailable(); + log.debug("Limiting BG data to oldest data available: " + DateUtil.dateAndTimeString(start)); + } bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime((long) (start - 60 * 60 * 1000L * (24 + dia)), false); log.debug("BG data loaded. Size: " + bgReadings.size() + " Start date: " + DateUtil.dateAndTimeString(start)); } @@ -399,6 +403,10 @@ public class IobCobCalculatorPlugin extends PluginBase { @Nullable public AutosensData getLastAutosensDataSynchronized(String reason) { + while (thread != null && thread.getState() != Thread.State.TERMINATED) { + SystemClock.sleep(100); + log.debug("getLastAutosensDataSynchronized is waiting for calculation thread"); + } synchronized (dataLock) { return getLastAutosensData(reason); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java index a4374541a5..2eef4f07c2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java @@ -341,7 +341,7 @@ public class IobCobOref1Thread extends Thread { AutosensResult sensitivity = iobCobCalculatorPlugin.detectSensitivityWithLock(oldestTimeWithData, bgTime); if (Config.logAutosensData) log.debug("Sensitivity result: " + sensitivity.toString()); - autosensData.autosensRatio = sensitivity.ratio; + autosensData.autosensResult = sensitivity; if (Config.logAutosensData) log.debug(autosensData.toString()); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java index de4a70224c..ae01d80d22 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java @@ -268,7 +268,7 @@ public class IobCobThread extends Thread { AutosensResult sensitivity = iobCobCalculatorPlugin.detectSensitivityWithLock(oldestTimeWithData, bgTime); if (Config.logAutosensData) log.debug("Sensitivity result: " + sensitivity.toString()); - autosensData.autosensRatio = sensitivity.ratio; + autosensData.autosensResult = sensitivity; if (Config.logAutosensData) log.debug(autosensData.toString()); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java index 937489bc8f..60853ed4d1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java @@ -173,7 +173,7 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface { startPart = new Date(); if (MainApp.getConstraintChecker().isAutosensModeEnabled().value()) { - lastAutosensResult = IobCobCalculatorPlugin.getPlugin().detectSensitivityWithLock(IobCobCalculatorPlugin.getPlugin().oldestDataAvailable(), System.currentTimeMillis()); + lastAutosensResult = IobCobCalculatorPlugin.getPlugin().getLastAutosensDataSynchronized("OpenAPSPlugin").autosensResult; } else { lastAutosensResult = new AutosensResult(); lastAutosensResult.sensResult = "autosens disabled"; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java index 3227be526e..1bb4c25bb3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java @@ -180,7 +180,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface { startPart = new Date(); if (MainApp.getConstraintChecker().isAutosensModeEnabled().value()) { - lastAutosensResult = IobCobCalculatorPlugin.getPlugin().detectSensitivityWithLock(IobCobCalculatorPlugin.getPlugin().oldestDataAvailable(), System.currentTimeMillis()); + lastAutosensResult = IobCobCalculatorPlugin.getPlugin().getLastAutosensDataSynchronized("OpenAPSPlugin").autosensResult; } else { lastAutosensResult = new AutosensResult(); lastAutosensResult.sensResult = "autosens disabled"; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java index 24ba8c1cea..20feb76124 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java @@ -490,9 +490,9 @@ public class GraphData { for (long time = fromTime; time <= toTime; time += 5 * 60 * 1000L) { AutosensData autosensData = IobCobCalculatorPlugin.getPlugin().getAutosensData(time); if (autosensData != null) { - ratioArray.add(new ScaledDataPoint(time, autosensData.autosensRatio - 1, ratioScale)); - maxRatioValueFound = Math.max(maxRatioValueFound, autosensData.autosensRatio - 1); - minRatioValueFound = Math.min(minRatioValueFound, autosensData.autosensRatio - 1); + ratioArray.add(new ScaledDataPoint(time, autosensData.autosensResult.ratio - 1, ratioScale)); + maxRatioValueFound = Math.max(maxRatioValueFound, autosensData.autosensResult.ratio - 1); + minRatioValueFound = Math.min(minRatioValueFound, autosensData.autosensResult.ratio - 1); } } From cfee405914e44284c4cc78408e97831bdbd2646f Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 20 Jul 2018 10:52:58 +0200 Subject: [PATCH 18/24] use thread.join --- .../IobCobCalculator/IobCobCalculatorPlugin.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java index a81bd81fcf..e848645d1b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java @@ -403,9 +403,13 @@ public class IobCobCalculatorPlugin extends PluginBase { @Nullable public AutosensData getLastAutosensDataSynchronized(String reason) { - while (thread != null && thread.getState() != Thread.State.TERMINATED) { - SystemClock.sleep(100); - log.debug("getLastAutosensDataSynchronized is waiting for calculation thread"); + if (thread != null && thread.getState() != Thread.State.TERMINATED) { + log.debug("getLastAutosensDataSynchronized is waiting for calculation thread: " + reason); + try { + thread.join(5000); + } catch (InterruptedException ignored) { + } + log.debug("getLastAutosensDataSynchronized finished waiting for calculation thread: " + reason); } synchronized (dataLock) { return getLastAutosensData(reason); @@ -505,14 +509,10 @@ public class IobCobCalculatorPlugin extends PluginBase { public AutosensResult detectSensitivityWithLock(long fromTime, long toTime) { synchronized (dataLock) { - return detectSensitivity(fromTime, toTime); + return ConfigBuilderPlugin.getActiveSensitivity().detectSensitivity(fromTime, toTime); } } - static AutosensResult detectSensitivity(long fromTime, long toTime) { - return ConfigBuilderPlugin.getActiveSensitivity().detectSensitivity(fromTime, toTime); - } - public static JSONArray convertToJSONArray(IobTotal[] iobArray) { JSONArray array = new JSONArray(); for (int i = 0; i < iobArray.length; i++) { From ab70d5b67d33cf62c0d913ac9f7cb7b317234d3f Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 20 Jul 2018 17:52:31 +0200 Subject: [PATCH 19/24] use Thread.isAlive() --- .../IobCobCalculator/IobCobCalculatorPlugin.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java index e848645d1b..eecf23d437 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java @@ -392,10 +392,10 @@ public class IobCobCalculatorPlugin extends PluginBase { time = roundUpTime(previous); AutosensData data = autosensDataTable.get(time); if (data != null) { - //log.debug(">>> getAutosensData Cache hit " + data.log(time)); + //log.debug(">>> AUTOSENSDATA Cache hit " + data.toString()); return data; } else { -// log.debug(">>> getAutosensData Cache miss " + new Date(time).toLocaleString()); + //log.debug(">>> AUTOSENSDATA Cache miss " + new Date(time).toLocaleString()); return null; } } @@ -403,13 +403,13 @@ public class IobCobCalculatorPlugin extends PluginBase { @Nullable public AutosensData getLastAutosensDataSynchronized(String reason) { - if (thread != null && thread.getState() != Thread.State.TERMINATED) { - log.debug("getLastAutosensDataSynchronized is waiting for calculation thread: " + reason); + if (thread != null && thread.isAlive()) { + log.debug("AUTOSENSDATA is waiting for calculation thread: " + reason); try { thread.join(5000); } catch (InterruptedException ignored) { } - log.debug("getLastAutosensDataSynchronized finished waiting for calculation thread: " + reason); + log.debug("AUTOSENSDATA finished waiting for calculation thread: " + reason); } synchronized (dataLock) { return getLastAutosensData(reason); @@ -464,6 +464,7 @@ public class IobCobCalculatorPlugin extends PluginBase { log.debug("AUTOSENSDATA null: data is old (" + reason + ") size()=" + autosensDataTable.size() + " lastdata=" + DateUtil.dateAndTimeString(data.time)); return null; } else { + log.debug("AUTOSENSDATA (" + reason + ") " + data.toString()); return data; } } From b52c1f3aa656c3428033a052b13dc9823b5b8de6 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 20 Jul 2018 22:40:44 +0200 Subject: [PATCH 20/24] postpone EventAutosensCalculationFinished --- .../plugins/IobCobCalculator/IobCobOref1Thread.java | 9 +++++++-- .../plugins/IobCobCalculator/IobCobThread.java | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java index 2eef4f07c2..53167b0eaa 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobOref1Thread.java @@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.IobCobCalculator; import android.content.Context; import android.os.PowerManager; +import android.os.SystemClock; import android.support.v4.util.LongSparseArray; import com.crashlytics.android.answers.CustomEvent; @@ -69,6 +70,7 @@ public class IobCobOref1Thread extends Thread { public final void run() { mWakeLock.acquire(); try { + log.debug("AUTOSENSDATA thread started: " + from); if (MainApp.getConfigBuilder() == null) { log.debug("Aborting calculation thread (ConfigBuilder not ready): " + from); return; // app still initializing @@ -346,11 +348,14 @@ public class IobCobOref1Thread extends Thread { log.debug(autosensData.toString()); } } - MainApp.bus().post(new EventAutosensCalculationFinished(cause)); - log.debug("Finishing calculation thread: " + from); + new Thread(() -> { + SystemClock.sleep(1000); + MainApp.bus().post(new EventAutosensCalculationFinished(cause)); + }).start(); } finally { mWakeLock.release(); MainApp.bus().post(new EventIobCalculationProgress("")); + log.debug("AUTOSENSDATA thread ended: " + from); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java index ae01d80d22..307ab7f95f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java @@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.IobCobCalculator; import android.content.Context; import android.os.PowerManager; +import android.os.SystemClock; import android.support.v4.util.LongSparseArray; import com.crashlytics.android.answers.CustomEvent; @@ -68,6 +69,7 @@ public class IobCobThread extends Thread { public final void run() { mWakeLock.acquire(); try { + log.debug("AUTOSENSDATA thread started: " + from); if (MainApp.getConfigBuilder() == null) { log.debug("Aborting calculation thread (ConfigBuilder not ready): " + from); return; // app still initializing @@ -273,11 +275,14 @@ public class IobCobThread extends Thread { log.debug(autosensData.toString()); } } - MainApp.bus().post(new EventAutosensCalculationFinished(cause)); - log.debug("Finishing calculation thread: " + from); + new Thread(() -> { + SystemClock.sleep(1000); + MainApp.bus().post(new EventAutosensCalculationFinished(cause)); + }).start(); } finally { mWakeLock.release(); MainApp.bus().post(new EventIobCalculationProgress("")); + log.debug("AUTOSENSDATA thread ended: " + from); } } From 8402dfb9c7c391d4d4251414f0297e094e52998e Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 21 Jul 2018 15:24:56 +0200 Subject: [PATCH 21/24] more compact log with function name --- app/src/main/assets/logback.xml | 53 +++++++++++++++++---------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/app/src/main/assets/logback.xml b/app/src/main/assets/logback.xml index d6facdb7dd..a09a312301 100644 --- a/app/src/main/assets/logback.xml +++ b/app/src/main/assets/logback.xml @@ -1,36 +1,39 @@ - - - - ${EXT_FILES_DIR}/AndroidAPS.log - + + + + ${EXT_FILES_DIR}/AndroidAPS.log + - ${EXT_FILES_DIR}/AndroidAPS._%d{yyyy-MM-dd}_%d{HH-mm-ss, aux}_.%i.zip + ${EXT_FILES_DIR}/AndroidAPS._%d{yyyy-MM-dd}_%d{HH-mm-ss, aux}_.%i.zip + - + 5MB 120 - - - %d{HH:mm:ss.SSS} [%thread] %-5level [%class:%line]: %msg%n - - + + + %d{HH:mm:ss.SSS} [%thread] %-5level [%class{0}.%M\(\):%line]: %msg%n + + - - %logger{0} - - - [%thread] %-5level [%class:%line]: %msg%n - - - - - - - - + + %logger{0} + + + [%thread] [%class{0}.%M\(\):%line]: %msg%n + + + + + + + + From bcbf43223623ba1ee124dd5850eee48d0c62a7c2 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Sat, 21 Jul 2018 18:34:48 +0200 Subject: [PATCH 22/24] allow update for insight -> don't alarm --- .../androidaps/plugins/PumpInsight/InsightPlugin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java index 3df6a776d5..0d7d074fbd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java @@ -455,7 +455,7 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai bolusingEvent.bolusId = bolusId; bolusingEvent.percent = 0; MainApp.bus().post(bolusingEvent); - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, true); } else { log.debug("Failure to deliver treatment"); } From 983e9c3658a96322ec7e12fd7090a3ea052ddd9f Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Sat, 21 Jul 2018 19:21:40 +0200 Subject: [PATCH 23/24] allow updated Treatments for insight 2 --- .../plugins/PumpInsight/history/HistoryLogAdapter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 fcb2ee536c..bff37c9fdd 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 @@ -83,6 +83,6 @@ class HistoryLogAdapter { detailedBolusInfo.source = Source.PUMP; detailedBolusInfo.pumpId = record_id; detailedBolusInfo.insulin = insulin; - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, true); } } From 77d232633191f3023af11639df511fddbfe47757 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Sat, 21 Jul 2018 22:21:12 +0200 Subject: [PATCH 24/24] remove debug toasts --- .../nightscout/androidaps/plugins/Treatments/Treatment.java | 4 ---- 1 file changed, 4 deletions(-) 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 f8ec7563a1..73423c0eab 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 @@ -26,7 +26,6 @@ import info.nightscout.androidaps.plugins.Overview.graphExtensions.PointsWithLab import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.JsonHelper; -import info.nightscout.utils.ToastUtils; @DatabaseTable(tableName = Treatment.TABLE_TREATMENTS) public class Treatment implements DataPointWithLabelInterface { @@ -140,15 +139,12 @@ public class Treatment implements DataPointWithLabelInterface { */ public boolean equalsRePumpHistory(Treatment other) { if (date != other.date) { - ToastUtils.showToastInUiThread(MainApp.instance(), "date: " + date + " vs " + other.date); return false; } if (insulin != other.insulin) { - ToastUtils.showToastInUiThread(MainApp.instance(), "insulin: " + insulin + " vs " + other.insulin); return false; } if (carbs != other.carbs) { - ToastUtils.showToastInUiThread(MainApp.instance(), "carbs: " + carbs + " vs " + other.carbs); return false; } return true;