fix history iob calculation

This commit is contained in:
Milos Kozak 2017-04-25 10:48:30 +02:00
parent 5a3eb0cc25
commit 0232229020
10 changed files with 50 additions and 34 deletions

View file

@ -53,8 +53,8 @@ public class TempBasal {
public boolean isAbsolute = false; // true if if set as absolute value in U
public IobTotal iobCalc(Date time) {
IobTotal result = new IobTotal(time.getTime());
public IobTotal iobCalc(long time) {
IobTotal result = new IobTotal(time);
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
InsulinInterface insulinInterface = MainApp.getConfigBuilder().getActiveInsulin();
@ -66,7 +66,7 @@ public class TempBasal {
if (basalRate == null)
return result;
int realDuration = getRealDuration();
int realDuration = getDurationToTime(time);
if (realDuration > 0) {
Double netBasalRate = 0d;
@ -103,8 +103,8 @@ public class TempBasal {
Iob aIOB = insulinInterface.iobCalc(tempBolusPart, time, profile.getDia());
result.basaliob += aIOB.iobContrib;
result.activity += aIOB.activityContrib;
Double dia_ago = time.getTime() - profile.getDia() * 60 * 60 * 1000;
if (date > dia_ago && date <= time.getTime()) {
Double dia_ago = time - profile.getDia() * 60 * 60 * 1000;
if (date > dia_ago && date <= time) {
result.netbasalinsulin += tempBolusPart.insulin;
if (tempBolusPart.insulin > 0) {
result.hightempinsulin += tempBolusPart.insulin;
@ -117,26 +117,32 @@ public class TempBasal {
}
// Determine end of basal
public Date getTimeEnd() {
Date tempBasalTimePlannedEnd = getPlannedTimeEnd();
Date now = new Date();
public long getTimeEnd() {
long tempBasalTimePlannedEnd = getPlannedTimeEnd();
long now = new Date().getTime();
if (timeEnd != null && timeEnd.getTime() < tempBasalTimePlannedEnd.getTime()) {
tempBasalTimePlannedEnd = timeEnd;
if (timeEnd != null && timeEnd.getTime() < tempBasalTimePlannedEnd) {
tempBasalTimePlannedEnd = timeEnd.getTime();
}
if (now.getTime() < tempBasalTimePlannedEnd.getTime())
if (now < tempBasalTimePlannedEnd)
tempBasalTimePlannedEnd = now;
return tempBasalTimePlannedEnd;
}
public Date getPlannedTimeEnd() {
return new Date(timeStart.getTime() + 60 * 1_000 * duration);
public long getPlannedTimeEnd() {
return timeStart.getTime() + 60 * 1_000 * duration;
}
public int getRealDuration() {
Long msecs = getTimeEnd().getTime() - timeStart.getTime();
long msecs = getTimeEnd() - timeStart.getTime();
return Math.round(msecs / 60f / 1000);
}
public int getDurationToTime(long time) {
long endTime = Math.min(time, getTimeEnd());
long msecs = endTime - timeStart.getTime();
return Math.round(msecs / 60f / 1000);
}
@ -146,7 +152,7 @@ public class TempBasal {
public int getPlannedRemainingMinutes() {
if (timeEnd != null) return 0;
float remainingMin = (getPlannedTimeEnd().getTime() - new Date().getTime()) / 1000f / 60;
float remainingMin = (getPlannedTimeEnd() - new Date().getTime()) / 1000f / 60;
return (remainingMin < 0) ? 0 : Math.round(remainingMin);
}
@ -172,7 +178,7 @@ public class TempBasal {
public boolean isInProgress(Date time) {
if (timeStart.getTime() > time.getTime()) return false; // in the future
if (timeEnd == null) { // open end
if (timeStart.getTime() < time.getTime() && getPlannedTimeEnd().getTime() > time.getTime())
if (timeStart.getTime() < time.getTime() && getPlannedTimeEnd() > time.getTime())
return true; // in interval
return false;
}

View file

@ -17,5 +17,5 @@ public interface InsulinInterface {
String getFriendlyName();
String getComment();
double getDia();
public Iob iobCalc(Treatment treatment, Date time, Double dia);
public Iob iobCalc(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, new Date(time), dia);
Iob iob = insulin.iobCalc(t, time, dia);
activityArray.add(new DataPoint(time / 60 / 1000, iob.activityContrib));
iobArray.add(new DataPoint(time / 60 / 1000, iob.iobContrib));
}

View file

@ -90,7 +90,7 @@ public class InsulinFastactingPlugin implements PluginBase, InsulinInterface {
}
@Override
public Iob iobCalc(Treatment treatment, Date time, Double dia) {
public Iob iobCalc(Treatment treatment, long time, Double dia) {
Iob result = new Iob();
Double scaleFactor = 3.0 / dia;
@ -99,7 +99,7 @@ public class InsulinFastactingPlugin implements PluginBase, InsulinInterface {
if (treatment.insulin != 0d) {
Long bolusTime = treatment.created_at.getTime();
Double minAgo = scaleFactor * (time.getTime() - bolusTime) / 1000d / 60d;
Double minAgo = scaleFactor * (time - bolusTime) / 1000d / 60d;
if (minAgo < peak) {
Double x1 = minAgo / 5d + 1;

View file

@ -90,7 +90,7 @@ public class InsulinFastactingProlongedPlugin implements PluginBase, InsulinInte
}
@Override
public Iob iobCalc(Treatment treatment, Date time, Double dia) {
public Iob iobCalc(Treatment treatment, long time, Double dia) {
Iob result = new Iob();
//Double scaleFactor = 3.0 / dia;
@ -101,7 +101,7 @@ public class InsulinFastactingProlongedPlugin implements PluginBase, InsulinInte
if (treatment.insulin != 0d) {
Long bolusTime = treatment.created_at.getTime();
Double minAgo = (time.getTime() - bolusTime) / 1000d / 60d;
Double minAgo = (time - bolusTime) / 1000d / 60d;
if (minAgo < peak) {
Double x1 = 6 / dia * minAgo / 5d + 1;

View file

@ -461,6 +461,17 @@ public class NSProfile {
return (int) (passed / 1000);
}
public static int secondsFromMidnight(long date) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(date);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
long passed = date - c.getTimeInMillis();
return (int) (passed / 1000);
}
public static Double toMgdl(Double value, String units) {
if (units.equals(Constants.MGDL)) return value;
else return value * Constants.MMOLL_TO_MGDL;

View file

@ -76,7 +76,7 @@ public class TempBasalsFragment extends Fragment {
holder.percent.setText(DecimalFormatter.to0Decimal(tempBasal.percent) + "%");
}
holder.realDuration.setText(DecimalFormatter.to0Decimal(tempBasal.getRealDuration()) + " min");
IobTotal iob = tempBasal.iobCalc(new Date());
IobTotal iob = tempBasal.iobCalc(new Date().getTime());
holder.iob.setText(DecimalFormatter.to2Decimal(iob.basaliob) + " U");
holder.netInsulin.setText(DecimalFormatter.to2Decimal(iob.netInsulin) + " U");
holder.netRatio.setText(DecimalFormatter.to2Decimal(iob.netRatio) + " U/h");
@ -85,7 +85,7 @@ public class TempBasalsFragment extends Fragment {
holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.colorInProgress));
else if (tempBasal.timeEnd == null)
holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.colorNotEnded));
else if (tempBasal.iobCalc(new Date()).basaliob != 0)
else if (tempBasal.iobCalc(new Date().getTime()).basaliob != 0)
holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.colorAffectingIOB));
else
holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.cardColorBackground));

View file

@ -128,8 +128,8 @@ public class TempBasalsPlugin implements PluginBase, TempBasalsInterface {
for (int position = list.size() - 1; position >= 0; position--) {
TempBasal t = list.get(position);
boolean update = false;
if (t.timeEnd == null && t.getPlannedTimeEnd().getTime() < now) {
t.timeEnd = new Date(t.getPlannedTimeEnd().getTime());
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;
@ -137,7 +137,7 @@ public class TempBasalsPlugin implements PluginBase, TempBasalsInterface {
if (position > 0) {
Date startofnewer = list.get(position - 1).timeStart;
if (t.timeEnd == null) {
t.timeEnd = new Date(Math.min(startofnewer.getTime(), t.getPlannedTimeEnd().getTime()));
t.timeEnd = new Date(Math.min(startofnewer.getTime(), t.getPlannedTimeEnd()));
if (Config.logTempBasalsCut)
log.debug("Add timeEnd to old record");
update = true;
@ -180,19 +180,19 @@ public class TempBasalsPlugin implements PluginBase, TempBasalsInterface {
public IobTotal getCalculationToTime(long time) {
checkForExpired(tempBasals);
checkForExpired(extendedBoluses);
Date now = new Date(time);
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(now);
IobTotal calc = t.iobCalc(time);
log.debug("BasalIOB " + new Date(time) + " >>> " + calc.basaliob);
total.plus(calc);
}
if (useExtendedBoluses) {
for (Integer pos = 0; pos < extendedBoluses.size(); pos++) {
TempBasal t = extendedBoluses.get(pos);
if (t.timeStart.getTime() > time) continue;
IobTotal calc = t.iobCalc(now);
IobTotal calc = t.iobCalc(time);
total.plus(calc);
}
}

View file

@ -87,7 +87,7 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener
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(), profile.getDia());
Iob iob = insulinInterface.iobCalc(treatments.get(position), 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

@ -128,14 +128,13 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface {
Double dia = profile.getDia();
Date now = new Date(time);
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, now, dia);
Iob tIOB = insulinInterface.iobCalc(t, time, dia);
total.iob += tIOB.iobContrib;
total.activity += tIOB.activityContrib;
Iob bIOB = insulinInterface.iobCalc(t, now, dia / SP.getInt("openapsama_bolussnooze_dia_divisor", 2));
Iob bIOB = insulinInterface.iobCalc(t, time, dia / SP.getInt("openapsama_bolussnooze_dia_divisor", 2));
total.bolussnooze += bIOB.iobContrib;
}
return total;