Merge remote-tracking branch 'milosremote/dev' into cob-rework

This commit is contained in:
AdrianLxM 2018-04-19 23:54:36 +02:00
commit d8180cc52b
6 changed files with 140 additions and 35 deletions

View file

@ -28,6 +28,7 @@ import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.ProfileStore; 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.activities.DanaRNSHistorySync;
import info.nightscout.androidaps.plugins.PumpDanaR.comm.RecordTypes; import info.nightscout.androidaps.plugins.PumpDanaR.comm.RecordTypes;
import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin;
import info.nightscout.utils.JsonHelper;
import info.nightscout.utils.NSUpload; import info.nightscout.utils.NSUpload;
import info.nightscout.utils.PercentageSplitter; import info.nightscout.utils.PercentageSplitter;
import info.nightscout.utils.ToastUtils; import info.nightscout.utils.ToastUtils;
@ -662,12 +664,12 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
public void createTemptargetFromJsonIfNotExists(JSONObject trJson) { public void createTemptargetFromJsonIfNotExists(JSONObject trJson) {
try { try {
String units = MainApp.getConfigBuilder().getProfileUnits(); String units = JsonHelper.safeGetString(trJson, "units", MainApp.getConfigBuilder().getProfileUnits());
TempTarget tempTarget = new TempTarget() TempTarget tempTarget = new TempTarget()
.date(trJson.getLong("mills")) .date(trJson.getLong("mills"))
.duration(trJson.getInt("duration")) .duration(trJson.getInt("duration"))
.low(trJson.getDouble("targetBottom")) .low(Profile.toMgdl(trJson.getDouble("targetBottom"), units))
.high(trJson.getDouble("targetTop")) .high(Profile.toMgdl(trJson.getDouble("targetTop"), units))
.reason(trJson.getString("reason")) .reason(trJson.getString("reason"))
._id(trJson.getString("_id")) ._id(trJson.getString("_id"))
.source(Source.NIGHTSCOUT); .source(Source.NIGHTSCOUT);

View file

@ -51,6 +51,7 @@ import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DefaultValueHelper;
import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.FabricPrivacy;
import info.nightscout.utils.HardLimits; import info.nightscout.utils.HardLimits;
import info.nightscout.utils.JsonHelper; import info.nightscout.utils.JsonHelper;
@ -210,19 +211,22 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
} }
boolean erase = false; boolean erase = false;
String units = MainApp.getConfigBuilder().getProfileUnits();
DefaultValueHelper helper = new DefaultValueHelper();
if (MainApp.gs(R.string.eatingsoon).equals(reasonList.get(position))) { if (MainApp.gs(R.string.eatingsoon).equals(reasonList.get(position))) {
defaultDuration = SP.getDouble(R.string.key_eatingsoon_duration, 0d); defaultDuration = helper.determineEatingSoonTTDuration();
defaultTarget = SP.getDouble(R.string.key_eatingsoon_target, 0d); defaultTarget = helper.determineEatingSoonTT(units);
} else if (MainApp.gs(R.string.activity).equals(reasonList.get(position))) { } else if (MainApp.gs(R.string.activity).equals(reasonList.get(position))) {
defaultDuration = SP.getDouble(R.string.key_activity_duration, 0d); defaultDuration = helper.determineActivityTTDuration();
defaultTarget = SP.getDouble(R.string.key_activity_target, 0d); defaultTarget = helper.determineActivityTT(units);
} else if (MainApp.gs(R.string.hypo).equals(reasonList.get(position))) { } else if (MainApp.gs(R.string.hypo).equals(reasonList.get(position))) {
defaultDuration = SP.getDouble(R.string.key_hypo_duration, 0d); defaultDuration = helper.determineHypoTTDuration();
defaultTarget = SP.getDouble(R.string.key_hypo_target, 0d); defaultTarget = helper.determineHypoTT(units);
} else { } else {
defaultDuration = 0; defaultDuration = 0;
erase = true; erase = true;
} }
if (defaultTarget != 0 || erase) { if (defaultTarget != 0 || erase) {
editTemptarget.setValue(defaultTarget); editTemptarget.setValue(defaultTarget);
} }

View file

@ -44,6 +44,7 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.Callback;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.DefaultValueHelper;
import info.nightscout.utils.NumberPicker; import info.nightscout.utils.NumberPicker;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
import info.nightscout.utils.ToastUtils; import info.nightscout.utils.ToastUtils;
@ -290,28 +291,27 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, C
int carbs = editCarbs.getValue().intValue(); int carbs = editCarbs.getValue().intValue();
Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(carbs)).value(); Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(carbs)).value();
int activityTTDuration = SP.getInt(R.string.key_activity_duration, Constants.defaultActivityTTDuration); final String units = currentProfile.getUnits();
activityTTDuration = activityTTDuration > 0 ? activityTTDuration : Constants.defaultActivityTTDuration; DefaultValueHelper helper = new DefaultValueHelper();
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;
int eatingSoonTTDuration = SP.getInt(R.string.key_eatingsoon_duration, Constants.defaultEatingSoonTTDuration); int activityTTDuration = helper.determineActivityTTDuration();
eatingSoonTTDuration = eatingSoonTTDuration > 0 ? eatingSoonTTDuration : Constants.defaultEatingSoonTTDuration; double activityTT = helper.determineActivityTT(units);
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 hypoTTDuration = SP.getInt(R.string.key_hypo_duration, Constants.defaultHypoTTDuration); int eatingSoonTTDuration = helper.determineEatingSoonTTDuration();
hypoTTDuration = hypoTTDuration > 0 ? hypoTTDuration : Constants.defaultHypoTTDuration; double eatingSoonTT = helper.determineEatingSoonTT(units);
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 hypoTTDuration = helper.determineHypoTTDuration();
double hypoTT = helper.determineHypoTT(units);
List<String> actions = new LinkedList<>(); List<String> actions = new LinkedList<>();
if (startActivityTTCheckbox.isChecked()) { if (startActivityTTCheckbox.isChecked()) {
String unitLabel = "mg/dl";
if (currentProfile.getUnits().equals(Constants.MMOL)) { if (currentProfile.getUnits().equals(Constants.MMOL)) {
actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "<font color='" + MainApp.gc(R.color.high) + "'>" + DecimalFormatter.to1Decimal(activityTT) + " mmol/l (" + activityTTDuration + " min)</font>"); unitLabel = "mmol/l";
} else }
actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "<font color='" + MainApp.gc(R.color.high) + "'>" + DecimalFormatter.to0Decimal(activityTT) + " mg/dl (" + activityTTDuration + " min)</font>");
actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "<font color='" + MainApp.gc(R.color.high) + "'>" + DecimalFormatter.to1Decimal(activityTT) + " " + unitLabel + " (" + activityTTDuration + " min)</font>");
} }
if (startEatingSoonTTCheckbox.isChecked()) { if (startEatingSoonTTCheckbox.isChecked()) {

View file

@ -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;
}
}

