AndroidAPS/app/src/main/java/info/nightscout/androidaps/db/Treatment.java

153 lines
4.7 KiB
Java
Raw Normal View History

2016-06-05 01:40:35 +02:00
package info.nightscout.androidaps.db;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.MainApp;
2016-07-17 15:04:33 +02:00
import info.nightscout.androidaps.data.Iob;
2016-08-05 23:54:03 +02:00
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
2017-01-03 19:06:35 +01:00
import info.nightscout.androidaps.plugins.Overview.graphExtensions.DataPointWithLabelInterface;
2017-02-17 13:18:36 +01:00
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
2016-06-05 01:40:35 +02:00
import info.nightscout.utils.DateUtil;
2016-07-17 15:04:33 +02:00
import info.nightscout.utils.DecimalFormatter;
2016-06-05 01:40:35 +02:00
2016-07-21 15:10:42 +02:00
@DatabaseTable(tableName = DatabaseHelper.DATABASE_TREATMENTS)
2016-07-17 15:04:33 +02:00
public class Treatment implements DataPointWithLabelInterface {
2016-06-05 01:40:35 +02:00
private static Logger log = LoggerFactory.getLogger(Treatment.class);
public long getTimeIndex() {
2016-06-24 17:30:25 +02:00
return created_at.getTime();
2016-06-05 01:40:35 +02:00
}
public void setTimeIndex(long timeIndex) {
this.timeIndex = timeIndex;
}
@DatabaseField(id = true, useGetSet = true)
public long timeIndex;
@DatabaseField
public String _id;
@DatabaseField
public Date created_at;
@DatabaseField
public Double insulin = 0d;
2016-06-05 01:40:35 +02:00
@DatabaseField
public Double carbs = 0d;
2016-06-05 01:40:35 +02:00
@DatabaseField
public boolean mealBolus = true; // true for meal bolus , false for correction bolus
2016-06-05 01:40:35 +02:00
public void copyFrom(Treatment t) {
this._id = t._id;
this.created_at = t.created_at;
this.insulin = t.insulin;
this.carbs = t.carbs;
this.mealBolus = t.mealBolus;
2016-06-05 01:40:35 +02:00
}
2016-06-05 14:53:03 +02:00
2016-06-10 18:50:46 +02:00
public Iob iobCalc(Date time, Double dia) {
2016-06-05 14:53:03 +02:00
Iob result = new Iob();
Double scaleFactor = 3.0 / dia;
2016-06-05 01:40:35 +02:00
Double peak = 75d;
Double end = 180d;
2016-06-05 14:53:03 +02:00
if (this.insulin != 0d) {
Long bolusTime = this.created_at.getTime();
Double minAgo = scaleFactor * (time.getTime() - bolusTime) / 1000d / 60d;
2016-06-05 01:40:35 +02:00
if (minAgo < peak) {
2016-06-06 10:42:46 +02:00
Double x1 = minAgo / 5d + 1;
2016-06-05 14:53:03 +02:00
result.iobContrib = this.insulin * (1 - 0.001852 * x1 * x1 + 0.001852 * x1);
// units: BG (mg/dL) = (BG/U) * U insulin * scalar
2016-06-06 11:24:17 +02:00
result.activityContrib = this.insulin * (2 / dia / 60 / peak) * minAgo;
2016-06-05 14:53:03 +02:00
2016-06-05 01:40:35 +02:00
} else if (minAgo < end) {
2016-06-05 14:53:03 +02:00
Double x2 = (minAgo - 75) / 5;
result.iobContrib = this.insulin * (0.001323 * x2 * x2 - 0.054233 * x2 + 0.55556);
2016-11-29 04:08:45 +01:00
result.activityContrib = this.insulin * (2 / dia / 60 - (minAgo - peak) * 2 / dia / 60 / (60 * 3 - peak));
2016-06-05 01:40:35 +02:00
}
}
2016-06-05 14:53:03 +02:00
return result;
2016-06-05 01:40:35 +02:00
}
public long getMillisecondsFromStart() {
return new Date().getTime() - created_at.getTime();
}
public String log() {
return "Treatment{" +
"timeIndex: " + timeIndex +
", _id: " + _id +
", insulin: " + insulin +
", carbs: " + carbs +
", mealBolus: " + mealBolus +
2016-06-05 01:40:35 +02:00
", created_at: " +
"}";
}
2016-07-17 15:04:33 +02:00
// DataPointInterface
@Override
public double getX() {
return timeIndex;
}
// default when no sgv around available
private double yValue = 0;
@Override
public double getY() {
return yValue;
}
@Override
public String getLabel() {
String label = "";
if (insulin > 0) label += DecimalFormatter.to2Decimal(insulin) + "U";
if (carbs > 0)
label += (label.equals("") ? "" : " ") + DecimalFormatter.to0Decimal(carbs) + "g";
2016-07-17 15:04:33 +02:00
return label;
}
public void setYValue(List<BgReading> bgReadingsArray) {
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
if (profile == null) return;
for (int r = bgReadingsArray.size() - 1; r >= 0; r--) {
BgReading reading = bgReadingsArray.get(r);
if (reading.timeIndex > timeIndex) continue;
yValue = NSProfile.fromMgdlToUnits(reading.value, profile.getUnits());
break;
}
}
2016-06-05 01:40:35 +02:00
public void sendToNSClient() {
JSONObject data = new JSONObject();
try {
if (mealBolus)
data.put("eventType", "Meal Bolus");
else
data.put("eventType", "Correction Bolus");
2016-06-05 01:40:35 +02:00
if (insulin != 0d) data.put("insulin", insulin);
if (carbs != 0d) data.put("carbs", carbs.intValue());
data.put("created_at", DateUtil.toISOString(created_at));
data.put("timeIndex", timeIndex);
} catch (JSONException e) {
e.printStackTrace();
}
2016-08-05 23:54:03 +02:00
ConfigBuilderPlugin.uploadCareportalEntryToNS(data);
2016-06-05 01:40:35 +02:00
}
}