Merge pull request #14 from MilosKozak/master

save state on openapsma
This commit is contained in:
LadyViktoria 2016-06-17 14:46:05 +02:00 committed by GitHub
commit a176aecf38
7 changed files with 201 additions and 51 deletions

View file

@ -38,7 +38,7 @@ public class MainActivity extends AppCompatActivity {
private ViewPager mPager; private ViewPager mPager;
private static TabPageAdapter pageAdapter; private static TabPageAdapter pageAdapter;
private static ArrayList<PluginBase> pluginsList = new ArrayList<PluginBase>(); private static ArrayList<PluginBase> pluginsList = null;
private static ConfigBuilderFragment configBuilderFragment; private static ConfigBuilderFragment configBuilderFragment;
@ -53,25 +53,27 @@ public class MainActivity extends AppCompatActivity {
if (Config.logFunctionCalls) if (Config.logFunctionCalls)
log.debug("onCreate"); log.debug("onCreate");
// Register all tabs in app here if (pluginsList == null) {
pluginsList.add(OverviewFragment.newInstance()); pluginsList = new ArrayList<PluginBase>();
pluginsList.add(VirtualPumpFragment.newInstance()); // Register all tabs in app here
pluginsList.add(LowSuspendFragment.newInstance()); pluginsList.add(OverviewFragment.newInstance());
pluginsList.add(OpenAPSMAFragment.newInstance()); pluginsList.add(VirtualPumpFragment.newInstance());
pluginsList.add(NSProfileViewerFragment.newInstance()); pluginsList.add(LowSuspendFragment.newInstance());
pluginsList.add(SimpleProfileFragment.newInstance()); pluginsList.add(OpenAPSMAFragment.newInstance());
pluginsList.add(TreatmentsFragment.newInstance()); pluginsList.add(NSProfileViewerFragment.newInstance());
pluginsList.add(TempBasalsFragment.newInstance()); pluginsList.add(SimpleProfileFragment.newInstance());
pluginsList.add(ObjectivesFragment.newInstance()); pluginsList.add(TreatmentsFragment.newInstance());
pluginsList.add(configBuilderFragment = ConfigBuilderFragment.newInstance()); pluginsList.add(TempBasalsFragment.newInstance());
pluginsList.add(ObjectivesFragment.newInstance());
pluginsList.add(configBuilderFragment = ConfigBuilderFragment.newInstance());
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar = (Toolbar) findViewById(R.id.toolbar); registerBus();
setSupportActionBar(toolbar);
registerBus(); configBuilderFragment.initialize();
setUpTabs(false);
configBuilderFragment.initialize(); }
setUpTabs(false);
} }
@Subscribe @Subscribe

View file

@ -8,6 +8,8 @@ import java.util.List;
import android.content.Context; import android.content.Context;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
@ -168,7 +170,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
/* /*
* Returns glucose_status for openAPS or null if no actual data available * Returns glucose_status for openAPS or null if no actual data available
*/ */
public static class GlucoseStatus { public static class GlucoseStatus implements Parcelable {
public double glucose = 0d; public double glucose = 0d;
public double delta = 0d; public double delta = 0d;
public double avgdelta = 0d; public double avgdelta = 0d;
@ -183,6 +185,35 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
context.getString(R.string.delta) + " " + formatNumber0decimalplaces.format(delta) + "\n" + context.getString(R.string.delta) + " " + formatNumber0decimalplaces.format(delta) + "\n" +
context.getString(R.string.avgdelta) + " " + formatNumber2decimalplaces.format(avgdelta); context.getString(R.string.avgdelta) + " " + formatNumber2decimalplaces.format(avgdelta);
} }
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeDouble(avgdelta);
dest.writeDouble(delta);
dest.writeDouble(glucose);
}
public final Parcelable.Creator<GlucoseStatus> CREATOR = new Parcelable.Creator<GlucoseStatus>() {
public GlucoseStatus createFromParcel(Parcel in) {
return new GlucoseStatus(in);
}
public GlucoseStatus[] newArray(int size) {
return new GlucoseStatus[size];
}
};
private GlucoseStatus(Parcel in) {
avgdelta = in.readDouble();
delta = in.readDouble();
glucose = in.readDouble();
}
public GlucoseStatus() {}
} }
@Nullable @Nullable

