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 static TabPageAdapter pageAdapter;
private static ArrayList<PluginBase> pluginsList = new ArrayList<PluginBase>();
private static ArrayList<PluginBase> pluginsList = null;
private static ConfigBuilderFragment configBuilderFragment;
@ -53,6 +53,8 @@ public class MainActivity extends AppCompatActivity {
if (Config.logFunctionCalls)
log.debug("onCreate");
if (pluginsList == null) {
pluginsList = new ArrayList<PluginBase>();
// Register all tabs in app here
pluginsList.add(OverviewFragment.newInstance());
pluginsList.add(VirtualPumpFragment.newInstance());
@ -64,7 +66,6 @@ public class MainActivity extends AppCompatActivity {
pluginsList.add(TempBasalsFragment.newInstance());
pluginsList.add(ObjectivesFragment.newInstance());
pluginsList.add(configBuilderFragment = ConfigBuilderFragment.newInstance());
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
@ -73,6 +74,7 @@ public class MainActivity extends AppCompatActivity {
configBuilderFragment.initialize();
setUpTabs(false);
}
}
@Subscribe
public void onStatusEvent(final EventRefreshGui ev) {

View file

@ -8,6 +8,8 @@ import java.util.List;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
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
*/
public static class GlucoseStatus {
public static class GlucoseStatus implements Parcelable {
public double glucose = 0d;
public double delta = 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.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

View file

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

View file

@ -1,5 +1,8 @@
package info.nightscout.androidaps.plugins.OpenAPSMA;
import android.os.Parcel;
import android.os.Parcelable;
import com.eclipsesource.v8.JavaVoidCallback;
import com.eclipsesource.v8.V8;
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.client.data.NSProfile;
public class DetermineBasalAdapterJS {
public class DetermineBasalAdapterJS implements Parcelable {
private static Logger log = LoggerFactory.getLogger(DetermineBasalAdapterJS.class);
private final ScriptReader mScriptReader;
private ScriptReader mScriptReader = null;
V8 mV8rt;
private V8Object mProfile;
private V8Object mGlucoseStatus;
@ -37,6 +40,54 @@ public class DetermineBasalAdapterJS {
private final String PARAM_profile = "profile";
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 {
mV8rt = V8.createV8Runtime();
mScriptReader = scriptReader;
@ -125,27 +176,35 @@ public class DetermineBasalAdapterJS {
} catch (JSONException e) {
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;
}
String getGlucoseStatusParam() {
return mV8rt.executeStringScript("JSON.stringify(" + PARAM_glucoseStatus + ");");
return storedGlucoseStatus;
}
String getCurrentTempParam() {
return mV8rt.executeStringScript("JSON.stringify(" + PARAM_currentTemp + ");");
return storedCurrentTemp;
}
String getIobDataParam() {
return mV8rt.executeStringScript("JSON.stringify(" + PARAM_iobData + ");");
return storedIobData;
}
String getProfileParam() {
return mV8rt.executeStringScript("JSON.stringify(" + PARAM_profile + ");");
return storedProfile;
}
String getMealDataParam() {
return mV8rt.executeStringScript("JSON.stringify(" + PARAM_meal_data + ");");
return storedMeal_data;
}
private void loadScript() throws IOException {

View file

@ -3,6 +3,8 @@ package info.nightscout.androidaps.plugins.OpenAPSMA;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
@ -53,8 +55,45 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
TextView resultView;
TextView requestView;
// last values
class LastRun implements Parcelable {
DetermineBasalAdapterJS lastDetermineBasalAdapterJS = null;
Date lastAPSRun = null;
APSResult lastAPSResult = 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 fragmentVisible = true;
@ -101,12 +140,12 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
@Override
public APSResult getLastAPSResult() {
return lastAPSResult;
return lastRun.lastAPSResult;
}
@Override
public Date getLastAPSRun() {
return lastAPSRun;
return lastRun.lastAPSRun;
}
public static OpenAPSMAFragment newInstance() {
@ -135,9 +174,19 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
resultView = (TextView) view.findViewById(R.id.openapsma_result);
requestView = (TextView) view.findViewById(R.id.openapsma_request);
if (savedInstanceState != null) {
lastRun = savedInstanceState.getParcelable("lastrun");
}
updateGUI();
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("lastrun", lastRun);
}
private void registerBus() {
try {
MainApp.bus().unregister(this);
@ -227,6 +276,8 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
minBgDefault = "5";
}
Date now = new Date();
// TODO: objectives limits
double maxIob = Double.parseDouble(SP.getString("max_iob", "1.5").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);
glucoseStatusView.setText(determineBasalAdapterJS.getGlucoseStatusParam());
currentTempView.setText(determineBasalAdapterJS.getCurrentTempParam());
iobDataView.setText(determineBasalAdapterJS.getIobDataParam());
profileView.setText(determineBasalAdapterJS.getProfileParam());
mealDataView.setText(determineBasalAdapterJS.getMealDataParam());
DetermineBasalResult determineBasalResult = determineBasalAdapterJS.invoke();
resultView.setText(determineBasalResult.json.toString());
requestView.setText(determineBasalResult.toString());
lastRunView.setText(new Date().toLocaleString());
determineBasalAdapterJS.release();
try {
determineBasalResult.json.put("timestamp", DateUtil.toISOString(new Date()));
determineBasalResult.json.put("timestamp", DateUtil.toISOString(now));
} catch (JSONException e) {
e.printStackTrace();
}
lastAPSResult = determineBasalResult;
lastAPSRun = new Date();
lastRun = new LastRun();
lastRun.lastDetermineBasalAdapterJS = determineBasalAdapterJS;
lastRun.lastAPSResult = determineBasalResult;
lastRun.lastAPSRun = now;
updateGUI();
//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) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser)
if (isVisibleToUser) {
updateData();
log.debug("Overview visible");
} else {
log.debug("Overview hidden");
}
}
}

View file

@ -5,7 +5,7 @@ buildscript {
jcenter()
}
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
// in the individual module build.gradle files