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

124 lines
3 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;
import java.util.Date;
2017-01-13 23:43:17 +01:00
import info.nightscout.androidaps.Constants;
2017-05-21 22:05:03 +02:00
import info.nightscout.androidaps.interfaces.Interval;
import info.nightscout.utils.DateUtil;
2017-01-13 23:43:17 +01:00
import info.nightscout.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 {
2017-01-13 22:36:47 +01:00
private static Logger log = LoggerFactory.getLogger(TempTarget.class);
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
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() {
return match(new Date().getTime());
}
@Override
public boolean isEndingEvent() {
return durationInMinutes == 0;
}
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-01-13 22:36:47 +01:00
public String log() {
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 +
'}';
}
}