Merge branch 'automation' into triggerLastBolus

This commit is contained in:
Milos Kozak 2019-06-21 15:34:06 +02:00 committed by GitHub
commit 7e8059c3d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 801 additions and 1 deletions

View file

@ -19,6 +19,8 @@ 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.TriggerCOB;
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;
@ -39,7 +41,9 @@ public class ChooseTriggerDialog extends DialogFragment {
add(new TriggerTime());
add(new TriggerRecurringTime());
add(new TriggerBg());
add(new TriggerDelta());
add(new TriggerIob());
add(new TriggerCOB());
add(new TriggerProfilePercent());
add(new TriggerTempTarget());
add(new TriggerWifiSsid());

View file

@ -0,0 +1,150 @@
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;
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) {
}
};
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;
}
}
public static List<String> labels() {
List<String> 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 DeltaType deltaType;
NumberPicker numberPicker;
public InputDelta() {
super();
}
public InputDelta(double value, double minValue, double maxValue, double step, DecimalFormat decimalFormat, DeltaType 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<String> 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(
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 = DeltaType.values()[position];
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner.setSelection(this.deltaType.ordinal());
// 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, DeltaType type) {
this.value = value;
this.deltaType = type;
if (numberPicker != null)
numberPicker.setValue(value);
return this;
}
public double getValue() {
return value;
}
public DeltaType getDeltaType() {
return deltaType;
}
}

View file

@ -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.CobInfo;
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 TriggerCOB extends Trigger {
private static Logger log = LoggerFactory.getLogger(L.AUTOMATION);
private final int minValue = 0;
private final int maxValue = (int) (SP.getInt(R.string.key_treatmentssafety_maxcarbs, 48));
private final double step = 1;
private DecimalFormat decimalFormat = new DecimalFormat("1");
private InputDouble value = new InputDouble(0, (double) minValue, (double) maxValue, step, decimalFormat);
private Comparator comparator = new Comparator();
public TriggerCOB() {
super();
}
private TriggerCOB(TriggerCOB triggerCOB) {
super();
value = new InputDouble(triggerCOB.value);
lastRun = triggerCOB.lastRun;
comparator = new Comparator(triggerCOB.comparator);
}
public double getValue() {
return value.getValue();
}
public Comparator getComparator() {
return comparator;
}
@Override
public synchronized boolean shouldRun() {
CobInfo cobInfo = IobCobCalculatorPlugin.getPlugin().getCobInfo(false, "AutomationTriggerCOB");
if (cobInfo == 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((cobInfo.displayCob), 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", TriggerCOB.class.getName());
JSONObject data = new JSONObject();
data.put("carbs", 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, "carbs"));
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.triggercoblabel;
}
@Override
public String friendlyDescription() {
return MainApp.gs(R.string.cobcompared, MainApp.gs(comparator.getValue().getStringRes()), getValue());
}
@Override
public Optional<Integer> icon() {
return Optional.of(R.drawable.icon_cp_bolus_carbs);
}
@Override
public Trigger duplicate() {
return new TriggerCOB(this);
}
TriggerCOB setValue(int requestedValue) {
this.value.setValue(requestedValue);
return this;
}
TriggerCOB lastRun(long lastRun) {
this.lastRun = lastRun;
return this;
}
TriggerCOB 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.triggercoblabel))
.add(comparator)
.add(new LabelWithElement(MainApp.gs(R.string.triggercoblabel) + ": ", "", value))
.build(root);
}
}

View file

@ -0,0 +1,207 @@
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.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;
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;
private DeltaType deltaType;
private InputDelta value = new InputDelta( (double) minValue,(double) minValue, (double) maxValue, step, decimalFormat, deltaType);
private Comparator comparator = new Comparator();
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;
}
public double getValue() {
deltaType = value.getDeltaType();
return value.getValue();
}
public DeltaType getType() {
return deltaType;
}
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 == DeltaType.SHORT_AVERAGE)
delta = glucoseStatus.short_avgdelta;
else if (deltaType == DeltaType.LONG_AVERAGE)
delta = glucoseStatus.long_avgdelta;
else
delta = glucoseStatus.delta;
if (lastRun > DateUtil.now() - T.mins(5).msecs())
return false;
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("deltaType", 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 = 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")));
} 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(), MainApp.gs(deltaType.getStringRes()));
}
@Override
public Optional<Integer> icon() {
return Optional.of(R.drawable.icon_auto_delta);
}
@Override
public Trigger duplicate() {
return new TriggerDelta(this);
}
TriggerDelta setValue(double requestedValue, DeltaType requestedType) {
this.value.setValue(requestedValue, requestedType);
this.deltaType = requestedType;
return this;
}
TriggerDelta setUnits(String units) {
this.units = units;
return this;
}
void initializer(){
if (this.units.equals(Constants.MMOL)) {
this.maxValue = 4d;
this.minValue = -4d;
this.step = 0.1d;
this.decimalFormat = new DecimalFormat("0.1");
this.deltaType = DeltaType.DELTA;
} else {
this.maxValue = 72d;
this.minValue = -72d;
this.step = 1d;
this.deltaType = DeltaType.DELTA;
}
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);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 392 B

View file

