Lowsuspend finished

This commit is contained in:
Milos Kozak 2016-06-12 10:53:19 +02:00
parent 743350f4b3
commit a66ece9af5
8 changed files with 460 additions and 101 deletions

View file

@ -1,6 +1,7 @@
package info.nightscout.androidaps.db;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -167,6 +168,17 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
public double glucose = 0d;
public double delta = 0d;
public double avgdelta = 0d;
@Override
public String toString() {
DecimalFormat formatNumber0decimalplaces = new DecimalFormat("0");
DecimalFormat formatNumber2decimalplaces = new DecimalFormat("0.00");
return "{\"glucose\"=" + formatNumber0decimalplaces.format(glucose) +
",\"delta\"=" + formatNumber0decimalplaces.format(delta) +
",\"avgdelta\"=" + formatNumber2decimalplaces.format(avgdelta) +
"}";
}
}
public GlucoseStatus getGlucoseStatusData() {

View file

@ -1,5 +1,13 @@
package info.nightscout.androidaps.plugins;
import android.content.Context;
import java.text.DecimalFormat;
import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
/**
* Created by mike on 09.06.2016.
*/
@ -8,4 +16,19 @@ public class APSResult {
public double rate;
public int duration;
public boolean changeRequested = false;
@Override
public String toString() {
Context context = MainApp.instance().getApplicationContext();
DecimalFormat formatNumber0decimalplaces = new DecimalFormat("0");
DecimalFormat formatNumber2decimalplaces = new DecimalFormat("0.00");
if (changeRequested)
return context.getString(R.string.rate) + " " + formatNumber2decimalplaces.format(rate) + " U/h\n" +
context.getString(R.string.duration) + " " + formatNumber0decimalplaces.format(duration) + " min\n" +
context.getString(R.string.reason) + " " + reason;
else
return MainApp.instance().getApplicationContext().getString(R.string.nochangerequested);
}
}

View file

@ -1,18 +1,51 @@
package info.nightscout.androidaps.plugins.LowSuspend;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.DecimalFormat;
import java.util.Date;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Pump;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.plugins.APSBase;
import info.nightscout.androidaps.plugins.APSResult;
import info.nightscout.androidaps.plugins.PluginBase;
import info.nightscout.client.data.NSProfile;
public class LowSuspendFragment extends Fragment implements View.OnClickListener, PluginBase, APSBase {
private static Logger log = LoggerFactory.getLogger(LowSuspendFragment.class);
Button run;
TextView lastRunView;
TextView glucoseStatusView;
TextView minBgView;
TextView resultView;
TextView requestView;
Date lastAPSRun = null;
APSResult lastAPSResult = null;
public class LowSuspendFragment extends Fragment implements PluginBase {
@Override
public int getType() {
@ -24,6 +57,16 @@ public class LowSuspendFragment extends Fragment implements PluginBase {
return true;
}
@Override
public APSResult getLastAPSResult() {
return lastAPSResult;
}
@Override
public Date getLastAPSRun() {
return lastAPSRun;
}
public static LowSuspendFragment newInstance() {
LowSuspendFragment fragment = new LowSuspendFragment();
return fragment;
@ -38,8 +81,17 @@ public class LowSuspendFragment extends Fragment implements PluginBase {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.lowsuspend_fragment, container, false);
View view = inflater.inflate(R.layout.lowsuspend_fragment, container, false);
run = (Button) view.findViewById(R.id.lowsuspend_run);
run.setOnClickListener(this);
lastRunView = (TextView) view.findViewById(R.id.lowsuspend_lastrun);
glucoseStatusView = (TextView) view.findViewById(R.id.lowsuspend_glucosestatus);
minBgView = (TextView) view.findViewById(R.id.lowsuspend_minbg);
resultView = (TextView) view.findViewById(R.id.lowsuspend_result);
requestView = (TextView) view.findViewById(R.id.lowsuspend_request);
return view;
}
private void registerBus() {
@ -50,4 +102,107 @@ public class LowSuspendFragment extends Fragment implements PluginBase {
}
MainApp.bus().register(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.lowsuspend_run:
invoke();
break;
}
}
@Subscribe
public void onStatusEvent(final EventNewBG ev) {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
invoke();
}
});
else
log.debug("EventNewBG: Activity is null");
}
@Override
public void invoke() {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
DatabaseHelper.GlucoseStatus glucoseStatus = MainApp.getDbHelper().getGlucoseStatusData();
DecimalFormat formatNumber1decimalplaces = new DecimalFormat("0.0");
NSProfile profile = MainApp.getNSProfile();
Pump pump = MainApp.getActivePump();
if (glucoseStatus == null) {
resultView.setText(getString(R.string.openapsma_noglucosedata));
if (Config.logAPSResult) log.debug(getString(R.string.openapsma_noglucosedata));
return;
}
if (profile == null) {
resultView.setText(getString(R.string.openapsma_noprofile));
if (Config.logAPSResult) log.debug(getString(R.string.openapsma_noprofile));
return;
}
if (pump == null) {
resultView.setText(getString(R.string.openapsma_nopump));
if (Config.logAPSResult) log.debug(getString(R.string.openapsma_nopump));
return;
}
String minBgDefault = "90";
if (!MainApp.getNSProfile().getUnits().equals(Constants.MGDL)) {
minBgDefault = "5";
}
double minBg = NSProfile.toMgdl(Double.parseDouble(SP.getString("min_bg", minBgDefault).replace(",", ".")), profile.getUnits());
boolean lowProjected = (glucoseStatus.glucose + 6.0 * glucoseStatus.avgdelta) < minBg;
boolean low = glucoseStatus.glucose < minBg;
APSResult request = new APSResult();
Double baseBasalRate = pump.getBaseBasalRate();
boolean isTempBasalInProgress = pump.isTempBasalInProgress();
Double tempBasalRate = pump.getTempBasalAbsoluteRate();
if (low && !lowProjected) {
if (!isTempBasalInProgress || tempBasalRate != 0d) {
request.changeRequested = true;
request.rate = 0d;
request.duration = 30;
request.reason = getString(R.string.lowsuspend_lowmessage);
} else {
request.changeRequested = false;
request.reason = getString(R.string.nochangerequested);
}
} else if (lowProjected) {
if (!isTempBasalInProgress || tempBasalRate != 0d) {
request.changeRequested = true;
request.rate = 0d;
request.duration = 30;
request.reason = getString(R.string.lowsuspend_lowprojectedmessage);
} else {
request.changeRequested = false;
request.reason = getString(R.string.nochangerequested);
}
} else if (tempBasalRate == 0d){
request.changeRequested = true;
request.rate = baseBasalRate;
request.duration = 30;
request.reason = getString(R.string.lowsuspend_cancelmessage);
} else {
request.changeRequested = false;
request.reason = getString(R.string.nochangerequested);
}
glucoseStatusView.setText(glucoseStatus.toString());
minBgView.setText(formatNumber1decimalplaces.format(minBg) + " " + profile.getUnits());
resultView.setText(getString(R.string.lowsuspend_low) + " " + low + "\n" + getString(R.string.lowsuspend_lowprojected) + " " + lowProjected);
requestView.setText(request.toString());
lastRunView.setText(new Date().toLocaleString());
lastAPSResult = request;
lastAPSRun = new Date();
}
}

