From a66ece9af5c81c12ade63f8a8e09e215f968bd1f Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 12 Jun 2016 10:53:19 +0200 Subject: [PATCH] Lowsuspend finished --- .../androidaps/db/DatabaseHelper.java | 12 + .../androidaps/plugins/APSResult.java | 23 ++ .../LowSuspend/LowSuspendFragment.java | 161 ++++++++++++- .../OpenAPSMA/DetermineBasalResult.java | 1 + .../plugins/OpenAPSMA/OpenAPSMAFragment.java | 16 +- .../main/res/layout/lowsuspend_fragment.xml | 120 ++++++++++ .../main/res/layout/openapsma_fragment.xml | 214 ++++++++++-------- app/src/main/res/values/strings.xml | 14 ++ 8 files changed, 460 insertions(+), 101 deletions(-) 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 e21d040991..a5a122b5ca 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -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() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/APSResult.java b/app/src/main/java/info/nightscout/androidaps/plugins/APSResult.java index b5d2b42724..ad9402393d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/APSResult.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/APSResult.java @@ -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); + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/LowSuspend/LowSuspendFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/LowSuspend/LowSuspendFragment.java index 492ca5e397..ef72636f0c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/LowSuspend/LowSuspendFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/LowSuspend/LowSuspendFragment.java @@ -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(); + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalResult.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalResult.java index c45a69be8d..b7927035a3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalResult.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalResult.java @@ -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; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java index 5cd97d22ee..258625d077 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java @@ -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(); diff --git a/app/src/main/res/layout/lowsuspend_fragment.xml b/app/src/main/res/layout/lowsuspend_fragment.xml index f8274271a6..f4617f950b 100644 --- a/app/src/main/res/layout/lowsuspend_fragment.xml +++ b/app/src/main/res/layout/lowsuspend_fragment.xml @@ -4,4 +4,124 @@ android:layout_height="match_parent" tools:context="info.nightscout.androidaps.plugins.LowSuspend.LowSuspendFragment"> + + +