From a57409c388e485c2d47dc4d208c89cf16af938f2 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 23 Jun 2016 17:07:38 +0200 Subject: [PATCH] temp basal rendering --- .../info/nightscout/androidaps/Config.java | 1 + .../nightscout/androidaps/data/Result.java | 6 +- .../androidaps/db/DatabaseHelper.java | 2 +- .../nightscout/androidaps/db/TempBasal.java | 20 +++- .../interfaces/TempBasalsInterface.java | 5 + .../LowSuspend/LowSuspendFragment.java | 3 +- .../plugins/OpenAPSMA/OpenAPSMAFragment.java | 9 +- .../Dialogs/NewExtendedBolusDialog.java | 5 +- .../Overview/Dialogs/NewTempBasalDialog.java | 4 +- .../Overview/Dialogs/NewTreatmentDialog.java | 7 +- .../Overview/Dialogs/WizardDialog.java | 13 +- .../plugins/Overview/OverviewFragment.java | 112 ++++++++++++++---- .../SafetyFragment/SafetyFragment.java | 7 +- .../SimpleProfile/SimpleProfileFragment.java | 15 +-- .../TempBasals/TempBasalsFragment.java | 21 +++- .../VirtualPump/VirtualPumpFragment.java | 64 ++++++---- .../java/info/nightscout/utils/SafeParse.java | 26 ++++ 17 files changed, 233 insertions(+), 87 deletions(-) create mode 100644 app/src/main/java/info/nightscout/utils/SafeParse.java diff --git a/app/src/main/java/info/nightscout/androidaps/Config.java b/app/src/main/java/info/nightscout/androidaps/Config.java index d81a8cd4a0..976b8f8f82 100644 --- a/app/src/main/java/info/nightscout/androidaps/Config.java +++ b/app/src/main/java/info/nightscout/androidaps/Config.java @@ -20,6 +20,7 @@ public class Config { public static final boolean logPrefsChange = true; public static final boolean logConfigBuilder = true; public static final boolean logConstraintsChanges = true; + public static final boolean logTempBasalsCut = true; // Developing mode only - never turn on // TODO: remove fakeGlucoseData diff --git a/app/src/main/java/info/nightscout/androidaps/data/Result.java b/app/src/main/java/info/nightscout/androidaps/data/Result.java index a079db7d52..92af8f91b1 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/Result.java +++ b/app/src/main/java/info/nightscout/androidaps/data/Result.java @@ -13,6 +13,7 @@ public class Result extends Object implements Parcelable{ public Double absolute = -1d; // absolute rate [U/h] , isPercent = false public Integer percent = -1; // percent of current basal [%] (100% = current basal), isPercent = true public boolean isPercent = false; // if true percent is used, otherwise absolute + public boolean isTempCancel = false; // if true we are caceling temp basal // Result of treatment delivery public Double bolusDelivered = 0d; // real value of delivered insulin public Integer carbsDelivered = 0; // real value of delivered carbs @@ -24,7 +25,10 @@ public class Result extends Object implements Parcelable{ public String toString() { String ret = "Success: " + success; if (enacted) { - if (isPercent) { + if (isTempCancel) { + ret += "\nEnacted: " + enacted + "\nComment: " + comment + "\n" + + "Temp cancel"; + } else if (isPercent) { ret += "\nEnacted: " + enacted + "\nComment: " + comment + "\nDuration: " + duration + " min\nPercent: " + percent + "%"; } else { ret += "\nEnacted: " + enacted + "\nComment: " + comment + "\nDuration: " + duration + " min\nAbsolute: " + absolute + " U/h"; 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 30ef491185..62ec418503 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -181,7 +181,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { Dao daoBgreadings = getDaoBgReadings(); List bgReadings; QueryBuilder queryBuilder = daoBgreadings.queryBuilder(); - queryBuilder.orderBy("timeIndex", false); + queryBuilder.orderBy("timeIndex", true); Where where = queryBuilder.where(); where.ge("timeIndex", (long) Math.ceil(mills / 60000d)); PreparedQuery preparedQuery = queryBuilder.prepare(); diff --git a/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java b/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java index c4b9d0a0c4..ba2396fb4b 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java +++ b/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java @@ -145,15 +145,27 @@ public class TempBasal { } public boolean isInProgress() { - long now = new Date().getTime(); - if (timeStart.getTime() > now) return false; // in the future + return isInProgress(new Date()); + } + + public double tempBasalConvertedToAbsolute() { + if (isAbsolute) return absolute; + else { + NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); + double absval = profile.getBasal(NSProfile.secondsFromMidnight()) * percent / 100; + return absval; + } + } + + public boolean isInProgress(Date time) { + if (timeStart.getTime() > time.getTime()) return false; // in the future if (timeEnd == null) { // open end - if (timeStart.getTime() < now && getPlannedTimeEnd().getTime() > now) + if (timeStart.getTime() < time.getTime() && getPlannedTimeEnd().getTime() > time.getTime()) return true; // in interval return false; } // closed end - if (timeStart.getTime() < now && timeEnd.getTime() > now) return true; // in interval + if (timeStart.getTime() < time.getTime() && timeEnd.getTime() > time.getTime()) return true; // in interval return false; } diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/TempBasalsInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/TempBasalsInterface.java index d2cceaa78b..158a34f7f0 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/TempBasalsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/TempBasalsInterface.java @@ -1,5 +1,8 @@ package info.nightscout.androidaps.interfaces; +import java.util.Date; + +import info.nightscout.androidaps.db.TempBasal; import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal; /** @@ -8,4 +11,6 @@ import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal; public interface TempBasalsInterface { void updateTotalIOB(); IobTotal getLastCalculation(); + + TempBasal getTempBasal (Date time); } 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 49015b5bc9..a264321ffc 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 @@ -30,6 +30,7 @@ import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.plugins.APSResult; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.client.data.NSProfile; +import info.nightscout.utils.SafeParse; /** * LOW SUSPEND ALGORITHM @@ -251,7 +252,7 @@ public class LowSuspendFragment extends Fragment implements View.OnClickListener minBgDefault = "5"; } - double minBg = NSProfile.toMgdl(Double.parseDouble(SP.getString("lowsuspend_lowthreshold", minBgDefault).replace(",", ".")), profile.getUnits()); + double minBg = NSProfile.toMgdl(SafeParse.stringToDouble(SP.getString("lowsuspend_lowthreshold", minBgDefault)), profile.getUnits()); boolean lowProjected = (glucoseStatus.glucose + 6.0 * glucoseStatus.avgdelta) < minBg; boolean low = glucoseStatus.glucose < minBg; 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 73c191f4ba..5c42b18f75 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 @@ -37,6 +37,7 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment; import info.nightscout.client.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.Round; +import info.nightscout.utils.SafeParse; public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, PluginBase, APSInterface { private static Logger log = LoggerFactory.getLogger(OpenAPSMAFragment.class); @@ -254,10 +255,10 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, Date now = new Date(); - double maxIob = Double.parseDouble(SP.getString("openapsma_max_iob", "1.5").replace(",", ".")); - double maxBasal = Double.parseDouble(SP.getString("openapsma_max_basal", "1").replace(",", ".")); - double minBg = NSProfile.toMgdl(Double.parseDouble(SP.getString("openapsma_min_bg", minBgDefault).replace(",", ".")), units); - double maxBg = NSProfile.toMgdl(Double.parseDouble(SP.getString("openapsma_max_bg", maxBgDefault).replace(",", ".")), units); + double maxIob = SafeParse.stringToDouble(SP.getString("openapsma_max_iob", "1.5")); + double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1")); + double minBg = NSProfile.toMgdl(SafeParse.stringToDouble(SP.getString("openapsma_min_bg", minBgDefault)), units); + double maxBg = NSProfile.toMgdl(SafeParse.stringToDouble(SP.getString("openapsma_max_bg", minBgDefault)), units); minBg = Round.roundTo(minBg, 1d); maxBg = Round.roundTo(maxBg, 1d); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewExtendedBolusDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewExtendedBolusDialog.java index c4bf884b80..3a32ac68fb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewExtendedBolusDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewExtendedBolusDialog.java @@ -16,6 +16,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Result; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.utils.SafeParse; public class NewExtendedBolusDialog extends DialogFragment implements View.OnClickListener { @@ -48,9 +49,7 @@ public class NewExtendedBolusDialog extends DialogFragment implements View.OnCli switch (view.getId()) { case R.id.overview_newextendedbolus_okbutton: try { - int basalPercent = 100; - String insulinText = insulinEdit.getText().toString().replace(",", "."); - Double insulin = Double.parseDouble(!insulinText.equals("") ? insulinText : "0"); + Double insulin = SafeParse.stringToDouble(insulinEdit.getText().toString()); int durationInMinutes = 30; if (h10Radio.isChecked()) durationInMinutes = 60; if (h20Radio.isChecked()) durationInMinutes = 120; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTempBasalDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTempBasalDialog.java index 556452e770..3cc5473495 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTempBasalDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTempBasalDialog.java @@ -16,6 +16,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Result; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.utils.SafeParse; public class NewTempBasalDialog extends DialogFragment implements View.OnClickListener { @@ -53,8 +54,7 @@ public class NewTempBasalDialog extends DialogFragment implements View.OnClickLi case R.id.overview_newtempbasal_okbutton: try { int basalPercent = 100; - String basalText = basalEdit.getText().toString().replace(",", "."); - Double basal = Double.parseDouble(!basalText.equals("") ? basalText : "0"); + Double basal = SafeParse.stringToDouble(basalEdit.getText().toString()); final boolean setAsPercent = percentRadio.isChecked(); int durationInMinutes = 30; if (h10Radio.isChecked()) durationInMinutes = 60; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java index 0f552134c8..d0bf1fb57d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java @@ -17,6 +17,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Result; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.utils.SafeParse; public class NewTreatmentDialog extends DialogFragment implements OnClickListener { @@ -46,10 +47,8 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene case R.id.treatments_newtreatment_deliverbutton: try { - String insulinText = this.insulin.getText().toString().replace(",", "."); - String carbsText = this.carbs.getText().toString().replace(",", "."); - Double insulin = Double.parseDouble(!insulinText.equals("") ? insulinText : "0"); - Integer carbs = Integer.parseInt(!carbsText.equals("") ? carbsText : "0"); + Double insulin = SafeParse.stringToDouble(this.insulin.getText().toString()); + Integer carbs = SafeParse.stringToInt(this.carbs.getText().toString()); String confirmMessage = getString(R.string.entertreatmentquestion); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java index b2c9734baa..b2010427d4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java @@ -216,16 +216,9 @@ public class WizardDialog extends DialogFragment implements OnClickListener { NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); // Entered values - String i_bg = this.bgInput.getText().toString().replace("," , "."); - String i_carbs = this.carbsInput.getText().toString().replace(",", "."); - String i_correction = this.correctionInput.getText().toString().replace(",", "."); - Double c_bg = 0d; - try { c_bg = Double.parseDouble(i_bg.equals("") ? "0" : i_bg); } catch (Exception e) {} - Integer c_carbs = 0; - try { c_carbs = Integer.parseInt(i_carbs.equals("") ? "0" : i_carbs); } catch (Exception e) {} - c_carbs = (Integer) Math.round(c_carbs); - Double c_correction = 0d; - try { c_correction = Double.parseDouble(i_correction.equals("") ? "0" : i_correction); } catch (Exception e) {} + Double c_bg = SafeParse.stringToDouble(this.bgInput.getText().toString()); + Integer c_carbs = SafeParse.stringToInt(this.carbsInput.getText().toString()); + Double c_correction = SafeParse.stringToDouble(this.correctionInput.getText().toString()); if(c_correction != MainApp.getConfigBuilder().applyBolusConstraints(c_correction)) { this.correctionInput.setText(""); wizardDialogDeliverButton.setVisibility(Button.GONE); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index b610324c2f..2b4681dcdf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -2,9 +2,11 @@ package info.nightscout.androidaps.plugins.Overview; import android.app.Activity; import android.graphics.Color; +import android.graphics.DashPathEffect; import android.graphics.Paint; import android.os.Bundle; import android.os.Handler; +import android.provider.ContactsContract; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.view.LayoutInflater; @@ -15,6 +17,8 @@ import android.widget.LinearLayout; import android.widget.TextView; import com.jjoe64.graphview.GraphView; +import com.jjoe64.graphview.ValueDependentColor; +import com.jjoe64.graphview.series.BarGraphSeries; import com.jjoe64.graphview.series.DataPoint; import com.jjoe64.graphview.series.LineGraphSeries; import com.jjoe64.graphview.series.PointsGraphSeries; @@ -317,7 +321,8 @@ public class OverviewFragment extends Fragment implements PluginBase { int agoMin = (int) (agoMsec / 60d / 1000d); timeAgoView.setText(agoMin + " " + getString(R.string.minago)); - // **** BG graph **** + // ****** GRAPH ******* + // allign to hours Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(new Date().getTime()); @@ -333,6 +338,56 @@ public class OverviewFragment extends Fragment implements PluginBase { Double highLine = NSProfile.toUnits(180d, 10d, units); Double maxY = NSProfile.toUnits(400d, 20d, units); // TODO: add some scale support + BarGraphSeries basalsSeries = null; + LineGraphSeries seriesLow = null; + LineGraphSeries seriesHigh = null; + LineGraphSeries seriesNow = null; + PointsGraphSeries seriesInRage = null; + PointsGraphSeries seriesOutOfRange = null; + + bgGraph.removeAllSeries(); + + // **** TEMP BASALS graph **** + class BarDataPoint extends DataPoint { + public BarDataPoint(double x, double y, boolean isTempBasal) { + super(x, y); + this.isTempBasal = isTempBasal; + } + public boolean isTempBasal = false; + } + + Double maxAllowedBasal = MainApp.getConfigBuilder().applyBasalConstraints(1000d); + + long now = new Date().getTime(); + List basalArray = new ArrayList(); + for (long time = fromTime; time < now; time += 5 * 60 * 1000l) { + TempBasal tb = MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(new Date(time)); + if (tb != null) + basalArray.add(new BarDataPoint(time, tb.tempBasalConvertedToAbsolute(), true)); + else + basalArray.add(new BarDataPoint(time, profile.getBasal(NSProfile.secondsFromMidnight(new Date(time))), false)); + } + BarDataPoint[] basal = new BarDataPoint[basalArray.size()]; + log.debug("Bars: " + basalArray.size()); + basal = basalArray.toArray(basal); + bgGraph.addSeries(basalsSeries = new BarGraphSeries(basal)); + basalsSeries.setValueDependentColor(new ValueDependentColor() { + @Override + public int get(DataPoint data) { + BarDataPoint point = (BarDataPoint) data; + if (point.isTempBasal) return Color.CYAN; + else return Color.BLUE; + } + }); + + // set second scale + bgGraph.getSecondScale().addSeries(basalsSeries); + bgGraph.getSecondScale().setMinY(0); + bgGraph.getSecondScale().setMaxY(maxAllowedBasal * 4); + bgGraph.getGridLabelRenderer().setVerticalLabelsSecondScaleColor(MainApp.instance().getResources().getColor(R.color.background_material_dark)); + + + // **** BG graph **** List bgReadingsArray = MainApp.getDbHelper().getDataFromTime(fromTime); List inRangeArray = new ArrayList(); List outOfRangeArray = new ArrayList(); @@ -354,38 +409,53 @@ public class OverviewFragment extends Fragment implements PluginBase { outOfRange = outOfRangeArray.toArray(outOfRange); - // targets - LineGraphSeries seriesLow = new LineGraphSeries(new DataPoint[]{ - new DataPoint(fromTime, lowLine), - new DataPoint(toTime, lowLine) - }); - seriesLow.setColor(Color.RED); - bgGraph.addSeries(seriesLow); - - LineGraphSeries seriesHigh = new LineGraphSeries(new DataPoint[]{ - new DataPoint(fromTime, highLine), - new DataPoint(toTime, highLine) - }); - seriesHigh.setColor(Color.RED); - bgGraph.addSeries(seriesHigh); - - if (inRange.length > 0) { - PointsGraphSeries seriesInRage = new PointsGraphSeries(inRange); - bgGraph.addSeries(seriesInRage); + bgGraph.addSeries(seriesInRage = new PointsGraphSeries(inRange)); seriesInRage.setShape(PointsGraphSeries.Shape.POINT); seriesInRage.setSize(5); seriesInRage.setColor(Color.GREEN); } if (outOfRange.length > 0) { - PointsGraphSeries seriesOutOfRange = new PointsGraphSeries(outOfRange); - bgGraph.addSeries(seriesOutOfRange); + bgGraph.addSeries(seriesOutOfRange = new PointsGraphSeries(outOfRange)); seriesOutOfRange.setShape(PointsGraphSeries.Shape.POINT); seriesOutOfRange.setSize(5); seriesOutOfRange.setColor(Color.RED); } + + // **** HIGH and LOW targets graph **** + DataPoint[] lowDataPoints = new DataPoint[]{ + new DataPoint(fromTime, lowLine), + new DataPoint(toTime, lowLine) + }; + DataPoint[] highDataPoints = new DataPoint[]{ + new DataPoint(fromTime, highLine), + new DataPoint(toTime, highLine) + }; + bgGraph.addSeries(seriesLow = new LineGraphSeries(lowDataPoints)); + seriesLow.setColor(Color.RED); + bgGraph.addSeries(seriesHigh = new LineGraphSeries(highDataPoints)); + seriesHigh.setColor(Color.RED); + + // **** NOW line **** + DataPoint[] nowPoints = new DataPoint[]{ + new DataPoint(now, 0), + new DataPoint(now, maxY) + }; + bgGraph.addSeries(seriesNow = new LineGraphSeries(nowPoints)); + seriesNow.setColor(Color.GREEN); + seriesNow.setDrawDataPoints(false); + //seriesNow.setThickness(1); + // custom paint to make a dotted line + Paint paint = new Paint(); + paint.setStyle(Paint.Style.STROKE); + paint.setStrokeWidth(1); + paint.setPathEffect(new DashPathEffect(new float[]{4, 20}, 0)); + paint.setColor(Color.WHITE); + seriesNow.setCustomPaint(paint); + + // set manual x bounds to have nice steps bgGraph.getViewport().setMaxX(toTime); bgGraph.getViewport().setMinX(fromTime); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyFragment.java index 5661c969a2..eab25c57d4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyFragment.java @@ -17,6 +17,7 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.APSResult; import info.nightscout.client.data.NSProfile; import info.nightscout.utils.Round; +import info.nightscout.utils.SafeParse; public class SafetyFragment extends Fragment implements PluginBase, ConstraintsInterface { private static Logger log = LoggerFactory.getLogger(SafetyFragment.class); @@ -83,7 +84,7 @@ public class SafetyFragment extends Fragment implements PluginBase, ConstraintsI @Override public Double applyBasalConstraints(Double absoluteRate) { SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - Double maxBasal = Double.parseDouble(SP.getString("openapsma_max_basal", "1").replace(",", ".")); + Double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1")); NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); if (absoluteRate < 0) absoluteRate = 0d; @@ -113,7 +114,7 @@ public class SafetyFragment extends Fragment implements PluginBase, ConstraintsI @Override public Integer applyBasalConstraints(Integer percentRate) { SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - Double maxBasal = Double.parseDouble(SP.getString("openapsma_max_basal", "1").replace(",", ".")); + Double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1")); NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); Double currentBasal = profile.getBasal(profile.secondsFromMidnight()); @@ -158,7 +159,7 @@ public class SafetyFragment extends Fragment implements PluginBase, ConstraintsI public Double applyBolusConstraints(Double insulin) { SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); try { - Double maxBolus = Double.parseDouble(SP.getString("treatmentssafety_maxbolus", "3")); + Double maxBolus = SafeParse.stringToDouble(SP.getString("treatmentssafety_maxbolus", "3")); if (insulin < 0) insulin = 0d; if (insulin > maxBolus) insulin = maxBolus; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java index 0ce100b324..5e523bc7a4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java @@ -27,6 +27,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.client.data.NSProfile; +import info.nightscout.utils.SafeParse; public class SimpleProfileFragment extends Fragment implements PluginBase, ProfileInterface { private static Logger log = LoggerFactory.getLogger(SimpleProfileFragment.class); @@ -156,13 +157,13 @@ public class SimpleProfileFragment extends Fragment implements PluginBase, Profi @Override public void onTextChanged(CharSequence s, int start, int before, int count) { - try { dia = Double.parseDouble(diaView.getText().toString().replace(",", ".")); } catch (Exception e) {}; - try { ic = Double.parseDouble(icView.getText().toString().replace(",", ".")); } catch (Exception e) {}; - try { isf = Double.parseDouble(isfView.getText().toString().replace(",", ".")); } catch (Exception e) {}; - try { car = Double.parseDouble(carView.getText().toString().replace(",", ".")); } catch (Exception e) {}; - try { basal = Double.parseDouble(basalView.getText().toString().replace(",", ".")); } catch (Exception e) {}; - try { targetLow = Double.parseDouble(targetlowView.getText().toString().replace(",", ".")); } catch (Exception e) {}; - try { targetHigh = Double.parseDouble(targethighView.getText().toString().replace(",", ".")); } catch (Exception e) {}; + dia = SafeParse.stringToDouble(diaView.getText().toString()); + ic = SafeParse.stringToDouble(icView.getText().toString()); + isf = SafeParse.stringToDouble(isfView.getText().toString()); + car = SafeParse.stringToDouble(carView.getText().toString()); + basal = SafeParse.stringToDouble(basalView.getText().toString()); + targetLow = SafeParse.stringToDouble(targetlowView.getText().toString()); + targetHigh = SafeParse.stringToDouble(targethighView.getText().toString()); storeSettings(); } }; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java index 872f103021..aa1783ccdf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java @@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.TempBasals; import android.app.Activity; import android.os.Bundle; +import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.*; import android.view.LayoutInflater; @@ -27,6 +28,7 @@ import java.util.Date; import java.util.List; import java.util.Locale; +import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.TempBasal; @@ -123,12 +125,16 @@ public class TempBasalsFragment extends Fragment implements PluginBase, TempBasa boolean update = false; if (t.timeEnd == null && t.getPlannedTimeEnd().getTime() < now) { t.timeEnd = new Date(t.getPlannedTimeEnd().getTime()); + if (Config.logTempBasalsCut) + log.debug("Add timeEnd to old record"); update = true; } if (position > 0) { Date startofnewer = tempBasals.get(position - 1).timeStart; if (t.timeEnd == null) { t.timeEnd = new Date(Math.min(startofnewer.getTime(), t.getPlannedTimeEnd().getTime())); + if (Config.logTempBasalsCut) + log.debug("Add timeEnd to old record"); update = true; } else if (t.timeEnd.getTime() > startofnewer.getTime()) { t.timeEnd = startofnewer; @@ -137,8 +143,11 @@ public class TempBasalsFragment extends Fragment implements PluginBase, TempBasa } if (update) { dao.update(t); - log.debug("Fixing unfinished temp end: " + t.log()); - if (position > 0) log.debug("Previous: " + tempBasals.get(position - 1).log()); + if (Config.logTempBasalsCut) { + log.debug("Fixing unfinished temp end: " + t.log()); + if (position > 0) + log.debug("Previous: " + tempBasals.get(position - 1).log()); + } } } @@ -186,6 +195,14 @@ public class TempBasalsFragment extends Fragment implements PluginBase, TempBasa lastCalculation = total; } + @Nullable + @Override + public TempBasal getTempBasal(Date time) { + for (TempBasal t: tempBasals) { + if (t.isInProgress(time)) return t; + } + return null; + } public static class RecyclerViewAdapter extends RecyclerView.Adapter { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpFragment.java index 6b55c96b15..bba47b6ea4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpFragment.java @@ -38,7 +38,7 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt Double defaultBasalValue = 0.2d; - TempBasal tempBasal = null; + //TempBasal tempBasal = null; TempBasal extendedBolus = null; Integer batteryPercent = 50; Integer resevoirInUnits = 50; @@ -127,18 +127,22 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt void checkForExpiredTempsAndExtended() { long now = new Date().getTime(); if (isTempBasalInProgress()) { - long plannedTimeEnd = tempBasal.getPlannedTimeEnd().getTime(); + //long plannedTimeEnd = tempBasal.getPlannedTimeEnd().getTime(); + long plannedTimeEnd = getTempBasal().getPlannedTimeEnd().getTime(); if (plannedTimeEnd < now) { - tempBasal.timeEnd = new Date(plannedTimeEnd); + //tempBasal.timeEnd = new Date(plannedTimeEnd); + getTempBasal().timeEnd = new Date(plannedTimeEnd); try { - MainApp.instance().getDbHelper().getDaoTempBasals().update(tempBasal); + //MainApp.instance().getDbHelper().getDaoTempBasals().update(tempBasal); + MainApp.instance().getDbHelper().getDaoTempBasals().update(getTempBasal()); } catch (SQLException e) { e.printStackTrace(); log.error(e.getMessage()); } if (Config.logPumpComm) - log.debug("Canceling expired temp: " + tempBasal); - tempBasal = null; + //log.debug("Canceling expired temp: " + tempBasal); + log.debug("Canceling expired temp: " + getTempBasal()); + //tempBasal = null; MainApp.bus().post(new EventTreatmentChange()); } } @@ -161,7 +165,8 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt @Override public boolean isTempBasalInProgress() { - return tempBasal != null; + //return tempBasal != null; + return getTempBasal() != null; } @Override @@ -196,28 +201,33 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt public double getTempBasalAbsoluteRate() { if (!isTempBasalInProgress()) return 0; - if (tempBasal.isAbsolute) { - return tempBasal.absolute; + //if (tempBasal.isAbsolute) { + if (getTempBasal().isAbsolute) { + //return tempBasal.absolute; + return getTempBasal().absolute; } else { NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); if (profile == null) return defaultBasalValue; Double baseRate = profile.getBasal(profile.secondsFromMidnight()); - Double tempRate = baseRate * (tempBasal.percent / 100d); + //Double tempRate = baseRate * (tempBasal.percent / 100d); + Double tempRate = baseRate * (getTempBasal().percent / 100d); return baseRate + tempRate; } } @Override public TempBasal getTempBasal() { - return tempBasal; + //return tempBasal; + return MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(new Date()); } @Override public double getTempBasalRemainingMinutes() { if (!isTempBasalInProgress()) return 0; - return tempBasal.getPlannedRemainingMinutes(); + //return tempBasal.getPlannedRemainingMinutes(); + return getTempBasal().getPlannedRemainingMinutes(); } @Override @@ -240,7 +250,8 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt Result result = cancelTempBasal(); if (!result.success) return result; - tempBasal = new TempBasal(); + //tempBasal = new TempBasal(); + TempBasal tempBasal = new TempBasal(); tempBasal.timeStart = new Date(); tempBasal.isAbsolute = true; tempBasal.absolute = absoluteRate; @@ -272,7 +283,8 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt if (!result.success) return result; } - tempBasal = new TempBasal(); + //tempBasal = new TempBasal(); + TempBasal tempBasal = new TempBasal(); tempBasal.timeStart = new Date(); tempBasal.isAbsolute = false; tempBasal.percent = percent; @@ -332,13 +344,15 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt checkForExpiredTempsAndExtended(); Result result = new Result(); result.success = true; + result.isTempCancel = true; result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); if (isTempBasalInProgress()) { result.enacted = true; - tempBasal.timeEnd = new Date(); + //tempBasal.timeEnd = new Date(); + getTempBasal().timeEnd = new Date(); try { - MainApp.instance().getDbHelper().getDaoTempBasals().update(tempBasal); - tempBasal = null; + MainApp.instance().getDbHelper().getDaoTempBasals().update(getTempBasal()); + //tempBasal = null; if (Config.logPumpComm) log.debug("Canceling temp basal: " + result); updateGUI(); @@ -359,7 +373,7 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt if (isExtendedBoluslInProgress()) { extendedBolus.timeEnd = new Date(); try { - MainApp.instance().getDbHelper().getDaoTempBasals().update(tempBasal); + MainApp.instance().getDbHelper().getDaoTempBasals().update(extendedBolus); } catch (SQLException e) { e.printStackTrace(); result.success = false; @@ -390,12 +404,13 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt try { battery.put("percent", batteryPercent); status.put("status", "normal"); - //status.put("lastbolus", last_bolus_amount); - //status.put("lastbolustime", DateUtil.toISOString(last_bolus_time)); if (isTempBasalInProgress()) { - status.put("tempbasalpct", tempBasal.percent); - status.put("tempbasalstart", DateUtil.toISOString(tempBasal.timeStart)); - status.put("tempbasalremainmin", tempBasal.getPlannedRemainingMinutes()); + //status.put("tempbasalpct", tempBasal.percent); + //status.put("tempbasalstart", DateUtil.toISOString(tempBasal.timeStart)); + //status.put("tempbasalremainmin", tempBasal.getPlannedRemainingMinutes()); + status.put("tempbasalpct", getTempBasal().percent); + status.put("tempbasalstart", DateUtil.toISOString(getTempBasal().timeStart)); + status.put("tempbasalremainmin", getTempBasal().getPlannedRemainingMinutes()); } status.put("timestamp", DateUtil.toISOString(new Date())); @@ -430,7 +445,8 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt basaBasalRateView.setText(getBaseBasalRate() + "U"); if (isTempBasalInProgress()) { - tempBasalView.setText(tempBasal.toString()); + //tempBasalView.setText(tempBasal.toString()); + tempBasalView.setText(getTempBasal().toString()); } else { tempBasalView.setText(""); } diff --git a/app/src/main/java/info/nightscout/utils/SafeParse.java b/app/src/main/java/info/nightscout/utils/SafeParse.java new file mode 100644 index 0000000000..5e9b132a1a --- /dev/null +++ b/app/src/main/java/info/nightscout/utils/SafeParse.java @@ -0,0 +1,26 @@ +package info.nightscout.utils; + +/** + * Created by mike on 23.06.2016. + */ +public class SafeParse { + public static Double stringToDouble(String input) { + Double result = 0d; + input = input.replace(",", "."); + try { + result = Double.parseDouble(input); + } catch (Exception e) { + } + return result; + } + + public static Integer stringToInt(String input) { + Integer result = 0; + input = input.replace(",", "."); + try { + result = Integer.parseInt(input); + } catch (Exception e) { + } + return result; + } +}