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

164 lines
5.4 KiB
Java
Raw Normal View History

2017-01-04 22:33:17 +01:00
package info.nightscout.androidaps.data;
2017-01-24 02:21:25 +01:00
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
2017-01-06 22:42:37 +01:00
import android.support.annotation.Nullable;
2017-01-04 22:33:17 +01:00
import android.text.Html;
import android.text.Spanned;
2017-02-10 16:53:39 +01:00
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
2017-01-06 22:42:37 +01:00
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
2017-01-04 22:33:17 +01:00
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
2017-01-06 22:42:37 +01:00
import info.nightscout.androidaps.db.BgReading;
2018-03-26 16:51:25 +02:00
import info.nightscout.utils.DateUtil;
2017-01-04 22:33:17 +01:00
import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.Round;
/**
* Created by mike on 04.01.2017.
*/
public class GlucoseStatus {
2017-02-10 16:53:39 +01:00
private static Logger log = LoggerFactory.getLogger(GlucoseStatus.class);
2017-01-04 22:33:17 +01:00
public double glucose = 0d;
public double delta = 0d;
public double avgdelta = 0d;
2017-01-06 22:42:37 +01:00
public double short_avgdelta = 0d;
public double long_avgdelta = 0d;
2017-12-11 18:45:04 +01:00
public long date = 0L;
2017-01-04 22:33:17 +01:00
2017-01-24 02:21:25 +01:00
2017-01-04 22:33:17 +01:00
@Override
public String toString() {
2018-03-26 16:51:25 +02:00
return MainApp.gs(R.string.glucose) + " " + DecimalFormatter.to0Decimal(glucose) + " mg/dl\n" +
MainApp.gs(R.string.delta) + " " + DecimalFormatter.to0Decimal(delta) + " mg/dl\n" +
MainApp.gs(R.string.short_avgdelta) + " " + DecimalFormatter.to2Decimal(short_avgdelta) + " mg/dl\n" +
MainApp.gs(R.string.long_avgdelta) + " " + DecimalFormatter.to2Decimal(long_avgdelta) + " mg/dl";
2017-01-04 22:33:17 +01:00
}
public GlucoseStatus() {
}
public GlucoseStatus round() {
this.glucose = Round.roundTo(this.glucose, 0.1);
this.delta = Round.roundTo(this.delta, 0.01);
this.avgdelta = Round.roundTo(this.avgdelta, 0.01);
2017-01-06 22:42:37 +01:00
this.short_avgdelta = Round.roundTo(this.short_avgdelta, 0.01);
this.long_avgdelta = Round.roundTo(this.long_avgdelta, 0.01);
2017-01-04 22:33:17 +01:00
return this;
}
2017-01-06 22:42:37 +01:00
2018-01-04 16:20:49 +01:00
@Nullable
public static GlucoseStatus getGlucoseStatusData(){
return getGlucoseStatusData(false);
}
2017-01-06 22:42:37 +01:00
@Nullable
2018-01-04 16:20:49 +01:00
public static GlucoseStatus getGlucoseStatusData(boolean allowOldData) {
2017-01-06 22:42:37 +01:00
// load 45min
2018-03-26 16:51:25 +02:00
long fromtime = DateUtil.now() - 60 * 1000L * 45;
2017-01-09 10:57:15 +01:00
List<BgReading> data = MainApp.getDbHelper().getBgreadingsDataFromTime(fromtime, false);
2017-01-06 22:42:37 +01:00
int sizeRecords = data.size();
2018-03-26 16:51:25 +02:00
if (sizeRecords == 0) {
return null;
}
if (data.get(0).date < DateUtil.now() - 7 * 60 * 1000L && !allowOldData) {
2017-01-06 22:42:37 +01:00
return null;
}
BgReading now = data.get(0);
2017-05-21 22:05:03 +02:00
long now_date = now.date;
2017-01-06 22:42:37 +01:00
double change;
2018-03-26 16:51:25 +02:00
if (sizeRecords == 1) {
GlucoseStatus status = new GlucoseStatus();
status.glucose = now.value;
status.short_avgdelta = 0d;
status.delta = 0d;
status.long_avgdelta = 0d;
status.avgdelta = 0d; // for OpenAPS MA
2018-03-26 16:51:25 +02:00
status.date = now_date;
return status.round();
}
2017-01-06 22:42:37 +01:00
ArrayList<Double> last_deltas = new ArrayList<Double>();
ArrayList<Double> short_deltas = new ArrayList<Double>();
ArrayList<Double> long_deltas = new ArrayList<Double>();
for (int i = 1; i < data.size(); i++) {
if (data.get(i).value > 38) {
BgReading then = data.get(i);
2017-05-21 22:05:03 +02:00
long then_date = then.date;
2017-01-06 22:42:37 +01:00
double avgdelta = 0;
long minutesago;
2017-01-06 22:42:37 +01:00
minutesago = Math.round((now_date - then_date) / (1000d * 60));
2017-01-06 22:42:37 +01:00
// multiply by 5 to get the same units as delta, i.e. mg/dL/5m
change = now.value - then.value;
avgdelta = change / minutesago * 5;
// use the average of all data points in the last 2.5m for all further "now" calculations
if (0 < minutesago && minutesago < 2.5) {
now.value = (now.value + then.value) / 2;
now_date = (now_date + then_date) / 2;
// short_deltas are calculated from everything ~5-15 minutes ago
} else if (2.5 < minutesago && minutesago < 17.5) {
//console.error(minutesago, avgdelta);
short_deltas.add(avgdelta);
// last_deltas are calculated from everything ~5 minutes ago
if (2.5 < minutesago && minutesago < 7.5) {
last_deltas.add(avgdelta);
}
// long_deltas are calculated from everything ~20-40 minutes ago
} else if (17.5 < minutesago && minutesago < 42.5) {
long_deltas.add(avgdelta);
}
}
}
GlucoseStatus status = new GlucoseStatus();
status.glucose = now.value;
2017-12-11 18:45:04 +01:00
status.date = now_date;
2017-01-24 02:21:25 +01:00
2017-01-06 22:42:37 +01:00
status.short_avgdelta = average(short_deltas);
2017-01-24 02:21:25 +01:00
if (last_deltas.isEmpty()) {
2017-01-24 02:21:25 +01:00
status.delta = status.short_avgdelta;
} else {
status.delta = average(last_deltas);
}
2017-01-06 22:42:37 +01:00
status.long_avgdelta = average(long_deltas);
status.avgdelta = status.short_avgdelta; // for OpenAPS MA
return status.round();
}
public static double average(ArrayList<Double> array) {
double sum = 0d;
if (array.size() == 0)
return 0d;
for (Double value : array) {
sum += value;
}
return sum / array.size();
}
2017-01-04 22:33:17 +01:00
}