new trigger and tests
This commit is contained in:
parent
e898a8444c
commit
e220a42e4b
3 changed files with 238 additions and 0 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Integer> 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);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue