Add Last Carb Time

This commit is contained in:
Tim Gunn 2020-05-31 16:41:19 +12:00
parent 15717f0271
commit 04bd98e0e5
No known key found for this signature in database
GPG key ID: C9BC1E9D0D0AED8C
2 changed files with 36 additions and 0 deletions

View file

@ -613,6 +613,29 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
}
}
/**
* Returns the newest record with carbs > 0
*/
@Nullable
public Treatment getLastCarb() {
try {
QueryBuilder<Treatment, Long> 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<Treatment> 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())

View file

@ -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;