allow to enforce Treatment plugin

This commit is contained in:
Milos Kozak 2017-05-12 17:30:52 +02:00
parent c6830baf8f
commit 9fc1d59bd0
12 changed files with 499 additions and 3 deletions

View file

@ -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;

View file

@ -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());

View file

@ -56,4 +56,7 @@ public interface PumpInterface {
// Short info for SMS, Wear etc
String shortStatus(boolean veryShort);
// Enforced Treatment plugin
String treatmentPlugin();
}

View file

@ -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
**/

View file

@ -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
}

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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

View file

@ -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;
}
}

View file

@ -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<Treatment> treatments;
private static List<TempBasal> tempBasals;
private static List<TempBasal> 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<TempBasal> 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<TempBasal, Long> 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<Treatment> getTreatments() {
return treatments;
}
@Override
public List<Treatment> getTreatments5MinBack(long time) {
List<Treatment> 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();
}
}