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

218 lines
5.3 KiB
Java
Raw Normal View History

2017-06-02 10:25:49 +02:00
package info.nightscout.androidaps.db;
2017-06-05 00:50:31 +02:00
import android.graphics.Color;
2017-06-02 10:25:49 +02:00
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
2017-06-13 19:37:31 +02:00
import org.json.JSONException;
import org.json.JSONObject;
2017-06-02 10:25:49 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
2017-06-08 20:52:33 +02:00
import java.util.Objects;
2017-06-02 10:25:49 +02:00
2017-06-13 19:37:31 +02:00
import info.nightscout.androidaps.data.Profile;
2017-06-02 10:25:49 +02:00
import info.nightscout.androidaps.interfaces.Interval;
2017-06-02 12:27:21 +02:00
import info.nightscout.androidaps.plugins.Overview.graphExtensions.DataPointWithLabelInterface;
2017-06-05 00:50:31 +02:00
import info.nightscout.androidaps.plugins.Overview.graphExtensions.PointsWithLabelGraphSeries;
2017-06-02 10:25:49 +02:00
import info.nightscout.utils.DateUtil;
@DatabaseTable(tableName = DatabaseHelper.DATABASE_PROFILESWITCHES)
2017-06-02 12:27:21 +02:00
public class ProfileSwitch implements Interval, DataPointWithLabelInterface {
2017-06-02 10:25:49 +02:00
private static Logger log = LoggerFactory.getLogger(ProfileSwitch.class);
@DatabaseField(id = true)
public long date;
@DatabaseField
public boolean isValid = true;
@DatabaseField
public int source = Source.NONE;
@DatabaseField
public String _id = null; // NS _id
@DatabaseField
public boolean isCPP = false; // CPP NS="CircadianPercentageProfile"
@DatabaseField
public int timeshift = 0; // CPP NS="timeshift"
@DatabaseField
public int percentage = 100; // CPP NS="percentage"
@DatabaseField
public String profileName = null;
@DatabaseField
public String profileJson = null;
@DatabaseField
public String profilePlugin = null; // NSProfilePlugin.class.getName();
@DatabaseField
public int durationInMinutes = 0;
2017-06-13 19:37:31 +02:00
private Profile profile = null;
public Profile getProfileObject() {
if (profile == null)
try {
profile = new Profile(new JSONObject(profileJson));
} catch (JSONException e) {
e.printStackTrace();
}
return profile;
}
2017-06-08 20:52:33 +02:00
public boolean isEqual(ProfileSwitch other) {
if (date != other.date) {
return false;
}
if (durationInMinutes != other.durationInMinutes)
return false;
if (percentage != other.percentage)
return false;
if (timeshift != other.timeshift)
return false;
if (isCPP != other.isCPP)
return false;
if (!Objects.equals(_id, other._id))
return false;
if (!Objects.equals(profilePlugin, other.profilePlugin))
return false;
if (!Objects.equals(profileJson, other.profileJson))
return false;
if (!Objects.equals(profileName, other.profileName))
return false;
return true;
}
public void copyFrom(ProfileSwitch t) {
date = t.date;
_id = t._id;
durationInMinutes = t.durationInMinutes;
percentage = t.percentage;
timeshift = t.timeshift;
isCPP = t.isCPP;
profilePlugin = t.profilePlugin;
profileJson = t.profileJson;
profileName = t.profileName;
}
2017-06-02 10:25:49 +02:00
// -------- Interval interface ---------
Long cuttedEnd = null;
public long durationInMsec() {
return durationInMinutes * 60 * 1000L;
}
public long start() {
return date;
}
// planned end time at time of creation
public long originalEnd() {
return date + durationInMinutes * 60 * 1000L;
}
// 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;
}
@Override
public boolean isInProgress() {
2017-06-15 23:12:12 +02:00
return match(System.currentTimeMillis());
2017-06-02 10:25:49 +02:00
}
@Override
public boolean isEndingEvent() {
return durationInMinutes == 0;
}
// -------- Interval interface end ---------
2017-06-02 12:27:21 +02:00
// ----------------- DataPointInterface --------------------
@Override
public double getX() {
return date;
}
// default when no sgv around available
private double yValue = 0;
@Override
public double getY() {
return yValue;
}
2017-06-05 00:50:31 +02:00
@Override
public void setY(double y) {
yValue = y;
}
2017-06-02 12:27:21 +02:00
@Override
public String getLabel() {
return profileName;
}
2017-06-05 00:50:31 +02:00
@Override
public long getDuration() {
return 0;
}
@Override
public PointsWithLabelGraphSeries.Shape getShape() {
return PointsWithLabelGraphSeries.Shape.PROFILE;
}
@Override
public float getSize() {
return 10;
}
@Override
public int getColor() {
return Color.CYAN;
}
2017-06-12 13:11:16 +02:00
public String toString() {
2017-06-02 10:25:49 +02:00
return "ProfileSwitch{" +
"date=" + date +
"date=" + DateUtil.dateAndTimeString(date) +
", isValid=" + isValid +
", duration=" + durationInMinutes +
", profileName=" + profileName +
", percentage=" + percentage +
", timeshift=" + timeshift +
'}';
}
}