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 7c7b373414..93fef87342 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -280,7 +280,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { return getDao(TempTarget.class); } - private Dao getDaoBgReadings() throws SQLException { + private Dao getDaoBgReadings() throws SQLException { return getDao(BgReading.class); } @@ -369,8 +369,8 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { } /* - * Return last BgReading from database or null if db is empty - */ + * Return last BgReading from database or null if db is empty + */ @Nullable public static BgReading lastBg() { List bgList = null; @@ -394,9 +394,9 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { } /* - * Return bg reading if not old ( <9 min ) - * or null if older - */ + * Return bg reading if not old ( <9 min ) + * or null if older + */ @Nullable public static BgReading actualBg() { BgReading lastBg = lastBg(); @@ -446,7 +446,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { } // ------------------- TDD handling ----------------------- - public void createOrUpdateTDD(TDD tdd){ + public void createOrUpdateTDD(TDD tdd) { try { Dao dao = getDaoTDD(); dao.createOrUpdate(tdd); @@ -472,7 +472,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { } - // ------------- DbRequests handling ------------------- public void create(DbRequest dbr) { @@ -509,7 +508,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { queryBuilder.limit(10L); PreparedQuery preparedQuery = queryBuilder.prepare(); List dbList = getDaoDbRequest().query(preparedQuery); - log.error("deleteDbRequestbyMongoId query size: " + dbList.size()); for (DbRequest r : dbList) { delete(r); } @@ -714,7 +712,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { getDaoDanaRHistory().createOrUpdate(record); //If it is a TDD, store it for stats also. - if(record.recordCode == RecordTypes.RECORD_TYPE_DAILY){ + if (record.recordCode == RecordTypes.RECORD_TYPE_DAILY) { createOrUpdateTDD(new TDD(record.recordDate, record.recordDailyBolus, record.recordDailyBasal, 0)); } @@ -1154,37 +1152,10 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { } */ - public void createExtendedBolusFromJsonIfNotExists(JSONObject trJson) { - try { - QueryBuilder queryBuilder = null; - queryBuilder = getDaoExtendedBolus().queryBuilder(); - Where where = queryBuilder.where(); - where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills")); - PreparedQuery preparedQuery = queryBuilder.prepare(); - List list = getDaoExtendedBolus().query(preparedQuery); - ExtendedBolus extendedBolus; - if (list.size() == 0) { - extendedBolus = new ExtendedBolus(); - extendedBolus.source = Source.NIGHTSCOUT; - if (Config.logIncommingData) - log.debug("Adding ExtendedBolus record to database: " + trJson.toString()); - // Record does not exists. add - } else if (list.size() == 1) { - extendedBolus = list.get(0); - if (Config.logIncommingData) - log.debug("Updating ExtendedBolus record in database: " + trJson.toString()); - } else { - log.error("Something went wrong"); - return; - } - extendedBolus.date = trJson.getLong("mills"); - extendedBolus.durationInMinutes = trJson.has("duration") ? trJson.getInt("duration") : 0; - extendedBolus.insulin = trJson.getDouble("relative"); - extendedBolus._id = trJson.getString("_id"); + public void createExtendedBolusFromJsonIfNotExists(JSONObject json) { + ExtendedBolus extendedBolus = ExtendedBolus.createFromJson(json); + if (extendedBolus != null) createOrUpdate(extendedBolus); - } catch (SQLException | JSONException e) { - log.error("Unhandled exception", e); - } } private static void scheduleExtendedBolusChange() { diff --git a/app/src/main/java/info/nightscout/androidaps/db/ExtendedBolus.java b/app/src/main/java/info/nightscout/androidaps/db/ExtendedBolus.java index 8e7022b609..418de86c6c 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/ExtendedBolus.java +++ b/app/src/main/java/info/nightscout/androidaps/db/ExtendedBolus.java @@ -9,6 +9,8 @@ import android.graphics.Color; import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.table.DatabaseTable; +import org.json.JSONException; +import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,6 +27,7 @@ import info.nightscout.androidaps.plugins.Overview.graphExtensions.PointsWithLab import info.nightscout.androidaps.plugins.Treatments.Treatment; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.JsonHelper; import info.nightscout.utils.Round; /** @@ -89,6 +92,16 @@ public class ExtendedBolus implements Interval, DataPointWithLabelInterface { pumpId = t.pumpId; } + public static ExtendedBolus createFromJson(JSONObject json) { + ExtendedBolus extendedBolus = new ExtendedBolus(); + extendedBolus.source = Source.NIGHTSCOUT; + extendedBolus.date = JsonHelper.safeGetLong(json, "mills"); + extendedBolus.durationInMinutes = JsonHelper.safeGetInt(json, "duration"); + extendedBolus.insulin = JsonHelper.safeGetDouble(json, "relative") / 60 * extendedBolus.durationInMinutes; + extendedBolus._id = JsonHelper.safeGetString(json, "_id"); + extendedBolus.pumpId = JsonHelper.safeGetLong(json, "pumpId"); + return extendedBolus; + } // -------- Interval interface --------- Long cuttedEnd = null;