View file

@ -65,9 +65,7 @@ public class LowSuspendFragment extends Fragment implements View.OnClickListener
dest.writeInt(lastLowProjected ? 1 : 0); dest.writeInt(lastLowProjected ? 1 : 0);
dest.writeDouble(lastMinBg); dest.writeDouble(lastMinBg);
dest.writeString(lastUnits); dest.writeString(lastUnits);
dest.writeDouble(lastGlucoseStatus.avgdelta); dest.writeParcelable(lastGlucoseStatus, 0);
dest.writeDouble(lastGlucoseStatus.delta);
dest.writeDouble(lastGlucoseStatus.glucose);
dest.writeLong(lastAPSRun.getTime()); dest.writeLong(lastAPSRun.getTime());
dest.writeParcelable(lastAPSResult, 0); dest.writeParcelable(lastAPSResult, 0);
} }
@ -87,10 +85,7 @@ public class LowSuspendFragment extends Fragment implements View.OnClickListener
lastLowProjected = in.readInt() == 1; lastLowProjected = in.readInt() == 1;
lastMinBg = in.readDouble(); lastMinBg = in.readDouble();
lastUnits = in.readString(); lastUnits = in.readString();
lastGlucoseStatus = new DatabaseHelper.GlucoseStatus(); lastGlucoseStatus = in.readParcelable(DatabaseHelper.GlucoseStatus.class.getClassLoader());
lastGlucoseStatus.avgdelta = in.readDouble();
lastGlucoseStatus.delta = in.readDouble();
lastGlucoseStatus.glucose = in.readDouble();
lastAPSRun = new Date(in.readLong()); lastAPSRun = new Date(in.readLong());
lastAPSResult = in.readParcelable(APSResult.class.getClassLoader()); lastAPSResult = in.readParcelable(APSResult.class.getClassLoader());
} }

View file