@ -1402,6 +1402,8 @@
<string name="wifissidcompared">WiFi SSID %1$s %2$s</string>
<string name="autosenscompared">Autosens %1$s %2$s %%</string>
<string name="autosenslabel">Autosens %</string>
<string name="deltacompared">%3$s %1$s %2$s</string>
<string name="deltalabel">BG difference</string>
<string name="currentlocation">Current Location</string>
<string name="location">Location</string>
<string name="latitude_short">Lat:</string>
@ -1411,6 +1413,8 @@
<string name="locationis">Location is %1$s</string>
<string name="lastboluslabel">Last bolus ago</string>
<string name="lastboluscompared">Last bolus time %1$s %2$s min ago</string>
<string name="triggercoblabel">COB</string>
<string name="cobcompared">COB %1$s %2$.0f</string>
<plurals name="objective_days">
<item quantity="one">%1$d day</item>
@ -1424,4 +1428,4 @@
<item quantity="one">%1$d minute</item>
<item quantity="other">%1$d minutes</item>
</plurals>
</resources>
</resources>

View file

@ -0,0 +1,122 @@
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.data.IobTotal;
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.iob.iobCobCalculator.CobInfo;
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.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.powermock.api.mockito.PowerMockito.verifyNew;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, Bus.class, ProfileFunctions.class, DateUtil.class, IobCobCalculatorPlugin.class, SP.class, L.class})
public class TriggerCOBTest {
long now = 1514766900000L;
IobCobCalculatorPlugin iobCobCalculatorPlugin;
@Test
public void shouldRunTest() {
// COB value is 6
PowerMockito.when(IobCobCalculatorPlugin.getPlugin().getCobInfo(false, "AutomationTriggerCOB")).thenReturn(new CobInfo(6d, 2d)); TriggerCOB t = new TriggerCOB().setValue(1).comparator(Comparator.Compare.IS_EQUAL);
Assert.assertFalse(t.shouldRun());
t = new TriggerCOB().setValue(6).comparator(Comparator.Compare.IS_EQUAL);
Assert.assertTrue(t.shouldRun());
t = new TriggerCOB().setValue(5).comparator(Comparator.Compare.IS_GREATER);
Assert.assertTrue(t.shouldRun());
t = new TriggerCOB().setValue(5).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER);
Assert.assertTrue(t.shouldRun());
t = new TriggerCOB().setValue(6).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
Assert.assertTrue(t.shouldRun());
t = new TriggerCOB().setValue(1).comparator(Comparator.Compare.IS_EQUAL);
Assert.assertFalse(t.shouldRun());
t = new TriggerCOB().setValue(10).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
Assert.assertTrue(t.shouldRun());
t = new TriggerCOB().setValue(5).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
Assert.assertFalse(t.shouldRun());
t = new TriggerCOB().setValue(1).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1);
Assert.assertFalse(t.shouldRun());
}
@Test
public void copyConstructorTest() {
TriggerCOB t = new TriggerCOB().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
TriggerCOB t1 = (TriggerCOB) t.duplicate();
Assert.assertEquals(213d, t.getValue(), 0.01d);
Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue());
}
@Test
public void executeTest() {
TriggerCOB t = new TriggerCOB().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
t.executed(1);
Assert.assertEquals(1l, t.getLastRun());
}
String bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"carbs\":4},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerCOB\"}";
@Test
public void toJSONTest() {
TriggerCOB t = new TriggerCOB().setValue(4).comparator(Comparator.Compare.IS_EQUAL);
Assert.assertEquals(bgJson, t.toJSON());
}
@Test
public void fromJSONTest() throws JSONException {
TriggerCOB t = new TriggerCOB().setValue(4).comparator(Comparator.Compare.IS_EQUAL);
TriggerCOB t2 = (TriggerCOB) Trigger.instantiate(new JSONObject(t.toJSON()));
Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue());
Assert.assertEquals(4, t2.getValue(), 0.01d);
}
@Test
public void iconTest() {
Assert.assertEquals(Optional.of(R.drawable.icon_cp_bolus_carbs), new TriggerCOB().icon());
}
@Before
public void mock() {
AAPSMocker.mockMainApp();
AAPSMocker.mockBus();
iobCobCalculatorPlugin = AAPSMocker.mockIobCobCalculatorPlugin();
AAPSMocker.mockProfileFunctions();
AAPSMocker.mockSP();
AAPSMocker.mockL();
PowerMockito.mockStatic(DateUtil.class);
when(DateUtil.now()).thenReturn(now);
when(SP.getInt(any(), any())).thenReturn(48);
}
public CobInfo generateCobInfo(){
return new CobInfo(6d, 0d);
}
}

View file

@ -0,0 +1,161 @@
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.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;
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());
//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(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);
Assert.assertFalse(t.shouldRun());
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.LONG_AVERAGE).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER);
Assert.assertFalse(t.shouldRun());
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.LONG_AVERAGE).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, 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, 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, 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, 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,\"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, 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, 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(DeltaType.DELTA, t2.getType());
}
@Test
public void iconTest() {
Assert.assertEquals(Optional.of(R.drawable.icon_auto_delta), new TriggerDelta().icon());
}
@Before
public void mock() {
AAPSMocker.mockMainApp();
AAPSMocker.mockBus();
AAPSMocker.mockIobCobCalculatorPlugin();
AAPSMocker.mockProfileFunctions();
AAPSMocker.mockApplicationContext();
PowerMockito.mockStatic(DateUtil.class);
when(DateUtil.now()).thenReturn(now);
}
@Test
public void initializerTest(){
PowerMockito.when(ProfileFunctions.getInstance().getProfileUnits()).thenReturn(Constants.MMOL);
TriggerDelta t = new TriggerDelta();
Assert.assertTrue(t.getUnits().equals(Constants.MMOL));
PowerMockito.when(ProfileFunctions.getInstance().getProfileUnits()).thenReturn(Constants.MGDL);
t = new TriggerDelta();
Assert.assertEquals(Constants.MGDL, t.getUnits());
}
List<BgReading> generateValidBgData() {
List<BgReading> 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;
}
}