From 5c4910ed5bff5ced5e52140141a0b589511d7ea0 Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Thu, 30 May 2019 12:16:42 +0300 Subject: [PATCH 1/8] added trigger Delta --- .../dialogs/ChooseTriggerDialog.java | 2 + .../automation/elements/InputDelta.java | 132 +++++++++++ .../automation/triggers/TriggerDelta.java | 220 ++++++++++++++++++ app/src/main/res/drawable/as.png | Bin 0 -> 1639 bytes app/src/main/res/values/strings.xml | 4 +- .../automation/triggers/TriggerDeltaTest.java | 151 ++++++++++++ 6 files changed, 508 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java create mode 100644 app/src/main/res/drawable/as.png create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.java index cd251add31..956d802be0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.java @@ -19,6 +19,7 @@ import butterknife.Unbinder; import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.general.automation.triggers.Trigger; import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerBg; +import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerDelta; import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerIob; import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerLocation; import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerProfilePercent; @@ -37,6 +38,7 @@ public class ChooseTriggerDialog extends DialogFragment { add(new TriggerTime()); add(new TriggerRecurringTime()); add(new TriggerBg()); + add(new TriggerDelta()); add(new TriggerIob()); add(new TriggerProfilePercent()); add(new TriggerTempTarget()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java new file mode 100644 index 0000000000..d75d73e495 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java @@ -0,0 +1,132 @@ +package info.nightscout.androidaps.plugins.general.automation.elements; + +import android.text.Editable; +import android.text.TextWatcher; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.LinearLayout; +import android.widget.Spinner; + +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.List; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.utils.NumberPicker; + +public class InputDelta extends Element { + private Comparator.Compare compare = Comparator.Compare.IS_EQUAL; + + final TextWatcher textWatcher = new TextWatcher() { + @Override + public void afterTextChanged(Editable s) { + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + }; + + private double value; + double minValue; + double maxValue; + private double step; + private DecimalFormat decimalFormat; + private int deltaType; + + NumberPicker numberPicker; + + public InputDelta() { + super(); + } + + public InputDelta(double value, double minValue, double maxValue, double step, DecimalFormat decimalFormat, int deltaType) { + super(); + this.value = value; + this.minValue = minValue; + this.maxValue = maxValue; + this.step = step; + this.decimalFormat = decimalFormat; + this.deltaType = deltaType; + } + + public InputDelta(InputDelta another) { + super(); + value = another.getValue(); + minValue = another.minValue; + maxValue = another.maxValue; + step = another.step; + decimalFormat = another.decimalFormat; + deltaType = another.deltaType; + } + + + @Override + public void addToLayout(LinearLayout root) { + Spinner spinner = new Spinner(root.getContext()); + ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(root.getContext(), android.R.layout.simple_spinner_item, InputDelta.labels()); + spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spinner.setAdapter(spinnerArrayAdapter); + LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, + LinearLayout.LayoutParams.WRAP_CONTENT + ); + spinnerParams.setMargins(0, MainApp.dpToPx(4), 0, MainApp.dpToPx(4)); + spinner.setLayoutParams(spinnerParams); + spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + deltaType = spinner.getSelectedItemPosition(); + } + + @Override + public void onNothingSelected(AdapterView parent) { + } + }); + spinner.setSelection(this.deltaType); +// root.addView(spinner); + numberPicker = new NumberPicker(root.getContext(), null); + numberPicker.setParams(value, minValue, maxValue, step, decimalFormat, true, textWatcher); + numberPicker.setOnValueChangedListener(value -> this.value = value); + LinearLayout l = new LinearLayout(root.getContext()); + l.setOrientation(LinearLayout.VERTICAL); + l.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + + l.addView(spinner); + l.addView(numberPicker); + root.addView(l); + + } + + public InputDelta setValue(double value, int type) { + this.value = value; + this.deltaType = type; + if (numberPicker != null) + numberPicker.setValue(value); + return this; + } + + public double getValue() { + return value; + } + + public int getDeltaType() { + return deltaType; + } + + public static List labels() { + List list = new ArrayList<>(); + list.add(MainApp.gs(R.string.delta)); + list.add(MainApp.gs(R.string.short_avgdelta)); + list.add(MainApp.gs(R.string.long_avgdelta)); + return list; + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java new file mode 100644 index 0000000000..a9f1a7d2ae --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java @@ -0,0 +1,220 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers; + + +import android.support.v4.app.FragmentManager; +import android.widget.LinearLayout; + +import com.google.common.base.Optional; + +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.text.DecimalFormat; + +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; +import info.nightscout.androidaps.plugins.general.automation.elements.InputDelta; +import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement; +import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder; +import info.nightscout.androidaps.plugins.general.automation.elements.StaticLabel; +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; +import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.JsonHelper; +import info.nightscout.androidaps.utils.T; + +public class TriggerDelta extends Trigger { + private static Logger log = LoggerFactory.getLogger(L.AUTOMATION); + private double minValue = 0d; + private double maxValue = 1d; + private double step = 1; + private DecimalFormat decimalFormat = new DecimalFormat("1"); + private String units = ProfileFunctions.getInstance().getProfileUnits(); + private int deltaType = 0; // 0 is delta, 1 is short average delta, 2 is long average delta + + private InputDelta value = new InputDelta( (double) minValue,(double) minValue, (double) maxValue, step, decimalFormat, deltaType); + private Comparator comparator = new Comparator(); + + public TriggerDelta() { + super(); + setUnits(); + } + + private TriggerDelta(TriggerDelta triggerDelta) { + super(); + setUnits(); + value = triggerDelta.value; + lastRun = triggerDelta.lastRun; + } + + public double getValue() { + return value.getValue(); + } + + public double getType() { return value.getDeltaType(); } + + public String getUnits() { + return this.units; + } + + public Comparator getComparator() { + return comparator; + } + + @Override + public synchronized boolean shouldRun() { + GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); + if (glucoseStatus == null) + if (comparator.getValue() == Comparator.Compare.IS_NOT_AVAILABLE) + return true; + else + return false; + + // Setting type of delta + double delta; + + if (deltaType == 1) + delta = glucoseStatus.short_avgdelta; + else if (deltaType == 2) + delta = glucoseStatus.long_avgdelta; + else + delta = glucoseStatus.delta; + + if (lastRun > DateUtil.now() - T.mins(5).msecs()) + return false; + + if (glucoseStatus == null && comparator.getValue().equals(Comparator.Compare.IS_NOT_AVAILABLE)) { + if (L.isEnabled(L.AUTOMATION)) + log.debug("Ready for execution: delta is " + delta + friendlyDescription()); + return true; + } + + boolean doRun = comparator.getValue().check(delta, Profile.toMgdl(value.getValue(), this.units)); + if (doRun) { + if (L.isEnabled(L.AUTOMATION)) + log.debug("Ready for execution: delta is " + delta + " " + friendlyDescription()); + return true; + } + return false; + } + + @Override + public synchronized String toJSON() { + JSONObject o = new JSONObject(); + try { + o.put("type", TriggerDelta.class.getName()); + JSONObject data = new JSONObject(); + data.put("value", getValue()); + data.put("units", units); + data.put("lastRun", lastRun); + data.put("type", getType()); + data.put("comparator", comparator.getValue().toString()); + o.put("data", data); + } catch (JSONException e) { + e.printStackTrace(); + } + return o.toString(); + } + + @Override + Trigger fromJSON(String data) { + try { + JSONObject d = new JSONObject(data); + units = JsonHelper.safeGetString(d, "units"); + deltaType = JsonHelper.safeGetInt(d, "type"); + value.setValue(JsonHelper.safeGetDouble(d, "value"), deltaType); + lastRun = JsonHelper.safeGetLong(d, "lastRun"); + comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); + } catch (Exception e) { + e.printStackTrace(); + } + return this; + } + + @Override + public int friendlyName() { + return R.string.deltalabel; + } + + @Override + public String friendlyDescription() { + return MainApp.gs(R.string.deltacompared, MainApp.gs(comparator.getValue().getStringRes()), getValue(), typeToString(deltaType)); + } + + @Override + public Optional icon() { + return Optional.of(R.drawable.as); // TODO: Icon for delta + } + + @Override + public Trigger duplicate() { + return new TriggerDelta(this); + } + + TriggerDelta setValue(double requestedValue) { + this.value.setValue(requestedValue, deltaType); + return this; + } + + TriggerDelta setUnits(String units) { + this.units = units; + return this; + } + + void setUnits(){ + if (this.units.equals(Constants.MMOL)) { + this.maxValue = 4d; + this.minValue = 0.1d; + this.step = 0.1d; + this.decimalFormat = new DecimalFormat("0.1"); + this.deltaType = 0; + } else { + this.maxValue = 72d; + this.minValue = 2d; + this.step = 1d; + this.deltaType = 0; + } + value = new InputDelta( (double) minValue,(double) minValue, (double) maxValue, step, decimalFormat, deltaType); + } + + + TriggerDelta lastRun(long lastRun) { + this.lastRun = lastRun; + return this; + } + + TriggerDelta comparator(Comparator.Compare compare) { + this.comparator = new Comparator().setValue(compare); + return this; + } + + @Override + public void generateDialog(LinearLayout root, FragmentManager fragmentManager) { + + new LayoutBuilder() + .add(new StaticLabel(R.string.deltalabel)) + .add(comparator) + .add(new LabelWithElement(MainApp.gs(R.string.deltalabel) + ": ", "", value)) + .build(root); + } + + public String typeToString( int type ) { + switch (type) { + case 0: + return MainApp.gs(R.string.delta); + case 1: + return MainApp.gs(R.string.short_avgdelta); + case 2: + return MainApp.gs(R.string.long_avgdelta); + default: + return MainApp.gs(R.string.delta); + } + } + +} \ No newline at end of file diff --git a/app/src/main/res/drawable/as.png b/app/src/main/res/drawable/as.png new file mode 100644 index 0000000000000000000000000000000000000000..a37e4c34650a11891f07bf834cc9aa0a8a18f5c6 GIT binary patch literal 1639 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`k|nMYCBgY=CFO}lsSJ)O`AMk? zp1FzXsX?iUDV2pMQ*9U+nA0*tB1$5BeXNr6bM+EIYV;~{3xK*A7;Nk-3KEmEQ%e+* zQqwc@Y?a>c-mj#PnPRIHZt82`Ti~3Uk?B!Ylp0*+7m{3+ootz+WN)WnQ(*-(AUCxn zQK2F?C$HG5!d3}vt`(3C64qBz04piUwpD^SD#ABF!8yMuRl!uxSU1_g&``n5OwZ87 z)XdCKN5ROz&`93^h|F{iO{`4Ktc=VRpg;*|TTx1yRgjAt)Gi>;Rw<*Tq`*pFzr4I$ zuiRKKzbIYb(9+TpWQLKEE>MMTab;dfVufyAu`kBtHuNWFoz#!AFNG#Ad)HBe}%?0@jth%@)C>7xhtg4GcDhpEegHnt0 zON)|$@sXws(+mtd{1$-}0$pR}Uz7=ql*AmD{N&Qy)VvZ;7h5Huj9yA+ij|qUrKzit zfu)O~xw)&Qp`oL(le2}3nVGAbk%g0mfjP_!Yn>gL>67S11)*dm3Grhu&b>Ib)Y_+mlk-?>UhR=qy`0F5p3aM`+;VO{ z9a7>=D!x8pfH1iz@H^E)BVUWkq=2 zosi7C?lMQzmPK#3^WQP=i>+(zWyL36Uv{hA<0}-It+Hq~=Nu&~`R`wDZGWG4;JO9> zwpDyb*Icr&HjDDudA;S2dE;f5-@4ORr~aRq%(2GpVfT><0xS|adXF``pL6GB;SQj0dV%uzWQ7vBCj{k-C2!Hp9B2ZSRotXnI)&s^U@ zUf*!>U&hrJJC;mfJG|+FsK~RuKFiM?73%nGlQ!+J#H+fN42w0K8n@bG?+Zi*3J4lS z@co#uBweFxR)6@YUVsT6~+7g^5!or70c}smh6{y{3P_X#c2uy59_Wy z`q!j4oQ>=~u{`s`M325#QT3cDrdOYDU#~2~not exists Temp target %1$s WiFi SSID %1$s %2$s + BG %3$s %1$s %2$s + BG difference Current Location Location Lat: @@ -1420,4 +1422,4 @@ %1$d minute %1$d minutes - + \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java new file mode 100644 index 0000000000..fc86f9adb3 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java @@ -0,0 +1,151 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers; + +import com.google.common.base.Optional; +import com.squareup.otto.Bus; + +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.ArrayList; +import java.util.List; + +import info.AAPSMocker; +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.BgReading; +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; +import info.nightscout.androidaps.plugins.general.nsclient.data.NSSgv; +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; +import info.nightscout.androidaps.utils.DateUtil; + +import static org.powermock.api.mockito.PowerMockito.when; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, Bus.class, ProfileFunctions.class, DateUtil.class, IobCobCalculatorPlugin.class, GlucoseStatus.class}) +public class TriggerDeltaTest { + + long now = 1514766900000L; + + @Test + public void shouldRunTest() { + when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(generateValidBgData()); + + TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(73d).comparator(Comparator.Compare.IS_EQUAL); + Assert.assertFalse(t.shouldRun()); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-2d).comparator(Comparator.Compare.IS_EQUAL); + Assert.assertTrue(t.shouldRun()); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-3d).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); + Assert.assertTrue(t.shouldRun()); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + Assert.assertTrue(t.shouldRun()); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d).comparator(Comparator.Compare.IS_EQUAL); + Assert.assertFalse(t.shouldRun()); + t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.3d).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + Assert.assertTrue(t.shouldRun()); + t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.1d).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); + Assert.assertFalse(t.shouldRun()); + t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.5d).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); + Assert.assertTrue(t.shouldRun()); + t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.2d).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + Assert.assertFalse(t.shouldRun()); + + when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(new ArrayList<>()); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + Assert.assertFalse(t.shouldRun()); + t = new TriggerDelta().comparator(Comparator.Compare.IS_NOT_AVAILABLE); + Assert.assertTrue(t.shouldRun()); + + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(214).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); + Assert.assertFalse(t.shouldRun()); + + } + + @Test + public void copyConstructorTest() { + TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + TriggerDelta t1 = (TriggerDelta) t.duplicate(); + Assert.assertEquals(213d, t1.getValue(), 0.01d); + Assert.assertEquals(Constants.MGDL, t1.getUnits()); + Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue()); + } + + @Test + public void executeTest() { + TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + t.executed(1); + Assert.assertEquals(1l, t.getLastRun()); + } + + String deltaJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"units\":\"mmol\",\"type\":0,\"value\":4.1},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerDelta\"}"; + + @Test + public void toJSONTest() { + TriggerDelta t = new TriggerDelta().setUnits(Constants.MMOL).setValue(4.1d).comparator(Comparator.Compare.IS_EQUAL); + Assert.assertEquals(deltaJson, t.toJSON()); + } + + @Test + public void fromJSONTest() throws JSONException { + TriggerDelta t = new TriggerDelta().setUnits(Constants.MMOL).setValue(4.1d).comparator(Comparator.Compare.IS_EQUAL); + + TriggerDelta t2 = (TriggerDelta) Trigger.instantiate(new JSONObject(t.toJSON())); + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue()); + Assert.assertEquals(4.1d, t2.getValue(), 0.01d); + Assert.assertEquals(Constants.MMOL, t2.getUnits()); + Assert.assertEquals(0d, t2.getType(), 0.01d); + } + + @Test + public void iconTest() { + Assert.assertEquals(Optional.of(R.drawable.as), new TriggerDelta().icon()); + } + + @Test + public void typeToStringTest() { + TriggerDelta t = new TriggerDelta(); + Assert.assertEquals(MainApp.gs(R.string.delta), t.typeToString(0)); + Assert.assertEquals(MainApp.gs(R.string.short_avgdelta), t.typeToString(1)); + Assert.assertEquals(MainApp.gs(R.string.long_avgdelta), t.typeToString(2)); + } + + + @Before + public void mock() { + AAPSMocker.mockMainApp(); + AAPSMocker.mockBus(); + AAPSMocker.mockIobCobCalculatorPlugin(); + AAPSMocker.mockProfileFunctions(); + AAPSMocker.mockApplicationContext(); + + PowerMockito.mockStatic(DateUtil.class); + when(DateUtil.now()).thenReturn(now); + + } + + List generateValidBgData() { + List list = new ArrayList<>(); + try { + list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":214,\"mills\":1514766900000,\"direction\":\"Flat\"}")))); + list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":216,\"mills\":1514766600000,\"direction\":\"Flat\"}")))); // +2 + list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":219,\"mills\":1514766300000,\"direction\":\"Flat\"}")))); // +3 + list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":223,\"mills\":1514766000000,\"direction\":\"Flat\"}")))); // +4 + list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":222,\"mills\":1514765700000,\"direction\":\"Flat\"}")))); + list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":224,\"mills\":1514765400000,\"direction\":\"Flat\"}")))); + list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":226,\"mills\":1514765100000,\"direction\":\"Flat\"}")))); + list.add(new BgReading(new NSSgv(new JSONObject("{\"mgdl\":228,\"mills\":1514764800000,\"direction\":\"Flat\"}")))); + } catch (JSONException e) { + e.printStackTrace(); + } + return list; + } +} From 116865896beda38cf2f72ee248dba95eea722f37 Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Mon, 3 Jun 2019 15:06:40 +0300 Subject: [PATCH 2/8] Automation (#69) (#70) * Adding trigger AutosensValue * new trigger and tests * AS value in % * adding tests * fixed tests * fix for maxValue and tests * Using InputDouble * icon added * unavailable data and IS_NOT_AVAILABLE case * Fixing after comments * Autosens !=0 && IS_NOT_AVAILABLE doesn't exist enymore * cleanup --- .../dialogs/ChooseTriggerDialog.java | 2 + .../triggers/TriggerAutosensValue.java | 152 ++++++++++++++++++ app/src/main/res/values/strings.xml | 2 + .../triggers/TriggerAutosensValueTest.java | 132 +++++++++++++++ 4 files changed, 288 insertions(+) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValue.java create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValueTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.java index 956d802be0..353ce05e90 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.java @@ -27,6 +27,7 @@ import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerRec import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTempTarget; import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTime; import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerWifiSsid; +import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAutosensValue; public class ChooseTriggerDialog extends DialogFragment { @@ -44,6 +45,7 @@ public class ChooseTriggerDialog extends DialogFragment { add(new TriggerTempTarget()); add(new TriggerWifiSsid()); add(new TriggerLocation()); + add(new TriggerAutosensValue()); }}; private Unbinder mUnbinder; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValue.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValue.java new file mode 100644 index 0000000000..98c8928dcf --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValue.java @@ -0,0 +1,152 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers; + + +import android.support.v4.app.FragmentManager; +import android.widget.LinearLayout; + +import com.google.common.base.Optional; + +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.text.DecimalFormat; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; +import info.nightscout.androidaps.plugins.general.automation.elements.InputDouble; +import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement; +import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder; +import info.nightscout.androidaps.plugins.general.automation.elements.StaticLabel; +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData; +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; +import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.JsonHelper; +import info.nightscout.androidaps.utils.SP; +import info.nightscout.androidaps.utils.T; + +public class TriggerAutosensValue extends Trigger { + private static Logger log = LoggerFactory.getLogger(L.AUTOMATION); + private final int minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); + private final int maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); + private final double step = 1; + private DecimalFormat decimalFormat = new DecimalFormat("1"); + private InputDouble value = new InputDouble(100, (double) minValue, (double) maxValue, step, decimalFormat); + private Comparator comparator = new Comparator(); + + public TriggerAutosensValue() { + super(); + } + + private TriggerAutosensValue(TriggerAutosensValue triggerAutosensValue) { + super(); + value = new InputDouble(triggerAutosensValue.value); + lastRun = triggerAutosensValue.lastRun; + comparator = new Comparator(triggerAutosensValue.comparator); + } + + public double getValue() { + return value.getValue(); + } + + public Comparator getComparator() { + return comparator; + } + + @Override + public synchronized boolean shouldRun() { + AutosensData autosensData = IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Automation trigger"); + if (autosensData == null) + if (comparator.getValue() == Comparator.Compare.IS_NOT_AVAILABLE) + return true; + else + return false; + + if (lastRun > DateUtil.now() - T.mins(5).msecs()) + return false; + + boolean doRun = comparator.getValue().check((autosensData.autosensResult.ratio), getValue() / 100d); + if (doRun) { + if (L.isEnabled(L.AUTOMATION)) + log.debug("Ready for execution: " + friendlyDescription()); + return true; + } + return false; + } + + @Override + public synchronized String toJSON() { + JSONObject o = new JSONObject(); + try { + o.put("type", TriggerAutosensValue.class.getName()); + JSONObject data = new JSONObject(); + data.put("value", getValue()); + data.put("lastRun", lastRun); + data.put("comparator", comparator.getValue().toString()); + o.put("data", data); + } catch (JSONException e) { + e.printStackTrace(); + } + return o.toString(); + } + + @Override + Trigger fromJSON(String data) { + try { + JSONObject d = new JSONObject(data); + value.setValue(JsonHelper.safeGetDouble(d, "value")); + lastRun = JsonHelper.safeGetLong(d, "lastRun"); + comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); + } catch (Exception e) { + e.printStackTrace(); + } + return this; + } + + @Override + public int friendlyName() { + return R.string.autosenslabel; + } + + @Override + public String friendlyDescription() { + return MainApp.gs(R.string.autosenscompared, MainApp.gs(comparator.getValue().getStringRes()), getValue()); + } + + @Override + public Optional icon() { + return Optional.of(R.drawable.as); + } + + @Override + public Trigger duplicate() { + return new TriggerAutosensValue(this); + } + + TriggerAutosensValue setValue(int requestedValue) { + this.value.setValue(requestedValue); + return this; + } + + TriggerAutosensValue lastRun(long lastRun) { + this.lastRun = lastRun; + return this; + } + + TriggerAutosensValue comparator(Comparator.Compare compare) { + this.comparator = new Comparator().setValue(compare); + return this; + } + + @Override + public void generateDialog(LinearLayout root, FragmentManager fragmentManager) { + new LayoutBuilder() + .add(new StaticLabel(R.string.autosenslabel)) + .add(comparator) + .add(new LabelWithElement(MainApp.gs(R.string.autosenslabel) + ": ", "", value)) + .build(root); + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f258c3ef9d..a8a5925d6f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1400,6 +1400,8 @@ not exists Temp target %1$s WiFi SSID %1$s %2$s + Autosens %1$s %2$s %% + Autosens % BG %3$s %1$s %2$s BG difference Current Location diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValueTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValueTest.java new file mode 100644 index 0000000000..ab1851aefc --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValueTest.java @@ -0,0 +1,132 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers; + +import com.google.common.base.Optional; +import com.squareup.otto.Bus; + +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import info.AAPSMocker; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData; +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; +import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.SP; + +import static org.mockito.ArgumentMatchers.anyDouble; +import static org.powermock.api.mockito.PowerMockito.when; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, Bus.class, ProfileFunctions.class, DateUtil.class, IobCobCalculatorPlugin.class, SP.class}) +public class TriggerAutosensValueTest { + + long now = 1514766900000L; + + @Test + public void shouldRunTest() { + Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d); + Mockito.when(SP.getDouble(Mockito.eq("openapsama_autosens_min"), anyDouble())).thenReturn(0.7d); + when(IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Automation trigger")).thenReturn(generateAutosensData()); + TriggerAutosensValue t = new TriggerAutosensValue().setValue(110).comparator(Comparator.Compare.IS_EQUAL); + Assert.assertEquals(110, t.getValue(), 0.01d); + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t.getComparator().getValue()); + Assert.assertFalse(t.shouldRun()); + t = new TriggerAutosensValue().setValue(100).comparator(Comparator.Compare.IS_EQUAL); + Assert.assertEquals(100, t.getValue(), 0.01d); + Assert.assertTrue(t.shouldRun()); + t = new TriggerAutosensValue().setValue(50).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); + Assert.assertTrue(t.shouldRun()); + t = new TriggerAutosensValue().setValue(310).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + Assert.assertTrue(t.shouldRun()); + t = new TriggerAutosensValue().setValue(420).comparator(Comparator.Compare.IS_EQUAL); + Assert.assertFalse(t.shouldRun()); + t = new TriggerAutosensValue().setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + Assert.assertTrue(t.shouldRun()); + t = new TriggerAutosensValue().setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); + Assert.assertFalse(t.shouldRun()); + t = new TriggerAutosensValue().setValue(20).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); + Assert.assertTrue(t.shouldRun()); + t = new TriggerAutosensValue().setValue(390).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + Assert.assertTrue(t.shouldRun()); + + when(IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Automation trigger")).thenReturn(new AutosensData()); + t = new TriggerAutosensValue().setValue(80).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + Assert.assertFalse(t.shouldRun()); + + // Test autosensData == null and Comparator == IS_NOT_AVAILABLE + when(IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Automation trigger")).thenReturn(null); + t = new TriggerAutosensValue().comparator(Comparator.Compare.IS_NOT_AVAILABLE); + Assert.assertTrue(t.shouldRun()); + + t = new TriggerAutosensValue().setValue(214).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); + Assert.assertFalse(t.shouldRun()); + + } + + @Test + public void copyConstructorTest() { + TriggerAutosensValue t = new TriggerAutosensValue().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + TriggerAutosensValue t1 = (TriggerAutosensValue) t.duplicate(); + Assert.assertEquals(213, t1.getValue(), 0.01d); + Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue()); + } + + @Test + public void executeTest() { + TriggerAutosensValue t = new TriggerAutosensValue().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + t.executed(1); + Assert.assertEquals(1l, t.getLastRun()); + } + + String ASJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"value\":410},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAutosensValue\"}"; + + @Test + public void toJSONTest() { + TriggerAutosensValue t = new TriggerAutosensValue().setValue(410).comparator(Comparator.Compare.IS_EQUAL); + Assert.assertEquals(ASJson, t.toJSON()); + } + + @Test + public void fromJSONTest() throws JSONException { + TriggerAutosensValue t = new TriggerAutosensValue().setValue(410).comparator(Comparator.Compare.IS_EQUAL); + + TriggerAutosensValue t2 = (TriggerAutosensValue) Trigger.instantiate(new JSONObject(t.toJSON())); + Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue()); + Assert.assertEquals(410, t2.getValue(), 0.01d); + } + + @Test + public void iconTest() { + Assert.assertEquals(Optional.of(R.drawable.as), new TriggerAutosensValue().icon()); + } + + + @Before + public void mock() { + AAPSMocker.mockMainApp(); + AAPSMocker.mockBus(); + AAPSMocker.mockIobCobCalculatorPlugin(); + AAPSMocker.mockProfileFunctions(); + AAPSMocker.mockSP(); + PowerMockito.mockStatic(DateUtil.class); + when(DateUtil.now()).thenReturn(now); + + } + + AutosensData generateAutosensData() { + AutosensData data = new AutosensData(); + return data; + } + +} From 2a023232a7620ff5ca883dd0672f8d2b3fddab1f Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Mon, 17 Jun 2019 11:22:58 +0300 Subject: [PATCH 3/8] added enum DeltaType --- .../automation/elements/InputDelta.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java index d75d73e495..2aeae192e5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.general.automation.elements; +import android.support.annotation.StringRes; import android.text.Editable; import android.text.TextWatcher; import android.view.View; @@ -34,6 +35,25 @@ public class InputDelta extends Element { } }; + public enum DeltaType { + DELTA, + SHORT_AVERAGE, + LONG_AVERAGE; + + public @StringRes + int getStringRes() { + switch (this) { + case DELTA: + return R.string.delta; + case SHORT_AVERAGE: + return R.string.short_avgdelta; + case LONG_AVERAGE: + return R.string.long_avgdelta; + default: + return R.string.unknown; + } + } + } private double value; double minValue; double maxValue; From 6e14e3df90c56822d5b6dc03654c90ec577cbc88 Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Mon, 17 Jun 2019 16:03:35 +0300 Subject: [PATCH 4/8] working again --- .../automation/elements/InputDelta.java | 36 ++++++++--------- .../automation/triggers/TriggerDelta.java | 39 +++++++------------ 2 files changed, 32 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java index 2aeae192e5..c035bffe41 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java @@ -20,7 +20,6 @@ import info.nightscout.androidaps.utils.NumberPicker; public class InputDelta extends Element { private Comparator.Compare compare = Comparator.Compare.IS_EQUAL; - final TextWatcher textWatcher = new TextWatcher() { @Override public void afterTextChanged(Editable s) { @@ -53,13 +52,22 @@ public class InputDelta extends Element { return R.string.unknown; } } + + public static List labels() { + List list = new ArrayList<>(); + for (DeltaType d : DeltaType.values()) { + list.add(MainApp.gs(d.getStringRes())); + } + return list; + } } + private double value; double minValue; double maxValue; private double step; private DecimalFormat decimalFormat; - private int deltaType; + private DeltaType deltaType; NumberPicker numberPicker; @@ -67,7 +75,7 @@ public class InputDelta extends Element { super(); } - public InputDelta(double value, double minValue, double maxValue, double step, DecimalFormat decimalFormat, int deltaType) { + public InputDelta(double value, double minValue, double maxValue, double step, DecimalFormat decimalFormat, DeltaType deltaType) { super(); this.value = value; this.minValue = minValue; @@ -91,7 +99,7 @@ public class InputDelta extends Element { @Override public void addToLayout(LinearLayout root) { Spinner spinner = new Spinner(root.getContext()); - ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(root.getContext(), android.R.layout.simple_spinner_item, InputDelta.labels()); + ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(root.getContext(), android.R.layout.simple_spinner_item, DeltaType.labels()); spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(spinnerArrayAdapter); LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams( @@ -103,14 +111,14 @@ public class InputDelta extends Element { spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { - deltaType = spinner.getSelectedItemPosition(); + deltaType = DeltaType.values()[position]; } @Override public void onNothingSelected(AdapterView parent) { } }); - spinner.setSelection(this.deltaType); + spinner.setSelection(this.deltaType.ordinal()); // root.addView(spinner); numberPicker = new NumberPicker(root.getContext(), null); numberPicker.setParams(value, minValue, maxValue, step, decimalFormat, true, textWatcher); @@ -118,14 +126,12 @@ public class InputDelta extends Element { LinearLayout l = new LinearLayout(root.getContext()); l.setOrientation(LinearLayout.VERTICAL); l.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); - l.addView(spinner); l.addView(numberPicker); root.addView(l); - } - public InputDelta setValue(double value, int type) { + public InputDelta setValue(double value, DeltaType type) { this.value = value; this.deltaType = type; if (numberPicker != null) @@ -137,16 +143,8 @@ public class InputDelta extends Element { return value; } - public int getDeltaType() { + public DeltaType getDeltaType() { return deltaType; } - public static List labels() { - List list = new ArrayList<>(); - list.add(MainApp.gs(R.string.delta)); - list.add(MainApp.gs(R.string.short_avgdelta)); - list.add(MainApp.gs(R.string.long_avgdelta)); - return list; - } - -} +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java index a9f1a7d2ae..0760e9e027 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java @@ -24,6 +24,7 @@ import info.nightscout.androidaps.plugins.general.automation.elements.InputDelta import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement; import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder; import info.nightscout.androidaps.plugins.general.automation.elements.StaticLabel; +import info.nightscout.androidaps.plugins.general.automation.elements.InputDelta.DeltaType; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.JsonHelper; @@ -36,7 +37,7 @@ public class TriggerDelta extends Trigger { private double step = 1; private DecimalFormat decimalFormat = new DecimalFormat("1"); private String units = ProfileFunctions.getInstance().getProfileUnits(); - private int deltaType = 0; // 0 is delta, 1 is short average delta, 2 is long average delta + private DeltaType deltaType; private InputDelta value = new InputDelta( (double) minValue,(double) minValue, (double) maxValue, step, decimalFormat, deltaType); private Comparator comparator = new Comparator(); @@ -54,10 +55,13 @@ public class TriggerDelta extends Trigger { } public double getValue() { + deltaType = value.getDeltaType(); return value.getValue(); } - public double getType() { return value.getDeltaType(); } + public DeltaType getType() { + return value.getDeltaType(); + } public String getUnits() { return this.units; @@ -79,9 +83,9 @@ public class TriggerDelta extends Trigger { // Setting type of delta double delta; - if (deltaType == 1) + if (deltaType == DeltaType.SHORT_AVERAGE) delta = glucoseStatus.short_avgdelta; - else if (deltaType == 2) + else if (deltaType == DeltaType.LONG_AVERAGE) delta = glucoseStatus.long_avgdelta; else delta = glucoseStatus.delta; @@ -127,7 +131,8 @@ public class TriggerDelta extends Trigger { try { JSONObject d = new JSONObject(data); units = JsonHelper.safeGetString(d, "units"); - deltaType = JsonHelper.safeGetInt(d, "type"); + int savedDeltaType = JsonHelper.safeGetInt(d, "type"); + deltaType = DeltaType.valueOf(JsonHelper.safeGetString(d, "type", "")); value.setValue(JsonHelper.safeGetDouble(d, "value"), deltaType); lastRun = JsonHelper.safeGetLong(d, "lastRun"); comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); @@ -144,7 +149,7 @@ public class TriggerDelta extends Trigger { @Override public String friendlyDescription() { - return MainApp.gs(R.string.deltacompared, MainApp.gs(comparator.getValue().getStringRes()), getValue(), typeToString(deltaType)); + return MainApp.gs(R.string.deltacompared, MainApp.gs(comparator.getValue().getStringRes()), getValue(), deltaType); } @Override @@ -157,8 +162,8 @@ public class TriggerDelta extends Trigger { return new TriggerDelta(this); } - TriggerDelta setValue(double requestedValue) { - this.value.setValue(requestedValue, deltaType); + TriggerDelta setValue(double requestedValue, DeltaType requestedType) { + this.value.setValue(requestedValue, requestedType); return this; } @@ -173,12 +178,12 @@ public class TriggerDelta extends Trigger { this.minValue = 0.1d; this.step = 0.1d; this.decimalFormat = new DecimalFormat("0.1"); - this.deltaType = 0; + this.deltaType = DeltaType.DELTA; } else { this.maxValue = 72d; this.minValue = 2d; this.step = 1d; - this.deltaType = 0; + this.deltaType = DeltaType.DELTA; } value = new InputDelta( (double) minValue,(double) minValue, (double) maxValue, step, decimalFormat, deltaType); } @@ -196,7 +201,6 @@ public class TriggerDelta extends Trigger { @Override public void generateDialog(LinearLayout root, FragmentManager fragmentManager) { - new LayoutBuilder() .add(new StaticLabel(R.string.deltalabel)) .add(comparator) @@ -204,17 +208,4 @@ public class TriggerDelta extends Trigger { .build(root); } - public String typeToString( int type ) { - switch (type) { - case 0: - return MainApp.gs(R.string.delta); - case 1: - return MainApp.gs(R.string.short_avgdelta); - case 2: - return MainApp.gs(R.string.long_avgdelta); - default: - return MainApp.gs(R.string.delta); - } - } - } \ No newline at end of file From f15ead9c6f3132a309cc9d12a6fa52b3e19a2986 Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Mon, 17 Jun 2019 16:49:57 +0300 Subject: [PATCH 5/8] fixed tests and added DeltaType testing --- .../automation/triggers/TriggerDelta.java | 13 ++--- app/src/main/res/values/strings.xml | 2 +- .../automation/triggers/TriggerDeltaTest.java | 49 ++++++++++--------- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java index 0760e9e027..cf3d200d0e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java @@ -93,12 +93,6 @@ public class TriggerDelta extends Trigger { if (lastRun > DateUtil.now() - T.mins(5).msecs()) return false; - if (glucoseStatus == null && comparator.getValue().equals(Comparator.Compare.IS_NOT_AVAILABLE)) { - if (L.isEnabled(L.AUTOMATION)) - log.debug("Ready for execution: delta is " + delta + friendlyDescription()); - return true; - } - boolean doRun = comparator.getValue().check(delta, Profile.toMgdl(value.getValue(), this.units)); if (doRun) { if (L.isEnabled(L.AUTOMATION)) @@ -117,7 +111,7 @@ public class TriggerDelta extends Trigger { data.put("value", getValue()); data.put("units", units); data.put("lastRun", lastRun); - data.put("type", getType()); + data.put("deltaType", getType()); data.put("comparator", comparator.getValue().toString()); o.put("data", data); } catch (JSONException e) { @@ -131,8 +125,7 @@ public class TriggerDelta extends Trigger { try { JSONObject d = new JSONObject(data); units = JsonHelper.safeGetString(d, "units"); - int savedDeltaType = JsonHelper.safeGetInt(d, "type"); - deltaType = DeltaType.valueOf(JsonHelper.safeGetString(d, "type", "")); + deltaType = DeltaType.valueOf(JsonHelper.safeGetString(d, "deltaType", "")); value.setValue(JsonHelper.safeGetDouble(d, "value"), deltaType); lastRun = JsonHelper.safeGetLong(d, "lastRun"); comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); @@ -149,7 +142,7 @@ public class TriggerDelta extends Trigger { @Override public String friendlyDescription() { - return MainApp.gs(R.string.deltacompared, MainApp.gs(comparator.getValue().getStringRes()), getValue(), deltaType); + return MainApp.gs(R.string.deltacompared, MainApp.gs(comparator.getValue().getStringRes()), getValue(), MainApp.gs(deltaType.getStringRes())); } @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a8a5925d6f..58cf9bce14 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1402,7 +1402,7 @@ WiFi SSID %1$s %2$s Autosens %1$s %2$s %% Autosens % - BG %3$s %1$s %2$s + %3$s %1$s %2$s BG difference Current Location Location diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java index fc86f9adb3..23b104991d 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java @@ -23,6 +23,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; +import info.nightscout.androidaps.plugins.general.automation.elements.InputDelta.DeltaType; import info.nightscout.androidaps.plugins.general.nsclient.data.NSSgv; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; @@ -40,69 +41,70 @@ public class TriggerDeltaTest { public void shouldRunTest() { when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(generateValidBgData()); - TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(73d).comparator(Comparator.Compare.IS_EQUAL); + TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(73d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL); Assert.assertFalse(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-2d).comparator(Comparator.Compare.IS_EQUAL); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-2d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL); Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-3d).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-3d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d).comparator(Comparator.Compare.IS_EQUAL); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL); Assert.assertFalse(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.3d).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.3d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.1d).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); + t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.1d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertFalse(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.5d).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); + t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.5d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.2d).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.2d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertFalse(t.shouldRun()); when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(new ArrayList<>()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertFalse(t.shouldRun()); t = new TriggerDelta().comparator(Comparator.Compare.IS_NOT_AVAILABLE); Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(214).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(214, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); Assert.assertFalse(t.shouldRun()); } @Test public void copyConstructorTest() { - TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); TriggerDelta t1 = (TriggerDelta) t.duplicate(); Assert.assertEquals(213d, t1.getValue(), 0.01d); Assert.assertEquals(Constants.MGDL, t1.getUnits()); + Assert.assertEquals(DeltaType.DELTA, t.getType()); Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue()); } @Test public void executeTest() { - TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); t.executed(1); Assert.assertEquals(1l, t.getLastRun()); } - String deltaJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"units\":\"mmol\",\"type\":0,\"value\":4.1},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerDelta\"}"; + String deltaJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"deltaType\":\"DELTA\",\"units\":\"mmol\",\"value\":4.1},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerDelta\"}"; @Test public void toJSONTest() { - TriggerDelta t = new TriggerDelta().setUnits(Constants.MMOL).setValue(4.1d).comparator(Comparator.Compare.IS_EQUAL); + TriggerDelta t = new TriggerDelta().setUnits(Constants.MMOL).setValue(4.1d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL); Assert.assertEquals(deltaJson, t.toJSON()); } @Test public void fromJSONTest() throws JSONException { - TriggerDelta t = new TriggerDelta().setUnits(Constants.MMOL).setValue(4.1d).comparator(Comparator.Compare.IS_EQUAL); + TriggerDelta t = new TriggerDelta().setUnits(Constants.MMOL).setValue(4.1d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL); TriggerDelta t2 = (TriggerDelta) Trigger.instantiate(new JSONObject(t.toJSON())); Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue()); Assert.assertEquals(4.1d, t2.getValue(), 0.01d); Assert.assertEquals(Constants.MMOL, t2.getUnits()); - Assert.assertEquals(0d, t2.getType(), 0.01d); + Assert.assertEquals(DeltaType.DELTA, t2.getType()); } @Test @@ -111,14 +113,15 @@ public class TriggerDeltaTest { } @Test - public void typeToStringTest() { - TriggerDelta t = new TriggerDelta(); - Assert.assertEquals(MainApp.gs(R.string.delta), t.typeToString(0)); - Assert.assertEquals(MainApp.gs(R.string.short_avgdelta), t.typeToString(1)); - Assert.assertEquals(MainApp.gs(R.string.long_avgdelta), t.typeToString(2)); + public void deltaTypeTest() { + TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + Assert.assertEquals(DeltaType.DELTA, t.getType()); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.SHORT_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + Assert.assertEquals(DeltaType.SHORT_AVERAGE, t.getType()); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + Assert.assertEquals(DeltaType.LONG_AVERAGE, t.getType()); } - @Before public void mock() { AAPSMocker.mockMainApp(); From b9ddbe70111c3d9ef776656b701c2080eef0ef0c Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Tue, 18 Jun 2019 10:56:27 +0300 Subject: [PATCH 6/8] tests --- .../automation/triggers/TriggerDelta.java | 18 ++++++++-- .../automation/triggers/TriggerDeltaTest.java | 35 ++++++++++++++----- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java index cf3d200d0e..cc5651f41b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java @@ -44,12 +44,12 @@ public class TriggerDelta extends Trigger { public TriggerDelta() { super(); - setUnits(); + initializer(); } private TriggerDelta(TriggerDelta triggerDelta) { super(); - setUnits(); + initializer(); value = triggerDelta.value; lastRun = triggerDelta.lastRun; } @@ -165,7 +165,7 @@ public class TriggerDelta extends Trigger { return this; } - void setUnits(){ + void initializer(){ if (this.units.equals(Constants.MMOL)) { this.maxValue = 4d; this.minValue = 0.1d; @@ -201,4 +201,16 @@ public class TriggerDelta extends Trigger { .build(root); } + //Used for testing deltaType + public double deltaValue(){ + GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); + + if (deltaType == DeltaType.SHORT_AVERAGE) + return glucoseStatus.short_avgdelta; + else if (deltaType == DeltaType.LONG_AVERAGE) + return glucoseStatus.long_avgdelta; + else + return glucoseStatus.delta; + } + } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java index 23b104991d..5a09a19700 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java @@ -20,6 +20,7 @@ import info.AAPSMocker; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; @@ -43,21 +44,21 @@ public class TriggerDeltaTest { TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(73d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL); Assert.assertFalse(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-2d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-2d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL); Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-3d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-3d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL); Assert.assertFalse(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.3d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.3d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.1d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); + t = new TriggerDelta().setUnits(Constants.MMOL).setValue(0.1d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertFalse(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.5d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); + t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.5d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.2d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); + t = new TriggerDelta().setUnits(Constants.MMOL).setValue(-0.2d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertFalse(t.shouldRun()); when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(new ArrayList<>()); @@ -114,12 +115,18 @@ public class TriggerDeltaTest { @Test public void deltaTypeTest() { + when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(generateValidBgData()); TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertEquals(DeltaType.DELTA, t.getType()); + Assert.assertEquals(-2d, t.deltaValue(), 0d); + Assert.assertTrue(t.shouldRun()); t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.SHORT_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertEquals(DeltaType.SHORT_AVERAGE, t.getType()); + Assert.assertEquals(-2d, t.deltaValue(), 0d); t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertEquals(DeltaType.LONG_AVERAGE, t.getType()); + Assert.assertEquals(-2d, t.deltaValue(), 0d); + } @Before @@ -135,6 +142,18 @@ public class TriggerDeltaTest { } + public void initializerTest(){ + PowerMockito.when(ProfileFunctions.getInstance().getProfileUnits()).thenReturn(Constants.MMOL); + TriggerDelta t = new TriggerDelta(); + Assert.assertTrue(t.getUnits().equals(Constants.MMOL)); + Assert.assertTrue(t.getUnits().equals(Constants.MGDL)); + Assert.assertNull(t.getUnits()); + Assert.assertNotNull(t.getUnits()); + when(ProfileFunctions.getInstance().getProfileUnits()).thenReturn(Constants.MGDL); + t = new TriggerDelta(); + Assert.assertEquals(Constants.MGDL, t.getUnits()); + } + List generateValidBgData() { List list = new ArrayList<>(); try { From 3f86788eb4b21f0441614edcade6db1e7d395446 Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Tue, 18 Jun 2019 14:47:44 +0300 Subject: [PATCH 7/8] Added DeltaType tests and negative delta values allowed --- .../automation/triggers/TriggerDelta.java | 23 ++++------- .../automation/triggers/TriggerDeltaTest.java | 40 +++++++------------ 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java index cc5651f41b..ce5c922aa8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java @@ -36,7 +36,7 @@ public class TriggerDelta extends Trigger { private double maxValue = 1d; private double step = 1; private DecimalFormat decimalFormat = new DecimalFormat("1"); - private String units = ProfileFunctions.getInstance().getProfileUnits(); + private String units; private DeltaType deltaType; private InputDelta value = new InputDelta( (double) minValue,(double) minValue, (double) maxValue, step, decimalFormat, deltaType); @@ -44,11 +44,13 @@ public class TriggerDelta extends Trigger { public TriggerDelta() { super(); + this.units = ProfileFunctions.getInstance().getProfileUnits(); initializer(); } private TriggerDelta(TriggerDelta triggerDelta) { super(); + this.units = ProfileFunctions.getInstance().getProfileUnits(); initializer(); value = triggerDelta.value; lastRun = triggerDelta.lastRun; @@ -60,7 +62,7 @@ public class TriggerDelta extends Trigger { } public DeltaType getType() { - return value.getDeltaType(); + return deltaType; } public String getUnits() { @@ -157,6 +159,7 @@ public class TriggerDelta extends Trigger { TriggerDelta setValue(double requestedValue, DeltaType requestedType) { this.value.setValue(requestedValue, requestedType); + this.deltaType = requestedType; return this; } @@ -168,13 +171,13 @@ public class TriggerDelta extends Trigger { void initializer(){ if (this.units.equals(Constants.MMOL)) { this.maxValue = 4d; - this.minValue = 0.1d; + this.minValue = -4d; this.step = 0.1d; this.decimalFormat = new DecimalFormat("0.1"); this.deltaType = DeltaType.DELTA; } else { this.maxValue = 72d; - this.minValue = 2d; + this.minValue = -72d; this.step = 1d; this.deltaType = DeltaType.DELTA; } @@ -201,16 +204,4 @@ public class TriggerDelta extends Trigger { .build(root); } - //Used for testing deltaType - public double deltaValue(){ - GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); - - if (deltaType == DeltaType.SHORT_AVERAGE) - return glucoseStatus.short_avgdelta; - else if (deltaType == DeltaType.LONG_AVERAGE) - return glucoseStatus.long_avgdelta; - else - return glucoseStatus.delta; - } - } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java index 5a09a19700..d93cd97989 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDeltaTest.java @@ -20,7 +20,6 @@ import info.AAPSMocker; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; @@ -41,13 +40,18 @@ public class TriggerDeltaTest { @Test public void shouldRunTest() { when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(generateValidBgData()); - - TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(73d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL); + //Test if time passed is less than 5 min + TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-3d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER).lastRun(now-1); Assert.assertFalse(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-2d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL); - Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-3d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(73d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL); + Assert.assertFalse(t.shouldRun()); + Assert.assertEquals(DeltaType.LONG_AVERAGE, t.getType()); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-2d, DeltaType.SHORT_AVERAGE).comparator(Comparator.Compare.IS_EQUAL); + Assert.assertFalse(t.shouldRun()); + Assert.assertEquals(DeltaType.SHORT_AVERAGE, t.getType()); + t = new TriggerDelta().setUnits(Constants.MGDL).setValue(-3d, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER); Assert.assertTrue(t.shouldRun()); + Assert.assertEquals(DeltaType.DELTA, t.getType()); t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); Assert.assertTrue(t.shouldRun()); t = new TriggerDelta().setUnits(Constants.MGDL).setValue(2d, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL); @@ -70,6 +74,8 @@ public class TriggerDeltaTest { t = new TriggerDelta().setUnits(Constants.MGDL).setValue(214, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1); Assert.assertFalse(t.shouldRun()); + + } @Test @@ -113,22 +119,6 @@ public class TriggerDeltaTest { Assert.assertEquals(Optional.of(R.drawable.as), new TriggerDelta().icon()); } - @Test - public void deltaTypeTest() { - when(IobCobCalculatorPlugin.getPlugin().getBgReadings()).thenReturn(generateValidBgData()); - TriggerDelta t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.DELTA).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertEquals(DeltaType.DELTA, t.getType()); - Assert.assertEquals(-2d, t.deltaValue(), 0d); - Assert.assertTrue(t.shouldRun()); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.SHORT_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertEquals(DeltaType.SHORT_AVERAGE, t.getType()); - Assert.assertEquals(-2d, t.deltaValue(), 0d); - t = new TriggerDelta().setUnits(Constants.MGDL).setValue(213, DeltaType.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); - Assert.assertEquals(DeltaType.LONG_AVERAGE, t.getType()); - Assert.assertEquals(-2d, t.deltaValue(), 0d); - - } - @Before public void mock() { AAPSMocker.mockMainApp(); @@ -142,14 +132,12 @@ public class TriggerDeltaTest { } + @Test public void initializerTest(){ PowerMockito.when(ProfileFunctions.getInstance().getProfileUnits()).thenReturn(Constants.MMOL); TriggerDelta t = new TriggerDelta(); Assert.assertTrue(t.getUnits().equals(Constants.MMOL)); - Assert.assertTrue(t.getUnits().equals(Constants.MGDL)); - Assert.assertNull(t.getUnits()); - Assert.assertNotNull(t.getUnits()); - when(ProfileFunctions.getInstance().getProfileUnits()).thenReturn(Constants.MGDL); + PowerMockito.when(ProfileFunctions.getInstance().getProfileUnits()).thenReturn(Constants.MGDL); t = new TriggerDelta(); Assert.assertEquals(Constants.MGDL, t.getUnits()); } From f8b3878899a0bd1274d020d9a50dd90f213f3c34 Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Wed, 19 Jun 2019 12:11:10 +0300 Subject: [PATCH 8/8] icon --- .../general/automation/triggers/TriggerDelta.java | 2 +- app/src/main/res/drawable/icon_auto_delta.png | Bin 0 -> 392 bytes .../automation/triggers/TriggerDeltaTest.java | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 app/src/main/res/drawable/icon_auto_delta.png diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java index ce5c922aa8..dcf6e187e4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java @@ -149,7 +149,7 @@ public class TriggerDelta extends Trigger { @Override public Optional icon() { - return Optional.of(R.drawable.as); // TODO: Icon for delta + return Optional.of(R.drawable.icon_auto_delta); } @Override diff --git a/app/src/main/res/drawable/icon_auto_delta.png b/app/src/main/res/drawable/icon_auto_delta.png new file mode 100644 index 0000000000000000000000000000000000000000..ddb0930b0b74d5fbaff5a5a0d69ddd40da580069 GIT binary patch literal 392 zcmV;30eAk1P)(_`g8%^e{{R4h=>PzAFaQARU;qF*m;eA5Z<1fd zMgRZ-4oO5oRCwB?(ydCwK@-{^wHkEWP3h{a-?U>3w62o^s;i(%CbW(5Ub!7PeF z41&>M5Zizcuw`FBn|8-C!k=|n-Sxt7=bX8mIrq+loU{MLK)1svOh@6mZvm`eD+;sy z3hb9$?^z%UI~c|r4x%vFtAitK;s!Nr|5+dkix^At6YdJ#=*7VqE+NU!Xkk7I3%?3P zVFq(JsnF*o@3q&5_Ky^WTMQ<7t-_7qy}