AndroidAPS/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java

137 lines
4.6 KiB
Java
Raw Normal View History

2017-01-04 22:33:17 +01:00
package info.nightscout.androidaps.data;
2016-06-10 18:50:46 +02:00
2018-03-27 16:41:37 +02:00
import com.rits.cloning.Cloner;
2016-06-10 18:50:46 +02:00
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2016-06-10 18:50:46 +02:00
import java.util.Date;
2019-02-26 20:38:27 +01:00
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.Round;
2016-06-10 18:50:46 +02:00
public class IobTotal {
private static Logger log = LoggerFactory.getLogger(IobTotal.class);
2017-06-15 22:55:07 +02:00
public double iob;
public double activity;
public double bolussnooze;
public double basaliob;
public double netbasalinsulin;
public double hightempinsulin;
2016-06-10 18:50:46 +02:00
2017-08-10 17:28:41 +02:00
// oref1
2017-08-15 23:13:37 +02:00
public long lastBolusTime;
2017-12-11 18:45:04 +01:00
public IobTotal iobWithZeroTemp;
2017-08-10 17:28:41 +02:00
2017-06-15 22:55:07 +02:00
public double netInsulin = 0d; // for calculations from temp basals only
public double netRatio = 0d; // net ratio at start of temp basal
2016-06-10 18:50:46 +02:00
2017-06-15 22:55:07 +02:00
public double extendedBolusInsulin = 0d; // total insulin for extended bolus
long time;
2017-12-11 18:45:04 +01:00
2018-03-27 16:41:37 +02:00
public IobTotal copy() {
Cloner cloner = new Cloner();
return cloner.deepClone(this);
2017-12-11 18:45:04 +01:00
}
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-08-15 23:13:37 +02:00
this.lastBolusTime = 0;
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;
extendedBolusInsulin += other.extendedBolusInsulin;
2016-06-10 18:50:46 +02:00
return this;
}
public static IobTotal combine(IobTotal bolusIOB, IobTotal basalIob) {
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;
2017-10-22 02:20:11 +02:00
result.basaliob = bolusIOB.basaliob + basalIob.basaliob;
result.netbasalinsulin = bolusIOB.netbasalinsulin + basalIob.netbasalinsulin;
result.hightempinsulin = basalIob.hightempinsulin + bolusIOB.hightempinsulin;
2018-03-27 16:41:37 +02:00
result.netInsulin = basalIob.netInsulin + bolusIOB.netInsulin;
result.extendedBolusInsulin = basalIob.extendedBolusInsulin + bolusIOB.extendedBolusInsulin;
2017-08-15 23:13:37 +02:00
result.lastBolusTime = bolusIOB.lastBolusTime;
2017-12-11 18:45:04 +01:00
result.iobWithZeroTemp = basalIob.iobWithZeroTemp;
2016-06-10 18:50:46 +02:00
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);
2018-03-27 16:41:37 +02:00
this.netInsulin = Round.roundTo(this.netInsulin, 0.001);
this.extendedBolusInsulin = Round.roundTo(this.extendedBolusInsulin, 0.001);
2016-06-20 12:03:05 +02:00
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) {
log.error("Unhandled exception", e);
2016-06-10 18:50:46 +02:00
}
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-08-15 23:13:37 +02:00
json.put("lastBolusTime", lastBolusTime);
json.put("time", DateUtil.toISOString(new Date(time)));
2017-12-11 18:45:04 +01:00
/*
This is requested by SMB determine_basal but by based on Scott's info
it's MDT specific safety check only
It's causing rounding issues in determine_basal
JSONObject lastTemp = new JSONObject();
lastTemp.put("date", lastTempDate);
lastTemp.put("rate", lastTempRate);
lastTemp.put("duration", lastTempDuration);
json.put("lastTemp", lastTemp);
*/
if (iobWithZeroTemp != null) {
JSONObject iwzt = iobWithZeroTemp.determineBasalJson();
json.put("iobWithZeroTemp", iwzt);
}
2017-01-06 22:42:37 +01:00
} catch (JSONException e) {
log.error("Unhandled exception", e);
2017-01-06 22:42:37 +01:00
}
return json;
}
2016-06-10 18:50:46 +02:00
}