@ -1,5 +1,8 @@
package info.nightscout.androidaps.plugins.OpenAPSMA; package info.nightscout.androidaps.plugins.OpenAPSMA;
import android.os.Parcel;
import android.os.Parcelable;
import com.eclipsesource.v8.JavaVoidCallback; import com.eclipsesource.v8.JavaVoidCallback;
import com.eclipsesource.v8.V8; import com.eclipsesource.v8.V8;
import com.eclipsesource.v8.V8Array; import com.eclipsesource.v8.V8Array;
@ -19,11 +22,11 @@ import info.nightscout.androidaps.plugins.ScriptReader;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment; import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment;
import info.nightscout.client.data.NSProfile; import info.nightscout.client.data.NSProfile;
public class DetermineBasalAdapterJS { public class DetermineBasalAdapterJS implements Parcelable {
private static Logger log = LoggerFactory.getLogger(DetermineBasalAdapterJS.class); private static Logger log = LoggerFactory.getLogger(DetermineBasalAdapterJS.class);
private final ScriptReader mScriptReader; private ScriptReader mScriptReader = null;
V8 mV8rt; V8 mV8rt;
private V8Object mProfile; private V8Object mProfile;
private V8Object mGlucoseStatus; private V8Object mGlucoseStatus;
@ -37,6 +40,54 @@ public class DetermineBasalAdapterJS {
private final String PARAM_profile = "profile"; private final String PARAM_profile = "profile";
private final String PARAM_meal_data = "meal_data"; private final String PARAM_meal_data = "meal_data";
private String storedCurrentTemp = null;
private String storedIobData = null;
private String storedGlucoseStatus = null;
private String storedProfile = null;
private String storedMeal_data = null;
/**
* Parcelable implementation
* result string for display only
**/
protected DetermineBasalAdapterJS(Parcel in) {
storedCurrentTemp = in.readString();
storedIobData = in.readString();
storedGlucoseStatus = in.readString();
storedProfile = in.readString();
storedMeal_data = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(storedCurrentTemp);
dest.writeString(storedIobData);
dest.writeString(storedGlucoseStatus);
dest.writeString(storedProfile);
dest.writeString(storedMeal_data);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<DetermineBasalAdapterJS> CREATOR = new Creator<DetermineBasalAdapterJS>() {
@Override
public DetermineBasalAdapterJS createFromParcel(Parcel in) {
return new DetermineBasalAdapterJS(in);
}
@Override
public DetermineBasalAdapterJS[] newArray(int size) {
return new DetermineBasalAdapterJS[size];
}
};
/**
* Main code
*/
public DetermineBasalAdapterJS(ScriptReader scriptReader) throws IOException { public DetermineBasalAdapterJS(ScriptReader scriptReader) throws IOException {
mV8rt = V8.createV8Runtime(); mV8rt = V8.createV8Runtime();
mScriptReader = scriptReader; mScriptReader = scriptReader;
@ -125,27 +176,35 @@ public class DetermineBasalAdapterJS {
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
// Store input params for Parcelable
storedGlucoseStatus = mV8rt.executeStringScript("JSON.stringify(" + PARAM_glucoseStatus + ");");
storedIobData = mV8rt.executeStringScript("JSON.stringify(" + PARAM_iobData + ");");
storedCurrentTemp = mV8rt.executeStringScript("JSON.stringify(" + PARAM_currentTemp + ");");
storedProfile = mV8rt.executeStringScript("JSON.stringify(" + PARAM_profile + ");");
storedMeal_data = mV8rt.executeStringScript("JSON.stringify(" + PARAM_meal_data + ");");
return result; return result;
} }
String getGlucoseStatusParam() { String getGlucoseStatusParam() {
return mV8rt.executeStringScript("JSON.stringify(" + PARAM_glucoseStatus + ");"); return storedGlucoseStatus;
} }
String getCurrentTempParam() { String getCurrentTempParam() {
return mV8rt.executeStringScript("JSON.stringify(" + PARAM_currentTemp + ");"); return storedCurrentTemp;
} }
String getIobDataParam() { String getIobDataParam() {
return mV8rt.executeStringScript("JSON.stringify(" + PARAM_iobData + ");"); return storedIobData;
} }
String getProfileParam() { String getProfileParam() {
return mV8rt.executeStringScript("JSON.stringify(" + PARAM_profile + ");"); return storedProfile;
} }
String getMealDataParam() { String getMealDataParam() {
return mV8rt.executeStringScript("JSON.stringify(" + PARAM_meal_data + ");"); return storedMeal_data;
} }
private void loadScript() throws IOException { private void loadScript() throws IOException {

View file

@ -3,6 +3,8 @@ package info.nightscout.androidaps.plugins.OpenAPSMA;
import android.app.Activity; import android.app.Activity;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -53,8 +55,45 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
TextView resultView; TextView resultView;
TextView requestView; TextView requestView;
Date lastAPSRun = null; // last values
APSResult lastAPSResult = null; class LastRun implements Parcelable {
DetermineBasalAdapterJS lastDetermineBasalAdapterJS = null;
Date lastAPSRun = null;
DetermineBasalResult lastAPSResult = null;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(lastDetermineBasalAdapterJS, 0);
dest.writeParcelable(lastAPSResult, 0);
dest.writeLong(lastAPSRun.getTime());
dest.writeParcelable(lastAPSResult, 0);
}
public final Parcelable.Creator<LastRun> CREATOR = new Parcelable.Creator<LastRun>() {
public LastRun createFromParcel(Parcel in) {
return new LastRun(in);
}
public LastRun[] newArray(int size) {
return new LastRun[size];
}
};
private LastRun(Parcel in) {
lastDetermineBasalAdapterJS = in.readParcelable(DetermineBasalAdapterJS.class.getClassLoader());
lastAPSResult = in.readParcelable(DetermineBasalResult.class.getClassLoader());
lastAPSRun = new Date(in.readLong());
lastAPSResult = in.readParcelable(APSResult.class.getClassLoader());
}
public LastRun() {}
}
LastRun lastRun = null;
boolean fragmentEnabled = false; boolean fragmentEnabled = false;
boolean fragmentVisible = true; boolean fragmentVisible = true;
@ -101,12 +140,12 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
@Override @Override
public APSResult getLastAPSResult() { public APSResult getLastAPSResult() {
return lastAPSResult; return lastRun.lastAPSResult;
} }
@Override @Override
public Date getLastAPSRun() { public Date getLastAPSRun() {
return lastAPSRun; return lastRun.lastAPSRun;
} }
public static OpenAPSMAFragment newInstance() { public static OpenAPSMAFragment newInstance() {
@ -135,9 +174,19 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
resultView = (TextView) view.findViewById(R.id.openapsma_result); resultView = (TextView) view.findViewById(R.id.openapsma_result);
requestView = (TextView) view.findViewById(R.id.openapsma_request); requestView = (TextView) view.findViewById(R.id.openapsma_request);
if (savedInstanceState != null) {
lastRun = savedInstanceState.getParcelable("lastrun");
}
updateGUI();
return view; return view;
} }
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("lastrun", lastRun);
}
private void registerBus() { private void registerBus() {
try { try {
MainApp.bus().unregister(this); MainApp.bus().unregister(this);
@ -227,6 +276,8 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
minBgDefault = "5"; minBgDefault = "5";
} }
Date now = new Date();
// TODO: objectives limits // TODO: objectives limits
double maxIob = Double.parseDouble(SP.getString("max_iob", "1.5").replace(",", ".")); double maxIob = Double.parseDouble(SP.getString("max_iob", "1.5").replace(",", "."));
double maxBasal = Double.parseDouble(SP.getString("max_basal", "1").replace(",", ".")); double maxBasal = Double.parseDouble(SP.getString("max_basal", "1").replace(",", "."));
@ -247,29 +298,37 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
determineBasalAdapterJS.setData(profile, maxIob, maxBasal, minBg, maxBg, pump, iobTotal, glucoseStatus, mealData); determineBasalAdapterJS.setData(profile, maxIob, maxBasal, minBg, maxBg, pump, iobTotal, glucoseStatus, mealData);
glucoseStatusView.setText(determineBasalAdapterJS.getGlucoseStatusParam());
currentTempView.setText(determineBasalAdapterJS.getCurrentTempParam());
iobDataView.setText(determineBasalAdapterJS.getIobDataParam());
profileView.setText(determineBasalAdapterJS.getProfileParam());
mealDataView.setText(determineBasalAdapterJS.getMealDataParam());
DetermineBasalResult determineBasalResult = determineBasalAdapterJS.invoke(); DetermineBasalResult determineBasalResult = determineBasalAdapterJS.invoke();
resultView.setText(determineBasalResult.json.toString());
requestView.setText(determineBasalResult.toString());
lastRunView.setText(new Date().toLocaleString());
determineBasalAdapterJS.release(); determineBasalAdapterJS.release();
try { try {
determineBasalResult.json.put("timestamp", DateUtil.toISOString(new Date())); determineBasalResult.json.put("timestamp", DateUtil.toISOString(now));
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
lastAPSResult = determineBasalResult;
lastAPSRun = new Date(); lastRun = new LastRun();
lastRun.lastDetermineBasalAdapterJS = determineBasalAdapterJS;
lastRun.lastAPSResult = determineBasalResult;
lastRun.lastAPSRun = now;
updateGUI();
//deviceStatus.suggested = determineBasalResult.json; //deviceStatus.suggested = determineBasalResult.json;
}
void updateGUI() {
if (lastRun != null) {
glucoseStatusView.setText(lastRun.lastDetermineBasalAdapterJS.getGlucoseStatusParam());
currentTempView.setText(lastRun.lastDetermineBasalAdapterJS.getCurrentTempParam());
iobDataView.setText(lastRun.lastDetermineBasalAdapterJS.getIobDataParam());
profileView.setText(lastRun.lastDetermineBasalAdapterJS.getProfileParam());
mealDataView.setText(lastRun.lastDetermineBasalAdapterJS.getMealDataParam());
resultView.setText(lastRun.lastAPSResult.json.toString());
requestView.setText(lastRun.lastAPSResult.toString());
lastRunView.setText(lastRun.lastAPSRun.toLocaleString());
}
} }
} }

View file

@ -268,8 +268,12 @@ public class OverviewFragment extends Fragment implements PluginBase {
public void setUserVisibleHint(boolean isVisibleToUser) { public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser); super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) if (isVisibleToUser) {
updateData(); updateData();
log.debug("Overview visible");
} else {
log.debug("Overview hidden");
}
} }
} }

View file

@ -5,7 +5,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.1.0' classpath 'com.android.tools.build:gradle:2.1.2'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files