View file

@ -14,10 +14,9 @@ import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Loop.LoopPlugin; 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.events.EventNewNotification;
import info.nightscout.androidaps.plugins.Overview.notifications.Notification; 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. * Created by adrian on 17/12/17.
@ -26,12 +25,12 @@ import info.nightscout.utils.NSUpload;
public class LocalAlertUtils { public class LocalAlertUtils {
private static Logger log = LoggerFactory.getLogger(LocalAlertUtils.class); private static Logger log = LoggerFactory.getLogger(LocalAlertUtils.class);
public static int missedReadingsThreshold() { public static long missedReadingsThreshold() {
return SP.getInt(MainApp.sResources.getString(R.string.key_missed_bg_readings_threshold), 30) * 60 * 1000; return T.mins(SP.getInt(MainApp.sResources.getString(R.string.key_missed_bg_readings_threshold), 30)).msecs();
} }
private static int pumpUnreachableThreshold() { private static long pumpUnreachableThreshold() {
return SP.getInt(MainApp.sResources.getString(R.string.key_pump_unreachable_threshold), 30) * 60 * 1000; return T.mins(SP.getInt(MainApp.sResources.getString(R.string.key_pump_unreachable_threshold), 30)).msecs();
} }
public static void checkPumpUnreachableAlarm(Date lastConnection, boolean isStatusOutdated) { public static void checkPumpUnreachableAlarm(Date lastConnection, boolean isStatusOutdated) {
@ -49,11 +48,13 @@ public class LocalAlertUtils {
NSUpload.uploadError(n.text); 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. /*Presnoozes the alarms with 5 minutes if no snooze exists.
* Call only at startup! * Call only at startup!
*/ */
public static void presnoozeAlarms() { public static void presnoozeAlarms() {
if (SP.getLong("nextMissedReadingsAlarm", 0l) < System.currentTimeMillis()) { if (SP.getLong("nextMissedReadingsAlarm", 0l) < System.currentTimeMillis()) {
SP.putLong("nextMissedReadingsAlarm", System.currentTimeMillis() + 5 * 60 * 1000); SP.putLong("nextMissedReadingsAlarm", System.currentTimeMillis() + 5 * 60 * 1000);
@ -74,7 +75,7 @@ public class LocalAlertUtils {
SP.putLong("nextPumpDisconnectedAlarm", nextPumpDisconnectedAlarm); 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 //TODO: persist the actual time the pump is read and simplify the whole logic when to alarm
final PumpInterface pump = ConfigBuilderPlugin.getActivePump(); final PumpInterface pump = ConfigBuilderPlugin.getActivePump();

View file

@ -310,13 +310,21 @@ public class NSUpload {
public static void uploadTempTarget(TempTarget tempTarget) { public static void uploadTempTarget(TempTarget tempTarget) {
try { try {
Profile profile = MainApp.getConfigBuilder().getProfile();
if (profile == null) {
log.error("Profile is null. Skipping upload");
return;
}
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
data.put("eventType", CareportalEvent.TEMPORARYTARGET); data.put("eventType", CareportalEvent.TEMPORARYTARGET);
data.put("duration", tempTarget.durationInMinutes); data.put("duration", tempTarget.durationInMinutes);
data.put("reason", tempTarget.reason); data.put("reason", tempTarget.reason);
data.put("targetBottom", tempTarget.low); data.put("targetBottom", Profile.fromMgdlToUnits(tempTarget.low, profile.getUnits()));
data.put("targetTop", tempTarget.high); data.put("targetTop", Profile.fromMgdlToUnits(tempTarget.high, profile.getUnits()));
data.put("created_at", DateUtil.toISOString(tempTarget.date)); data.put("created_at", DateUtil.toISOString(tempTarget.date));
data.put("units", profile.getUnits());
data.put("enteredBy", MainApp.instance().getString(R.string.app_name)); data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
uploadCareportalEntryToNS(data); uploadCareportalEntryToNS(data);
} catch (JSONException e) { } catch (JSONException e) {