calculate iob based on insulin stored with treatment

This commit is contained in:
Milos Kozak 2017-05-12 17:57:57 +02:00
parent 9fc1d59bd0
commit 863e6454eb
10 changed files with 35 additions and 14 deletions

View file

@ -18,6 +18,7 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.interfaces.InsulinInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.Actions.ActionsFragment;
@ -212,6 +213,21 @@ public class MainApp extends Application {
return newList;
}
@Nullable
public static InsulinInterface getInsulinIterfaceById(int id) {
ArrayList<PluginBase> newList = new ArrayList<>();
if (pluginsList != null) {
for (PluginBase p : pluginsList) {
if (p.getType() == PluginBase.INSULIN && ((InsulinInterface)p).getId() == id)
return (InsulinInterface) p;
}
} else {
log.error("InsulinInterface not found");
}
return null;
}
public static ArrayList<PluginBase> getSpecificPluginsVisibleInList(int type) {
ArrayList<PluginBase> newList = new ArrayList<>();

View file

@ -98,7 +98,7 @@ public class TempBasal {
tempBolusPart.insulin = tempBolusSize;
tempBolusPart.created_at = new Date(date);
Iob aIOB = insulinInterface.iobCalc(tempBolusPart, time, profile.getDia());
Iob aIOB = insulinInterface.iobCalcForTreatment(tempBolusPart, time, profile.getDia());
result.basaliob += aIOB.iobContrib;
result.activity += aIOB.activityContrib;
Double dia_ago = time - profile.getDia() * 60 * 60 * 1000;

View file

@ -14,6 +14,7 @@ import java.util.List;
import info.nightscout.androidaps.Constants;
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.Overview.graphExtensions.DataPointWithLabelInterface;
@ -147,4 +148,11 @@ public class Treatment implements DataPointWithLabelInterface {
ConfigBuilderPlugin.uploadCareportalEntryToNS(data);
}
public Iob iobCalc(long time, double dia) {
InsulinInterface insulinInterface = MainApp.getInsulinIterfaceById(insulinType);
if (insulinInterface == null)
insulinInterface = ConfigBuilderPlugin.getActiveInsulin();
return insulinInterface.iobCalcForTreatment(this, time, dia);
}
}

View file

@ -17,5 +17,5 @@ public interface InsulinInterface {
String getFriendlyName();
String getComment();
double getDia();
public Iob iobCalc(Treatment treatment, long time, Double dia);
public Iob iobCalcForTreatment(Treatment treatment, long time, Double dia);
}

View file

@ -50,7 +50,7 @@ public class ActivityGraph extends GraphView {
List<DataPoint> iobArray = new ArrayList<DataPoint>();
for (long time = 0; time <= hours * 60 * 60 * 1000; time += 5 * 60 * 1000L) {
Iob iob = insulin.iobCalc(t, time, dia);
Iob iob = t.iobCalc(time, dia);
activityArray.add(new DataPoint(time / 60 / 1000, iob.activityContrib));
iobArray.add(new DataPoint(time / 60 / 1000, iob.iobContrib));
}

View file

@ -100,7 +100,7 @@ public class InsulinFastactingPlugin implements PluginBase, InsulinInterface {
}
@Override
public Iob iobCalc(Treatment treatment, long time, Double dia) {
public Iob iobCalcForTreatment(Treatment treatment, long time, Double dia) {
Iob result = new Iob();
Double scaleFactor = 3.0 / dia;

View file

@ -100,7 +100,7 @@ public class InsulinFastactingProlongedPlugin implements PluginBase, InsulinInte
}
@Override
public Iob iobCalc(Treatment treatment, long time, Double dia) {
public Iob iobCalcForTreatment(Treatment treatment, long time, Double dia) {
Iob result = new Iob();
//Double scaleFactor = 3.0 / dia;

View file

@ -205,7 +205,6 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface {
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;
@ -214,10 +213,10 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface {
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);
Iob tIOB = t.iobCalc(time, dia);
total.iob += tIOB.iobContrib;
total.activity += tIOB.activityContrib;
Iob bIOB = insulinInterface.iobCalc(t, time, dia / SP.getInt("openapsama_bolussnooze_dia_divisor", 2));
Iob bIOB = t.iobCalc(time, dia / SP.getInt("openapsama_bolussnooze_dia_divisor", 2));
total.bolussnooze += bIOB.iobContrib;
}
return total;

View file

@ -75,13 +75,12 @@ public class TreatmentsBolusFragment extends Fragment implements View.OnClickLis
if (MainApp.getConfigBuilder() == null || ConfigBuilderPlugin.getActiveProfile() == null) // app not initialized yet
return;
NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile();
InsulinInterface insulinInterface = ConfigBuilderPlugin.getActiveInsulin();
if (profile == null || insulinInterface == null)
if (profile == null)
return;
holder.date.setText(DateUtil.dateAndTimeString(treatments.get(position).created_at));
holder.insulin.setText(DecimalFormatter.to2Decimal(treatments.get(position).insulin) + " U");
holder.carbs.setText(DecimalFormatter.to0Decimal(treatments.get(position).carbs) + " g");
Iob iob = insulinInterface.iobCalc(treatments.get(position), new Date().getTime(), profile.getDia());
Iob iob = treatments.get(position).iobCalc(new Date().getTime(), profile.getDia());
holder.iob.setText(DecimalFormatter.to2Decimal(iob.iobContrib) + " U");
holder.activity.setText(DecimalFormatter.to3Decimal(iob.activityContrib) + " U");
holder.mealOrCorrection.setText(treatments.get(position).mealBolus ? MainApp.sResources.getString(R.string.mealbolus) : MainApp.sResources.getString(R.string.correctionbous));

View file

@ -203,7 +203,6 @@ public class TreatmentsFromHistoryPlugin implements PluginBase, TreatmentsInterf
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;
@ -212,10 +211,10 @@ public class TreatmentsFromHistoryPlugin implements PluginBase, TreatmentsInterf
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);
Iob tIOB = t.iobCalc(time, dia);
total.iob += tIOB.iobContrib;
total.activity += tIOB.activityContrib;
Iob bIOB = insulinInterface.iobCalc(t, time, dia / SP.getInt("openapsama_bolussnooze_dia_divisor", 2));
Iob bIOB = t.iobCalc(time, dia / SP.getInt("openapsama_bolussnooze_dia_divisor", 2));
total.bolussnooze += bIOB.iobContrib;
}