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

205 lines
5 KiB
Java
Raw Normal View History

2017-01-13 22:36:47 +01:00
package info.nightscout.androidaps.db;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2017-06-08 19:34:46 +02:00
import java.util.Objects;
2017-01-13 22:36:47 +01:00
2017-01-13 23:43:17 +01:00
import info.nightscout.androidaps.Constants;
2019-04-18 19:33:23 +02:00
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Profile;
2017-05-21 22:05:03 +02:00
import info.nightscout.androidaps.interfaces.Interval;
2018-07-30 15:46:20 +02:00
import info.nightscout.androidaps.logging.L;
2019-02-26 20:38:27 +01:00
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.DecimalFormatter;
2017-01-13 22:36:47 +01:00
@DatabaseTable(tableName = DatabaseHelper.DATABASE_TEMPTARGETS)
2017-05-21 22:05:03 +02:00
public class TempTarget implements Interval {
2018-07-30 15:46:20 +02:00
private static Logger log = LoggerFactory.getLogger(L.DATABASE);
2017-01-13 22:36:47 +01:00
2017-05-21 22:05:03 +02:00
@DatabaseField(id = true)
public long date;
2017-01-13 22:36:47 +01:00
2017-05-21 22:05:03 +02:00
@DatabaseField
public boolean isValid = true;
2017-01-13 22:36:47 +01:00
@DatabaseField
2017-05-21 22:05:03 +02:00
public int source = Source.NONE;
@DatabaseField
public String _id = null; // NS _id
2017-01-13 22:36:47 +01:00
@DatabaseField
2017-01-13 23:43:17 +01:00
public double low; // in mgdl
2017-01-13 22:36:47 +01:00
@DatabaseField
2017-01-13 23:43:17 +01:00
public double high; // in mgdl
2017-01-13 22:36:47 +01:00
@DatabaseField
public String reason;
@DatabaseField
2017-05-21 22:05:03 +02:00
public int durationInMinutes;
2017-01-13 22:36:47 +01:00
2018-03-08 14:52:02 +01:00
public double target() {
return (low + high) / 2;
}
2017-06-08 19:34:46 +02:00
public boolean isEqual(TempTarget other) {
if (date != other.date) {
return false;
}
if (durationInMinutes != other.durationInMinutes)
return false;
if (low != other.low)
return false;
if (high != other.high)
return false;
2017-07-20 00:53:05 +02:00
if (!Objects.equals(reason, other.reason))
2017-06-08 19:34:46 +02:00
return false;
if (!Objects.equals(_id, other._id))
return false;
return true;
}
public void copyFrom(TempTarget t) {
date = t.date;
_id = t._id;
durationInMinutes = t.durationInMinutes;
low = t.low;
high = t.high;
reason = t.reason;
}
2018-03-26 19:46:18 +02:00
public TempTarget date(long date) {
this.date = date;
return this;
}
public TempTarget low(double low) {
this.low = low;
return this;
}
public TempTarget high(double high) {
this.high = high;
return this;
}
public TempTarget duration(int duration) {
this.durationInMinutes = duration;
return this;
}
public TempTarget reason(String reason) {
this.reason = reason;
return this;
}
public TempTarget _id(String _id) {
this._id = _id;
return this;
}
public TempTarget source(int source) {
this.source = source;
return this;
}
2017-05-21 22:05:03 +02:00
// -------- Interval interface ---------
Long cuttedEnd = null;
public long durationInMsec() {
return durationInMinutes * 60 * 1000L;
}
public long start() {
return date;
}
2017-01-14 00:37:35 +01:00
2017-05-21 22:05:03 +02:00
// planned end time at time of creation
public long originalEnd() {
return date + durationInMinutes * 60 * 1000L;
2017-01-13 22:36:47 +01:00
}
2017-05-21 22:05:03 +02:00
// end time after cut
public long end() {
if (cuttedEnd != null)
return cuttedEnd;
return originalEnd();
}
public void cutEndTo(long end) {
cuttedEnd = end;
}
public boolean match(long time) {
if (start() <= time && end() >= time)
return true;
return false;
}
public boolean before(long time) {
if (end() < time)
return true;
return false;
}
public boolean after(long time) {
if (start() > time)
return true;
return false;
}
2017-05-23 20:15:14 +02:00
@Override
public boolean isInProgress() {
2017-06-15 23:12:12 +02:00
return match(System.currentTimeMillis());
2017-05-23 20:15:14 +02:00
}
@Override
public boolean isEndingEvent() {
return durationInMinutes == 0;
}
2018-01-21 13:37:38 +01:00
@Override
public boolean isValid() {
return true;
}
2017-05-21 22:05:03 +02:00
// -------- Interval interface end ---------
2017-01-13 23:43:17 +01:00
public String lowValueToUnitsToString(String units) {
if (units.equals(Constants.MGDL)) return DecimalFormatter.to0Decimal(low);
else return DecimalFormatter.to1Decimal(low * Constants.MGDL_TO_MMOLL);
}
public String highValueToUnitsToString(String units) {
if (units.equals(Constants.MGDL)) return DecimalFormatter.to0Decimal(high);
else return DecimalFormatter.to1Decimal(low * Constants.MGDL_TO_MMOLL);
}
2017-06-08 19:34:46 +02:00
public String toString() {
2017-05-21 22:05:03 +02:00
return "TemporaryTarget{" +
"date=" + date +
"date=" + DateUtil.dateAndTimeString(date) +
", isValid=" + isValid +
", duration=" + durationInMinutes +
2017-01-13 22:36:47 +01:00
", reason=" + reason +
", low=" + low +
", high=" + high +
'}';
}
2019-04-18 19:33:23 +02:00
public String friendlyDescription(String units) {
return Profile.toTargetRangeString(low, high, Constants.MGDL, units) +
units +
"@" + MainApp.gs(R.string.mins, durationInMinutes) +
(reason != null && !reason.equals("") ? "(" + reason + ")" : "");
}
2017-01-13 22:36:47 +01:00
}