From 83da3447ac3563a52584d84f9e85607f700d1471 Mon Sep 17 00:00:00 2001 From: "Markus M. May" Date: Wed, 18 Apr 2018 22:11:32 +0200 Subject: [PATCH 1/3] Cleanup mmol/mgdl handling in Dialogs --- .../Dialogs/NewNSTreatmentDialog.java | 16 ++-- .../Overview/Dialogs/NewCarbsDialog.java | 30 +++---- .../nightscout/utils/DefaultValueHelper.java | 90 +++++++++++++++++++ 3 files changed, 115 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/info/nightscout/utils/DefaultValueHelper.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java index 68d844739b..56aa0158cf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java @@ -51,6 +51,7 @@ import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.utils.DateUtil; +import info.nightscout.utils.DefaultValueHelper; import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.HardLimits; import info.nightscout.utils.JsonHelper; @@ -210,19 +211,22 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick } boolean erase = false; + String units = MainApp.getConfigBuilder().getProfileUnits(); + DefaultValueHelper helper = new DefaultValueHelper(); if (MainApp.gs(R.string.eatingsoon).equals(reasonList.get(position))) { - defaultDuration = SP.getDouble(R.string.key_eatingsoon_duration, 0d); - defaultTarget = SP.getDouble(R.string.key_eatingsoon_target, 0d); + defaultDuration = helper.determineEatingSoonTTDuration(); + defaultTarget = helper.determineEatingSoonTT(units); } else if (MainApp.gs(R.string.activity).equals(reasonList.get(position))) { - defaultDuration = SP.getDouble(R.string.key_activity_duration, 0d); - defaultTarget = SP.getDouble(R.string.key_activity_target, 0d); + defaultDuration = helper.determineActivityTTDuration(); + defaultTarget = helper.determineActivityTT(units); } else if (MainApp.gs(R.string.hypo).equals(reasonList.get(position))) { - defaultDuration = SP.getDouble(R.string.key_hypo_duration, 0d); - defaultTarget = SP.getDouble(R.string.key_hypo_target, 0d); + defaultDuration = helper.determineHypoTTDuration(); + defaultTarget = helper.determineHypoTT(units); } else { defaultDuration = 0; erase = true; } + if (defaultTarget != 0 || erase) { editTemptarget.setValue(defaultTarget); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java index d8bdb6648d..6be2b149ca 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java @@ -50,6 +50,7 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.androidaps.queue.Callback; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.DefaultValueHelper; import info.nightscout.utils.NumberPicker; import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; @@ -322,26 +323,25 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D if (currentProfile == null) return; - int activityTTDuration = SP.getInt(R.string.key_activity_duration, Constants.defaultActivityTTDuration); - activityTTDuration = activityTTDuration > 0 ? activityTTDuration : Constants.defaultActivityTTDuration; - double activityTT = SP.getDouble(R.string.key_activity_target, currentProfile.getUnits().equals(Constants.MMOL) ? Constants.defaultActivityTTmmol : Constants.defaultActivityTTmgdl); - activityTT = activityTT > 0 ? activityTT : currentProfile.getUnits().equals(Constants.MMOL) ? Constants.defaultActivityTTmmol : Constants.defaultActivityTTmgdl; + final String units = currentProfile.getUnits(); + DefaultValueHelper helper = new DefaultValueHelper(); - int eatingSoonTTDuration = SP.getInt(R.string.key_eatingsoon_duration, Constants.defaultEatingSoonTTDuration); - eatingSoonTTDuration = eatingSoonTTDuration > 0 ? eatingSoonTTDuration : Constants.defaultEatingSoonTTDuration; - double eatingSoonTT = SP.getDouble(R.string.key_eatingsoon_target, currentProfile.getUnits().equals(Constants.MMOL) ? Constants.defaultEatingSoonTTmmol : Constants.defaultEatingSoonTTmgdl); - eatingSoonTT = eatingSoonTT > 0 ? eatingSoonTT : currentProfile.getUnits().equals(Constants.MMOL) ? Constants.defaultEatingSoonTTmmol : Constants.defaultEatingSoonTTmgdl; + int activityTTDuration = helper.determineActivityTTDuration(); + double activityTT = helper.determineActivityTT(units); - int hypoTTDuration = SP.getInt(R.string.key_hypo_duration, Constants.defaultHypoTTDuration); - hypoTTDuration = hypoTTDuration > 0 ? hypoTTDuration : Constants.defaultHypoTTDuration; - double hypoTT = SP.getDouble(R.string.key_hypo_target, currentProfile.getUnits().equals(Constants.MMOL) ? Constants.defaultHypoTTmmol : Constants.defaultHypoTTmgdl); - hypoTT = hypoTT > 0 ? hypoTT : currentProfile.getUnits().equals(Constants.MMOL) ? Constants.defaultHypoTTmmol : Constants.defaultHypoTTmgdl; + int eatingSoonTTDuration = helper.determineEatingSoonTTDuration(); + double eatingSoonTT = helper.determineEatingSoonTT(units); + + int hypoTTDuration = helper.determineHypoTTDuration(); + double hypoTT = helper.determineHypoTT(units); if (startActivityTTCheckbox.isChecked()) { + String unitLabel = "mg/dl"; if (currentProfile.getUnits().equals(Constants.MMOL)) { - actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to1Decimal(activityTT) + " mmol/l (" + activityTTDuration + " min)"); - } else - actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to0Decimal(activityTT) + " mg/dl (" + activityTTDuration + " min)"); + unitLabel = "mmol/l"; + } + + actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to1Decimal(activityTT) + " " + unitLabel + " (" + activityTTDuration + " min)"); } if (startEatingSoonTTCheckbox.isChecked()) { diff --git a/app/src/main/java/info/nightscout/utils/DefaultValueHelper.java b/app/src/main/java/info/nightscout/utils/DefaultValueHelper.java new file mode 100644 index 0000000000..ba1aba90b8 --- /dev/null +++ b/app/src/main/java/info/nightscout/utils/DefaultValueHelper.java @@ -0,0 +1,90 @@ +package info.nightscout.utils; + +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.R; + +public class DefaultValueHelper { + + /** + * returns the corresponding EatingSoon TempTarget based on the given units (MMOL / MGDL) + * + * @param units + * @return + */ + public double getDefaultEatingSoonTT(String units) { + return Constants.MMOL.equals(units) ? Constants.defaultEatingSoonTTmmol + : Constants.defaultEatingSoonTTmgdl; + } + + /** + * returns the corresponding Activity TempTarget based on the given units (MMOL / MGDL) + * + * @param units + * @return + */ + public double getDefaultActivityTT(String units) { + return Constants.MMOL.equals(units) ? Constants.defaultActivityTTmmol + : Constants.defaultActivityTTmgdl; + } + + /** + * returns the corresponding Hypo TempTarget based on the given units (MMOL / MGDL) + * + * @param units + * @return + */ + public double getDefaultHypoTT(String units) { + return Constants.MMOL.equals(units) ? Constants.defaultHypoTTmmol + : Constants.defaultHypoTTmgdl; + } + + /** + * returns the configured EatingSoon TempTarget, if this is set to 0, the Default-Value is returned. + * + * @param units + * @return + */ + public double determineEatingSoonTT(String units) { + double value = SP.getDouble(R.string.key_eatingsoon_target, this.getDefaultEatingSoonTT(units)); + return value > 0 ? value : this.getDefaultEatingSoonTT(units); + } + + public int determineEatingSoonTTDuration() { + int value = SP.getInt(R.string.key_eatingsoon_duration, Constants.defaultEatingSoonTTDuration); + return value > 0 ? value : Constants.defaultEatingSoonTTDuration; + } + + + /** + * returns the configured Activity TempTarget, if this is set to 0, the Default-Value is returned. + * + * @param units + * @return + */ + public double determineActivityTT(String units) { + double value = SP.getDouble(R.string.key_activity_target, this.getDefaultActivityTT(units)); + return value > 0 ? value : this.getDefaultActivityTT(units); + } + + public int determineActivityTTDuration() { + int value = SP.getInt(R.string.key_activity_duration, Constants.defaultActivityTTDuration); + return value > 0 ? value : Constants.defaultActivityTTDuration; + } + + /** + * returns the configured Hypo TempTarget, if this is set to 0, the Default-Value is returned. + * + * @param units + * @return + */ + public double determineHypoTT(String units) { + double value = SP.getDouble(R.string.key_hypo_target, this.getDefaultHypoTT(units)); + return value > 0 ? value : this.getDefaultHypoTT(units); + } + + public int determineHypoTTDuration() { + int value = SP.getInt(R.string.key_hypo_duration, Constants.defaultHypoTTDuration); + return value > 0 ? value : Constants.defaultHypoTTDuration; + } + +} From 51530fbcf845c613aac712ca3752bf6ad3346a8a Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 19 Apr 2018 23:18:19 +0200 Subject: [PATCH 2/3] fix temptarget NS sync --- .../nightscout/androidaps/db/DatabaseHelper.java | 8 +++++--- .../main/java/info/nightscout/utils/NSUpload.java | 12 ++++++++++-- 2 files changed, 15 insertions(+), 5 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 f45e57cfbe..7174c19ec4 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -28,6 +28,7 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.ProfileStore; @@ -45,6 +46,7 @@ import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventNewHistor import info.nightscout.androidaps.plugins.PumpDanaR.activities.DanaRNSHistorySync; import info.nightscout.androidaps.plugins.PumpDanaR.comm.RecordTypes; import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; +import info.nightscout.utils.JsonHelper; import info.nightscout.utils.NSUpload; import info.nightscout.utils.PercentageSplitter; import info.nightscout.utils.ToastUtils; @@ -662,12 +664,12 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public void createTemptargetFromJsonIfNotExists(JSONObject trJson) { try { - String units = MainApp.getConfigBuilder().getProfileUnits(); + String units = JsonHelper.safeGetString(trJson, "units", MainApp.getConfigBuilder().getProfileUnits()); TempTarget tempTarget = new TempTarget() .date(trJson.getLong("mills")) .duration(trJson.getInt("duration")) - .low(trJson.getDouble("targetBottom")) - .high(trJson.getDouble("targetTop")) + .low(Profile.toMgdl(trJson.getDouble("targetBottom"), units)) + .high(Profile.toMgdl(trJson.getDouble("targetTop"), units)) .reason(trJson.getString("reason")) ._id(trJson.getString("_id")) .source(Source.NIGHTSCOUT); diff --git a/app/src/main/java/info/nightscout/utils/NSUpload.java b/app/src/main/java/info/nightscout/utils/NSUpload.java index e617191732..3cd252f19a 100644 --- a/app/src/main/java/info/nightscout/utils/NSUpload.java +++ b/app/src/main/java/info/nightscout/utils/NSUpload.java @@ -310,13 +310,21 @@ public class NSUpload { public static void uploadTempTarget(TempTarget tempTarget) { try { + Profile profile = MainApp.getConfigBuilder().getProfile(); + + if (profile == null) { + log.error("Profile is null. Skipping upload"); + return; + } + JSONObject data = new JSONObject(); data.put("eventType", CareportalEvent.TEMPORARYTARGET); data.put("duration", tempTarget.durationInMinutes); data.put("reason", tempTarget.reason); - data.put("targetBottom", tempTarget.low); - data.put("targetTop", tempTarget.high); + data.put("targetBottom", Profile.fromMgdlToUnits(tempTarget.low, profile.getUnits())); + data.put("targetTop", Profile.fromMgdlToUnits(tempTarget.high, profile.getUnits())); data.put("created_at", DateUtil.toISOString(tempTarget.date)); + data.put("units", profile.getUnits()); data.put("enteredBy", MainApp.instance().getString(R.string.app_name)); uploadCareportalEntryToNS(data); } catch (JSONException e) { From 645e83b6e61fd925f82b505f8a67620838508f91 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 19 Apr 2018 23:42:37 +0200 Subject: [PATCH 3/3] dismiss notification if pump is reachable --- .../nightscout/utils/LocalAlertUtils.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/info/nightscout/utils/LocalAlertUtils.java b/app/src/main/java/info/nightscout/utils/LocalAlertUtils.java index 24b0d2ff05..cc1a22355a 100644 --- a/app/src/main/java/info/nightscout/utils/LocalAlertUtils.java +++ b/app/src/main/java/info/nightscout/utils/LocalAlertUtils.java @@ -14,10 +14,9 @@ import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; +import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.notifications.Notification; -import info.nightscout.androidaps.receivers.KeepAliveReceiver; -import info.nightscout.utils.NSUpload; /** * Created by adrian on 17/12/17. @@ -26,12 +25,12 @@ import info.nightscout.utils.NSUpload; public class LocalAlertUtils { private static Logger log = LoggerFactory.getLogger(LocalAlertUtils.class); - public static int missedReadingsThreshold() { - return SP.getInt(MainApp.sResources.getString(R.string.key_missed_bg_readings_threshold), 30) * 60 * 1000; + public static long missedReadingsThreshold() { + return T.mins(SP.getInt(MainApp.sResources.getString(R.string.key_missed_bg_readings_threshold), 30)).msecs(); } - private static int pumpUnreachableThreshold() { - return SP.getInt(MainApp.sResources.getString(R.string.key_pump_unreachable_threshold), 30) * 60 * 1000; + private static long pumpUnreachableThreshold() { + return T.mins(SP.getInt(MainApp.sResources.getString(R.string.key_pump_unreachable_threshold), 30)).msecs(); } public static void checkPumpUnreachableAlarm(Date lastConnection, boolean isStatusOutdated) { @@ -49,11 +48,13 @@ public class LocalAlertUtils { NSUpload.uploadError(n.text); } } + if (!isStatusOutdated && !alarmTimeoutExpired) + MainApp.bus().post(new EventDismissNotification(Notification.PUMP_UNREACHABLE)); } /*Presnoozes the alarms with 5 minutes if no snooze exists. - * Call only at startup! - */ + * Call only at startup! + */ public static void presnoozeAlarms() { if (SP.getLong("nextMissedReadingsAlarm", 0l) < System.currentTimeMillis()) { SP.putLong("nextMissedReadingsAlarm", System.currentTimeMillis() + 5 * 60 * 1000); @@ -74,7 +75,7 @@ public class LocalAlertUtils { SP.putLong("nextPumpDisconnectedAlarm", nextPumpDisconnectedAlarm); } - public static void notifyPumpStatusRead(){ + public static void notifyPumpStatusRead() { //TODO: persist the actual time the pump is read and simplify the whole logic when to alarm final PumpInterface pump = ConfigBuilderPlugin.getActivePump();