diff --git a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java index d11838bb85..d2563343b4 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -120,10 +120,10 @@ public class DataService extends IntentService { bgReading.value = bundle.getDouble(Intents.EXTRA_BG_ESTIMATE); bgReading.slope = bundle.getDouble(Intents.EXTRA_BG_SLOPE); bgReading.battery_level = bundle.getInt(Intents.EXTRA_SENSOR_BATTERY); - bgReading.timestamp = bundle.getLong(Intents.EXTRA_TIMESTAMP); + bgReading.timeIndex = bundle.getLong(Intents.EXTRA_TIMESTAMP); bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); - if (bgReading.timestamp < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) { + if (bgReading.timeIndex < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) { if (Config.logIncommingBG) log.debug("Ignoring old XDRIPREC BG " + bgReading.toString()); return; @@ -320,7 +320,7 @@ public class DataService extends IntentService { JSONObject sgvJson = new JSONObject(sgvstring); NSSgv nsSgv = new NSSgv(sgvJson); BgReading bgReading = new BgReading(nsSgv); - if (bgReading.timestamp < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) { + if (bgReading.timeIndex < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) { if (Config.logIncommingData) log.debug("Ignoring old BG: " + bgReading.toString()); return; @@ -337,7 +337,7 @@ public class DataService extends IntentService { JSONObject sgvJson = jsonArray.getJSONObject(i); NSSgv nsSgv = new NSSgv(sgvJson); BgReading bgReading = new BgReading(nsSgv); - if (bgReading.timestamp < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) { + if (bgReading.timeIndex < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) { if (Config.logIncommingData) log.debug("Ignoring old BG: " + bgReading.toString()); } else { diff --git a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java index eeac3cf264..90150792b3 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java +++ b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java @@ -22,19 +22,16 @@ public class BgReading implements DataPointInterface { public static final DecimalFormat mgdlFormat = new DecimalFormat("0"); public long getTimeIndex() { - return (long) Math.ceil(timestamp / 60000d); + return timeIndex; } public void setTimeIndex(long timeIndex) { - this.timestamp = timeIndex; + this.timeIndex = timeIndex; } @DatabaseField(id = true, useGetSet = true) public long timeIndex; - @DatabaseField - public long timestamp; - @DatabaseField public double value; @@ -52,7 +49,7 @@ public class BgReading implements DataPointInterface { public BgReading() {} public BgReading(NSSgv sgv) { - timestamp = sgv.getMills(); + timeIndex = sgv.getMills(); value = sgv.getMgdl(); raw = sgv.getFiltered(); } @@ -73,8 +70,7 @@ public class BgReading implements DataPointInterface { public String toString() { return "BgReading{" + "timeIndex=" + timeIndex + - ", timestamp=" + timestamp + - ", date=" + new Date(timestamp) + + ", date=" + new Date(timeIndex) + ", value=" + value + ", slope=" + slope + ", raw=" + raw + @@ -84,7 +80,7 @@ public class BgReading implements DataPointInterface { @Override public double getX() { - return timestamp; + return timeIndex; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index e086997dc7..7af72f22df 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -38,7 +38,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public static final String DATABASE_NAME = "AndroidAPSDb"; - private static final int DATABASE_VERSION = 1; + private static final int DATABASE_VERSION = 2; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); @@ -83,15 +83,15 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public void cleanUpDatabases() { // TODO: call it somewhere log.debug("Before BgReadings size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), "BgReadings")); - getWritableDatabase().delete("BgReadings", "timeIndex" + " < '" + Math.ceil((new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) / 60000d) + "'", null); + getWritableDatabase().delete("BgReadings", "timeIndex" + " < '" + (new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null); log.debug("After BgReadings size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), "BgReadings")); log.debug("Before TempBasals size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), "TempBasals")); - getWritableDatabase().delete("TempBasals", "timeIndex" + " < '" + Math.ceil((new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) / 60000d) + "'", null); + getWritableDatabase().delete("TempBasals", "timeIndex" + " < '" + (new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null); log.debug("After TempBasals size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), "TempBasals")); log.debug("Before Treatments size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), "Treatments")); - getWritableDatabase().delete("Treatments", "timeIndex" + " < '" + Math.ceil((new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) / 60000d) + "'", null); + getWritableDatabase().delete("Treatments", "timeIndex" + " < '" + (new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null); log.debug("After Treatments size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), "Treatments")); } @@ -146,14 +146,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { Dao daoBgReadings = MainApp.getDbHelper().getDaoBgReadings(); QueryBuilder queryBuilder = daoBgReadings.queryBuilder(); queryBuilder.orderBy("timeIndex", false); - queryBuilder.limit(1l); + queryBuilder.limit(1L); PreparedQuery preparedQuery = queryBuilder.prepare(); bgList = daoBgReadings.query(preparedQuery); } catch (SQLException e) { log.debug(e.getMessage(), e); } - if (bgList.size() > 0) + if (bgList != null && bgList.size() > 0) return bgList.get(0); else return null; @@ -170,7 +170,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { if (lastBg == null) return null; - if (lastBg.timestamp > new Date().getTime() - 9 * 60 * 1000) + if (lastBg.timeIndex > new Date().getTime() - 9 * 60 * 1000) return lastBg; return null; @@ -271,7 +271,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { int sizeRecords = bgReadings.size(); - if (sizeRecords < 4 || bgReadings.get(sizeRecords - 4).timestamp < new Date().getTime() - 7 * 60 * 1000l) { + if (sizeRecords < 4 || bgReadings.get(sizeRecords - 4).timeIndex < new Date().getTime() - 7 * 60 * 1000L) { if (Config.fakeGlucoseData) { return new GlucoseStatus(Math.random() * 400 + 40, (Math. random() - 0.5)* 18, (Math. random() - 0.5)* 18); } diff --git a/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java b/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java index ba2396fb4b..9dc341d458 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java +++ b/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java @@ -24,7 +24,7 @@ public class TempBasal { private static Logger log = LoggerFactory.getLogger(TempBasal.class); public long getTimeIndex() { - return (long) Math.ceil(timeStart.getTime() / 60000d); + return timeStart.getTime(); } public void setTimeIndex(long timeIndex) { @@ -34,7 +34,6 @@ public class TempBasal { @DatabaseField(id = true, useGetSet = true) public long timeIndex; - @DatabaseField public Date timeStart; diff --git a/app/src/main/java/info/nightscout/androidaps/db/Treatment.java b/app/src/main/java/info/nightscout/androidaps/db/Treatment.java index 6db41b5cd0..012d6e8488 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/Treatment.java +++ b/app/src/main/java/info/nightscout/androidaps/db/Treatment.java @@ -28,7 +28,7 @@ public class Treatment { private static Logger log = LoggerFactory.getLogger(Treatment.class); public long getTimeIndex() { - return (long) Math.ceil(created_at.getTime() / 60000d); + return created_at.getTime(); } public void setTimeIndex(long timeIndex) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index 3e83542e95..5c961f8d96 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -318,7 +318,7 @@ public class OverviewFragment extends Fragment implements PluginBase { flag &= ~Paint.STRIKE_THRU_TEXT_FLAG; bgView.setPaintFlags(flag); - Long agoMsec = new Date().getTime() - lastBG.timestamp; + Long agoMsec = new Date().getTime() - lastBG.timeIndex; int agoMin = (int) (agoMsec / 60d / 1000d); timeAgoView.setText(agoMin + " " + getString(R.string.minago));