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 def47b6440..75e830e654 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; diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java index 708e3be32d..1173936430 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java @@ -150,13 +150,10 @@ public abstract class PluginBase { protected void onStart() { if (getType() == PluginType.PUMP) { - try { - new Thread(() -> { - SystemClock.sleep(3000); - ConfigBuilderPlugin.getCommandQueue().readStatus("Pump driver changed.", null); - }).start(); - } catch (Exception ignored) { // Thread fail to start in tests - } + new Thread(() -> { + SystemClock.sleep(3000); + ConfigBuilderPlugin.getCommandQueue().readStatus("Pump driver changed.", null); + }).start(); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java index 219236263c..6a9613e92a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java @@ -169,7 +169,11 @@ public class SafetyPlugin extends PluginBase implements ConstraintsInterface { @Override public Constraint applyMaxIOBConstraints(Constraint maxIob) { - double maxIobPref = SP.getDouble(R.string.key_openapsma_max_iob, 1.5d); + double maxIobPref; + if (OpenAPSSMBPlugin.getPlugin().isEnabled(PluginType.APS)) + maxIobPref = SP.getDouble(R.string.key_openapssmb_max_iob, 3d); + else + maxIobPref = SP.getDouble(R.string.key_openapsma_max_iob, 1.5d); maxIob.setIfSmaller(maxIobPref, String.format(MainApp.gs(R.string.limitingiob), maxIobPref, MainApp.gs(R.string.maxvalueinpreferences)), this); if (OpenAPSMAPlugin.getPlugin().isEnabled(PluginType.APS)) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java index 21a59c326f..6cc856d46f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java @@ -242,7 +242,6 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai // TODO review if (!Config.NSCLIENT && !Config.G5UPLOADER) NSUpload.uploadDeviceStatus(); - lastDataTime = new Date(); } @Override @@ -283,7 +282,6 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai public void getPumpStatus() { log("getPumpStatus"); - lastDataTime = new Date(); if (Connector.get().isPumpConnected()) { log("is connected.. requesting status"); final UUID uuid = aSyncTaskRunner(new StatusTaskRunner(connector.getServiceConnector()), "Status"); @@ -439,7 +437,6 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai updateGui(); connector.tryToGetPumpStatusAgain(); - lastDataTime = new Date(); connector.requestHistorySync(30000); if (result.success) while (true) { @@ -538,8 +535,6 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai if (Config.logPumpComm) log.debug("Setting temp basal absolute: " + pumpEnactResult.success); - lastDataTime = new Date(); - updateGui(); connector.requestHistorySync(5000); @@ -621,7 +616,6 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai TemporaryBasal tempStop = new TemporaryBasal().date(System.currentTimeMillis()).source(Source.USER); TreatmentsPlugin.getPlugin().addToHistoryTempBasal(tempStop); } - lastDataTime = new Date(); updateGui(); if (Config.logPumpComm) log.debug("Canceling temp basal: "); // TODO get more info @@ -950,6 +944,7 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai singleMessageTaskRunner.fetch(new TaskRunner.ResultCallback() { @Override public void onResult(Object o) { + lastDataTime = new Date(); log(name + " success"); event.response_object = o; if (o instanceof BolusMessage) { @@ -988,6 +983,7 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai task.fetch(new TaskRunner.ResultCallback() { @Override public void onResult(Object o) { + lastDataTime = new Date(); log(name + " success"); event.response_object = o; event.success = true; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityOref0/SensitivityOref0Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityOref0/SensitivityOref0Plugin.java index 842c4c83bc..3e9cc2cf9e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityOref0/SensitivityOref0Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityOref0/SensitivityOref0Plugin.java @@ -125,7 +125,8 @@ public class SensitivityOref0Plugin extends PluginBase implements SensitivityInt for (double i = 0.9; i > 0.1; i = i - 0.02) { if (IobCobCalculatorPlugin.percentile(deviations, (i + 0.02)) >= 0 && IobCobCalculatorPlugin.percentile(deviations, i) < 0) { - log.debug(Math.round(100 * i) + "% of non-meal deviations negative (target 45%-50%)"); + if (Config.logAutosensData) + log.debug(Math.round(100 * i) + "% of non-meal deviations negative (target 45%-50%)"); } } double pSensitive = IobCobCalculatorPlugin.percentile(deviations, 0.50); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d13e47c779..8ac005b04e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -203,7 +203,7 @@ Max U/hr a Temp Basal can be set to This value is called max basal in OpenAPS context Maximum basal IOB OpenAPS can deliver [U] - This value is called Max IOB in OpenAPS context\nThis will default to zero. After several days or weeks, depending on your comfort level, you may choose to adjust this number. + This value is called Max IOB in OpenAPS context\nThis is maximal insulin in [U] APS can deliver at once. Bulgarian DISMISS Language @@ -991,4 +991,7 @@ Disconnected (%d m) Automatic careportal events Automatically upload insulin, cannula and battery changes and pump alarms to Nightscout + openapsmb_max_iob + Maximum total IOB OpenAPS can\'t go over [U] + This value is called Max IOB in OpenAPS context\nOpenAPS will not add more insulin if current IOB is greater than this value diff --git a/app/src/main/res/xml/pref_openapssmb.xml b/app/src/main/res/xml/pref_openapssmb.xml index 098d874eaa..1bdad6392c 100644 --- a/app/src/main/res/xml/pref_openapssmb.xml +++ b/app/src/main/res/xml/pref_openapssmb.xml @@ -11,11 +11,11 @@ android:dialogMessage="@string/openapsma_maxbasal_summary" android:title="@string/openapsma_maxbasal_title" /> + android:dialogMessage="@string/openapssmb_maxiob_summary" + android:title="@string/openapssmb_maxiob_title" /> d = constraintChecker.getMaxIOBAllowed(); Assert.assertEquals(1.5d, d.value()); - Assert.assertEquals(true, d.getReasonList().size() == 4); + Assert.assertEquals(3, d.getReasonList().size()); Assert.assertEquals("Safety: Limiting IOB to 1.5 U because of max value in preferences", d.getMostLimitedReasons()); } + @Test + public void iobSMBShouldBeLimited() { + // No limit by default + when(SP.getDouble(R.string.key_openapssmb_max_iob, 3d)).thenReturn(3d); + when(SP.getString(R.string.key_age, "")).thenReturn("teenage"); + OpenAPSSMBPlugin.getPlugin().setPluginEnabled(PluginType.APS, true); + + // Apply all limits + Constraint d = constraintChecker.getMaxIOBAllowed(); + Assert.assertEquals(3d, d.value()); + Assert.assertEquals(4, d.getReasonList().size()); + Assert.assertEquals("Safety: Limiting IOB to 3.0 U because of max value in preferences", d.getMostLimitedReasons()); + + } + @Before public void prepareMock() throws Exception { diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java index 0bd35e49ff..76901e5db4 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java @@ -210,7 +210,7 @@ public class SafetyPluginTest { when(SP.getString(R.string.key_age, "")).thenReturn("teenage"); OpenAPSMAPlugin.getPlugin().setPluginEnabled(PluginType.APS, true); OpenAPSAMAPlugin.getPlugin().setPluginEnabled(PluginType.APS, true); - OpenAPSSMBPlugin.getPlugin().setPluginEnabled(PluginType.APS, true); + //OpenAPSSMBPlugin.getPlugin().setPluginEnabled(PluginType.APS, true); // Apply all limits Constraint d = new Constraint<>(Constants.REALLYHIGHIOB); @@ -218,8 +218,7 @@ public class SafetyPluginTest { Assert.assertEquals(1.5d, d.value()); Assert.assertEquals("Safety: Limiting IOB to 1.5 U because of max value in preferences\n" + "Safety: Limiting IOB to 7.0 U because of hard limit\n" + - "Safety: Limiting IOB to 7.0 U because of hard limit\n" + - "Safety: Limiting IOB to 12.0 U because of hard limit", d.getReasons()); + "Safety: Limiting IOB to 7.0 U because of hard limit", d.getReasons()); Assert.assertEquals("Safety: Limiting IOB to 1.5 U because of max value in preferences", d.getMostLimitedReasons()); }