Merge pull request #87 from anbraten/automation-location-left-trigger
Automation location left trigger
This commit is contained in:
commit
2fa3805030
4 changed files with 138 additions and 1 deletions
|
@ -0,0 +1,19 @@
|
|||
package info.nightscout.androidaps.plugins.general.automation.elements;
|
||||
|
||||
public class InputOption {
|
||||
private int stringRes;
|
||||
private String value;
|
||||
|
||||
public InputOption(int stringRes, String value) {
|
||||
this.stringRes = stringRes;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getStringRes() {
|
||||
return stringRes;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
package info.nightscout.androidaps.plugins.general.automation.elements;
|
||||
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 InputSelect extends Element {
|
||||
|
||||
private InputOption selected;
|
||||
private ArrayList<InputOption> options;
|
||||
|
||||
public InputSelect() {
|
||||
super();
|
||||
}
|
||||
|
||||
public InputSelect(ArrayList<InputOption> options) {
|
||||
super();
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
public InputSelect(ArrayList<InputOption> options, InputOption selected) {
|
||||
super();
|
||||
this.options = options;
|
||||
this.selected = selected;
|
||||
}
|
||||
|
||||
public InputSelect(InputSelect another) {
|
||||
super();
|
||||
options = another.options;
|
||||
selected = another.selected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addToLayout(LinearLayout root) {
|
||||
ArrayAdapter<String> adapter = new ArrayAdapter<>(root.getContext(),
|
||||
R.layout.spinner_centered, labels());
|
||||
Spinner spinner = new Spinner(root.getContext());
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
spinner.setAdapter(adapter);
|
||||
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) {
|
||||
setValue(options.get(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(AdapterView<?> parent) {
|
||||
}
|
||||
});
|
||||
spinner.setSelection(selected == null ? 0 : options.indexOf(selected));
|
||||
root.addView(spinner);
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return selected == null ? null : selected.getValue();
|
||||
}
|
||||
|
||||
public InputSelect setValue(InputOption selected) {
|
||||
this.selected = selected;
|
||||
return this;
|
||||
}
|
||||
|
||||
public InputSelect setValue(String selected) {
|
||||
for (InputOption o: options) {
|
||||
if (o.getValue().equals(selected)) {
|
||||
this.selected = o;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
private List<String> labels() {
|
||||
List<String> list = new ArrayList<>();
|
||||
for (InputOption o : options) {
|
||||
list.add(MainApp.gs(o.getStringRes()));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
|
@ -13,12 +13,16 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import info.nightscout.androidaps.MainApp;
|
||||
import info.nightscout.androidaps.R;
|
||||
import info.nightscout.androidaps.logging.L;
|
||||
import info.nightscout.androidaps.plugins.general.automation.elements.InputButton;
|
||||
import info.nightscout.androidaps.plugins.general.automation.elements.InputDouble;
|
||||
import info.nightscout.androidaps.plugins.general.automation.elements.InputOption;
|
||||
import info.nightscout.androidaps.plugins.general.automation.elements.InputSelect;
|
||||
import info.nightscout.androidaps.plugins.general.automation.elements.InputString;
|
||||
import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement;
|
||||
import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder;
|
||||
|
@ -31,9 +35,17 @@ import info.nightscout.androidaps.utils.T;
|
|||
public class TriggerLocation extends Trigger {
|
||||
private static Logger log = LoggerFactory.getLogger(L.AUTOMATION);
|
||||
|
||||
private static final String modeEntered = "entered";
|
||||
private static final String modeLeft = "left";
|
||||
private static final ArrayList<InputOption> modes = new ArrayList<>(Arrays.asList(
|
||||
new InputOption(R.string.location_entered, modeEntered),
|
||||
new InputOption(R.string.location_left, modeLeft)
|
||||
));
|
||||
|
||||
InputDouble latitude = new InputDouble(0d, -90d, +90d, 0.000001d, new DecimalFormat("0.000000"));
|
||||
InputDouble longitude = new InputDouble(0d, -180d, +180d, 0.000001d, new DecimalFormat("0.000000"));
|
||||
InputDouble distance = new InputDouble(200d, 0, 100000, 10d, new DecimalFormat("0"));
|
||||
InputSelect mode = new InputSelect(modes);
|
||||
InputString name = new InputString();
|
||||
|
||||
private Runnable buttonAction = () -> {
|
||||
|
@ -54,6 +66,7 @@ public class TriggerLocation extends Trigger {
|
|||
latitude = new InputDouble(triggerLocation.latitude);
|
||||
longitude = new InputDouble(triggerLocation.longitude);
|
||||
distance = new InputDouble(triggerLocation.distance);
|
||||
mode = new InputSelect(triggerLocation.mode);
|
||||
lastRun = triggerLocation.lastRun;
|
||||
name = triggerLocation.name;
|
||||
}
|
||||
|
@ -72,7 +85,8 @@ public class TriggerLocation extends Trigger {
|
|||
a.setLongitude(longitude.getValue());
|
||||
double calculatedDistance = location.distanceTo(a);
|
||||
|
||||
if (calculatedDistance < distance.getValue()) {
|
||||
if ((mode.equals(modeEntered) && calculatedDistance < distance.getValue()) ||
|
||||
(mode.equals(modeLeft) && calculatedDistance > distance.getValue())) {
|
||||
if (L.isEnabled(L.AUTOMATION))
|
||||
log.debug("Ready for execution: " + friendlyDescription());
|
||||
return true;
|
||||
|
@ -90,6 +104,7 @@ public class TriggerLocation extends Trigger {
|
|||
data.put("longitude", longitude.getValue());
|
||||
data.put("distance", distance.getValue());
|
||||
data.put("name", name.getValue());
|
||||
data.put("mode", mode.getValue());
|
||||
data.put("lastRun", lastRun);
|
||||
o.put("data", data);
|
||||
} catch (JSONException e) {
|
||||
|
@ -106,6 +121,7 @@ public class TriggerLocation extends Trigger {
|
|||
longitude.setValue(JsonHelper.safeGetDouble(d, "longitude"));
|
||||
distance.setValue(JsonHelper.safeGetDouble(d, "distance"));
|
||||
name.setValue(JsonHelper.safeGetString(d, "name"));
|
||||
mode.setValue(JsonHelper.safeGetString(d, "mode", modeEntered));
|
||||
lastRun = JsonHelper.safeGetLong(d, "lastRun");
|
||||
} catch (Exception e) {
|
||||
log.error("Unhandled exception", e);
|
||||
|
@ -154,6 +170,11 @@ public class TriggerLocation extends Trigger {
|
|||
return this;
|
||||
}
|
||||
|
||||
TriggerLocation setMode(String value) {
|
||||
mode.setValue(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateDialog(LinearLayout root, FragmentManager fragmentManager) {
|
||||
new LayoutBuilder()
|
||||
|
@ -162,6 +183,7 @@ public class TriggerLocation extends Trigger {
|
|||
.add(new LabelWithElement(MainApp.gs(R.string.latitude_short), "", latitude))
|
||||
.add(new LabelWithElement(MainApp.gs(R.string.longitude_short), "", longitude))
|
||||
.add(new LabelWithElement(MainApp.gs(R.string.distance_short), "", distance))
|
||||
.add(new LabelWithElement(MainApp.gs(R.string.location_mode), "", mode))
|
||||
.add(new InputButton(MainApp.gs(R.string.currentlocation), buttonAction), LocationService.getLastLocation() != null)
|
||||
.build(root);
|
||||
}
|
||||
|
|
|
@ -1411,6 +1411,9 @@
|
|||
<string name="distance_short">Dist [m]:</string>
|
||||
<string name="name_short">Name:</string>
|
||||
<string name="locationis">Location is %1$s</string>
|
||||
<string name="location_mode">On location</string>
|
||||
<string name="location_entered">entered</string>
|
||||
<string name="location_left">left</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>
|
||||
|
|
Loading…
Reference in a new issue