diff --git a/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java b/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java index e6098cee54..60c7553cc3 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java +++ b/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java @@ -144,7 +144,6 @@ public class CareportalEvent implements DataPointWithLabelInterface, Interval { return true; } } - //log.debug("No found event for time: " + DateUtil.dateAndTimeString(time)); return false; } 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 742f767cec..a7ca159794 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -1434,6 +1434,24 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { return new ArrayList<>(); } + public List getProfileSwitchEventsFromTime(long mills, boolean ascending) { + try { + Dao daoProfileSwitch = getDaoProfileSwitch(); + List profileSwitches; + QueryBuilder queryBuilder = daoProfileSwitch.queryBuilder(); + queryBuilder.orderBy("date", ascending); + queryBuilder.limit(100L); + Where where = queryBuilder.where(); + where.ge("date", mills); + PreparedQuery preparedQuery = queryBuilder.prepare(); + profileSwitches = daoProfileSwitch.query(preparedQuery); + return profileSwitches; + } catch (SQLException e) { + log.error("Unhandled exception", e); + } + return new ArrayList<>(); + } + public boolean createOrUpdate(ProfileSwitch profileSwitch) { try { ProfileSwitch old; diff --git a/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java b/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java index f003976d8f..0527864fd2 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java +++ b/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java @@ -11,6 +11,7 @@ import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.List; import java.util.Objects; import info.nightscout.androidaps.MainApp; @@ -25,6 +26,7 @@ import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.plugins.ProfileLocal.LocalProfilePlugin; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.T; @DatabaseTable(tableName = DatabaseHelper.DATABASE_PROFILESWITCHES) public class ProfileSwitch implements Interval, DataPointWithLabelInterface { @@ -218,6 +220,26 @@ public class ProfileSwitch implements Interval, DataPointWithLabelInterface { MainApp.bus().post(new EventNewNotification(notification)); } + public static boolean isEvent5minBack(List list, long time, boolean zeroDurationOnly) { + for (int i = 0; i < list.size(); i++) { + ProfileSwitch event = list.get(i); + if (event.date <= time && event.date > (time - T.mins(5).msecs())) { + if (zeroDurationOnly) { + if (event.durationInMinutes == 0) { + if (L.isEnabled(L.DATABASE)) + log.debug("Found ProfileSwitch event for time: " + DateUtil.dateAndTimeFullString(time) + " " + event.toString()); + return true; + } + } else { + if (L.isEnabled(L.DATABASE)) + log.debug("Found ProfileSwitch event for time: " + DateUtil.dateAndTimeFullString(time) + " " + event.toString()); + return true; + } + } + } + return false; + } + // -------- Interval interface end --------- // ----------------- DataPointInterface -------------------- diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityAAPSPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityAAPSPlugin.java index 993a51edf8..432420df85 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityAAPSPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityAAPSPlugin.java @@ -14,6 +14,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.CareportalEvent; +import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; @@ -82,6 +83,7 @@ public class SensitivityAAPSPlugin extends AbstractSensitivityPlugin { List siteChanges = MainApp.getDbHelper().getCareportalEventsFromTime(fromTime, CareportalEvent.SITECHANGE, true); + List profileSwitches = MainApp.getDbHelper().getProfileSwitchEventsFromTime(fromTime, true); List deviationsArray = new ArrayList<>(); String pastSensitivity = ""; @@ -105,6 +107,12 @@ public class SensitivityAAPSPlugin extends AbstractSensitivityPlugin { pastSensitivity += "(SITECHANGE)"; } + // reset deviations after profile switch + if (ProfileSwitch.isEvent5minBack(profileSwitches, autosensData.time, true)) { + deviationsArray.clear(); + pastSensitivity += "(PROFILESWITCH)"; + } + double deviation = autosensData.deviation; //set positive deviations to zero if bg < 80 diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityOref0Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityOref0Plugin.java index de0e9c9c0c..53a8be8661 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityOref0Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityOref0Plugin.java @@ -14,6 +14,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.CareportalEvent; +import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; @@ -77,6 +78,7 @@ public class SensitivityOref0Plugin extends AbstractSensitivityPlugin { List siteChanges = MainApp.getDbHelper().getCareportalEventsFromTime(fromTime, CareportalEvent.SITECHANGE, true); + List profileSwitches = MainApp.getDbHelper().getProfileSwitchEventsFromTime(fromTime, true); List deviationsArray = new ArrayList<>(); String pastSensitivity = ""; @@ -100,6 +102,12 @@ public class SensitivityOref0Plugin extends AbstractSensitivityPlugin { pastSensitivity += "(SITECHANGE)"; } + // reset deviations after profile switch + if (ProfileSwitch.isEvent5minBack(profileSwitches, autosensData.time, true)) { + deviationsArray.clear(); + pastSensitivity += "(PROFILESWITCH)"; + } + double deviation = autosensData.deviation; //set positive deviations to zero if bg < 80 diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityOref1Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityOref1Plugin.java index 06656617c5..7d91c7fc4a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityOref1Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityOref1Plugin.java @@ -14,6 +14,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.CareportalEvent; +import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; @@ -76,6 +77,7 @@ public class SensitivityOref1Plugin extends AbstractSensitivityPlugin { } List siteChanges = MainApp.getDbHelper().getCareportalEventsFromTime(fromTime, CareportalEvent.SITECHANGE, true); + List profileSwitches = MainApp.getDbHelper().getProfileSwitchEventsFromTime(fromTime, true); List deviationsArray = new ArrayList<>(); String pastSensitivity = ""; @@ -99,6 +101,12 @@ public class SensitivityOref1Plugin extends AbstractSensitivityPlugin { pastSensitivity += "(SITECHANGE)"; } + // reset deviations after profile switch + if (ProfileSwitch.isEvent5minBack(profileSwitches, autosensData.time, true)) { + deviationsArray.clear(); + pastSensitivity += "(PROFILESWITCH)"; + } + double deviation = autosensData.deviation; //set positive deviations to zero if bg < 80 diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityWeightedAveragePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityWeightedAveragePlugin.java index 95b027a86c..2aeb26bd1a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityWeightedAveragePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Sensitivity/SensitivityWeightedAveragePlugin.java @@ -12,6 +12,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.CareportalEvent; +import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; @@ -80,6 +81,7 @@ public class SensitivityWeightedAveragePlugin extends AbstractSensitivityPlugin } List siteChanges = MainApp.getDbHelper().getCareportalEventsFromTime(fromTime, CareportalEvent.SITECHANGE, true); + List profileSwitches = MainApp.getDbHelper().getProfileSwitchEventsFromTime(fromTime, true); String pastSensitivity = ""; int index = 0; @@ -109,6 +111,12 @@ public class SensitivityWeightedAveragePlugin extends AbstractSensitivityPlugin pastSensitivity += "(SITECHANGE)"; } + // reset deviations after profile switch + if (ProfileSwitch.isEvent5minBack(profileSwitches, autosensData.time, true)) { + data.clear(); + pastSensitivity += "(PROFILESWITCH)"; + } + double deviation = autosensData.deviation; //set positive deviations to zero if bg < 80