refactor IOB calculation & fix iob_array.time

This commit is contained in:
Milos Kozak 2017-01-13 20:30:48 +01:00
parent 9d8d925f75
commit d9fffc5625
8 changed files with 9 additions and 18 deletions

View file

@ -23,13 +23,16 @@ public class IobTotal {
public Double netInsulin = 0d; // for calculations from temp basals only public Double netInsulin = 0d; // for calculations from temp basals only
public Double netRatio = 0d; // for calculations from temp basals only public Double netRatio = 0d; // for calculations from temp basals only
public IobTotal() { long time;
public IobTotal(long time) {
this.iob = 0d; this.iob = 0d;
this.activity = 0d; this.activity = 0d;
this.bolussnooze = 0d; this.bolussnooze = 0d;
this.basaliob = 0d; this.basaliob = 0d;
this.netbasalinsulin = 0d; this.netbasalinsulin = 0d;
this.hightempinsulin = 0d; this.hightempinsulin = 0d;
this.time = time;
} }
public IobTotal plus(IobTotal other) { public IobTotal plus(IobTotal other) {
@ -45,7 +48,7 @@ public class IobTotal {
} }
public static IobTotal combine(IobTotal bolusIOB, IobTotal basalIob) { public static IobTotal combine(IobTotal bolusIOB, IobTotal basalIob) {
IobTotal result = new IobTotal(); IobTotal result = new IobTotal(bolusIOB.time);
result.iob = bolusIOB.iob + basalIob.basaliob; result.iob = bolusIOB.iob + basalIob.basaliob;
result.activity = bolusIOB.activity + basalIob.activity; result.activity = bolusIOB.activity + basalIob.activity;
result.bolussnooze = bolusIOB.bolussnooze; result.bolussnooze = bolusIOB.bolussnooze;
@ -85,7 +88,7 @@ public class IobTotal {
json.put("basaliob", basaliob); json.put("basaliob", basaliob);
json.put("bolussnooze", bolussnooze); json.put("bolussnooze", bolussnooze);
json.put("activity", activity); json.put("activity", activity);
json.put("time", DateUtil.toISOString(new Date())); json.put("time", DateUtil.toISOString(new Date(time)));
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -95,19 +98,15 @@ public class IobTotal {
public static IobTotal calulateFromTreatmentsAndTemps() { public static IobTotal calulateFromTreatmentsAndTemps() {
ConfigBuilderPlugin.getActiveTreatments().updateTotalIOB(); ConfigBuilderPlugin.getActiveTreatments().updateTotalIOB();
IobTotal bolusIob = ConfigBuilderPlugin.getActiveTreatments().getLastCalculation().round(); IobTotal bolusIob = ConfigBuilderPlugin.getActiveTreatments().getLastCalculation().round();
if (bolusIob == null) bolusIob = new IobTotal();
ConfigBuilderPlugin.getActiveTempBasals().updateTotalIOB(); ConfigBuilderPlugin.getActiveTempBasals().updateTotalIOB();
IobTotal basalIob = ConfigBuilderPlugin.getActiveTempBasals().getLastCalculation().round(); IobTotal basalIob = ConfigBuilderPlugin.getActiveTempBasals().getLastCalculation().round();
if (basalIob == null) basalIob = new IobTotal();
IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round(); IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round();
return iobTotal; return iobTotal;
} }
public static IobTotal calulateFromTreatmentsAndTemps(long time) { public static IobTotal calulateFromTreatmentsAndTemps(long time) {
IobTotal bolusIob = ConfigBuilderPlugin.getActiveTreatments().getCalculationToTime(time).round(); IobTotal bolusIob = ConfigBuilderPlugin.getActiveTreatments().getCalculationToTime(time).round();
if (bolusIob == null) bolusIob = new IobTotal();
IobTotal basalIob = ConfigBuilderPlugin.getActiveTempBasals().getCalculationToTime(time).round(); IobTotal basalIob = ConfigBuilderPlugin.getActiveTempBasals().getCalculationToTime(time).round();
if (basalIob == null) basalIob = new IobTotal();
IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round(); IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round();
return iobTotal; return iobTotal;
} }

View file

@ -53,7 +53,7 @@ public class TempBasal {
public IobTotal iobCalc(Date time) { public IobTotal iobCalc(Date time) {
IobTotal result = new IobTotal(); IobTotal result = new IobTotal(time.getTime());
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile == null) if (profile == null)

View file

@ -621,10 +621,8 @@ public class OverviewFragment extends Fragment {
// iob // iob
MainApp.getConfigBuilder().getActiveTreatments().updateTotalIOB(); MainApp.getConfigBuilder().getActiveTreatments().updateTotalIOB();
IobTotal bolusIob = MainApp.getConfigBuilder().getActiveTreatments().getLastCalculation().round(); IobTotal bolusIob = MainApp.getConfigBuilder().getActiveTreatments().getLastCalculation().round();
if (bolusIob == null) bolusIob = new IobTotal();
MainApp.getConfigBuilder().getActiveTempBasals().updateTotalIOB(); MainApp.getConfigBuilder().getActiveTempBasals().updateTotalIOB();
IobTotal basalIob = MainApp.getConfigBuilder().getActiveTempBasals().getLastCalculation().round(); IobTotal basalIob = MainApp.getConfigBuilder().getActiveTempBasals().getLastCalculation().round();
if (basalIob == null) basalIob = new IobTotal();
String iobtext = getString(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U (" String iobtext = getString(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U ("
+ getString(R.string.bolus) + ": " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U " + getString(R.string.bolus) + ": " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U "

View file

@ -219,10 +219,8 @@ public class SmsCommunicatorPlugin implements PluginBase {
MainApp.getConfigBuilder().getActiveTreatments().updateTotalIOB(); MainApp.getConfigBuilder().getActiveTreatments().updateTotalIOB();
IobTotal bolusIob = MainApp.getConfigBuilder().getActiveTreatments().getLastCalculation().round(); IobTotal bolusIob = MainApp.getConfigBuilder().getActiveTreatments().getLastCalculation().round();
if (bolusIob == null) bolusIob = new IobTotal();
MainApp.getConfigBuilder().getActiveTempBasals().updateTotalIOB(); MainApp.getConfigBuilder().getActiveTempBasals().updateTotalIOB();
IobTotal basalIob = MainApp.getConfigBuilder().getActiveTempBasals().getLastCalculation().round(); IobTotal basalIob = MainApp.getConfigBuilder().getActiveTempBasals().getLastCalculation().round();
if (basalIob == null) basalIob = new IobTotal();
reply += MainApp.sResources.getString(R.string.sms_iob) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U (" reply += MainApp.sResources.getString(R.string.sms_iob) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U ("
+ MainApp.sResources.getString(R.string.sms_bolus) + " " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U " + MainApp.sResources.getString(R.string.sms_bolus) + " " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U "

View file

@ -174,7 +174,7 @@ public class TempBasalsPlugin implements PluginBase, TempBasalsInterface {
checkForExpired(tempBasals); checkForExpired(tempBasals);
checkForExpired(extendedBoluses); checkForExpired(extendedBoluses);
Date now = new Date(time); Date now = new Date(time);
IobTotal total = new IobTotal(); IobTotal total = new IobTotal(time);
for (Integer pos = 0; pos < tempBasals.size(); pos++) { for (Integer pos = 0; pos < tempBasals.size(); pos++) {
TempBasal t = tempBasals.get(pos); TempBasal t = tempBasals.get(pos);
IobTotal calc = t.iobCalc(now); IobTotal calc = t.iobCalc(now);

View file

@ -110,7 +110,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface {
@Override @Override
public IobTotal getCalculationToTime(long time) { public IobTotal getCalculationToTime(long time) {
IobTotal total = new IobTotal(); IobTotal total = new IobTotal(time);
if (MainApp.getConfigBuilder() == null || ConfigBuilderPlugin.getActiveProfile() == null) // app not initialized yet if (MainApp.getConfigBuilder() == null || ConfigBuilderPlugin.getActiveProfile() == null) // app not initialized yet
return total; return total;

View file

@ -444,10 +444,8 @@ public class WatchUpdaterService extends WearableListenerService implements
//IOB //IOB
MainApp.getConfigBuilder().getActiveTreatments().updateTotalIOB(); MainApp.getConfigBuilder().getActiveTreatments().updateTotalIOB();
IobTotal bolusIob = MainApp.getConfigBuilder().getActiveTreatments().getLastCalculation().round(); IobTotal bolusIob = MainApp.getConfigBuilder().getActiveTreatments().getLastCalculation().round();
if (bolusIob == null) bolusIob = new IobTotal();
MainApp.getConfigBuilder().getActiveTempBasals().updateTotalIOB(); MainApp.getConfigBuilder().getActiveTempBasals().updateTotalIOB();
IobTotal basalIob = MainApp.getConfigBuilder().getActiveTempBasals().getLastCalculation().round(); IobTotal basalIob = MainApp.getConfigBuilder().getActiveTempBasals().getLastCalculation().round();
if (basalIob == null) basalIob = new IobTotal();
status += (shortString?"":(getString(R.string.treatments_iob_label_string) + " ")) + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "(" status += (shortString?"":(getString(R.string.treatments_iob_label_string) + " ")) + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "("
+ DecimalFormatter.to2Decimal(bolusIob.iob) + "|" + DecimalFormatter.to2Decimal(bolusIob.iob) + "|"
+ DecimalFormatter.to2Decimal(basalIob.basaliob) + ")"; + DecimalFormatter.to2Decimal(basalIob.basaliob) + ")";

View file

@ -126,10 +126,8 @@ public class PersistentNotificationPlugin implements PluginBase{
//IOB //IOB
MainApp.getConfigBuilder().getActiveTreatments().updateTotalIOB(); MainApp.getConfigBuilder().getActiveTreatments().updateTotalIOB();
IobTotal bolusIob = MainApp.getConfigBuilder().getActiveTreatments().getLastCalculation().round(); IobTotal bolusIob = MainApp.getConfigBuilder().getActiveTreatments().getLastCalculation().round();
if (bolusIob == null) bolusIob = new IobTotal();
MainApp.getConfigBuilder().getActiveTempBasals().updateTotalIOB(); MainApp.getConfigBuilder().getActiveTempBasals().updateTotalIOB();
IobTotal basalIob = MainApp.getConfigBuilder().getActiveTempBasals().getLastCalculation().round(); IobTotal basalIob = MainApp.getConfigBuilder().getActiveTempBasals().getLastCalculation().round();
if (basalIob == null) basalIob = new IobTotal();
String line2 = ctx.getString(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U (" String line2 = ctx.getString(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U ("
+ ctx.getString(R.string.bolus) + ": " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U " + ctx.getString(R.string.bolus) + ": " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U "
+ ctx.getString(R.string.basal) + ": " + DecimalFormatter.to2Decimal(basalIob.basaliob) + "U)"; + ctx.getString(R.string.basal) + ": " + DecimalFormatter.to2Decimal(basalIob.basaliob) + "U)";