From e732c310434cb78ecf9cdb76570c04ed0f84a1ff Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 31 Aug 2019 10:09:53 +0200 Subject: [PATCH] fix NPE in APS plugins --- .../DetermineBasalAdapterAMAJS.java | 3 ++ .../aps/openAPSAMA/OpenAPSAMAPlugin.java | 36 ++++++++++------ .../openAPSMA/DetermineBasalAdapterMAJS.java | 3 ++ .../aps/openAPSMA/OpenAPSMAPlugin.java | 36 ++++++++++------ .../DetermineBasalAdapterSMBJS.java | 3 ++ .../aps/openAPSSMB/OpenAPSSMBPlugin.java | 41 +++++++++++-------- 6 files changed, 80 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/DetermineBasalAdapterAMAJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/DetermineBasalAdapterAMAJS.java index 7a1c2ee16e..703dc4255b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/DetermineBasalAdapterAMAJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/DetermineBasalAdapterAMAJS.java @@ -18,6 +18,8 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.nio.charset.StandardCharsets; +import javax.annotation.Nullable; + import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -60,6 +62,7 @@ public class DetermineBasalAdapterAMAJS { mScriptReader = scriptReader; } + @Nullable public DetermineBasalResultAMA invoke() { if (L.isEnabled(L.APS)) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAPlugin.java index f8c1bbcaf1..ef31ad599e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAPlugin.java @@ -29,6 +29,7 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.HardLimits; import info.nightscout.androidaps.utils.Profiler; import info.nightscout.androidaps.utils.Round; @@ -193,7 +194,8 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface { isTempTarget ); } catch (JSONException e) { - log.error("Unable to set data: " + e.toString()); + FabricPrivacy.logException(e); + return; } @@ -201,22 +203,30 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface { if (L.isEnabled(L.APS)) Profiler.log(log, "AMA calculation", start); // Fix bug determine basal - if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !TreatmentsPlugin.getPlugin().isTempBasalInProgress()) - determineBasalResultAMA.tempBasalRequested = false; + if (determineBasalResultAMA == null) { + if (L.isEnabled(L.APS)) + log.error("SMB calculation returned null"); + lastDetermineBasalAdapterAMAJS = null; + lastAPSResult = null; + lastAPSRun = 0; + } else { + if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !TreatmentsPlugin.getPlugin().isTempBasalInProgress()) + determineBasalResultAMA.tempBasalRequested = false; - determineBasalResultAMA.iob = iobArray[0]; + determineBasalResultAMA.iob = iobArray[0]; - long now = System.currentTimeMillis(); + long now = System.currentTimeMillis(); - try { - determineBasalResultAMA.json.put("timestamp", DateUtil.toISOString(now)); - } catch (JSONException e) { - log.error("Unhandled exception", e); + try { + determineBasalResultAMA.json.put("timestamp", DateUtil.toISOString(now)); + } catch (JSONException e) { + log.error("Unhandled exception", e); + } + + lastDetermineBasalAdapterAMAJS = determineBasalAdapterAMAJS; + lastAPSResult = determineBasalResultAMA; + lastAPSRun = now; } - - lastDetermineBasalAdapterAMAJS = determineBasalAdapterAMAJS; - lastAPSResult = determineBasalResultAMA; - lastAPSRun = now; RxBus.INSTANCE.send(new EventOpenAPSUpdateGui()); //deviceStatus.suggested = determineBasalResultAMA.json; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSMA/DetermineBasalAdapterMAJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSMA/DetermineBasalAdapterMAJS.java index 853af9ca83..1ded34b7cb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSMA/DetermineBasalAdapterMAJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSMA/DetermineBasalAdapterMAJS.java @@ -16,6 +16,8 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.nio.charset.StandardCharsets; +import javax.annotation.Nullable; + import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; @@ -48,6 +50,7 @@ public class DetermineBasalAdapterMAJS { mScriptReader = scriptReader; } + @Nullable public DetermineBasalResultMA invoke() { DetermineBasalResultMA determineBasalResultMA = null; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSMA/OpenAPSMAPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSMA/OpenAPSMAPlugin.java index c573c19af1..899c0151d8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSMA/OpenAPSMAPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSMA/OpenAPSMAPlugin.java @@ -26,6 +26,7 @@ import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.HardLimits; import info.nightscout.androidaps.utils.Profiler; import info.nightscout.androidaps.utils.Round; @@ -170,7 +171,8 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface { try { determineBasalAdapterMAJS.setData(profile, maxIob, maxBasal, minBg, maxBg, targetBg, ConfigBuilderPlugin.getPlugin().getActivePump().getBaseBasalRate(), iobTotal, glucoseStatus, mealData); } catch (JSONException e) { - log.error("Unhandled exception", e); + FabricPrivacy.logException(e); + return; } if (L.isEnabled(L.APS)) Profiler.log(log, "MA calculation", start); @@ -179,21 +181,29 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface { long now = System.currentTimeMillis(); DetermineBasalResultMA determineBasalResultMA = determineBasalAdapterMAJS.invoke(); - // Fix bug determinef basal - if (determineBasalResultMA.rate == 0d && determineBasalResultMA.duration == 0 && !TreatmentsPlugin.getPlugin().isTempBasalInProgress()) - determineBasalResultMA.tempBasalRequested = false; + if (determineBasalResultMA == null) { + if (L.isEnabled(L.APS)) + log.error("MA calculation returned null"); + lastDetermineBasalAdapterMAJS = null; + lastAPSResult = null; + lastAPSRun = 0; + } else { + // Fix bug determinef basal + if (determineBasalResultMA.rate == 0d && determineBasalResultMA.duration == 0 && !TreatmentsPlugin.getPlugin().isTempBasalInProgress()) + determineBasalResultMA.tempBasalRequested = false; - determineBasalResultMA.iob = iobTotal; + determineBasalResultMA.iob = iobTotal; - try { - determineBasalResultMA.json.put("timestamp", DateUtil.toISOString(now)); - } catch (JSONException e) { - log.error("Unhandled exception", e); + try { + determineBasalResultMA.json.put("timestamp", DateUtil.toISOString(now)); + } catch (JSONException e) { + log.error("Unhandled exception", e); + } + + lastDetermineBasalAdapterMAJS = determineBasalAdapterMAJS; + lastAPSResult = determineBasalResultMA; + lastAPSRun = now; } - - lastDetermineBasalAdapterMAJS = determineBasalAdapterMAJS; - lastAPSResult = determineBasalResultMA; - lastAPSRun = now; RxBus.INSTANCE.send(new EventOpenAPSUpdateGui()); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/DetermineBasalAdapterSMBJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/DetermineBasalAdapterSMBJS.java index 09f2148f7f..b7b6d82e5e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/DetermineBasalAdapterSMBJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/DetermineBasalAdapterSMBJS.java @@ -18,6 +18,8 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.nio.charset.StandardCharsets; +import javax.annotation.Nullable; + import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -69,6 +71,7 @@ public class DetermineBasalAdapterSMBJS { } + @Nullable public DetermineBasalResultSMB invoke() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBPlugin.java index 5f90e572f7..d410eba291 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBPlugin.java @@ -32,6 +32,7 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.HardLimits; import info.nightscout.androidaps.utils.Profiler; import info.nightscout.androidaps.utils.Round; @@ -219,7 +220,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface, Constr advancedFiltering.value() ); } catch (JSONException e) { - log.error(e.getMessage()); + FabricPrivacy.logException(e); return; } @@ -228,24 +229,32 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface, Constr DetermineBasalResultSMB determineBasalResultSMB = determineBasalAdapterSMBJS.invoke(); if (L.isEnabled(L.APS)) Profiler.log(log, "SMB calculation", start); - // TODO still needed with oref1? - // Fix bug determine basal - if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !TreatmentsPlugin.getPlugin().isTempBasalInProgress()) - determineBasalResultSMB.tempBasalRequested = false; + if (determineBasalResultSMB == null) { + if (L.isEnabled(L.APS)) + log.error("SMB calculation returned null"); + lastDetermineBasalAdapterSMBJS = null; + lastAPSResult = null; + lastAPSRun = 0; + } else { + // TODO still needed with oref1? + // Fix bug determine basal + if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !TreatmentsPlugin.getPlugin().isTempBasalInProgress()) + determineBasalResultSMB.tempBasalRequested = false; - determineBasalResultSMB.iob = iobArray[0]; + determineBasalResultSMB.iob = iobArray[0]; - try { - determineBasalResultSMB.json.put("timestamp", DateUtil.toISOString(now)); - } catch (JSONException e) { - log.error("Unhandled exception", e); + try { + determineBasalResultSMB.json.put("timestamp", DateUtil.toISOString(now)); + } catch (JSONException e) { + log.error("Unhandled exception", e); + } + + determineBasalResultSMB.inputConstraints = inputConstraints; + + lastDetermineBasalAdapterSMBJS = determineBasalAdapterSMBJS; + lastAPSResult = determineBasalResultSMB; + lastAPSRun = now; } - - determineBasalResultSMB.inputConstraints = inputConstraints; - - lastDetermineBasalAdapterSMBJS = determineBasalAdapterSMBJS; - lastAPSResult = determineBasalResultSMB; - lastAPSRun = now; RxBus.INSTANCE.send(new EventOpenAPSUpdateGui()); //deviceStatus.suggested = determineBasalResultAMA.json;