diff --git a/app/build.gradle b/app/build.gradle index 392060b6de..268a7951f3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -23,8 +23,8 @@ android { applicationId "info.nightscout.androidaps" minSdkVersion 21 targetSdkVersion 23 - versionCode 1008 - versionName "1.0.08" + versionCode 1009 + versionName "1.0.09" } buildTypes { release { diff --git a/app/src/main/java/info/nightscout/androidaps/db/Treatment.java b/app/src/main/java/info/nightscout/androidaps/db/Treatment.java index bc498246bc..d3084bc4c6 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/Treatment.java +++ b/app/src/main/java/info/nightscout/androidaps/db/Treatment.java @@ -20,6 +20,7 @@ import info.nightscout.androidaps.Config; import info.nightscout.androidaps.data.Iob; import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.client.data.DbLogger; import info.nightscout.client.data.NSProfile; import info.nightscout.utils.DateUtil; @@ -99,10 +100,6 @@ public class Treatment { } public void sendToNSClient() { - Context context = MainApp.instance().getApplicationContext(); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbAdd"); - bundle.putString("collection", "treatments"); JSONObject data = new JSONObject(); try { data.put("eventType", "Meal Bolus"); @@ -113,12 +110,7 @@ public class Treatment { } catch (JSONException e) { e.printStackTrace(); } - bundle.putString("data", data.toString()); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); - DbLogger.dbAdd(intent, data.toString(), Treatment.class); + ConfigBuilderFragment.uploadCareportalEntryToNS(data); } public void updateToNSClient() { 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 86deb47f1a..f4cc251835 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 @@ -46,6 +46,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.plugins.Careportal.CareportalFragment; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.Overview.Dialogs.NewExtendedBolusDialog; import info.nightscout.client.data.DbLogger; import info.nightscout.client.data.NSProfile; @@ -396,7 +397,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick if (layoutPercent.getVisibility() != View.GONE) data.put("percent", SafeParse.stringToDouble(percentEdit.getText().toString())); if (layoutAbsolute.getVisibility() != View.GONE) - data.put("absolute", profileSpinner.getSelectedItem().toString()); + data.put("absolute", SafeParse.stringToDouble(absoluteEdit.getText().toString())); if (options.profile) data.put("profile", profileSpinner.getSelectedItem().toString()); if (SafeParse.stringToDouble(carbTimeEdit.getText().toString()) != 0d) data.put("preBolus", SafeParse.stringToDouble(carbTimeEdit.getText().toString())); @@ -512,40 +513,11 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick builder.setMessage(confirmText); builder.setPositiveButton(getContext().getString(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { - sendTreatmentToNS(data); + ConfigBuilderFragment.uploadCareportalEntryToNS(data); } }); builder.setNegativeButton(getContext().getString(R.string.cancel), null); builder.show(); } - static void sendTreatmentToNS(JSONObject data) { - try { - if (data.has("preBolus") && data.has("carbs")) { - JSONObject prebolus = new JSONObject(); - prebolus.put("carbs", data.get("carbs")); - data.remove("carbs"); - prebolus.put("eventType", data.get("eventType")); - if (data.has("enteredBy")) prebolus.put("enteredBy", data.get("enteredBy")); - if (data.has("notes")) prebolus.put("notes", data.get("notes")); - long mills = DateUtil.fromISODateString(data.getString("created_at")).getTime(); - Date preBolusDate = new Date(mills + data.getInt("preBolus") * 60000L); - prebolus.put("created_at", DateUtil.toISOString(preBolusDate)); - sendTreatmentToNS(prebolus); - } - Context context = MainApp.instance().getApplicationContext(); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbAdd"); - bundle.putString("collection", "treatments"); - bundle.putString("data", data.toString()); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); - DbLogger.dbAdd(intent, data.toString(), NewExtendedBolusDialog.class); - } catch (Exception e) { - e.printStackTrace(); - } - - } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java index 4a1179ae01..276575d326 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java @@ -28,6 +28,7 @@ import org.slf4j.LoggerFactory; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Calendar; import java.util.Date; import java.util.List; @@ -54,6 +55,7 @@ import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.DeviceStatus; import info.nightscout.androidaps.plugins.Loop.LoopFragment; import info.nightscout.androidaps.plugins.OpenAPSMA.DetermineBasalResult; +import info.nightscout.androidaps.plugins.Overview.Dialogs.NewExtendedBolusDialog; import info.nightscout.client.data.DbLogger; import info.nightscout.client.data.NSProfile; import info.nightscout.utils.DateUtil; @@ -284,6 +286,69 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI return activePump.getExtendedBolus(); } +/* +{ + "_id": { + "$oid": "5789fea07ef0c37deb388240" + }, + "boluscalc": { + "profile": "Posunuta snidane", + "eventTime": "2016-07-16T09:30:14.139Z", + "targetBGLow": "5.6", + "targetBGHigh": "5.6", + "isf": "17", + "ic": "26", + "iob": "0.89", + "cob": "0", + "insulincob": "0", + "bg": "3.6", + "insulinbg": "-0.12", + "bgdiff": "-2", + "carbs": "42", + "gi": "2", + "insulincarbs": "1.62", + "othercorrection": "0", + "insulin": "0.6000000000000001", + "roundingcorrection": "-0.009999999999999898", + "carbsneeded": "0" + }, + "enteredBy": "", + "eventType": "Bolus Wizard", + "glucose": 3.6, + "glucoseType": "Sensor", + "units": "mmol", + "carbs": 42, + "insulin": 0.6, + "created_at": "2016-07-16T09:30:12.783Z" +} + */ + + public PumpEnactResult deliverTreatmentFromBolusWizard(Double insulin, Integer carbs, Double glucose, String glucoseType, int carbTime, JSONObject boluscalc) { + insulin = applyBolusConstraints(insulin); + carbs = applyCarbsConstraints(carbs); + + PumpEnactResult result = activePump.deliverTreatment(insulin, carbs); + + if (Config.logCongigBuilderActions) + log.debug("deliverTreatmentFromBolusWizard insulin: " + insulin + " carbs: " + carbs + " success: " + result.success + " enacted: " + result.enacted + " bolusDelivered: " + result.bolusDelivered); + + if (result.success) { + Treatment t = new Treatment(); + t.insulin = result.bolusDelivered; + t.carbs = (double) result.carbsDelivered; + t.created_at = new Date(); + try { + MainApp.getDbHelper().getDaoTreatments().create(t); + } catch (SQLException e) { + e.printStackTrace(); + } + t.setTimeIndex(t.getTimeIndex()); + uploadBolusWizardRecord(t, glucose, glucoseType, carbTime, boluscalc); + MainApp.bus().post(new EventTreatmentChange()); + } + return result; + } + @Override public PumpEnactResult deliverTreatment(Double insulin, Integer carbs) { insulin = applyBolusConstraints(insulin); @@ -1033,4 +1098,53 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI e.printStackTrace(); } } + + public void uploadBolusWizardRecord(Treatment t, double glucose, String glucoseType, int carbTime, JSONObject boluscalc) { + JSONObject data = new JSONObject(); + try { + data.put("eventType", "Bolus Wizard"); + if (t.insulin != 0d) data.put("insulin", t.insulin); + if (t.carbs != 0d) data.put("carbs", t.carbs.intValue()); + data.put("created_at", DateUtil.toISOString(t.created_at)); + data.put("timeIndex", t.timeIndex); + if (glucose != 0d) data.put("glucose", glucose); + data.put("glucoseType", glucoseType); + data.put("boluscalc", boluscalc); + if (carbTime != 0) data.put("preBolus", carbTime); + } catch (JSONException e) { + e.printStackTrace(); + } + uploadCareportalEntryToNS(data); + } + + public static void uploadCareportalEntryToNS(JSONObject data) { + try { + if (data.has("preBolus") && data.has("carbs")) { + JSONObject prebolus = new JSONObject(); + prebolus.put("carbs", data.get("carbs")); + data.remove("carbs"); + prebolus.put("eventType", data.get("eventType")); + if (data.has("enteredBy")) prebolus.put("enteredBy", data.get("enteredBy")); + if (data.has("notes")) prebolus.put("notes", data.get("notes")); + long mills = DateUtil.fromISODateString(data.getString("created_at")).getTime(); + Date preBolusDate = new Date(mills + data.getInt("preBolus") * 60000L); + prebolus.put("created_at", DateUtil.toISOString(preBolusDate)); + uploadCareportalEntryToNS(prebolus); + } + Context context = MainApp.instance().getApplicationContext(); + Bundle bundle = new Bundle(); + bundle.putString("action", "dbAdd"); + bundle.putString("collection", "treatments"); + bundle.putString("data", data.toString()); + Intent intent = new Intent(Intents.ACTION_DATABASE); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + DbLogger.dbAdd(intent, data.toString(), NewExtendedBolusDialog.class); + } catch (Exception e) { + e.printStackTrace(); + } + + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaConnection.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaConnection.java index 302af16966..3c5c24656e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaConnection.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaConnection.java @@ -387,6 +387,7 @@ public class DanaConnection { } public void bolus(Double amount, Treatment t) { + // TODO: progress dialog bolusingTreatment = t; MsgBolusStart start = new MsgBolusStart(amount); MsgBolusProgress progress = new MsgBolusProgress(MainApp.bus(), amount, t); 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 472ca8c0e3..75182bc1be 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 @@ -14,12 +14,20 @@ import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; +import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; +import android.widget.EditText; +import android.widget.Spinner; import android.widget.TextView; +import org.json.JSONException; +import org.json.JSONObject; + import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Date; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; @@ -29,8 +37,10 @@ import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.TempBasalsInterface; import info.nightscout.androidaps.interfaces.TreatmentsInterface; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal; import info.nightscout.client.data.NSProfile; +import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.PlusMinusEditText; import info.nightscout.utils.Round; @@ -55,13 +65,17 @@ public class WizardDialog extends DialogFragment implements OnClickListener { CheckBox basalIobCheckbox; TextView correctionInsulin; TextView total, totalInsulin; + EditText carbTimeEdit; + Spinner profileSpinner; PlusMinusEditText editBg; PlusMinusEditText editCarbs; PlusMinusEditText editCorr; + PlusMinusEditText editCarbTime; Integer calculatedCarbs = 0; Double calculatedTotalInsulin = 0d; + JSONObject boluscalcJSON; Handler mHandler; public static HandlerThread mHandlerThread; @@ -69,7 +83,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener { public WizardDialog() { mHandlerThread = new HandlerThread(NewExtendedBolusDialog.class.getSimpleName()); mHandlerThread.start(); - this.mHandler = new Handler(mHandlerThread.getLooper()); + mHandler = new Handler(mHandlerThread.getLooper()); } final private TextWatcher textWatcher = new TextWatcher() { @@ -126,6 +140,8 @@ public class WizardDialog extends DialogFragment implements OnClickListener { correctionInsulin = (TextView) view.findViewById(R.id.treatments_wizard_correctioninsulin); total = (TextView) view.findViewById(R.id.treatments_wizard_total); totalInsulin = (TextView) view.findViewById(R.id.treatments_wizard_totalinsulin); + carbTimeEdit = (EditText) view.findViewById(R.id.treatments_wizard_carbtimeinput); + profileSpinner = (Spinner) view.findViewById(R.id.treatments_wizard_profile); bgCheckbox.setOnCheckedChangeListener(onCheckedChangeListener); basalIobCheckbox.setOnCheckedChangeListener(onCheckedChangeListener); @@ -137,7 +153,9 @@ public class WizardDialog extends DialogFragment implements OnClickListener { editBg = new PlusMinusEditText(view, R.id.treatments_wizard_bginput, R.id.treatments_wizard_bginput_plus, R.id.treatments_wizard_bginput_minus, 0d, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false); editCarbs = new PlusMinusEditText(view, R.id.treatments_wizard_carbsinput, R.id.treatments_wizard_carbsinput_plus, R.id.treatments_wizard_carbsinput_minus, 0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false); editCorr = new PlusMinusEditText(view, R.id.treatments_wizard_correctioninput, R.id.treatments_wizard_correctioninput_plus, R.id.treatments_wizard_correctioninput_minus, 0d, 0d, maxCorrection, 0.05d, new DecimalFormat("0.00"), false); + editCarbTime = new PlusMinusEditText(view, R.id.treatments_wizard_carbtimeinput, R.id.treatments_wizard_carbtime_plus, R.id.treatments_wizard_carbtime_minus, 0d, -60d, 60d, 5d, new DecimalFormat("0"), false); initDialog(); + return view; } @@ -155,7 +173,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener { confirmMessage += "\n" + getString(R.string.bolus) + ": " + formatNumber2decimalplaces.format(insulinAfterConstraints) + "U"; confirmMessage += "\n" + getString(R.string.carbs) + ": " + carbsAfterConstraints + "g"; - if (insulinAfterConstraints - calculatedTotalInsulin != 0 || carbsAfterConstraints != calculatedCarbs) { + if (insulinAfterConstraints - calculatedTotalInsulin != 0 || carbsAfterConstraints != calculatedCarbs) { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle(getContext().getString(R.string.treatmentdeliveryerror)); builder.setMessage(getString(R.string.constraints_violation) + "\n" + getString(R.string.changeyourinput)); @@ -167,17 +185,24 @@ public class WizardDialog extends DialogFragment implements OnClickListener { final Double finalInsulinAfterConstraints = insulinAfterConstraints; final Integer finalCarbsAfterConstraints = carbsAfterConstraints; - AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext()); - builder.setTitle(this.getContext().getString(R.string.confirmation)); + AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); + builder.setTitle(getContext().getString(R.string.confirmation)); builder.setMessage(confirmMessage); builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) { - final PumpInterface pump = MainApp.getConfigBuilder().getActivePump(); + final ConfigBuilderFragment pump = MainApp.getConfigBuilder(); mHandler.post(new Runnable() { @Override public void run() { - PumpEnactResult result = pump.deliverTreatment(finalInsulinAfterConstraints, finalCarbsAfterConstraints); + PumpEnactResult result = pump.deliverTreatmentFromBolusWizard( + finalInsulinAfterConstraints, + finalCarbsAfterConstraints, + SafeParse.stringToDouble(bgInput.getText().toString()), + "Manual", + SafeParse.stringToInt(carbTimeEdit.getText().toString()), + boluscalcJSON + ); if (!result.success) { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle(getContext().getString(R.string.treatmentdeliveryerror)); @@ -207,13 +232,25 @@ public class WizardDialog extends DialogFragment implements OnClickListener { return; } + ArrayList profileList; + profileList = profile.getProfileList(); + ArrayAdapter adapter = new ArrayAdapter(getContext(), + android.R.layout.simple_spinner_item, profileList); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + profileSpinner.setAdapter(adapter); + // set selected to actual profile + for (int p = 0; p < profileList.size(); p++) { + if (profileList.get(p).equals(profile.getActiveProfile())) + profileSpinner.setSelection(p); + } + String units = profile.getUnits(); bgUnits.setText(units); if (units.equals(Constants.MGDL)) editBg.setStep(1d); else editBg.setStep(0.1d); // Set BG if not old - BgReading lastBg = MainApp.getDbHelper().lastBg(); + BgReading lastBg = MainApp.getDbHelper().actualBg(); if (lastBg != null) { Double lastBgValue = lastBg.valueToUnits(units); @@ -250,21 +287,25 @@ public class WizardDialog extends DialogFragment implements OnClickListener { IobTotal bolusIob = treatments.getLastCalculation(); IobTotal basalIob = tempBasals.getLastCalculation(); - bolusIobInsulin.setText("-" + DecimalFormatter.to2Decimal(bolusIob.iob) + "U"); - basalIobInsulin.setText("-" + DecimalFormatter.to2Decimal(basalIob.basaliob) + "U"); + bolusIobInsulin.setText(DecimalFormatter.to2Decimal(-bolusIob.iob) + "U"); + basalIobInsulin.setText(DecimalFormatter.to2Decimal(-basalIob.basaliob) + "U"); totalInsulin.setText(""); wizardDialogDeliverButton.setVisibility(Button.INVISIBLE); - } + } private void calculateInsulin() { NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); + if (profileSpinner == null || profileSpinner.getSelectedItem() == null) + return; // not initialized yet + String selectedAlternativeProfile = profileSpinner.getSelectedItem().toString(); + JSONObject specificProfile = profile.getSpecificProfile(selectedAlternativeProfile); // Entered values - 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()); + Double c_bg = SafeParse.stringToDouble(bgInput.getText().toString()); + Integer c_carbs = SafeParse.stringToInt(carbsInput.getText().toString()); + Double c_correction = SafeParse.stringToDouble(correctionInput.getText().toString()); Double corrAfterConstraint = MainApp.getConfigBuilder().applyBolusConstraints(c_correction); if (c_correction - corrAfterConstraint != 0) { // c_correction != corrAfterConstraint doesn't work correctionInput.removeTextChangedListener(textWatcher); @@ -286,9 +327,9 @@ public class WizardDialog extends DialogFragment implements OnClickListener { // Insulin from BG - Double sens = profile.getIsf(NSProfile.secondsFromMidnight()); - Double targetBGLow = profile.getTargetLow(NSProfile.secondsFromMidnight()); - Double targetBGHigh = profile.getTargetHigh(NSProfile.secondsFromMidnight()); + Double sens = profile.getIsf(specificProfile, NSProfile.secondsFromMidnight()); + Double targetBGLow = profile.getTargetLow(specificProfile, NSProfile.secondsFromMidnight()); + Double targetBGHigh = profile.getTargetHigh(specificProfile, NSProfile.secondsFromMidnight()); Double bgDiff; if (c_bg <= targetBGLow) { bgDiff = c_bg - targetBGLow; @@ -300,7 +341,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener { bgInsulin.setText(DecimalFormatter.to2Decimal(insulinFromBG) + "U"); // Insuling from carbs - Double ic = profile.getIc(NSProfile.secondsFromMidnight()); + Double ic = profile.getIc(specificProfile, NSProfile.secondsFromMidnight()); Double insulinFromCarbs = c_carbs / ic; carbs.setText(DecimalFormatter.to0Decimal(c_carbs) + "g IC: " + DecimalFormatter.to0Decimal(ic)); carbsInsulin.setText(DecimalFormatter.to2Decimal(insulinFromCarbs) + "U"); @@ -346,5 +387,28 @@ public class WizardDialog extends DialogFragment implements OnClickListener { } else { wizardDialogDeliverButton.setVisibility(Button.INVISIBLE); } + + boluscalcJSON = new JSONObject(); + try { + boluscalcJSON.put("profile", selectedAlternativeProfile); + boluscalcJSON.put("eventTime", DateUtil.toISOString(new Date())); + boluscalcJSON.put("targetBGLow", targetBGLow); + boluscalcJSON.put("targetBGHigh", targetBGHigh); + boluscalcJSON.put("isf", sens); + boluscalcJSON.put("ic", ic); + boluscalcJSON.put("iob", -(insulingFromBolusIOB + insulingFromBasalsIOB)); + boluscalcJSON.put("bolusiobused", bolusIobCheckbox.isChecked()); + boluscalcJSON.put("basaliobused", basalIobCheckbox.isChecked()); + boluscalcJSON.put("bg", c_bg); + boluscalcJSON.put("insulinbg", insulinFromBG); + boluscalcJSON.put("insulinbgused", bgCheckbox.isChecked()); + boluscalcJSON.put("bgdiff", bgDiff); + boluscalcJSON.put("insulincarbs", insulinFromCarbs); + boluscalcJSON.put("carbs", c_carbs); + boluscalcJSON.put("othercorrection", corrAfterConstraint); + boluscalcJSON.put("insulin", calculatedTotalInsulin); + } catch (JSONException e) { + e.printStackTrace(); + } } } \ No newline at end of file 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 e0e3fd270b..c1f3f4e2a8 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 @@ -651,6 +651,7 @@ public class OverviewFragment extends Fragment implements PluginBase { bgGraph.getViewport().setMinY(0); bgGraph.getViewport().setYAxisBoundsManual(true); bgGraph.getGridLabelRenderer().setNumVerticalLabels(numOfHorizLines); + // TODO: add treatments } } 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 1ad2ab5575..af94dc27bb 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 @@ -177,7 +177,7 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt @Override public TempBasal getTempBasal(Date time) { - return MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(new Date()); + return MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(time); } @Override diff --git a/app/src/main/java/info/nightscout/client/data/NSProfile.java b/app/src/main/java/info/nightscout/client/data/NSProfile.java index 6c1287e302..4d60a0353e 100644 --- a/app/src/main/java/info/nightscout/client/data/NSProfile.java +++ b/app/src/main/java/info/nightscout/client/data/NSProfile.java @@ -31,13 +31,25 @@ public class NSProfile { defaultProfileName = (String) json.get("defaultProfile"); store = json.getJSONObject("store"); if (activeProfile != null && store.has(activeProfile)) { - defaultProfileName = activeProfile; + defaultProfileName = activeProfile; } profile = store.getJSONObject(defaultProfileName); } catch (JSONException e) { e.printStackTrace(); } + return profile; + } + public JSONObject getSpecificProfile(String profileName) { + JSONObject profile = null; + try { + JSONObject store = json.getJSONObject("store"); + if (store.has(profileName)) { + profile = store.getJSONObject(profileName); + } + } catch (JSONException e) { + e.printStackTrace(); + } return profile; } @@ -50,7 +62,7 @@ public class NSProfile { store = json.getJSONObject("store"); Iterator keys = store.keys(); - while( keys.hasNext() ) { + while (keys.hasNext()) { String profileName = (String) keys.next(); ret.add(profileName); } @@ -63,7 +75,7 @@ public class NSProfile { public String log() { String ret = "\n"; - for (Integer hour = 0; hour < 24; hour ++) { + for (Integer hour = 0; hour < 24; hour++) { double value = getBasal(hour * 60 * 60); ret += "NS basal value for " + hour + ":00 is " + value + "\n"; } @@ -71,13 +83,16 @@ public class NSProfile { return ret; } - public JSONObject getData () { + public JSONObject getData() { return json; } public Double getDia() { + return getDia(getDefaultProfile()); + } + + public Double getDia(JSONObject profile) { Double dia; - JSONObject profile = getDefaultProfile(); if (profile != null) { try { dia = profile.getDouble("dia"); @@ -89,9 +104,12 @@ public class NSProfile { return 3D; } - public Double getCarbAbsorbtionRate() { + public Double getCarbAbsorbtionRate() { + return getCarbAbsorbtionRate(getDefaultProfile()); + } + + public Double getCarbAbsorbtionRate(JSONObject profile) { Double carbAbsorptionRate; - JSONObject profile = getDefaultProfile(); if (profile != null) { try { carbAbsorptionRate = profile.getDouble("carbs_hr"); @@ -100,13 +118,16 @@ public class NSProfile { e.printStackTrace(); } } - return 0D; + return 0D; } // mmol or mg/dl - public String getUnits() { - String units; - JSONObject profile = getDefaultProfile(); + public String getUnits() { + return getUnits(getDefaultProfile()); + } + + public String getUnits(JSONObject profile) { + String units; if (profile != null) { try { units = profile.getString("units"); @@ -115,12 +136,15 @@ public class NSProfile { e.printStackTrace(); } } - return "mg/dl"; + return "mg/dl"; } - public TimeZone getTimeZone() { - TimeZone timeZone; - JSONObject profile = getDefaultProfile(); + public TimeZone getTimeZone() { + return getTimeZone(getDefaultProfile()); + } + + public TimeZone getTimeZone(JSONObject profile) { + TimeZone timeZone; if (profile != null) { try { return TimeZone.getTimeZone(profile.getString("timezone")); @@ -128,13 +152,13 @@ public class NSProfile { e.printStackTrace(); } } - return TimeZone.getDefault(); + return TimeZone.getDefault(); } public Double getValueToTime(JSONArray array, Integer timeAsSeconds) { Double lastValue = null; - for(Integer index = 0; index < array.length(); index++) { + for (Integer index = 0; index < array.length(); index++) { try { JSONObject o = array.getJSONObject(index); Integer tas = o.getInt("timeAsSeconds"); @@ -154,7 +178,7 @@ public class NSProfile { public String getValuesList(JSONArray array, JSONArray array2, DecimalFormat format, String units) { String retValue = ""; - for(Integer index = 0; index < array.length(); index++) { + for (Integer index = 0; index < array.length(); index++) { try { JSONObject o = array.getJSONObject(index); retValue += o.getString("time"); @@ -175,10 +199,13 @@ public class NSProfile { } public Double getIsf(Integer timeAsSeconds) { - JSONObject profile = getDefaultProfile(); + return getIsf(getDefaultProfile(), timeAsSeconds); + } + + public Double getIsf(JSONObject profile, Integer timeAsSeconds) { if (profile != null) { try { - return getValueToTime(profile.getJSONArray("sens"),timeAsSeconds); + return getValueToTime(profile.getJSONArray("sens"), timeAsSeconds); } catch (JSONException e) { e.printStackTrace(); } @@ -187,7 +214,10 @@ public class NSProfile { } public String getIsfList() { - JSONObject profile = getDefaultProfile(); + return getIsfList(getDefaultProfile()); + } + + public String getIsfList(JSONObject profile) { if (profile != null) { try { return getValuesList(profile.getJSONArray("sens"), null, new DecimalFormat("0.0"), getUnits() + "/U"); @@ -199,10 +229,13 @@ public class NSProfile { } public Double getIc(Integer timeAsSeconds) { - JSONObject profile = getDefaultProfile(); + return getIc(getDefaultProfile(), timeAsSeconds); + } + + public Double getIc(JSONObject profile, Integer timeAsSeconds) { if (profile != null) { try { - return getValueToTime(profile.getJSONArray("carbratio"),timeAsSeconds); + return getValueToTime(profile.getJSONArray("carbratio"), timeAsSeconds); } catch (JSONException e) { e.printStackTrace(); } @@ -211,7 +244,10 @@ public class NSProfile { } public String getIcList() { - JSONObject profile = getDefaultProfile(); + return getIcList(getDefaultProfile()); + } + + public String getIcList(JSONObject profile) { if (profile != null) { try { return getValuesList(profile.getJSONArray("carbratio"), null, new DecimalFormat("0.0"), "g"); @@ -223,10 +259,13 @@ public class NSProfile { } public Double getBasal(Integer timeAsSeconds) { - JSONObject profile = getDefaultProfile(); + return getBasal(getDefaultProfile(), timeAsSeconds); + } + + public Double getBasal(JSONObject profile, Integer timeAsSeconds) { if (profile != null) { try { - return getValueToTime(profile.getJSONArray("basal"),timeAsSeconds); + return getValueToTime(profile.getJSONArray("basal"), timeAsSeconds); } catch (JSONException e) { e.printStackTrace(); } @@ -235,7 +274,10 @@ public class NSProfile { } public String getBasalList() { - JSONObject profile = getDefaultProfile(); + return getBasalList(getDefaultProfile()); + } + + public String getBasalList(JSONObject profile) { if (profile != null) { try { return getValuesList(profile.getJSONArray("basal"), null, new DecimalFormat("0.00"), "U"); @@ -247,10 +289,13 @@ public class NSProfile { } public Double getTargetLow(Integer timeAsSeconds) { - JSONObject profile = getDefaultProfile(); + return getTargetLow(getDefaultProfile(), timeAsSeconds); + } + + public Double getTargetLow(JSONObject profile, Integer timeAsSeconds) { if (profile != null) { try { - return getValueToTime(profile.getJSONArray("target_low"),timeAsSeconds); + return getValueToTime(profile.getJSONArray("target_low"), timeAsSeconds); } catch (JSONException e) { e.printStackTrace(); } @@ -259,7 +304,10 @@ public class NSProfile { } public Double getTargetHigh(Integer timeAsSeconds) { - JSONObject profile = getDefaultProfile(); + return getTargetHigh(getDefaultProfile(), timeAsSeconds); + } + + public Double getTargetHigh(JSONObject profile, Integer timeAsSeconds) { if (profile != null) { try { return getValueToTime(profile.getJSONArray("target_high"), timeAsSeconds); @@ -270,11 +318,14 @@ public class NSProfile { return 0D; } - public String getTargetList() { - JSONObject profile = getDefaultProfile(); + public String getTargetList() { + return getTargetList(getDefaultProfile()); + } + + public String getTargetList(JSONObject profile) { if (profile != null) { try { - return getValuesList(profile.getJSONArray("target_low"),profile.getJSONArray("target_high"), new DecimalFormat("0.0"), getUnits()); + return getValuesList(profile.getJSONArray("target_low"), profile.getJSONArray("target_high"), new DecimalFormat("0.0"), getUnits()); } catch (JSONException e) { e.printStackTrace(); } @@ -288,7 +339,7 @@ public class NSProfile { public Double getMaxDailyBasal() { Double max = 0d; - for (Integer hour = 0; hour < 24; hour ++) { + for (Integer hour = 0; hour < 24; hour++) { double value = getBasal(hour * 60 * 60); if (value > max) max = value; } diff --git a/app/src/main/java/info/nightscout/utils/DateUtil.java b/app/src/main/java/info/nightscout/utils/DateUtil.java index a0f52f2abc..6734953cc2 100644 --- a/app/src/main/java/info/nightscout/utils/DateUtil.java +++ b/app/src/main/java/info/nightscout/utils/DateUtil.java @@ -9,11 +9,12 @@ import java.util.TimeZone; * The Class DateUtil. A simple wrapper around SimpleDateFormat to ease the handling of iso date string <-> date obj * with TZ */ -public class DateUtil -{ +public class DateUtil { - /** The date format in iso. */ - public static String FORMAT_DATE_ISO="yyyy-MM-dd'T'HH:mm:ss'Z'"; + /** + * The date format in iso. + */ + public static String FORMAT_DATE_ISO = "yyyy-MM-dd'T'HH:mm:ss'Z'"; /** * Takes in an ISO date string of the following format: @@ -24,29 +25,30 @@ public class DateUtil * @throws Exception the exception */ public static Date fromISODateString(String isoDateString) - throws Exception - { - DateFormat f = new SimpleDateFormat(FORMAT_DATE_ISO); - return f.parse(isoDateString); + throws Exception { + SimpleDateFormat f = new SimpleDateFormat(FORMAT_DATE_ISO); + f.setTimeZone(TimeZone.getTimeZone("UTC")); + Date date = f.parse(isoDateString); + return date; } /** * Render date * - * @param date the date obj + * @param date the date obj * @param format - if not specified, will use FORMAT_DATE_ISO - * @param tz - tz to set to, if not specified uses local timezone + * @param tz - tz to set to, if not specified uses local timezone * @return the iso-formatted date string */ - public static String toISOString(Date date, String format, TimeZone tz) - { - if( format == null ) format = FORMAT_DATE_ISO; - if( tz == null ) tz = TimeZone.getDefault(); + public static String toISOString(Date date, String format, TimeZone tz) { + if (format == null) format = FORMAT_DATE_ISO; + if (tz == null) tz = TimeZone.getDefault(); DateFormat f = new SimpleDateFormat(format); f.setTimeZone(tz); return f.format(date); } - public static String toISOString(Date date) - { return toISOString(date,FORMAT_DATE_ISO,TimeZone.getTimeZone("UTC")); } + public static String toISOString(Date date) { + return toISOString(date, FORMAT_DATE_ISO, TimeZone.getTimeZone("UTC")); + } } \ No newline at end of file diff --git a/app/src/main/res/layout/overview_wizard_dialog.xml b/app/src/main/res/layout/overview_wizard_dialog.xml index 82dc753c24..00b0df9e2a 100644 --- a/app/src/main/res/layout/overview_wizard_dialog.xml +++ b/app/src/main/res/layout/overview_wizard_dialog.xml @@ -1,5 +1,6 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + android:orientation="horizontal"> - + - + + + + + + + + + + + + android:orientation="horizontal"> - + + + + + + + + android:orientation="horizontal"> - + - + - + - + + - + android:orientation="horizontal"> - + - + + + + + + + + android:orientation="horizontal"> - + - + - + - + + - + android:orientation="horizontal"> - + - + + + + + + + + android:orientation="horizontal"> - + + + + + + + + + + android:layout_gravity="center" + android:orientation="horizontal"> + + + + + + + + + + + + +