Skip to content

Commit

Permalink
Simulations enabled option
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Nov 17, 2016
1 parent fefbb2a commit 23869fa
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
15 changes: 14 additions & 1 deletion app/src/main/java/com/eveningoutpost/dexdrip/Models/Profile.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.eveningoutpost.dexdrip.Models;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.eveningoutpost.dexdrip.Home;
import com.eveningoutpost.dexdrip.UtilityModels.Constants;
import com.eveningoutpost.dexdrip.profileeditor.ProfileEditor;
import com.eveningoutpost.dexdrip.profileeditor.ProfileItem;
import com.eveningoutpost.dexdrip.xdrip;

import java.util.List;

Expand All @@ -33,6 +35,7 @@ public class Profile {
private static double stored_default_absorption_rate = 35;
private static double stored_default_insulin_action_time = 3.0;
private static double stored_default_carb_delay_minutes = 15;
private static boolean preferences_loaded = false;
private static List<ProfileItem> profileItemList;

public static double getSensitivity(double when) {
Expand Down Expand Up @@ -191,7 +194,16 @@ public static double[] evaluateEndGameMmol(double mmol, double endGameTime, doub
return new double[]{addcarbs, addinsulin};
}

public static void reloadPreferences(SharedPreferences prefs) {
public static void reloadPreferencesIfNeeded(SharedPreferences prefs) {
if (!preferences_loaded) reloadPreferences(prefs);
}

public static void reloadPreferences() {
Log.d(TAG,"Reloaded profile preferences");
reloadPreferences(PreferenceManager.getDefaultSharedPreferences(xdrip.getAppContext()));
}

public static synchronized void reloadPreferences(SharedPreferences prefs) {
validateTargetRange();
try {
Profile.setSensitivityDefault(tolerantParseDouble(prefs.getString("profile_insulin_sensitivity_default", "0")));
Expand Down Expand Up @@ -223,6 +235,7 @@ public static void reloadPreferences(SharedPreferences prefs) {
}
profileItemList = null;
populateProfile();
preferences_loaded = true;
}

private static double tolerantParseDouble(String str) throws NumberFormatException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public class BgGraphBuilder {
public static double capturePercentage = -1;
private int predictivehours = 0;
private boolean prediction_enabled = false;
private boolean simulation_enabled = false;
private static double avg1value = 0;
private static double avg2value = 0;
private static int avg1counter = 0;
Expand Down Expand Up @@ -187,7 +188,10 @@ public BgGraphBuilder(Context context, long start, long end, int numValues, bool
}
if (d)
Log.d(TAG, "Called timestamps: " + JoH.dateTimeText(start) + " -> " + JoH.dateTimeText(end));
this.prefs = PreferenceManager.getDefaultSharedPreferences(context);
prediction_enabled = show_prediction;
if (prediction_enabled)
simulation_enabled = prefs.getBoolean("simulations_enabled", true);
end_time = end / FUZZER;
start_time = start / FUZZER;

Expand All @@ -214,7 +218,6 @@ public BgGraphBuilder(Context context, long start, long end, int numValues, bool
calibrations = Calibration.latestForGraph(numValues, start, end);
treatments = Treatments.latestForGraph(numValues, start, end + (120 * 60 * 1000));
this.context = context;
this.prefs = PreferenceManager.getDefaultSharedPreferences(context);
this.highMark = tolerantParseDouble(prefs.getString("highValue", "170"));
this.lowMark = tolerantParseDouble(prefs.getString("lowValue", "70"));
this.doMgdl = (prefs.getString("units", "mgdl").equals("mgdl"));
Expand Down Expand Up @@ -1228,7 +1231,7 @@ private synchronized void addBgReadingValues(final boolean simple) {
double lasttimestamp = 0;

// this can be optimised to oncreate and onchange
Profile.reloadPreferences(prefs); // TODO handle this better now we use profile time blocks
Profile.reloadPreferencesIfNeeded(prefs); // TODO handle this better now we use profile time blocks


try {
Expand Down Expand Up @@ -1256,7 +1259,7 @@ private synchronized void addBgReadingValues(final boolean simple) {
final double relaxed_predicted_bg_limit = initial_predicted_bg * 1.20;
final double cob_insulin_max_draw_value = highMark * 1.20;
// final List<Iob> iobinfo_old = Treatments.ioBForGraph(numValues, (start_time * FUZZER));
final List<Iob> iobinfo = Treatments.ioBForGraph_new(NUM_VALUES, (start_time * FUZZER)); // for test
final List<Iob> iobinfo = (simulation_enabled) ? Treatments.ioBForGraph_new(NUM_VALUES, (start_time * FUZZER)) : null; // for test

long fuzzed_timestamp = (long) end_time; // initial value in case there are no iob records
if (d)
Expand All @@ -1267,7 +1270,7 @@ private synchronized void addBgReadingValues(final boolean simple) {
Log.d(TAG, "initial Fuzzed end timestamp: " + android.text.format.DateFormat.format("yyyy-MM-dd HH:mm:ss", fuzzed_timestamp * FUZZER));
if (d)
Log.d(TAG, "initial Fuzzed start timestamp: " + android.text.format.DateFormat.format("yyyy-MM-dd HH:mm:ss", (long) start_time * FUZZER));
if ((iobinfo != null) && (prediction_enabled)) {
if ((iobinfo != null) && (prediction_enabled) && (simulation_enabled)) {

double predict_weight = 0.1;

Expand Down Expand Up @@ -1374,7 +1377,7 @@ private synchronized void addBgReadingValues(final boolean simple) {
}

double[] evaluation;
if (prediction_enabled) {
if (prediction_enabled && simulation_enabled) {
if (doMgdl) {
// These routines need to understand how the profile is defined to use native instead of scaled
evaluation = Profile.evaluateEndGameMmol(predictedbg, lasttimestamp * FUZZER, end_time * FUZZER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public static List<BgSendQueue> cleanQueue() {
.from(BgSendQueue.class)
.where("mongo_success = ?", true)
.where("operation_type = ?", "create")
.limit(5000)
.execute();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ public void onPause() {
super.onPause();

if (dataChanged) {
Profile.reloadPreferences();
Intent intent = new Intent(ProfileEditor.this, Preferences.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("refresh", "");
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/xml/xdrip_plus_prefs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,13 @@
<PreferenceCategory
android:key="xdrip_plus_profile_category"
android:title="@string/xdrip_plus_prediction_settings">
<SwitchPreference
android:defaultValue="true"
android:key="simulations_enabled"
android:summary="Display mathamatical simulations based on profile data and carbs/insulin logs"
android:switchTextOff="@string/short_off_text_for_switches"
android:switchTextOn="@string/short_on_text_for_switches"
android:title="Predictive Simulations" />

<Preference
android:key="profile_carb_ratio_default"
Expand Down

0 comments on commit 23869fa

Please sign in to comment.