From e898a8444c06297710cb34114fe74450824665a5 Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Tue, 14 May 2019 16:47:37 +0300 Subject: [PATCH 01/12] Adding trigger AutosensValue --- app/src/main/res/values/strings.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dee0a621f8..40541e4c82 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 ratio Current Location Location Lat: From e220a42e4b4e6774dcaa3eb72ec8aa41f81918c8 Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Tue, 14 May 2019 16:50:07 +0300 Subject: [PATCH 02/12] new trigger and tests --- .../dialogs/ChooseTriggerDialog.java | 2 + .../automation/elements/InputAutosens.java | 88 +++++++++++ .../triggers/TriggerAutosensValue.java | 148 ++++++++++++++++++ 3 files changed, 238 insertions(+) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosens.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValue.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..7f3560bb18 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 @@ -26,6 +26,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 { @@ -42,6 +43,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/elements/InputAutosens.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosens.java new file mode 100644 index 0000000000..634db34d40 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosens.java @@ -0,0 +1,88 @@ +package info.nightscout.androidaps.plugins.general.automation.elements; + +import android.text.Editable; +import android.text.TextWatcher; +import android.widget.LinearLayout; + +import java.text.DecimalFormat; + +import info.nightscout.androidaps.utils.NumberPicker; +import info.nightscout.androidaps.utils.SP; + +public class InputAutosens extends Element { + + private double value; + double minValue = SP.getDouble("key_openapsama_autosens_min", 0.7d); + double maxValue = SP.getDouble("key_openapsama_autosens_max", 1.2d); + private double step = 0.01d; + private DecimalFormat decimalFormat = new DecimalFormat("0.00");; + + NumberPicker numberPicker; + final TextWatcher textWatcher = new TextWatcher() { + @Override + public void afterTextChanged(Editable s) { + value = Math.max(value, 0.7d); + value = Math.min(value, 2d); + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + }; + + + + + public InputAutosens() { + super(); + } + + public InputAutosens(double value, double minValue, double maxValue, double step, DecimalFormat decimalFormat) { + super(); + this.value = value; + this.minValue = minValue; + this.maxValue = maxValue; + this.step = step; + this.decimalFormat = decimalFormat; + } + + public InputAutosens(InputAutosens another) { + super(); + value = another.getValue(); + minValue = another.minValue; + maxValue = another.maxValue; + step = another.step; + decimalFormat = another.decimalFormat; + } + + + @Override + public void addToLayout(LinearLayout root) { + numberPicker = new NumberPicker(root.getContext(), null); + numberPicker.setParams(value, minValue, maxValue, step, decimalFormat, true, textWatcher); + numberPicker.setOnValueChangedListener(value -> this.value = value); + root.addView(numberPicker); + } + + public InputAutosens setValue(double value) { + minValue = SP.getDouble("key_openapsama_autosens_min", 0.7d); + maxValue = SP.getDouble("key_openapsama_autosens_max", 1.2d); + if (value > maxValue) + value = Math.max(value, this.maxValue); + if (value < minValue) + value = minValue; + this.value = value; + if (numberPicker != null) + numberPicker.setValue(value); + return this; + } + + public double getValue() { + return value; + } + +} 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..ac8f572f9e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValue.java @@ -0,0 +1,148 @@ +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 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.InputAutosens; +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.T; + +public class TriggerAutosensValue extends Trigger { + private static Logger log = LoggerFactory.getLogger(L.AUTOMATION); + + private InputAutosens value = new InputAutosens(); + private Comparator comparator = new Comparator(); + + public TriggerAutosensValue() { + super(); + } + + private TriggerAutosensValue(TriggerAutosensValue triggerAutosensValue) { + super(); + value = triggerAutosensValue.value; + lastRun = triggerAutosensValue.lastRun; + } + + 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) + return false; + + if (lastRun > DateUtil.now() - T.mins(5).msecs()) + return false; + + if (autosensData.autosensResult.ratio != 0 && comparator.getValue() == Comparator.Compare.IS_NOT_AVAILABLE) { + if (L.isEnabled(L.AUTOMATION)) + log.debug("Ready for execution: " + friendlyDescription()); + return true; + } + + boolean doRun = comparator.getValue().check((autosensData.autosensResult.ratio), getValue()); + 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.remove); // TODO icon + } + + @Override + public Trigger duplicate() { + return new TriggerAutosensValue(this); + } + + TriggerAutosensValue setValue(double 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); + } +} \ No newline at end of file From 5d86ca742afa89328a130ccc053be6a14199411c Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Wed, 15 May 2019 14:52:17 +0300 Subject: [PATCH 03/12] AS value in % --- .../automation/elements/InputAutosens.java | 41 +++++++++++-------- .../triggers/TriggerAutosensValue.java | 8 ++-- app/src/main/res/values/strings.xml | 4 +- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosens.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosens.java index 634db34d40..bda2b4fdf5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosens.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosens.java @@ -10,19 +10,18 @@ import info.nightscout.androidaps.utils.NumberPicker; import info.nightscout.androidaps.utils.SP; public class InputAutosens extends Element { - - private double value; - double minValue = SP.getDouble("key_openapsama_autosens_min", 0.7d); - double maxValue = SP.getDouble("key_openapsama_autosens_max", 1.2d); - private double step = 0.01d; - private DecimalFormat decimalFormat = new DecimalFormat("0.00");; + private int value; + int minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); + int maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); + private double step = 1; + private DecimalFormat decimalFormat = new DecimalFormat("1");; NumberPicker numberPicker; final TextWatcher textWatcher = new TextWatcher() { @Override public void afterTextChanged(Editable s) { - value = Math.max(value, 0.7d); - value = Math.min(value, 2d); + value = Math.max(value, 70); + value = Math.min(value, 200); } @Override @@ -43,9 +42,9 @@ public class InputAutosens extends Element { public InputAutosens(double value, double minValue, double maxValue, double step, DecimalFormat decimalFormat) { super(); - this.value = value; - this.minValue = minValue; - this.maxValue = maxValue; + this.value = (int) value; + this.minValue = (int) ( minValue * 100 ); + this.maxValue = (int) (maxValue * 100 ); this.step = step; this.decimalFormat = decimalFormat; } @@ -62,26 +61,32 @@ public class InputAutosens extends Element { @Override public void addToLayout(LinearLayout root) { + minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); + maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); + if (value > maxValue) + value = Math.max(value, this.maxValue); + if (value < minValue) + value = minValue; numberPicker = new NumberPicker(root.getContext(), null); - numberPicker.setParams(value, minValue, maxValue, step, decimalFormat, true, textWatcher); - numberPicker.setOnValueChangedListener(value -> this.value = value); + numberPicker.setParams((double) value, (double) minValue, (double) maxValue, step, decimalFormat, true, textWatcher); + numberPicker.setOnValueChangedListener(value -> this.value = (int) value); root.addView(numberPicker); } - public InputAutosens setValue(double value) { - minValue = SP.getDouble("key_openapsama_autosens_min", 0.7d); - maxValue = SP.getDouble("key_openapsama_autosens_max", 1.2d); + public InputAutosens setValue(int value) { + minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); + maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); if (value > maxValue) value = Math.max(value, this.maxValue); if (value < minValue) value = minValue; this.value = value; if (numberPicker != null) - numberPicker.setValue(value); + numberPicker.setValue((double) value); return this; } - public double getValue() { + public int getValue() { return value; } 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 index ac8f572f9e..a1a945a746 100644 --- 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 @@ -41,7 +41,7 @@ public class TriggerAutosensValue extends Trigger { lastRun = triggerAutosensValue.lastRun; } - public double getValue() { + public int getValue() { return value.getValue(); } @@ -64,7 +64,7 @@ public class TriggerAutosensValue extends Trigger { return true; } - boolean doRun = comparator.getValue().check((autosensData.autosensResult.ratio), getValue()); + boolean doRun = comparator.getValue().check((autosensData.autosensResult.ratio), (double) (getValue() / 100d)); if (doRun) { if (L.isEnabled(L.AUTOMATION)) log.debug("Ready for execution: " + friendlyDescription()); @@ -93,7 +93,7 @@ public class TriggerAutosensValue extends Trigger { Trigger fromJSON(String data) { try { JSONObject d = new JSONObject(data); - value.setValue(JsonHelper.safeGetDouble(d, "value")); + value.setValue(JsonHelper.safeGetInt(d, "value")); lastRun = JsonHelper.safeGetLong(d, "lastRun"); comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); } catch (Exception e) { @@ -122,7 +122,7 @@ public class TriggerAutosensValue extends Trigger { return new TriggerAutosensValue(this); } - TriggerAutosensValue setValue(double requestedValue) { + TriggerAutosensValue setValue(int requestedValue) { this.value.setValue(requestedValue); return this; } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 40541e4c82..1c243b09f7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1400,8 +1400,8 @@ not exists Temp target %1$s WiFi SSID %1$s %2$s - Autosens %1$s %2$s - Autosens ratio + Autosens %1$s %2$s %% + Autosens % Current Location Location Lat: From 8e9ef8cb6f40899ee0d5c0a5a3ebc67eb217eaad Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Wed, 15 May 2019 14:52:30 +0300 Subject: [PATCH 04/12] adding tests --- .../elements/InputAutosensTest.java | 56 ++++++++ .../triggers/TriggerAutosensValueTest.java | 125 ++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValueTest.java diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java new file mode 100644 index 0000000000..aaed9354d0 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java @@ -0,0 +1,56 @@ +package info.nightscout.androidaps.plugins.general.automation.elements; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +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.plugins.configBuilder.ProfileFunctions; +import info.nightscout.androidaps.utils.SP; + +import static org.mockito.ArgumentMatchers.anyDouble; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.when; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, ProfileFunctions.class, SP.class}) +public class InputAutosensTest { + + @Test + public void textWatcherTest() { + InputAutosens t = new InputAutosens().setValue(100); + t.textWatcher.beforeTextChanged(null, 0, 0, 0); + t.textWatcher.onTextChanged(null, 0, 0, 0); + t.textWatcher.afterTextChanged(null); + Assert.assertEquals(1d, t.getValue(), 0.01d); + + t = new InputAutosens().setValue(200); + t.textWatcher.afterTextChanged(null); + Assert.assertEquals(2d, t.getValue(), 0.01d); + } + + @Test + public void getSetValueTest() { + when(SP.getDouble(anyInt(), anyDouble())).thenReturn(0.7d); + InputAutosens i = new InputAutosens().setValue(500); + Assert.assertEquals(5d, i.getValue(), 0.01d); + Assert.assertEquals(0, i.minValue, 0.01d); + i = new InputAutosens().setValue(110); + Assert.assertEquals(1.1d, i.getValue(), 0.01d); + Assert.assertEquals(0, i.minValue, 0.01d); + } + + + @Before + public void prepare() { + AAPSMocker.mockMainApp(); + AAPSMocker.mockBus(); + AAPSMocker.mockStrings(); + AAPSMocker.mockProfileFunctions(); + AAPSMocker.mockSP(); + } +} \ No newline at end of file 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..7b4747be68 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValueTest.java @@ -0,0 +1,125 @@ +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 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.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() { + 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()); + 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.remove), 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 31224f894d016d921e708f35aa33123eeea1332e Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Wed, 15 May 2019 15:29:18 +0300 Subject: [PATCH 05/12] fixed tests --- .../general/automation/elements/InputAutosensTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java index aaed9354d0..98c14068b0 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java @@ -26,21 +26,21 @@ public class InputAutosensTest { t.textWatcher.beforeTextChanged(null, 0, 0, 0); t.textWatcher.onTextChanged(null, 0, 0, 0); t.textWatcher.afterTextChanged(null); - Assert.assertEquals(1d, t.getValue(), 0.01d); + Assert.assertEquals(100, t.getValue(), 0.01d); t = new InputAutosens().setValue(200); t.textWatcher.afterTextChanged(null); - Assert.assertEquals(2d, t.getValue(), 0.01d); + Assert.assertEquals(200, t.getValue(), 0.01d); } @Test public void getSetValueTest() { when(SP.getDouble(anyInt(), anyDouble())).thenReturn(0.7d); InputAutosens i = new InputAutosens().setValue(500); - Assert.assertEquals(5d, i.getValue(), 0.01d); + Assert.assertEquals(500, i.getValue(), 0.01d); Assert.assertEquals(0, i.minValue, 0.01d); i = new InputAutosens().setValue(110); - Assert.assertEquals(1.1d, i.getValue(), 0.01d); + Assert.assertEquals(110, i.getValue(), 0.01d); Assert.assertEquals(0, i.minValue, 0.01d); } From 3280f220ad6e99cd38242ade0c061b4dcd535f6f Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Wed, 15 May 2019 17:11:47 +0300 Subject: [PATCH 06/12] fix for maxValue and tests --- .../automation/elements/InputAutosens.java | 13 +++++-------- .../automation/elements/InputAutosensTest.java | 8 ++++++-- .../triggers/TriggerAutosensValueTest.java | 16 +++++++++++++--- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosens.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosens.java index bda2b4fdf5..15db454d8d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosens.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosens.java @@ -11,8 +11,8 @@ import info.nightscout.androidaps.utils.SP; public class InputAutosens extends Element { private int value; - int minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); - int maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); + public int minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); + public int maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); private double step = 1; private DecimalFormat decimalFormat = new DecimalFormat("1");; @@ -44,7 +44,7 @@ public class InputAutosens extends Element { super(); this.value = (int) value; this.minValue = (int) ( minValue * 100 ); - this.maxValue = (int) (maxValue * 100 ); + this.maxValue = (int) ( maxValue * 100 ); this.step = step; this.decimalFormat = decimalFormat; } @@ -63,10 +63,7 @@ public class InputAutosens extends Element { public void addToLayout(LinearLayout root) { minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); - if (value > maxValue) - value = Math.max(value, this.maxValue); - if (value < minValue) - value = minValue; + value = minValue & maxValue; numberPicker = new NumberPicker(root.getContext(), null); numberPicker.setParams((double) value, (double) minValue, (double) maxValue, step, decimalFormat, true, textWatcher); numberPicker.setOnValueChangedListener(value -> this.value = (int) value); @@ -77,7 +74,7 @@ public class InputAutosens extends Element { minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); if (value > maxValue) - value = Math.max(value, this.maxValue); + value = maxValue; if (value < minValue) value = minValue; this.value = value; diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java index 98c14068b0..2a803afdea 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java @@ -4,6 +4,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -22,6 +23,8 @@ public class InputAutosensTest { @Test public void textWatcherTest() { + when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d); + when(SP.getDouble(Mockito.eq("openapsama_autosens_min"), anyDouble())).thenReturn(0.7d); InputAutosens t = new InputAutosens().setValue(100); t.textWatcher.beforeTextChanged(null, 0, 0, 0); t.textWatcher.onTextChanged(null, 0, 0, 0); @@ -30,14 +33,15 @@ public class InputAutosensTest { t = new InputAutosens().setValue(200); t.textWatcher.afterTextChanged(null); - Assert.assertEquals(200, t.getValue(), 0.01d); + Assert.assertEquals(120, t.getValue(), 0.01d); } @Test public void getSetValueTest() { + when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d); when(SP.getDouble(anyInt(), anyDouble())).thenReturn(0.7d); InputAutosens i = new InputAutosens().setValue(500); - Assert.assertEquals(500, i.getValue(), 0.01d); + Assert.assertEquals(120, i.getValue(), 0.01d); Assert.assertEquals(0, i.minValue, 0.01d); i = new InputAutosens().setValue(110); Assert.assertEquals(110, i.getValue(), 0.01d); 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 index 7b4747be68..7f099c5931 100644 --- 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 @@ -9,6 +9,7 @@ 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; @@ -23,6 +24,7 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorP 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) @@ -33,6 +35,8 @@ public class TriggerAutosensValueTest { @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); @@ -69,9 +73,11 @@ public class TriggerAutosensValueTest { @Test public void copyConstructorTest() { + 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); 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(120, t1.getValue(), 0.01d); Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue()); } @@ -82,21 +88,25 @@ public class TriggerAutosensValueTest { Assert.assertEquals(1l, t.getLastRun()); } - String ASJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"value\":410},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAutosensValue\"}"; + String ASJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"value\":120},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAutosensValue\"}"; @Test public void toJSONTest() { + 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); TriggerAutosensValue t = new TriggerAutosensValue().setValue(410).comparator(Comparator.Compare.IS_EQUAL); Assert.assertEquals(ASJson, t.toJSON()); } @Test public void fromJSONTest() throws JSONException { + 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); 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); + Assert.assertEquals(120, t2.getValue(), 0.01d); } @Test From 4aaca7acb17e71c8cd8655075b20e7c843a4161f Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Thu, 16 May 2019 12:08:36 +0300 Subject: [PATCH 07/12] Using InputDouble --- .../automation/elements/InputAutosens.java | 90 ------------------- .../triggers/TriggerAutosensValue.java | 14 ++- .../elements/InputAutosensTest.java | 60 ------------- .../triggers/TriggerAutosensValueTest.java | 12 +-- 4 files changed, 13 insertions(+), 163 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosens.java delete mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosens.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosens.java deleted file mode 100644 index 15db454d8d..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosens.java +++ /dev/null @@ -1,90 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.elements; - -import android.text.Editable; -import android.text.TextWatcher; -import android.widget.LinearLayout; - -import java.text.DecimalFormat; - -import info.nightscout.androidaps.utils.NumberPicker; -import info.nightscout.androidaps.utils.SP; - -public class InputAutosens extends Element { - private int value; - public int minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); - public int maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); - private double step = 1; - private DecimalFormat decimalFormat = new DecimalFormat("1");; - - NumberPicker numberPicker; - final TextWatcher textWatcher = new TextWatcher() { - @Override - public void afterTextChanged(Editable s) { - value = Math.max(value, 70); - value = Math.min(value, 200); - } - - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - }; - - - - - public InputAutosens() { - super(); - } - - public InputAutosens(double value, double minValue, double maxValue, double step, DecimalFormat decimalFormat) { - super(); - this.value = (int) value; - this.minValue = (int) ( minValue * 100 ); - this.maxValue = (int) ( maxValue * 100 ); - this.step = step; - this.decimalFormat = decimalFormat; - } - - public InputAutosens(InputAutosens another) { - super(); - value = another.getValue(); - minValue = another.minValue; - maxValue = another.maxValue; - step = another.step; - decimalFormat = another.decimalFormat; - } - - - @Override - public void addToLayout(LinearLayout root) { - minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); - maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); - value = minValue & maxValue; - numberPicker = new NumberPicker(root.getContext(), null); - numberPicker.setParams((double) value, (double) minValue, (double) maxValue, step, decimalFormat, true, textWatcher); - numberPicker.setOnValueChangedListener(value -> this.value = (int) value); - root.addView(numberPicker); - } - - public InputAutosens setValue(int value) { - minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); - maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); - if (value > maxValue) - value = maxValue; - if (value < minValue) - value = minValue; - this.value = value; - if (numberPicker != null) - numberPicker.setValue((double) value); - return this; - } - - public int getValue() { - return value; - } - -} 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 index a1a945a746..c6ddad3ff9 100644 --- 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 @@ -11,11 +11,13 @@ 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.InputAutosens; +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; @@ -23,12 +25,16 @@ 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 InputAutosens value = new InputAutosens(); + public int minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); + public int maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); + private double step = 1; + private DecimalFormat decimalFormat = new DecimalFormat("1");; + private InputDouble value = new InputDouble( (double) minValue,(double) minValue, (double) maxValue, step, decimalFormat); private Comparator comparator = new Comparator(); public TriggerAutosensValue() { @@ -41,7 +47,7 @@ public class TriggerAutosensValue extends Trigger { lastRun = triggerAutosensValue.lastRun; } - public int getValue() { + public double getValue() { return value.getValue(); } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java deleted file mode 100644 index 2a803afdea..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/elements/InputAutosensTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.elements; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -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.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.utils.SP; - -import static org.mockito.ArgumentMatchers.anyDouble; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.Mockito.when; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, ProfileFunctions.class, SP.class}) -public class InputAutosensTest { - - @Test - public void textWatcherTest() { - when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d); - when(SP.getDouble(Mockito.eq("openapsama_autosens_min"), anyDouble())).thenReturn(0.7d); - InputAutosens t = new InputAutosens().setValue(100); - t.textWatcher.beforeTextChanged(null, 0, 0, 0); - t.textWatcher.onTextChanged(null, 0, 0, 0); - t.textWatcher.afterTextChanged(null); - Assert.assertEquals(100, t.getValue(), 0.01d); - - t = new InputAutosens().setValue(200); - t.textWatcher.afterTextChanged(null); - Assert.assertEquals(120, t.getValue(), 0.01d); - } - - @Test - public void getSetValueTest() { - when(SP.getDouble(Mockito.eq("openapsama_autosens_max"), anyDouble())).thenReturn(1.2d); - when(SP.getDouble(anyInt(), anyDouble())).thenReturn(0.7d); - InputAutosens i = new InputAutosens().setValue(500); - Assert.assertEquals(120, i.getValue(), 0.01d); - Assert.assertEquals(0, i.minValue, 0.01d); - i = new InputAutosens().setValue(110); - Assert.assertEquals(110, i.getValue(), 0.01d); - Assert.assertEquals(0, i.minValue, 0.01d); - } - - - @Before - public void prepare() { - AAPSMocker.mockMainApp(); - AAPSMocker.mockBus(); - AAPSMocker.mockStrings(); - AAPSMocker.mockProfileFunctions(); - AAPSMocker.mockSP(); - } -} \ No newline at end of file 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 index 7f099c5931..4c69b0d7d6 100644 --- 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 @@ -73,11 +73,9 @@ public class TriggerAutosensValueTest { @Test public void copyConstructorTest() { - 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); TriggerAutosensValue t = new TriggerAutosensValue().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER); TriggerAutosensValue t1 = (TriggerAutosensValue) t.duplicate(); - Assert.assertEquals(120, t1.getValue(), 0.01d); + Assert.assertEquals(213, t1.getValue(), 0.01d); Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue()); } @@ -88,25 +86,21 @@ public class TriggerAutosensValueTest { Assert.assertEquals(1l, t.getLastRun()); } - String ASJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"value\":120},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAutosensValue\"}"; + String ASJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"value\":410},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAutosensValue\"}"; @Test public void toJSONTest() { - 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); TriggerAutosensValue t = new TriggerAutosensValue().setValue(410).comparator(Comparator.Compare.IS_EQUAL); Assert.assertEquals(ASJson, t.toJSON()); } @Test public void fromJSONTest() throws JSONException { - 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); 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(120, t2.getValue(), 0.01d); + Assert.assertEquals(410, t2.getValue(), 0.01d); } @Test From ccd66d498ca8db370f53ca556caa72fcc6a4cc13 Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Thu, 16 May 2019 16:03:05 +0300 Subject: [PATCH 08/12] icon added --- .../triggers/TriggerAutosensValue.java | 2 +- app/src/main/res/drawable/as.png | Bin 0 -> 1639 bytes .../triggers/TriggerAutosensValueTest.java | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 app/src/main/res/drawable/as.png 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 index c6ddad3ff9..270b344a20 100644 --- 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 @@ -120,7 +120,7 @@ public class TriggerAutosensValue extends Trigger { @Override public Optional icon() { - return Optional.of(R.drawable.remove); // TODO icon + return Optional.of(R.drawable.as); } @Override 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~ Date: Mon, 20 May 2019 11:21:51 +0300 Subject: [PATCH 09/12] unavailable data and IS_NOT_AVAILABLE case --- .../general/automation/triggers/TriggerAutosensValue.java | 7 +++++-- .../automation/triggers/TriggerAutosensValueTest.java | 5 +++++ 2 files changed, 10 insertions(+), 2 deletions(-) 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 index 270b344a20..1dfdd5e95e 100644 --- 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 @@ -59,7 +59,10 @@ public class TriggerAutosensValue extends Trigger { public synchronized boolean shouldRun() { AutosensData autosensData = IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Automation trigger"); if (autosensData == null) - return false; + if (comparator.getValue() == Comparator.Compare.IS_NOT_AVAILABLE) + return true; + else + return false; if (lastRun > DateUtil.now() - T.mins(5).msecs()) return false; @@ -120,7 +123,7 @@ public class TriggerAutosensValue extends Trigger { @Override public Optional icon() { - return Optional.of(R.drawable.as); + return Optional.of(R.drawable.as); } @Override 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 index 12594557b1..41ec343e0b 100644 --- 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 @@ -66,6 +66,11 @@ public class TriggerAutosensValueTest { t = new TriggerAutosensValue().comparator(Comparator.Compare.IS_NOT_AVAILABLE); Assert.assertTrue(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()); From af739a3b66438a3a3550e270f43b8b0660c8c5ad Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Thu, 23 May 2019 10:59:28 +0300 Subject: [PATCH 10/12] Fixing after comments --- .../automation/triggers/TriggerAutosensValue.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) 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 index 1dfdd5e95e..1133413d57 100644 --- 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 @@ -43,8 +43,9 @@ public class TriggerAutosensValue extends Trigger { private TriggerAutosensValue(TriggerAutosensValue triggerAutosensValue) { super(); - value = triggerAutosensValue.value; + value = new InputDouble(triggerAutosensValue.value); lastRun = triggerAutosensValue.lastRun; + comparator = new Comparator(triggerAutosensValue.comparator); } public double getValue() { @@ -67,12 +68,6 @@ public class TriggerAutosensValue extends Trigger { if (lastRun > DateUtil.now() - T.mins(5).msecs()) return false; - if (autosensData.autosensResult.ratio != 0 && comparator.getValue() == Comparator.Compare.IS_NOT_AVAILABLE) { - if (L.isEnabled(L.AUTOMATION)) - log.debug("Ready for execution: " + friendlyDescription()); - return true; - } - boolean doRun = comparator.getValue().check((autosensData.autosensResult.ratio), (double) (getValue() / 100d)); if (doRun) { if (L.isEnabled(L.AUTOMATION)) @@ -102,7 +97,7 @@ public class TriggerAutosensValue extends Trigger { Trigger fromJSON(String data) { try { JSONObject d = new JSONObject(data); - value.setValue(JsonHelper.safeGetInt(d, "value")); + value.setValue(JsonHelper.safeGetDouble(d, "value")); lastRun = JsonHelper.safeGetLong(d, "lastRun"); comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); } catch (Exception e) { @@ -154,4 +149,4 @@ public class TriggerAutosensValue extends Trigger { .add(new LabelWithElement(MainApp.gs(R.string.autosenslabel) + ": ", "", value)) .build(root); } -} \ No newline at end of file +} From 63d5ef4aa7a06b87647c2ae21ddbabc7dcab7e4f Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Thu, 23 May 2019 11:01:16 +0300 Subject: [PATCH 11/12] Autosens !=0 && IS_NOT_AVAILABLE doesn't exist enymore --- .../general/automation/triggers/TriggerAutosensValueTest.java | 2 -- 1 file changed, 2 deletions(-) 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 index 41ec343e0b..ab1851aefc 100644 --- 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 @@ -63,8 +63,6 @@ public class TriggerAutosensValueTest { 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()); - t = new TriggerAutosensValue().comparator(Comparator.Compare.IS_NOT_AVAILABLE); - Assert.assertTrue(t.shouldRun()); // Test autosensData == null and Comparator == IS_NOT_AVAILABLE when(IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Automation trigger")).thenReturn(null); From 15e997abb3101dface37f8da9a7a546b4fa7bbbc Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 31 May 2019 10:44:38 +0200 Subject: [PATCH 12/12] cleanup --- .../automation/triggers/TriggerAutosensValue.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 index 1133413d57..98c8928dcf 100644 --- 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 @@ -30,11 +30,11 @@ import info.nightscout.androidaps.utils.T; public class TriggerAutosensValue extends Trigger { private static Logger log = LoggerFactory.getLogger(L.AUTOMATION); - public int minValue = (int) (SP.getDouble("openapsama_autosens_min", 0.7d) * 100); - public int maxValue = (int) (SP.getDouble("openapsama_autosens_max", 1.2d) * 100); - private double step = 1; - private DecimalFormat decimalFormat = new DecimalFormat("1");; - private InputDouble value = new InputDouble( (double) minValue,(double) minValue, (double) maxValue, step, decimalFormat); + 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() { @@ -68,7 +68,7 @@ public class TriggerAutosensValue extends Trigger { if (lastRun > DateUtil.now() - T.mins(5).msecs()) return false; - boolean doRun = comparator.getValue().check((autosensData.autosensResult.ratio), (double) (getValue() / 100d)); + boolean doRun = comparator.getValue().check((autosensData.autosensResult.ratio), getValue() / 100d); if (doRun) { if (L.isEnabled(L.AUTOMATION)) log.debug("Ready for execution: " + friendlyDescription());