From d9fffc5625bb056839bd9883ecdca041912f49fb Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 13 Jan 2017 20:30:48 +0100 Subject: [PATCH] refactor IOB calculation & fix iob_array.time --- .../info/nightscout/androidaps/data/IobTotal.java | 13 ++++++------- .../info/nightscout/androidaps/db/TempBasal.java | 2 +- .../plugins/Overview/OverviewFragment.java | 2 -- .../SmsCommunicator/SmsCommunicatorPlugin.java | 2 -- .../plugins/TempBasals/TempBasalsPlugin.java | 2 +- .../plugins/Treatments/TreatmentsPlugin.java | 2 +- .../Wear/wearintegration/WatchUpdaterService.java | 2 -- .../PersistentNotificationPlugin.java | 2 -- 8 files changed, 9 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java b/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java index 84d62768f7..f2a6ad0df2 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java +++ b/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java @@ -23,13 +23,16 @@ public class IobTotal { public Double netInsulin = 0d; // for calculations from temp basals only public Double netRatio = 0d; // for calculations from temp basals only - public IobTotal() { + long time; + + public IobTotal(long time) { this.iob = 0d; this.activity = 0d; this.bolussnooze = 0d; this.basaliob = 0d; this.netbasalinsulin = 0d; this.hightempinsulin = 0d; + this.time = time; } public IobTotal plus(IobTotal other) { @@ -45,7 +48,7 @@ public class IobTotal { } public static IobTotal combine(IobTotal bolusIOB, IobTotal basalIob) { - IobTotal result = new IobTotal(); + IobTotal result = new IobTotal(bolusIOB.time); result.iob = bolusIOB.iob + basalIob.basaliob; result.activity = bolusIOB.activity + basalIob.activity; result.bolussnooze = bolusIOB.bolussnooze; @@ -85,7 +88,7 @@ public class IobTotal { json.put("basaliob", basaliob); json.put("bolussnooze", bolussnooze); json.put("activity", activity); - json.put("time", DateUtil.toISOString(new Date())); + json.put("time", DateUtil.toISOString(new Date(time))); } catch (JSONException e) { e.printStackTrace(); } @@ -95,19 +98,15 @@ public class IobTotal { public static IobTotal calulateFromTreatmentsAndTemps() { ConfigBuilderPlugin.getActiveTreatments().updateTotalIOB(); IobTotal bolusIob = ConfigBuilderPlugin.getActiveTreatments().getLastCalculation().round(); - if (bolusIob == null) bolusIob = new IobTotal(); ConfigBuilderPlugin.getActiveTempBasals().updateTotalIOB(); IobTotal basalIob = ConfigBuilderPlugin.getActiveTempBasals().getLastCalculation().round(); - if (basalIob == null) basalIob = new IobTotal(); IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round(); return iobTotal; } public static IobTotal calulateFromTreatmentsAndTemps(long time) { IobTotal bolusIob = ConfigBuilderPlugin.getActiveTreatments().getCalculationToTime(time).round(); - if (bolusIob == null) bolusIob = new IobTotal(); IobTotal basalIob = ConfigBuilderPlugin.getActiveTempBasals().getCalculationToTime(time).round(); - if (basalIob == null) basalIob = new IobTotal(); IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round(); return iobTotal; } diff --git a/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java b/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java index 69ad708072..fa99118bca 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java +++ b/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java @@ -53,7 +53,7 @@ public class TempBasal { public IobTotal iobCalc(Date time) { - IobTotal result = new IobTotal(); + IobTotal result = new IobTotal(time.getTime()); NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); if (profile == null) 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 7e996b8619..d6ed1fa2df 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 @@ -621,10 +621,8 @@ public class OverviewFragment extends Fragment { // iob MainApp.getConfigBuilder().getActiveTreatments().updateTotalIOB(); IobTotal bolusIob = MainApp.getConfigBuilder().getActiveTreatments().getLastCalculation().round(); - if (bolusIob == null) bolusIob = new IobTotal(); MainApp.getConfigBuilder().getActiveTempBasals().updateTotalIOB(); IobTotal basalIob = MainApp.getConfigBuilder().getActiveTempBasals().getLastCalculation().round(); - if (basalIob == null) basalIob = new IobTotal(); String iobtext = getString(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U (" + getString(R.string.bolus) + ": " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U " diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index 3ef613ec84..f56057b1b6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -219,10 +219,8 @@ public class SmsCommunicatorPlugin implements PluginBase { MainApp.getConfigBuilder().getActiveTreatments().updateTotalIOB(); IobTotal bolusIob = MainApp.getConfigBuilder().getActiveTreatments().getLastCalculation().round(); - if (bolusIob == null) bolusIob = new IobTotal(); MainApp.getConfigBuilder().getActiveTempBasals().updateTotalIOB(); IobTotal basalIob = MainApp.getConfigBuilder().getActiveTempBasals().getLastCalculation().round(); - if (basalIob == null) basalIob = new IobTotal(); reply += MainApp.sResources.getString(R.string.sms_iob) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U (" + MainApp.sResources.getString(R.string.sms_bolus) + " " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U " diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsPlugin.java index 964ea23a15..fc4aa6cb54 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsPlugin.java @@ -174,7 +174,7 @@ public class TempBasalsPlugin implements PluginBase, TempBasalsInterface { checkForExpired(tempBasals); checkForExpired(extendedBoluses); Date now = new Date(time); - IobTotal total = new IobTotal(); + IobTotal total = new IobTotal(time); for (Integer pos = 0; pos < tempBasals.size(); pos++) { TempBasal t = tempBasals.get(pos); IobTotal calc = t.iobCalc(now); 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 c4dfe99cb1..91ebbc2e6a 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 @@ -110,7 +110,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { @Override public IobTotal getCalculationToTime(long time) { - IobTotal total = new IobTotal(); + IobTotal total = new IobTotal(time); if (MainApp.getConfigBuilder() == null || ConfigBuilderPlugin.getActiveProfile() == null) // app not initialized yet return total; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java index 96e4a951bd..66383cf7d5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java @@ -444,10 +444,8 @@ public class WatchUpdaterService extends WearableListenerService implements //IOB MainApp.getConfigBuilder().getActiveTreatments().updateTotalIOB(); IobTotal bolusIob = MainApp.getConfigBuilder().getActiveTreatments().getLastCalculation().round(); - if (bolusIob == null) bolusIob = new IobTotal(); MainApp.getConfigBuilder().getActiveTempBasals().updateTotalIOB(); IobTotal basalIob = MainApp.getConfigBuilder().getActiveTempBasals().getLastCalculation().round(); - if (basalIob == null) basalIob = new IobTotal(); status += (shortString?"":(getString(R.string.treatments_iob_label_string) + " ")) + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "(" + DecimalFormatter.to2Decimal(bolusIob.iob) + "|" + DecimalFormatter.to2Decimal(basalIob.basaliob) + ")"; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/persistentnotification/PersistentNotificationPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/persistentnotification/PersistentNotificationPlugin.java index 745997afc8..9e91547f3d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/persistentnotification/PersistentNotificationPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/persistentnotification/PersistentNotificationPlugin.java @@ -126,10 +126,8 @@ public class PersistentNotificationPlugin implements PluginBase{ //IOB MainApp.getConfigBuilder().getActiveTreatments().updateTotalIOB(); IobTotal bolusIob = MainApp.getConfigBuilder().getActiveTreatments().getLastCalculation().round(); - if (bolusIob == null) bolusIob = new IobTotal(); MainApp.getConfigBuilder().getActiveTempBasals().updateTotalIOB(); IobTotal basalIob = MainApp.getConfigBuilder().getActiveTempBasals().getLastCalculation().round(); - if (basalIob == null) basalIob = new IobTotal(); String line2 = ctx.getString(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U (" + ctx.getString(R.string.bolus) + ": " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U " + ctx.getString(R.string.basal) + ": " + DecimalFormatter.to2Decimal(basalIob.basaliob) + "U)";