View file

@ -20,6 +20,7 @@ public class DetermineBasalResult extends APSResult {
snoozeBG = result.getDouble("snoozeBG");
if(result.contains("rate")) {
rate = result.getDouble("rate");
if (rate < 0d) rate = 0d;
changeRequested = true;
} else {
rate = -1;

View file

@ -49,6 +49,7 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
TextView profileView;
TextView mealDataView;
TextView resultView;
TextView requestView;
Date lastAPSRun = null;
APSResult lastAPSResult = null;
@ -98,6 +99,7 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
profileView = (TextView) view.findViewById(R.id.openapsma_profile);
mealDataView = (TextView) view.findViewById(R.id.openapsma_mealdata);
resultView = (TextView) view.findViewById(R.id.openapsma_result);
requestView = (TextView) view.findViewById(R.id.openapsma_request);
return view;
}
@ -151,9 +153,7 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
@Override
public void invoke() {
// private DatermineBasalResult openAps(int glucoseValue, int delta, double deltaAvg15min, StatusEvent status, LowSuspendStatus lowSuspendStatus, IobTotal iobTotal, CarbCalc.Meal mealdata) {
DetermineBasalAdapterJS determineBasalAdapterJS = null;
DetermineBasalAdapterJS determineBasalAdapterJS = null;
try {
determineBasalAdapterJS = new DetermineBasalAdapterJS(new ScriptReader(MainApp.instance().getBaseContext()));
} catch (IOException e) {
@ -166,17 +166,20 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
Pump pump = MainApp.getActivePump();
if (glucoseStatus == null) {
if (Config.logAPSResult) log.debug("No glucose data available");
resultView.setText(getString(R.string.openapsma_noglucosedata));
if (Config.logAPSResult) log.debug(getString(R.string.openapsma_noglucosedata));
return;
}
if (profile == null) {
if (Config.logAPSResult) log.debug("No profile available");
resultView.setText(getString(R.string.openapsma_noprofile));
if (Config.logAPSResult) log.debug(getString(R.string.openapsma_noprofile));
return;
}
if (pump == null) {
if (Config.logAPSResult) log.debug("No pump available");
resultView.setText(getString(R.string.openapsma_nopump));
if (Config.logAPSResult) log.debug(getString(R.string.openapsma_nopump));
return;
}
@ -217,6 +220,7 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
DetermineBasalResult determineBasalResult = determineBasalAdapterJS.invoke();
resultView.setText(determineBasalResult.json.toString());
requestView.setText(determineBasalResult.toString());
lastRunView.setText(new Date().toLocaleString());
determineBasalAdapterJS.release();

View file

@ -4,4 +4,124 @@
android:layout_height="match_parent"
tools:context="info.nightscout.androidaps.plugins.LowSuspend.LowSuspendFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/lowsuspend_run"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/openapsma_run" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:background="#3e3d3d"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_lastrun_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lowsuspend_lastrun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#3e3d3d"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_inputparameters_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_glucosestatus_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/lowsuspend_glucosestatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_minbg_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/lowsuspend_minbg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:background="#3e3d3d"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_result_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lowsuspend_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:background="#3e3d3d"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_request_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lowsuspend_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
</FrameLayout>

View file

@ -6,137 +6,167 @@
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/openapsma_run"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/openapsma_run"
android:id="@+id/openapsma_run" />
android:text="@string/openapsma_run" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:background="#3e3d3d">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/openapsma_lastrun_label" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/openapsma_lastrun"
android:layout_marginLeft="10dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#3e3d3d"
android:layout_margin="10dp">
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/openapsma_inputparameters_label" />
android:text="@string/openapsma_lastrun_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/openapsma_lastrun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/openapsma_glucosestatus_label" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/openapsma_glucosestatus"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/openapsma_currenttemp_label" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/openapsma_currenttemp"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/openapsma_iobdata_label" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/openapsma_iobdata"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/openapsma_profile_label" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/openapsma_profile"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/openapsma_mealdata_label" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/openapsma_mealdata"
android:layout_marginLeft="10dp" />
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#3e3d3d"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_inputparameters_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_glucosestatus_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/openapsma_glucosestatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_currenttemp_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/openapsma_currenttemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_iobdata_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/openapsma_iobdata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_profile_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/openapsma_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_mealdata_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/openapsma_mealdata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:background="#3e3d3d">
android:background="#3e3d3d"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/openapsma_result_label" />
android:text="@string/openapsma_result_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/openapsma_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/openapsma_result"
android:layout_marginLeft="10dp" />
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:background="#3e3d3d"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openapsma_request_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/openapsma_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>

View file

@ -69,5 +69,19 @@
<string name="openapsma_profile_label">Profile</string>
<string name="openapsma_mealdata_label">Meal data</string>
<string name="openapsma_result_label">Result</string>
<string name="openapsma_noglucosedata">No glucose data available</string>
<string name="openapsma_noprofile">No profile available</string>
<string name="openapsma_nopump">No pump available</string>
<string name="nochangerequested">No change requested</string>
<string name="openapsma_request_label">Request</string>
<string name="openapsma_minbg_label">Low target</string>
<string name="lowsuspend_lowmessage">LOW: Temp basal 0%</string>
<string name="lowsuspend_lowprojectedmessage">LOW PROJECTED: Temp basal 0%</string>
<string name="lowsuspend_cancelmessage">LowSuspend: Cancel low temp</string>
<string name="lowsuspend_low">Low:</string>
<string name="lowsuspend_lowprojected">Low projected:</string>
<string name="rate">Rate:</string>
<string name="duration">Duration:</string>
<string name="reason">Reason:</string>
</resources>