TriggerTempTarget

This commit is contained in:
Milos Kozak 2019-04-22 12:42:19 +02:00
parent bef170699f
commit 1840ddabb3
7 changed files with 356 additions and 1 deletions

View file

@ -181,7 +181,7 @@ public class MainApp extends Application {
if (Config.OTHERPROFILES) pluginsList.add(LocalProfilePlugin.getPlugin());
pluginsList.add(TreatmentsPlugin.getPlugin());
if (Config.SAFETY) pluginsList.add(SafetyPlugin.getPlugin());
if (Config.SAFETY) pluginsList.add(VersionCheckerPlugin.INSTANCE);
//if (Config.SAFETY) pluginsList.add(VersionCheckerPlugin.INSTANCE);
if (Config.SAFETY) pluginsList.add(StorageConstraintPlugin.getPlugin());
if (Config.APS) pluginsList.add(ObjectivesPlugin.getPlugin());
pluginsList.add(SourceXdripPlugin.getPlugin());

View file

@ -22,6 +22,7 @@ import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerBg;
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerIob;
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerProfilePercent;
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerRecurringTime;
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTempTarget;
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTime;
public class ChooseTriggerDialog extends DialogFragment {
@ -36,6 +37,7 @@ public class ChooseTriggerDialog extends DialogFragment {
add(new TriggerBg());
add(new TriggerIob());
add(new TriggerProfilePercent());
add(new TriggerTempTarget());
}};
private Unbinder mUnbinder;

View file

@ -0,0 +1,89 @@
package info.nightscout.androidaps.plugins.general.automation.elements;
import android.support.annotation.StringRes;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
public class ComparatorExists extends Element {
public enum Compare {
EXISTS,
NOT_EXISTS;
public @StringRes
int getStringRes() {
switch (this) {
case EXISTS:
return R.string.exists;
case NOT_EXISTS:
return R.string.notexists;
default:
return R.string.unknown;
}
}
public static List<String> labels() {
List<String> list = new ArrayList<>();
for (Compare c : Compare.values()) {
list.add(MainApp.gs(c.getStringRes()));
}
return list;
}
}
private Compare compare = Compare.EXISTS;
public ComparatorExists() {
super();
}
public ComparatorExists(ComparatorExists another) {
super();
compare = another.getValue();
}
@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, Compare.labels());
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
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) {
compare = Compare.values()[position];
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner.setSelection(compare.ordinal());
root.addView(spinner);
}
public Compare getValue() {
return compare;
}
public ComparatorExists setValue(Compare compare) {
this.compare = compare;
return this;
}
}

View file

@ -0,0 +1,131 @@
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.db.TempTarget;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.general.automation.elements.Comparator;
import info.nightscout.androidaps.plugins.general.automation.elements.ComparatorExists;
import info.nightscout.androidaps.plugins.general.automation.elements.InputInsulin;
import info.nightscout.androidaps.plugins.general.automation.elements.Label;
import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.JsonHelper;
import info.nightscout.androidaps.utils.T;
public class TriggerTempTarget extends Trigger {
private static Logger log = LoggerFactory.getLogger(L.AUTOMATION);
private ComparatorExists comparator = new ComparatorExists();
public TriggerTempTarget() {
super();
}
private TriggerTempTarget(TriggerTempTarget triggerTempTarget) {
super();
comparator = new ComparatorExists(triggerTempTarget.comparator);
lastRun = triggerTempTarget.lastRun;
}
public ComparatorExists getComparator() {
return comparator;
}
@Override
public synchronized boolean shouldRun() {
TempTarget tt = TreatmentsPlugin.getPlugin().getTempTargetFromHistory();
if (lastRun > DateUtil.now() - T.mins(5).msecs())
return false;
if (tt == null && comparator.getValue() == ComparatorExists.Compare.NOT_EXISTS) {
if (L.isEnabled(L.AUTOMATION))
log.debug("Ready for execution: " + friendlyDescription());
return true;
}
if (tt != null && comparator.getValue() == ComparatorExists.Compare.EXISTS) {
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", TriggerTempTarget.class.getName());
JSONObject data = new JSONObject();
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);
lastRun = JsonHelper.safeGetLong(d, "lastRun");
comparator.setValue(ComparatorExists.Compare.valueOf(JsonHelper.safeGetString(d, "comparator")));
} catch (Exception e) {
e.printStackTrace();
}
return this;
}
@Override
public int friendlyName() {
return R.string.temptarget;
}
@Override
public String friendlyDescription() {
return MainApp.gs(R.string.temptargetcompared, MainApp.gs(comparator.getValue().getStringRes()));
}
@Override
public Optional<Integer> icon() {
return Optional.of(R.drawable.remove); // TODO icon
}
@Override
public Trigger duplicate() {
return new TriggerTempTarget(this);
}
TriggerTempTarget lastRun(long lastRun) {
this.lastRun = lastRun;
return this;
}
TriggerTempTarget comparator(ComparatorExists.Compare compare) {
this.comparator = new ComparatorExists().setValue(compare);
return this;
}
@Override
public void generateDialog(LinearLayout root, FragmentManager fragmentManager) {
new LayoutBuilder()
.add(comparator)
.build(root);
}
}

