2017-01-04 22:33:17 +01:00
|
|
|
package info.nightscout.androidaps.data;
|
2016-06-10 18:50:46 +02:00
|
|
|
|
2017-01-06 22:42:37 +01:00
|
|
|
import org.json.JSONArray;
|
2016-06-10 18:50:46 +02:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
2017-01-06 22:42:37 +01:00
|
|
|
import info.nightscout.androidaps.MainApp;
|
|
|
|
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
|
|
|
|
import info.nightscout.client.data.NSProfile;
|
2016-06-10 18:50:46 +02:00
|
|
|
import info.nightscout.utils.DateUtil;
|
2016-06-20 12:03:05 +02:00
|
|
|
import info.nightscout.utils.Round;
|
2016-06-10 18:50:46 +02:00
|
|
|
|
|
|
|
public class IobTotal {
|
|
|
|
public Double iob;
|
|
|
|
public Double activity;
|
|
|
|
public Double bolussnooze;
|
|
|
|
public Double basaliob;
|
|
|
|
public Double netbasalinsulin;
|
|
|
|
public Double hightempinsulin;
|
|
|
|
|
|
|
|
public Double netInsulin = 0d; // for calculations from temp basals only
|
|
|
|
public Double netRatio = 0d; // for calculations from temp basals only
|
|
|
|
|
2017-01-13 20:30:48 +01:00
|
|
|
long time;
|
|
|
|
|
|
|
|
public IobTotal(long time) {
|
2016-06-10 18:50:46 +02:00
|
|
|
this.iob = 0d;
|
|
|
|
this.activity = 0d;
|
|
|
|
this.bolussnooze = 0d;
|
|
|
|
this.basaliob = 0d;
|
|
|
|
this.netbasalinsulin = 0d;
|
|
|
|
this.hightempinsulin = 0d;
|
2017-01-13 20:30:48 +01:00
|
|
|
this.time = time;
|
2016-06-10 18:50:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public IobTotal plus(IobTotal other) {
|
|
|
|
iob += other.iob;
|
2016-06-25 15:00:42 +02:00
|
|
|
activity += other.activity;
|
|
|
|
bolussnooze += other.bolussnooze;
|
|
|
|
basaliob += other.basaliob;
|
|
|
|
netbasalinsulin += other.netbasalinsulin;
|
|
|
|
hightempinsulin += other.hightempinsulin;
|
2016-06-10 18:50:46 +02:00
|
|
|
netInsulin += other.netInsulin;
|
|
|
|
netRatio += other.netRatio;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IobTotal combine(IobTotal bolusIOB, IobTotal basalIob) {
|
2017-01-13 20:30:48 +01:00
|
|
|
IobTotal result = new IobTotal(bolusIOB.time);
|
2016-11-07 07:55:41 +01:00
|
|
|
result.iob = bolusIOB.iob + basalIob.basaliob;
|
2016-11-07 15:18:48 +01:00
|
|
|
result.activity = bolusIOB.activity + basalIob.activity;
|
2016-06-10 18:50:46 +02:00
|
|
|
result.bolussnooze = bolusIOB.bolussnooze;
|
2016-07-11 23:54:56 +02:00
|
|
|
result.basaliob = basalIob.basaliob;
|
2016-06-10 18:50:46 +02:00
|
|
|
result.netbasalinsulin = basalIob.netbasalinsulin;
|
|
|
|
result.hightempinsulin = basalIob.hightempinsulin;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-06-20 12:03:05 +02:00
|
|
|
public IobTotal round() {
|
|
|
|
this.iob = Round.roundTo(this.iob, 0.001);
|
|
|
|
this.activity = Round.roundTo(this.activity, 0.0001);
|
|
|
|
this.bolussnooze = Round.roundTo(this.bolussnooze, 0.0001);
|
|
|
|
this.basaliob = Round.roundTo(this.basaliob, 0.001);
|
|
|
|
this.netbasalinsulin = Round.roundTo(this.netbasalinsulin, 0.001);
|
|
|
|
this.hightempinsulin = Round.roundTo(this.hightempinsulin, 0.001);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-06-10 18:50:46 +02:00
|
|
|
public JSONObject json() {
|
|
|
|
JSONObject json = new JSONObject();
|
|
|
|
try {
|
2016-11-08 10:29:24 +01:00
|
|
|
json.put("iob", iob);
|
2016-08-05 00:32:48 +02:00
|
|
|
json.put("basaliob", basaliob);
|
2016-06-10 18:50:46 +02:00
|
|
|
json.put("activity", activity);
|
2016-08-05 00:32:48 +02:00
|
|
|
json.put("time", DateUtil.toISOString(new Date()));
|
2016-06-10 18:50:46 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return json;
|
|
|
|
}
|
2017-01-06 22:42:37 +01:00
|
|
|
|
|
|
|
public JSONObject determineBasalJson() {
|
|
|
|
JSONObject json = new JSONObject();
|
|
|
|
try {
|
|
|
|
json.put("iob", iob);
|
|
|
|
json.put("basaliob", basaliob);
|
|
|
|
json.put("bolussnooze", bolussnooze);
|
|
|
|
json.put("activity", activity);
|
2017-01-13 20:30:48 +01:00
|
|
|
json.put("time", DateUtil.toISOString(new Date(time)));
|
2017-01-06 22:42:37 +01:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IobTotal calulateFromTreatmentsAndTemps() {
|
|
|
|
ConfigBuilderPlugin.getActiveTreatments().updateTotalIOB();
|
|
|
|
IobTotal bolusIob = ConfigBuilderPlugin.getActiveTreatments().getLastCalculation().round();
|
|
|
|
ConfigBuilderPlugin.getActiveTempBasals().updateTotalIOB();
|
|
|
|
IobTotal basalIob = ConfigBuilderPlugin.getActiveTempBasals().getLastCalculation().round();
|
|
|
|
IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round();
|
|
|
|
return iobTotal;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IobTotal calulateFromTreatmentsAndTemps(long time) {
|
|
|
|
IobTotal bolusIob = ConfigBuilderPlugin.getActiveTreatments().getCalculationToTime(time).round();
|
|
|
|
IobTotal basalIob = ConfigBuilderPlugin.getActiveTempBasals().getCalculationToTime(time).round();
|
|
|
|
IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round();
|
|
|
|
return iobTotal;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IobTotal[] calculateIobArrayInDia() {
|
|
|
|
NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile();
|
|
|
|
// predict IOB out to DIA plus 30m
|
|
|
|
long time = new Date().getTime();
|
|
|
|
int len = (int) ((profile.getDia() *60 + 30) / 5);
|
|
|
|
IobTotal[] array = new IobTotal[len];
|
|
|
|
int pos = 0;
|
|
|
|
for (int i = 0; i < len; i++){
|
|
|
|
long t = time + i * 5 * 60000;
|
|
|
|
IobTotal iob = calulateFromTreatmentsAndTemps(t);
|
|
|
|
array[pos] = iob;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static JSONArray convertToJSONArray(IobTotal[] iobArray) {
|
|
|
|
JSONArray array = new JSONArray();
|
|
|
|
for (int i = 0; i < iobArray.length; i ++) {
|
|
|
|
array.put(iobArray[i].determineBasalJson());
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
2016-06-10 18:50:46 +02:00
|
|
|
}
|