From 638f6bc2911d6ec6fec3ccdb29e6d7e2a4494b45 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 25 Apr 2017 16:37:27 +0200 Subject: [PATCH] IobCobCalculationPlugin --- .../info/nightscout/androidaps/MainApp.java | 12 +- .../nightscout/androidaps/data/IobTotal.java | 39 --- .../nightscout/androidaps/data/MealData.java | 4 +- .../nightscout/androidaps/db/TempBasal.java | 9 +- .../IobCobCalculatorFragment.java | 16 ++ .../IobCobCalculatorPlugin.java | 256 ++++++++++++++++++ .../plugins/OpenAPSAMA/Autosens.java | 68 +---- .../DetermineBasalAdapterAMAJS.java | 7 +- .../plugins/OpenAPSAMA/OpenAPSAMAPlugin.java | 16 +- .../plugins/Overview/OverviewFragment.java | 14 +- .../plugins/TempBasals/TempBasalsPlugin.java | 2 +- 11 files changed, 312 insertions(+), 131 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorFragment.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 25ae1ed054..71a32cb5b4 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -23,6 +23,7 @@ import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.Actions.ActionsFragment; import info.nightscout.androidaps.plugins.Careportal.CareportalFragment; import info.nightscout.androidaps.plugins.InsulinFastactingProlonged.InsulinFastactingProlongedFragment; +import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorFragment; import info.nightscout.androidaps.plugins.ProfileCircadianPercentage.CircadianPercentageProfileFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; @@ -87,6 +88,7 @@ public class MainApp extends Application { pluginsList = new ArrayList<>(); // Register all tabs in app here pluginsList.add(OverviewFragment.getPlugin()); + pluginsList.add(IobCobCalculatorFragment.getPlugin()); if (Config.ACTION) pluginsList.add(ActionsFragment.getPlugin()); pluginsList.add(InsulinFastactingFragment.getPlugin()); pluginsList.add(InsulinFastactingProlongedFragment.getPlugin()); @@ -101,7 +103,8 @@ public class MainApp extends Application { pluginsList.add(NSProfileFragment.getPlugin()); if (Config.OTHERPROFILES) pluginsList.add(SimpleProfileFragment.getPlugin()); if (Config.OTHERPROFILES) pluginsList.add(LocalProfileFragment.getPlugin()); - if (Config.OTHERPROFILES) pluginsList.add(CircadianPercentageProfileFragment.getPlugin()); + if (Config.OTHERPROFILES) + pluginsList.add(CircadianPercentageProfileFragment.getPlugin()); if (Config.APS) pluginsList.add(TempTargetRangeFragment.getPlugin()); pluginsList.add(TreatmentsFragment.getPlugin()); if (Config.TEMPBASALS) pluginsList.add(TempBasalsFragment.getPlugin()); @@ -152,10 +155,9 @@ public class MainApp extends Application { } - - public void stopKeepAliveService(){ - if(keepAliveReceiver!=null) - keepAliveReceiver.cancelAlarm(this); + public void stopKeepAliveService() { + if (keepAliveReceiver != null) + keepAliveReceiver.cancelAlarm(this); } public static Bus bus() { 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 ce0a94ef5a..941480e3a1 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java +++ b/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java @@ -94,43 +94,4 @@ public class IobTotal { return json; } - public static IobTotal calulateFromTreatmentsAndTemps() { - ConfigBuilderPlugin.getActiveTreatments().updateTotalIOB(); - IobTotal bolusIob = ConfigBuilderPlugin.getActiveTreatments().getLastCalculation().round(); - ConfigBuilderPlugin.getActiveTempBasals().updateTotalIOB(); - IobTotal basalIob = ConfigBuilderPlugin.getActiveTempBasals().getLastCalculation().round(); - IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round(); - return iobTotal; - } - - public static IobTotal calulateFromTreatmentsAndTemps(long time) { - IobTotal bolusIob = ConfigBuilderPlugin.getActiveTreatments().getCalculationToTime(time).round(); - IobTotal basalIob = ConfigBuilderPlugin.getActiveTempBasals().getCalculationToTime(time).round(); - IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round(); - return iobTotal; - } - - public static IobTotal[] calculateIobArrayInDia() { - NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile(); - // predict IOB out to DIA plus 30m - long time = new Date().getTime(); - int len = (int) ((profile.getDia() *60 + 30) / 5); - IobTotal[] array = new IobTotal[len]; - int pos = 0; - for (int i = 0; i < len; i++){ - long t = time + i * 5 * 60000; - IobTotal iob = calulateFromTreatmentsAndTemps(t); - array[pos] = iob; - pos++; - } - return array; - } - - public static JSONArray convertToJSONArray(IobTotal[] iobArray) { - JSONArray array = new JSONArray(); - for (int i = 0; i < iobArray.length; i ++) { - array.put(iobArray[i].determineBasalJson()); - } - return array; - } } diff --git a/app/src/main/java/info/nightscout/androidaps/data/MealData.java b/app/src/main/java/info/nightscout/androidaps/data/MealData.java index 18919c3c8a..160d47813e 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/MealData.java +++ b/app/src/main/java/info/nightscout/androidaps/data/MealData.java @@ -25,8 +25,6 @@ public class MealData { NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); if (profile == null) return; - List bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime((long) (new Date().getTime() - 60 * 60 * 1000L * profile.getDia() * 2), false); - long now = new Date().getTime(); long dia_ago = now - (new Double(1.5d * profile.getDia() * 60 * 60 * 1000l)).longValue(); long t = treatment.created_at.getTime(); @@ -34,7 +32,7 @@ public class MealData { if (treatment.carbs >= 1) { carbs += treatment.carbs; if (MainApp.getSpecificPlugin(OpenAPSAMAPlugin.class).isEnabled(PluginBase.APS)) { - AutosensResult result = Autosens.detectSensitivityandCarbAbsorption(bgReadings, t); + AutosensResult result = Autosens.detectSensitivityandCarbAbsorption((long) (new Date().getTime() - 60 * 60 * 1000L * profile.getDia() * 2), t); double myCarbsAbsorbed = result.carbsAbsorbed; double myMealCOB = Math.max(0, carbs - myCarbsAbsorbed); mealCOB = Math.max(mealCOB, myMealCOB); 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 0bc268e8cc..43ec244d59 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java +++ b/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java @@ -12,6 +12,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.data.Iob; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.interfaces.InsulinInterface; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; @@ -55,13 +56,13 @@ public class TempBasal { public IobTotal iobCalc(long time) { IobTotal result = new IobTotal(time); - NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); - InsulinInterface insulinInterface = MainApp.getConfigBuilder().getActiveInsulin(); + NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile(); + InsulinInterface insulinInterface = ConfigBuilderPlugin.getActiveInsulin(); if (profile == null) return result; - Double basalRate = profile.getBasal(profile.secondsFromMidnight(time)); + Double basalRate = profile.getBasal(NSProfile.secondsFromMidnight(time)); if (basalRate == null) return result; @@ -94,7 +95,7 @@ public class TempBasal { Long tempBolusCount = Math.round(netBasalAmount / tempBolusSize); if (tempBolusCount > 0) { Long tempBolusSpacing = realDuration / tempBolusCount; - for (Long j = 0l; j < tempBolusCount; j++) { + for (Long j = 0L; j < tempBolusCount; j++) { Treatment tempBolusPart = new Treatment(insulinInterface); tempBolusPart.insulin = tempBolusSize; Long date = this.timeStart.getTime() + j * tempBolusSpacing * 60 * 1000; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorFragment.java new file mode 100644 index 0000000000..db1db95998 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorFragment.java @@ -0,0 +1,16 @@ +package info.nightscout.androidaps.plugins.IobCobCalculator; + +import android.support.v4.app.Fragment; + +/** + * Created by adrian on 17/11/16. + */ + +public class IobCobCalculatorFragment extends Fragment { + + private static IobCobCalculatorPlugin iobCobCalculatorPlugin = new IobCobCalculatorPlugin(); + + public static IobCobCalculatorPlugin getPlugin() { + return iobCobCalculatorPlugin; + } +} 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 new file mode 100644 index 0000000000..198da5fdab --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java @@ -0,0 +1,256 @@ +package info.nightscout.androidaps.plugins.IobCobCalculator; + +import android.os.Handler; +import android.os.HandlerThread; +import android.support.annotation.Nullable; + +import com.squareup.otto.Subscribe; + +import org.json.JSONArray; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; + +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.data.IobTotal; +import info.nightscout.androidaps.data.MealData; +import info.nightscout.androidaps.db.BgReading; +import info.nightscout.androidaps.events.EventNewBG; +import info.nightscout.androidaps.events.EventNewBasalProfile; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; + +/** + * Created by mike on 24.04.2017. + */ + +public class IobCobCalculatorPlugin implements PluginBase { + private static Logger log = LoggerFactory.getLogger(IobCobCalculatorPlugin.class); + + private static HashMap iobTable = new HashMap(); + private static HashMap mealDataTable = new HashMap(); + + private static List bgReadings = null; // newest at index 0 + private static List bucketed_data = null; + + private static double dia = Constants.defaultDIA; + + private static Handler sHandler = null; + private static HandlerThread sHandlerThread = null; + + @Override + public int getType() { + return GENERAL; + } + + @Override + public String getFragmentClass() { + return IobCobCalculatorFragment.class.getName(); + } + + @Override + public String getName() { + return "IOB COB Calculator"; + } + + @Override + public String getNameShort() { + return "IOC"; + } + + @Override + public boolean isEnabled(int type) { + return type == GENERAL; + } + + @Override + public boolean isVisibleInTabs(int type) { + return false; + } + + @Override + public boolean canBeHidden(int type) { + return false; + } + + @Override + public void setFragmentEnabled(int type, boolean fragmentEnabled) { + + } + + @Override + public void setFragmentVisible(int type, boolean fragmentVisible) { + + } + + public IobCobCalculatorPlugin() { + MainApp.bus().register(this); + if (sHandlerThread == null) { + sHandlerThread = new HandlerThread(IobCobCalculatorPlugin.class.getSimpleName()); + sHandlerThread.start(); + sHandler = new Handler(sHandlerThread.getLooper()); + } + onNewBg(new EventNewBG()); + } + + @Nullable + public static List getBucketedData(long fromTime) { + if (bucketed_data == null) { + log.debug("No bucketed data available"); + return null; + } + int index = indexNewerThan(fromTime); + if (index > -1) { + log.debug("Bucketed data striped off: " + index + "/" + bucketed_data.size()); + return bucketed_data.subList(0, index); + } + return null; + } + + private static int indexNewerThan(long time) { + for (int index = 0; index < bucketed_data.size(); index++) { + if (bucketed_data.get(index).timeIndex < time) + return index - 1; + } + return -1; + } + + private void loadBgData() { + onNewProfile(new EventNewBasalProfile(null)); + bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime((long) (new Date().getTime() - 60 * 60 * 1000L * (24 + dia)), false); + log.debug("BG data loaded. Size: " + bgReadings.size()); + } + + public void createBucketedData() { + if (bgReadings == null || bgReadings.size() < 3) { + bucketed_data = null; + return; + } + + bucketed_data = new ArrayList<>(); + bucketed_data.add(bgReadings.get(0)); + int j = 0; + for (int i = 1; i < bgReadings.size(); ++i) { + long bgTime = bgReadings.get(i).getTimeIndex(); + long lastbgTime = bgReadings.get(i - 1).getTimeIndex(); + //log.error("Processing " + i + ": " + new Date(bgTime).toString() + " " + bgReadings.get(i).value + " Previous: " + new Date(lastbgTime).toString() + " " + bgReadings.get(i - 1).value); + if (bgReadings.get(i).value < 39 || bgReadings.get(i - 1).value < 39) { + continue; + } + + long elapsed_minutes = (bgTime - lastbgTime) / (60 * 1000); + if (Math.abs(elapsed_minutes) > 8) { + // interpolate missing data points + double lastbg = bgReadings.get(i - 1).value; + elapsed_minutes = Math.abs(elapsed_minutes); + //console.error(elapsed_minutes); + long nextbgTime; + while (elapsed_minutes > 5) { + nextbgTime = lastbgTime - 5 * 60 * 1000; + j++; + BgReading newBgreading = new BgReading(); + newBgreading.timeIndex = nextbgTime; + double gapDelta = bgReadings.get(i).value - lastbg; + //console.error(gapDelta, lastbg, elapsed_minutes); + double nextbg = lastbg + (5d / elapsed_minutes * gapDelta); + newBgreading.value = Math.round(nextbg); + //console.error("Interpolated", bucketed_data[j]); + bucketed_data.add(newBgreading); + //log.error("******************************************************************************************************* Adding:" + new Date(newBgreading.timeIndex).toString() + " " + newBgreading.value); + + elapsed_minutes = elapsed_minutes - 5; + lastbg = nextbg; + lastbgTime = nextbgTime; + } + j++; + BgReading newBgreading = new BgReading(); + newBgreading.value = bgReadings.get(i).value; + newBgreading.timeIndex = bgTime; + bucketed_data.add(newBgreading); + //log.error("******************************************************************************************************* Copying:" + new Date(newBgreading.timeIndex).toString() + " " + newBgreading.value); + } else if (Math.abs(elapsed_minutes) > 2) { + j++; + BgReading newBgreading = new BgReading(); + newBgreading.value = bgReadings.get(i).value; + newBgreading.timeIndex = bgTime; + bucketed_data.add(newBgreading); + //log.error("******************************************************************************************************* Copying:" + new Date(newBgreading.timeIndex).toString() + " " + newBgreading.value); + } else { + bucketed_data.get(j).value = (bucketed_data.get(j).value + bgReadings.get(i).value) / 2; + //log.error("***** Average"); + } + } + log.debug("Bucketed data created. Size: " + bucketed_data.size()); + } + + public static IobTotal calulateFromTreatmentsAndTemps() { + ConfigBuilderPlugin.getActiveTreatments().updateTotalIOB(); + IobTotal bolusIob = ConfigBuilderPlugin.getActiveTreatments().getLastCalculation().round(); + ConfigBuilderPlugin.getActiveTempBasals().updateTotalIOB(); + IobTotal basalIob = ConfigBuilderPlugin.getActiveTempBasals().getLastCalculation().round(); + IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round(); + return iobTotal; + } + + public static IobTotal calulateFromTreatmentsAndTemps(long time) { + IobTotal bolusIob = ConfigBuilderPlugin.getActiveTreatments().getCalculationToTime(time).round(); + IobTotal basalIob = ConfigBuilderPlugin.getActiveTempBasals().getCalculationToTime(time).round(); + if (basalIob.basaliob > 0) { + log.debug(new Date(time).toLocaleString() + " basaliob: " + basalIob.basaliob ); + } + IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round(); + return iobTotal; + } + + public static IobTotal[] calculateIobArrayInDia() { + NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile(); + // predict IOB out to DIA plus 30m + long time = new Date().getTime(); + int len = (int) ((profile.getDia() * 60 + 30) / 5); + IobTotal[] array = new IobTotal[len]; + int pos = 0; + for (int i = 0; i < len; i++){ + long t = time + i * 5 * 60000; + IobTotal iob = calulateFromTreatmentsAndTemps(t); + array[pos] = iob; + pos++; + } + return array; + } + + public static JSONArray convertToJSONArray(IobTotal[] iobArray) { + JSONArray array = new JSONArray(); + for (int i = 0; i < iobArray.length; i ++) { + array.put(iobArray[i].determineBasalJson()); + } + return array; + } + + @Subscribe + public void onNewBg(EventNewBG ev) { + sHandler.post(new Runnable() { + @Override + public void run() { + loadBgData(); + createBucketedData(); + } + }); + } + + @Subscribe + public void onNewProfile(EventNewBasalProfile ev) { + if (MainApp.getConfigBuilder().getActiveProfile() == null) + return; + NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); + if (profile != null) { + dia = profile.getDia(); + } + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/Autosens.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/Autosens.java index 1c02842ddc..2629b1a11b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/Autosens.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/Autosens.java @@ -3,12 +3,12 @@ package info.nightscout.androidaps.plugins.OpenAPSAMA; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; @@ -20,70 +20,18 @@ import info.nightscout.utils.SafeParse; public class Autosens { private static Logger log = LoggerFactory.getLogger(Autosens.class); - public static AutosensResult detectSensitivityandCarbAbsorption(List glucose_data, Long mealTime) { + public static AutosensResult detectSensitivityandCarbAbsorption(long dataFromTime, Long mealTime) { NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); //console.error(mealTime); - if (glucose_data.size() < 3) - return new AutosensResult(); - double deviationSum = 0; double carbsAbsorbed = 0; - List bucketed_data = new ArrayList<>(); - bucketed_data.add(glucose_data.get(0)); - int j = 0; - for (int i = 1; i < glucose_data.size(); ++i) { - long bgTime = glucose_data.get(i).getTimeIndex(); - long lastbgTime = glucose_data.get(i - 1).getTimeIndex(); - //log.error("Processing " + i + ": " + new Date(bgTime).toString() + " " + glucose_data.get(i).value + " Previous: " + new Date(lastbgTime).toString() + " " + glucose_data.get(i - 1).value); - if (glucose_data.get(i).value < 39 || glucose_data.get(i - 1).value < 39) { - continue; - } + List bucketed_data = IobCobCalculatorPlugin.getBucketedData(dataFromTime); + if (bucketed_data == null) + return new AutosensResult(); - long elapsed_minutes = (bgTime - lastbgTime) / (60 * 1000); - if (Math.abs(elapsed_minutes) > 8) { - // interpolate missing data points - double lastbg = glucose_data.get(i - 1).value; - elapsed_minutes = Math.abs(elapsed_minutes); - //console.error(elapsed_minutes); - long nextbgTime; - while (elapsed_minutes > 5) { - nextbgTime = lastbgTime - 5 * 60 * 1000; - j++; - BgReading newBgreading = new BgReading(); - newBgreading.timeIndex = nextbgTime; - double gapDelta = glucose_data.get(i).value - lastbg; - //console.error(gapDelta, lastbg, elapsed_minutes); - double nextbg = lastbg + (5d / elapsed_minutes * gapDelta); - newBgreading.value = Math.round(nextbg); - //console.error("Interpolated", bucketed_data[j]); - bucketed_data.add(newBgreading); - //log.error("******************************************************************************************************* Adding:" + new Date(newBgreading.timeIndex).toString() + " " + newBgreading.value); - - elapsed_minutes = elapsed_minutes - 5; - lastbg = nextbg; - lastbgTime = nextbgTime; - } - j++; - BgReading newBgreading = new BgReading(); - newBgreading.value = glucose_data.get(i).value; - newBgreading.timeIndex = bgTime; - bucketed_data.add(newBgreading); - //log.error("******************************************************************************************************* Copying:" + new Date(newBgreading.timeIndex).toString() + " " + newBgreading.value); - } else if (Math.abs(elapsed_minutes) > 2) { - j++; - BgReading newBgreading = new BgReading(); - newBgreading.value = glucose_data.get(i).value; - newBgreading.timeIndex = bgTime; - bucketed_data.add(newBgreading); - //log.error("******************************************************************************************************* Copying:" + new Date(newBgreading.timeIndex).toString() + " " + newBgreading.value); - } else { - bucketed_data.get(j).value = (bucketed_data.get(j).value + glucose_data.get(i).value) / 2; - //log.error("***** Average"); - } - } //console.error(bucketed_data); double[] avgDeltas = new double[bucketed_data.size() - 2]; double[] bgis = new double[bucketed_data.size() - 2]; @@ -107,15 +55,15 @@ public class Autosens { double avgDelta; double delta; bg = bucketed_data.get(i).value; - if (bg < 40 || bucketed_data.get(i + 3).value < 40) { - log.error("! value < 40"); + if (bg < 39 || bucketed_data.get(i + 3).value < 39) { + log.error("! value < 39"); continue; } avgDelta = (bg - bucketed_data.get(i + 3).value) / 3; delta = (bg - bucketed_data.get(i + 1).value); // avgDelta = avgDelta.toFixed(2); - IobTotal iob = IobTotal.calulateFromTreatmentsAndTemps(bgTime); + IobTotal iob = IobCobCalculatorPlugin.calulateFromTreatmentsAndTemps(bgTime); double bgi = Math.round((-iob.activity * sens * 5) * 100) / 100d; // bgi = bgi.toFixed(2); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java index f03a892cae..e2c7f20284 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java @@ -1,8 +1,5 @@ package info.nightscout.androidaps.plugins.OpenAPSAMA; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; - import com.eclipsesource.v8.JavaVoidCallback; import com.eclipsesource.v8.V8; import com.eclipsesource.v8.V8Array; @@ -21,11 +18,11 @@ import info.nightscout.androidaps.data.GlucoseStatus; import info.nightscout.androidaps.data.MealData; import info.nightscout.androidaps.db.TempBasal; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.Loop.ScriptReader; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.SP; -import info.nightscout.utils.SafeParse; public class DetermineBasalAdapterAMAJS { private static Logger log = LoggerFactory.getLogger(DetermineBasalAdapterAMAJS.class); @@ -241,7 +238,7 @@ public class DetermineBasalAdapterAMAJS { mV8rt.add(PARAM_currentTemp, mCurrentTemp); - mIobData = mV8rt.executeArrayScript(IobTotal.convertToJSONArray(iobArray).toString()); + mIobData = mV8rt.executeArrayScript(IobCobCalculatorPlugin.convertToJSONArray(iobArray).toString()); mV8rt.add(PARAM_iobData, mIobData); mGlucoseStatus = new V8Object(mV8rt); 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 668c205287..2d530f7b57 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 @@ -1,15 +1,11 @@ package info.nightscout.androidaps.plugins.OpenAPSAMA; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; - import org.json.JSONException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.Date; -import java.util.List; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; @@ -17,11 +13,11 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.GlucoseStatus; import info.nightscout.androidaps.data.MealData; -import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.ScriptReader; import info.nightscout.androidaps.data.IobTotal; @@ -180,7 +176,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { Date start = new Date(); Date startPart = new Date(); - IobTotal[] iobArray = IobTotal.calculateIobArrayInDia(); + IobTotal[] iobArray = IobCobCalculatorPlugin.calculateIobArrayInDia(); Profiler.log(log, "calculateIobArrayInDia()", startPart); startPart = new Date(); @@ -215,15 +211,13 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { if (!checkOnlyHardLimits(profile.getMaxDailyBasal(), "max_daily_basal", 0.1, 10)) return; if (!checkOnlyHardLimits(pump.getBaseBasalRate(), "current_basal", 0.01, 5)) return; - startPart = new Date(); long oldestDataAvailable = MainApp.getConfigBuilder().getActiveTempBasals().oldestDataAvaialable(); - List bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime(Math.max(oldestDataAvailable, (long) (new Date().getTime() - 60 * 60 * 1000L * (24 + profile.getDia()))), false); - log.debug("Limiting data to oldest available temps: " + new Date(oldestDataAvailable).toString() + " (" + bgReadings.size() + " records)"); - Profiler.log(log, "getBgreadingsDataFromTime()", startPart); + long getBGDataFrom = Math.max(oldestDataAvailable, (long) (new Date().getTime() - 60 * 60 * 1000L * (24 + profile.getDia()))); + log.debug("Limiting data to oldest available temps: " + new Date(oldestDataAvailable).toString()); startPart = new Date(); if(MainApp.getConfigBuilder().isAMAModeEnabled()){ - lastAutosensResult = Autosens.detectSensitivityandCarbAbsorption(bgReadings, null); + lastAutosensResult = Autosens.detectSensitivityandCarbAbsorption(getBGDataFrom, null); } else { lastAutosensResult = new AutosensResult(); } 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 e4984097cb..596bd2a3de 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 @@ -76,6 +76,7 @@ import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialo import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin; +import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.Loop.events.EventNewOpenLoopNotification; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; @@ -97,6 +98,7 @@ import info.nightscout.androidaps.plugins.TempTargetRange.events.EventTempTarget import info.nightscout.utils.BolusWizard; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.Profiler; import info.nightscout.utils.Round; import info.nightscout.utils.SP; import info.nightscout.utils.ToastUtils; @@ -1053,13 +1055,18 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, Double maxCobValueFound = 0d; if (showIobView.isChecked() || showCobView.isChecked()) { + Date start = new Date(); List iobArray = new ArrayList<>(); List cobArray = new ArrayList<>(); + double lastIob = -1000; for (long time = fromTime; time <= endTime; time += 5 * 60 * 1000L) { if (showIobView.isChecked()) { - IobTotal iob = IobTotal.calulateFromTreatmentsAndTemps(time); - iobArray.add(new DataPoint(time, iob.iob)); - maxIobValueFound = Math.max(maxIobValueFound, Math.abs(iob.iob)); + IobTotal iob = IobCobCalculatorPlugin.calulateFromTreatmentsAndTemps(time); + if (lastIob != iob.iob) { + iobArray.add(new DataPoint(time, iob.iob)); + maxIobValueFound = Math.max(maxIobValueFound, Math.abs(iob.iob)); + lastIob = iob.iob; + } } if (showCobView.isChecked()) { //MealData mealData = MainApp.getConfigBuilder().getActiveTreatments().getMealData(); @@ -1067,6 +1074,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, //maxCobValueFound = Math.max(maxCobValueFound, mealData.mealCOB); } } + Profiler.log(log,"IOB precessed", start); DataPoint[] iobData = new DataPoint[iobArray.size()]; iobData = iobArray.toArray(iobData); iobSeries = new FixedLineGraphSeries<>(iobData); 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 fff563fd7d..98ca25513b 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 @@ -185,7 +185,7 @@ public class TempBasalsPlugin implements PluginBase, TempBasalsInterface { TempBasal t = tempBasals.get(pos); if (t.timeStart.getTime() > time) continue; IobTotal calc = t.iobCalc(time); - log.debug("BasalIOB " + new Date(time) + " >>> " + calc.basaliob); + //log.debug("BasalIOB " + new Date(time) + " >>> " + calc.basaliob); total.plus(calc); } if (useExtendedBoluses) {