View file

@ -1396,6 +1396,9 @@
<string name="profilepercentage">Profile percentage</string>
<string name="percent_u">Percent [%]:</string>
<string name="startprofile">Start profile %1$d%% for %2$d min</string>
<string name="exists">exists</string>
<string name="notexists">not exists</string>
<string name="temptargetcompared">Temp target %1$s</string>
<plurals name="objective_days">
<item quantity="one">%1$d day</item>

View file

@ -0,0 +1,34 @@
package info.nightscout.androidaps.plugins.general.automation.elements;
import junit.framework.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;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class})
public class ComparatorExistsTest {
@Test
public void labelsTest() {
Assert.assertEquals(2, ComparatorExists.Compare.labels().size());
}
@Test
public void getSetValueTest() {
ComparatorExists c = new ComparatorExists().setValue(ComparatorExists.Compare.NOT_EXISTS);
Assert.assertEquals(ComparatorExists.Compare.NOT_EXISTS, c.getValue());
}
@Before
public void prepare() {
AAPSMocker.mockMainApp();
AAPSMocker.mockStrings();
}
}

View file

@ -0,0 +1,96 @@
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.db.TempTarget;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.general.automation.elements.ComparatorExists;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.utils.DateUtil;
import static org.mockito.ArgumentMatchers.any;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, Bus.class, ProfileFunctions.class, DateUtil.class, TreatmentsPlugin.class})
public class TriggerTempTargetTest {
TreatmentsPlugin treatmentsPlugin;
long now = 1514766900000L;
@Test
public void shouldRunTest() {
when(treatmentsPlugin.getTempTargetFromHistory()).thenReturn(null);
TriggerTempTarget t = new TriggerTempTarget().comparator(ComparatorExists.Compare.EXISTS);
Assert.assertFalse(t.shouldRun());
t = new TriggerTempTarget().comparator(ComparatorExists.Compare.NOT_EXISTS);
Assert.assertTrue(t.shouldRun());
when(treatmentsPlugin.getTempTargetFromHistory()).thenReturn(new TempTarget());
t = new TriggerTempTarget().comparator(ComparatorExists.Compare.NOT_EXISTS);
Assert.assertFalse(t.shouldRun());
t = new TriggerTempTarget().comparator(ComparatorExists.Compare.EXISTS);
Assert.assertTrue(t.shouldRun());
t = new TriggerTempTarget().comparator(ComparatorExists.Compare.EXISTS).lastRun(now - 1);
Assert.assertFalse(t.shouldRun());
}
@Test
public void copyConstructorTest() {
TriggerTempTarget t = new TriggerTempTarget().comparator(ComparatorExists.Compare.NOT_EXISTS);
TriggerTempTarget t1 = (TriggerTempTarget) t.duplicate();
Assert.assertEquals(ComparatorExists.Compare.NOT_EXISTS, t.getComparator().getValue());
}
String ttJson = "{\"data\":{\"comparator\":\"EXISTS\",\"lastRun\":0},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTempTarget\"}";
@Test
public void toJSONTest() {
TriggerTempTarget t = new TriggerTempTarget().comparator(ComparatorExists.Compare.EXISTS);
Assert.assertEquals(ttJson, t.toJSON());
}
@Test
public void fromJSONTest() throws JSONException {
TriggerTempTarget t = new TriggerTempTarget().comparator(ComparatorExists.Compare.NOT_EXISTS);
TriggerTempTarget t2 = (TriggerTempTarget) Trigger.instantiate(new JSONObject(t.toJSON()));
Assert.assertEquals(ComparatorExists.Compare.NOT_EXISTS, t2.getComparator().getValue());
}
@Test
public void iconTest() {
Assert.assertEquals(Optional.of(R.drawable.remove), new TriggerTempTarget().icon());
}
@Before
public void mock() {
AAPSMocker.mockMainApp();
AAPSMocker.mockBus();
treatmentsPlugin = AAPSMocker.mockTreatmentPlugin();
PowerMockito.mockStatic(DateUtil.class);
when(DateUtil.now()).thenReturn(now);
}
}