diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java index 18d997178e..5dfceda204 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java @@ -613,6 +613,29 @@ public class TreatmentService extends OrmLiteBaseService { } } + /** + * Returns the newest record with carbs > 0 + */ + @Nullable + public Treatment getLastCarb() { + try { + QueryBuilder queryBuilder = getDao().queryBuilder(); + Where where = queryBuilder.where(); + where.gt("carbs", 0); + where.and().le("date", DateUtil.now()); + where.and().eq("isValid", true); + queryBuilder.orderBy("date", false); + queryBuilder.limit(1L); + + List result = getDao().query(queryBuilder.prepare()); + if (result.isEmpty()) + return null; + return result.get(0); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public void deleteNS(JSONObject json) { String _id = JsonHelper.safeGetString(json, "_id"); if (_id != null && !_id.isEmpty()) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java index 8d723cea71..68d1be2f23 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java @@ -358,6 +358,19 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface } } + public long getLastCarbTime() { + Treatment last = getService().getLastCarb(); + if (last == null) { + getAapsLogger().debug(LTag.DATATREATMENTS, "Last Carb time: NOTHING FOUND"); + return 0; + } + else { + getAapsLogger().debug(LTag.DATATREATMENTS, "Last Carb time: " + dateUtil.dateAndTimeString(last.date)); + return last.date; + } + } + + @Override public boolean isInHistoryRealTempBasalInProgress() { return getRealTempBasalFromHistory(System.currentTimeMillis()) != null;