From 9fc1d59bd0862264e4f5a64fa8878195abb20b0f Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 12 May 2017 17:30:52 +0200 Subject: [PATCH] allow to enforce Treatment plugin --- .../info/nightscout/androidaps/Config.java | 2 +- .../info/nightscout/androidaps/MainApp.java | 2 + .../androidaps/interfaces/PumpInterface.java | 3 + .../ConfigBuilder/ConfigBuilderPlugin.java | 10 + .../plugins/PumpDanaR/DanaRPlugin.java | 7 + .../PumpDanaRKorean/DanaRKoreanPlugin.java | 6 + .../plugins/PumpDanaRv2/DanaRv2Plugin.java | 6 + .../androidaps/plugins/PumpMDI/MDIPlugin.java | 6 + .../PumpVirtual/VirtualPumpPlugin.java | 6 + .../plugins/Treatments/TreatmentsPlugin.java | 6 +- .../TreatmentsFromHistoryFragment.java | 96 +++++ .../TreatmentsFromHistoryPlugin.java | 352 ++++++++++++++++++ 12 files changed, 499 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/TreatmentsFromHistory/TreatmentsFromHistoryFragment.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/TreatmentsFromHistory/TreatmentsFromHistoryPlugin.java diff --git a/app/src/main/java/info/nightscout/androidaps/Config.java b/app/src/main/java/info/nightscout/androidaps/Config.java index c68e3969ba..d478abae0b 100644 --- a/app/src/main/java/info/nightscout/androidaps/Config.java +++ b/app/src/main/java/info/nightscout/androidaps/Config.java @@ -13,7 +13,7 @@ public class Config { public static final boolean DANAR = true && BuildConfig.PUMPDRIVERS; - public static final boolean DANARv2 = false; + public static final boolean DANARv2 = true; public static final boolean ACTION = !BuildConfig.NSCLIENTOLNY; public static final boolean VIRTUALPUMP = !BuildConfig.NSCLIENTOLNY; diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 5553a92ec1..39b197a3a6 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -53,6 +53,7 @@ import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripFragment; import info.nightscout.androidaps.plugins.TempTargetRange.TempTargetRangeFragment; import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment; import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpFragment; +import info.nightscout.androidaps.plugins.TreatmentsFromHistory.TreatmentsFromHistoryFragment; import info.nightscout.androidaps.plugins.Wear.WearFragment; import info.nightscout.androidaps.plugins.Persistentnotification.PersistentNotificationPlugin; import info.nightscout.androidaps.plugins.XDripStatusline.StatuslineFragment; @@ -112,6 +113,7 @@ public class MainApp extends Application { pluginsList.add(CircadianPercentageProfileFragment.getPlugin()); if (Config.APS) pluginsList.add(TempTargetRangeFragment.getPlugin()); pluginsList.add(TreatmentsFragment.getPlugin()); + pluginsList.add(TreatmentsFromHistoryFragment.getPlugin()); if (Config.SAFETY) pluginsList.add(SafetyFragment.getPlugin()); if (Config.APS) pluginsList.add(ObjectivesFragment.getPlugin()); pluginsList.add(SourceXdripFragment.getPlugin()); diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java index b9c7e18650..a6134907a0 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java @@ -56,4 +56,7 @@ public interface PumpInterface { // Short info for SMS, Wear etc String shortStatus(boolean veryShort); + + // Enforced Treatment plugin + String treatmentPlugin(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 15d0cd815b..a4043acba2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -50,6 +50,7 @@ import info.nightscout.androidaps.plugins.Overview.events.EventDismissBolusprogr import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.PumpDanaR.comm.MsgError; +import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.utils.BatteryLevel; import info.nightscout.utils.DateUtil; import info.nightscout.utils.SP; @@ -771,6 +772,15 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain } } + @Override + public String treatmentPlugin() { + if (activePump != null) { + return activePump.treatmentPlugin(); + } else { + return null; + } + } + /** * Constraints interface **/ 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 b36b0398bf..56d24c756c 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 @@ -42,6 +42,7 @@ import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotificati import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.ProfileNS.NSProfilePlugin; import info.nightscout.androidaps.plugins.PumpDanaR.services.DanaRExecutionService; +import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.Round; @@ -217,6 +218,11 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf this.fragmentPumpVisible = fragmentVisible; } + @Override + public String treatmentPlugin() { + return TreatmentsPlugin.class.getName(); + } + @Override public boolean isInitialized() { return getDanaRPump().lastConnection.getTime() > 0 && getDanaRPump().isExtendedBolusEnabled; @@ -875,6 +881,7 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf ret += "Batt: " + getDanaRPump().batteryRemaining + "\n"; return ret; } + // TODO: daily total constraint } 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 9ea8cb2258..795e0fbaaf 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 @@ -42,6 +42,7 @@ import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; +import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.Round; @@ -219,6 +220,11 @@ public class DanaRKoreanPlugin implements PluginBase, PumpInterface, Constraints this.fragmentPumpVisible = fragmentVisible; } + @Override + public String treatmentPlugin() { + return TreatmentsPlugin.class.getName(); + } + @Override public boolean isInitialized() { return getDanaRPump().lastConnection.getTime() > 0 && !getDanaRPump().isConfigUD && !getDanaRPump().isEasyModeEnabled && getDanaRPump().isExtendedBolusEnabled; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java index 57a8f8b288..9157a7a173 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java @@ -42,6 +42,7 @@ import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.ProfileNS.NSProfilePlugin; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; import info.nightscout.androidaps.plugins.PumpDanaRv2.services.DanaRv2ExecutionService; +import info.nightscout.androidaps.plugins.TreatmentsFromHistory.TreatmentsFromHistoryPlugin; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.Round; @@ -200,6 +201,11 @@ public class DanaRv2Plugin implements PluginBase, PumpInterface, ConstraintsInte this.fragmentPumpVisible = fragmentVisible; } + @Override + public String treatmentPlugin() { + return TreatmentsFromHistoryPlugin.class.getName(); + } + @Override public boolean isInitialized() { return getDanaRPump().lastConnection.getTime() > 0 && getDanaRPump().isExtendedBolusEnabled; 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 e9010438c6..4d55544129 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 @@ -20,6 +20,7 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; +import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.utils.DateUtil; /** @@ -117,6 +118,11 @@ public class MDIPlugin implements PluginBase, PumpInterface { return PluginBase.PUMP; } + @Override + public String treatmentPlugin() { + return TreatmentsPlugin.class.getName(); + } + @Override public boolean isInitialized() { return true; 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 ce91fd4156..d3b7cf5736 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 @@ -26,6 +26,7 @@ import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.PumpVirtual.events.EventVirtualPumpUpdateGui; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; +import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.utils.DateUtil; /** @@ -135,6 +136,11 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface { return PluginBase.PUMP; } + @Override + public String treatmentPlugin() { + return TreatmentsPlugin.class.getName(); + } + @Override public boolean isInitialized() { return 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 324708ce90..bee7d8c4d0 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 @@ -83,7 +83,8 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { @Override public boolean isEnabled(int type) { - return type == TREATMENT && fragmentEnabled; + boolean canBeEnabled = MainApp.getConfigBuilder().treatmentPlugin() == null ? true : MainApp.getConfigBuilder().treatmentPlugin().equals(getClass().getName()); + return type == TREATMENT && fragmentEnabled && canBeEnabled; } @Override @@ -103,7 +104,8 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { @Override public boolean showInList(int type) { - return true; + boolean canBeEnabled = MainApp.getConfigBuilder().treatmentPlugin() == null ? true : MainApp.getConfigBuilder().treatmentPlugin().equals(getClass().getName()); + return canBeEnabled; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/TreatmentsFromHistory/TreatmentsFromHistoryFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/TreatmentsFromHistory/TreatmentsFromHistoryFragment.java new file mode 100644 index 0000000000..5a942d8c9d --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/TreatmentsFromHistory/TreatmentsFromHistoryFragment.java @@ -0,0 +1,96 @@ +package info.nightscout.androidaps.plugins.TreatmentsFromHistory; + +import android.content.Context; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.view.ViewPager; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; +import info.nightscout.androidaps.plugins.Treatments.fragments.TreatmentsBolusFragment; +import info.nightscout.androidaps.plugins.Treatments.fragments.TreatmentsTempBasalsFragment; + +public class TreatmentsFromHistoryFragment extends Fragment { + private static Logger log = LoggerFactory.getLogger(TreatmentsFromHistoryFragment.class); + + private static TreatmentsFromHistoryPlugin treatmentsPlugin = new TreatmentsFromHistoryPlugin(); + + public static TreatmentsFromHistoryPlugin getPlugin() { + return treatmentsPlugin; + } + + SectionsPagerAdapter sectionsPagerAdapter; + ViewPager viewPager; + + Context context; + + Fragment bolusFragment; + Fragment tempBasalsFragment; + + public class SectionsPagerAdapter extends FragmentPagerAdapter { + + public SectionsPagerAdapter(FragmentManager fm) { + super(fm); + } + + @Override + public Fragment getItem(int position) { + // getItem is called to instantiate the fragment for the given page. + switch (position){ + case 0: + return bolusFragment; + case 1: + return tempBasalsFragment; +/* + case 2: + return iobcobActiveFragmentObject; + case 3: + return basalvsTempBasalObject; +*/ + default: + return null; + } + } + + @Override + public int getCount() { + // Show 1 total pages. + return 2; + } + + @Override + public CharSequence getPageTitle(int position) { + switch (position) { + case 0: + return getString(R.string.bolus); + case 1: + return getString(R.string.tempbasals); + } + return null; + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.treatments_fragment, container, false); + + bolusFragment = new TreatmentsBolusFragment(); + tempBasalsFragment = new TreatmentsTempBasalsFragment(); + + sectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager()); + viewPager = (ViewPager) view.findViewById(R.id.treatments_pager); + viewPager.setAdapter(sectionsPagerAdapter); + context = getContext(); + + return view; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/TreatmentsFromHistory/TreatmentsFromHistoryPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/TreatmentsFromHistory/TreatmentsFromHistoryPlugin.java new file mode 100644 index 0000000000..c77c1825a5 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/TreatmentsFromHistory/TreatmentsFromHistoryPlugin.java @@ -0,0 +1,352 @@ +package info.nightscout.androidaps.plugins.TreatmentsFromHistory; + +import android.support.annotation.Nullable; + +import com.j256.ormlite.dao.Dao; +import com.squareup.otto.Subscribe; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Date; +import java.util.List; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.Iob; +import info.nightscout.androidaps.data.IobTotal; +import info.nightscout.androidaps.data.MealData; +import info.nightscout.androidaps.db.TempBasal; +import info.nightscout.androidaps.db.Treatment; +import info.nightscout.androidaps.events.EventPreferenceChange; +import info.nightscout.androidaps.events.EventTempBasalChange; +import info.nightscout.androidaps.events.EventTreatmentChange; +import info.nightscout.androidaps.interfaces.InsulinInterface; +import info.nightscout.androidaps.interfaces.PluginBase; +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.NSClientInternal.data.NSProfile; +import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment; +import info.nightscout.utils.SP; + +/** + * Created by mike on 05.08.2016. + */ +public class TreatmentsFromHistoryPlugin implements PluginBase, TreatmentsInterface { + private static Logger log = LoggerFactory.getLogger(TreatmentsFromHistoryPlugin.class); + + public static IobTotal lastTreatmentCalculation; + public static IobTotal lastTempBasalsCalculation; + + public static List treatments; + private static List tempBasals; + private static List extendedBoluses; + + private static boolean fragmentEnabled = true; + private static boolean fragmentVisible = true; + + private static TreatmentsFromHistoryPlugin treatmentsPlugin = new TreatmentsFromHistoryPlugin(); + + public static TreatmentsFromHistoryPlugin getPlugin() { + return treatmentsPlugin; + } + + @Override + public String getFragmentClass() { + return TreatmentsFromHistoryFragment.class.getName(); + } + + @Override + public String getName() { + return MainApp.instance().getString(R.string.treatments); + } + + @Override + public String getNameShort() { + String name = MainApp.sResources.getString(R.string.treatments_shortname); + if (!name.trim().isEmpty()) { + //only if translation exists + return name; + } + // use long name as fallback + return getName(); + } + + @Override + public boolean isEnabled(int type) { + boolean canBeEnabled = MainApp.getConfigBuilder().treatmentPlugin() == null ? true : MainApp.getConfigBuilder().treatmentPlugin().equals(getClass().getName()); + return type == TREATMENT && fragmentEnabled && canBeEnabled; + } + + @Override + public boolean isVisibleInTabs(int type) { + return type == TREATMENT && fragmentVisible; + } + + @Override + public boolean canBeHidden(int type) { + return true; + } + + @Override + public boolean hasFragment() { + return true; + } + + @Override + public boolean showInList(int type) { + boolean canBeEnabled = MainApp.getConfigBuilder().treatmentPlugin() == null ? true : MainApp.getConfigBuilder().treatmentPlugin().equals(getClass().getName()); + return canBeEnabled; + } + + @Override + public void setFragmentEnabled(int type, boolean fragmentEnabled) { + if (type == TREATMENT) this.fragmentEnabled = fragmentEnabled; + } + + @Override + public void setFragmentVisible(int type, boolean fragmentVisible) { + if (type == TREATMENT) this.fragmentVisible = fragmentVisible; + } + + @Override + public int getType() { + return PluginBase.TREATMENT; + } + + public TreatmentsFromHistoryPlugin() { + MainApp.bus().register(this); + initializeData(); + } + + public static void initializeData() { + // Treatments + double dia = Constants.defaultDIA; + if (MainApp.getConfigBuilder().getActiveProfile() != null && MainApp.getConfigBuilder().getActiveProfile().getProfile() != null) + dia = MainApp.getConfigBuilder().getActiveProfile().getProfile().getDia(); + long fromMills = (long) (new Date().getTime() - 60 * 60 * 1000L * (24 + dia)); + treatments = MainApp.getDbHelper().getTreatmentDataFromTime(fromMills, false); + + // Temp basals + tempBasals = MainApp.getDbHelper().getTempbasalsDataFromTime(fromMills, false, false); + extendedBoluses = MainApp.getDbHelper().getTempbasalsDataFromTime(fromMills, false, true); + + // Update ended + checkForExpiredExtended(); + checkForExpiredTemps(); + } + + public static void checkForExpiredTemps() { + checkForExpired(tempBasals); + } + + public static void checkForExpiredExtended() { + checkForExpired(extendedBoluses); + } + + private static void checkForExpired(List list) { + long now = new Date().getTime(); + for (int position = list.size() - 1; position >= 0; position--) { + TempBasal t = list.get(position); + boolean update = false; + if (t.timeEnd == null && t.getPlannedTimeEnd() < now) { + t.timeEnd = new Date(t.getPlannedTimeEnd()); + if (Config.logTempBasalsCut) + log.debug("Add timeEnd to old record"); + update = true; + } + if (position > 0) { + Date startofnewer = list.get(position - 1).timeStart; + if (t.timeEnd == null) { + t.timeEnd = new Date(Math.min(startofnewer.getTime(), t.getPlannedTimeEnd())); + if (Config.logTempBasalsCut) + log.debug("Add timeEnd to old record"); + update = true; + } else if (t.timeEnd.getTime() > startofnewer.getTime()) { + t.timeEnd = startofnewer; + update = true; + } + } + if (update) { + try { + Dao dao = MainApp.getDbHelper().getDaoTempBasals(); + dao.update(t); + } catch (SQLException e) { + e.printStackTrace(); + } + if (Config.logTempBasalsCut) { + log.debug("Fixing unfinished temp end: " + t.log()); + if (position > 0) + log.debug("Previous: " + list.get(position - 1).log()); + } + } + } + } + + @Override + public IobTotal getLastCalculationTreatments() { + return lastTreatmentCalculation; + } + + @Override + public IobTotal getCalculationToTimeTreatments(long time) { + IobTotal total = new IobTotal(time); + + if (MainApp.getConfigBuilder() == null || ConfigBuilderPlugin.getActiveProfile() == null) // app not initialized yet + return total; + NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile(); + InsulinInterface insulinInterface = MainApp.getConfigBuilder().getActiveInsulin(); + if (profile == null) + return total; + + Double dia = profile.getDia(); + + for (Integer pos = 0; pos < treatments.size(); pos++) { + Treatment t = treatments.get(pos); + if (t.created_at.getTime() > time) continue; + Iob tIOB = insulinInterface.iobCalc(t, time, dia); + total.iob += tIOB.iobContrib; + total.activity += tIOB.activityContrib; + Iob bIOB = insulinInterface.iobCalc(t, time, dia / SP.getInt("openapsama_bolussnooze_dia_divisor", 2)); + total.bolussnooze += bIOB.iobContrib; + } + + checkForExpired(extendedBoluses); + for (Integer pos = 0; pos < extendedBoluses.size(); pos++) { + TempBasal t = extendedBoluses.get(pos); + if (t.timeStart.getTime() > time) continue; + IobTotal calc = t.iobCalc(time); + total.plus(calc); + } + return total; + } + + @Override + public void updateTotalIOBTreatments() { + IobTotal total = getCalculationToTimeTreatments(new Date().getTime()); + + lastTreatmentCalculation = total; + } + + @Override + public MealData getMealData() { + MealData result = new MealData(); + + NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); + if (profile == null) return result; + + long now = new Date().getTime(); + long dia_ago = now - (new Double(1.5d * profile.getDia() * 60 * 60 * 1000l)).longValue(); + + for (Treatment treatment : treatments) { + long t = treatment.created_at.getTime(); + if (t > dia_ago && t <= now) { + if (treatment.carbs >= 1) { + result.carbs += treatment.carbs; + } + if (treatment.insulin > 0 && treatment.mealBolus) { + result.boluses += treatment.insulin; + } + } + } + + AutosensData autosensData = IobCobCalculatorPlugin.getLastAutosensData(); + if (autosensData != null) { + result.mealCOB = autosensData.cob; + } + return result; + } + + @Override + public List getTreatments() { + return treatments; + } + + @Override + public List getTreatments5MinBack(long time) { + List in5minback = new ArrayList<>(); + for (Integer pos = 0; pos < treatments.size(); pos++) { + Treatment t = treatments.get(pos); + if (t.created_at.getTime() <= time && t.created_at.getTime() > time - 5 * 60 * 1000) + in5minback.add(t); + } + return in5minback; + } + + @Subscribe + public void onStatusEvent(final EventTreatmentChange ev) { + initializeData(); + updateTotalIOBTreatments(); + } + + @Override + public IobTotal getLastCalculationTempBasals() { + return lastTempBasalsCalculation; + } + + @Override + public IobTotal getCalculationToTimeTempBasals(long time) { + checkForExpired(tempBasals); + IobTotal total = new IobTotal(time); + for (Integer pos = 0; pos < tempBasals.size(); pos++) { + TempBasal t = tempBasals.get(pos); + if (t.timeStart.getTime() > time) continue; + IobTotal calc = t.iobCalc(time); + //log.debug("BasalIOB " + new Date(time) + " >>> " + calc.basaliob); + total.plus(calc); + } + return total; + } + + @Override + public void updateTotalIOBTempBasals() { + IobTotal total = getCalculationToTimeTempBasals(new Date().getTime()); + + lastTempBasalsCalculation = total; + } + + @Nullable + @Override + public TempBasal getTempBasal(Date time) { + checkForExpired(tempBasals); + for (TempBasal t : tempBasals) { + if (t.isInProgress(time)) return t; + } + return null; + } + + @Override + public TempBasal getExtendedBolus(Date time) { + checkForExpired(extendedBoluses); + for (TempBasal t : extendedBoluses) { + if (t.isInProgress(time)) return t; + } + return null; + } + + @Override + public long oldestDataAvaialable() { + long oldestTemp = new Date().getTime(); + if (tempBasals.size() > 0) + oldestTemp = Math.min(oldestTemp, tempBasals.get(tempBasals.size() - 1).timeStart.getTime()); + if (extendedBoluses.size() > 0) + oldestTemp = Math.min(oldestTemp, extendedBoluses.get(extendedBoluses.size() - 1).timeStart.getTime()); + oldestTemp -= 15 * 60 * 1000L; // allow 15 min before + return oldestTemp; + } + + @Subscribe + public void onStatusEvent(final EventTempBasalChange ev) { + initializeData(); + updateTotalIOBTempBasals(); + } + +}