Adding GOING_IN & GOING_OUT modes

This commit is contained in:
Roumen Georgiev 2019-12-06 15:03:17 +02:00
parent 6c8a5aee47
commit 7889e3755d
3 changed files with 29 additions and 6 deletions

View file

@ -28,6 +28,10 @@ public class InputLocationMode extends Element {
return R.string.location_inside; return R.string.location_inside;
case OUTSIDE: case OUTSIDE:
return R.string.location_outside; return R.string.location_outside;
case GOING_IN:
return R.string.location_going_in;
case GOING_OUT:
return R.string.location_going_out;
default: default:
return R.string.unknown; return R.string.unknown;
} }
@ -79,7 +83,6 @@ public class InputLocationMode extends Element {
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// mode = Mode.values()[position];
setValue(Mode.values()[position]); setValue(Mode.values()[position]);
} }

View file

@ -29,14 +29,16 @@ import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.JsonHelper;
import info.nightscout.androidaps.utils.T; import info.nightscout.androidaps.utils.T;
import static info.nightscout.androidaps.plugins.general.automation.elements.InputLocationMode.Mode.*;
public class TriggerLocation extends Trigger { public class TriggerLocation extends Trigger {
private static Logger log = LoggerFactory.getLogger(L.AUTOMATION); private static Logger log = LoggerFactory.getLogger(L.AUTOMATION);
InputDouble latitude = new InputDouble(0d, -90d, +90d, 0.000001d, new DecimalFormat("0.000000")); 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 longitude = new InputDouble(0d, -180d, +180d, 0.000001d, new DecimalFormat("0.000000"));
InputDouble distance = new InputDouble(200d, 0, 100000, 10d, new DecimalFormat("0")); InputDouble distance = new InputDouble(200d, 0, 100000, 10d, new DecimalFormat("0"));
// Default mode selected is 0 - inside area
InputLocationMode modeSelected = new InputLocationMode(); InputLocationMode modeSelected = new InputLocationMode();
InputLocationMode.Mode lastMode = new InputLocationMode().getValue();
InputString name = new InputString(); InputString name = new InputString();
@ -76,8 +78,15 @@ public class TriggerLocation extends Trigger {
a.setLatitude(latitude.getValue()); a.setLatitude(latitude.getValue());
a.setLongitude(longitude.getValue()); a.setLongitude(longitude.getValue());
double calculatedDistance = location.distanceTo(a); double calculatedDistance = location.distanceTo(a);
if (((modeSelected.getValue().ordinal()) == 1d) && (calculatedDistance < distance.getValue()) || //Update lastmode every 5 mins
((modeSelected.getValue().ordinal() == 2d) && (calculatedDistance > distance.getValue()))) { lastMode = currentMode(calculatedDistance);
log.debug("Last mode is: "+lastMode);
log.debug("Wanted mode is: "+modeSelected.getValue());
if ((modeSelected.getValue() == INSIDE) && (calculatedDistance <= distance.getValue()) ||
((modeSelected.getValue() == OUTSIDE) && (calculatedDistance > distance.getValue())) ||
((modeSelected.getValue() == GOING_IN) && (calculatedDistance <= distance.getValue()) && (lastMode == OUTSIDE)) ||
((modeSelected.getValue() == GOING_OUT) && (calculatedDistance > distance.getValue()) && (lastMode == INSIDE))
) {
if (L.isEnabled(L.AUTOMATION)) if (L.isEnabled(L.AUTOMATION))
log.debug("Ready for execution: " + friendlyDescription()); log.debug("Ready for execution: " + friendlyDescription());
return true; return true;
@ -108,12 +117,14 @@ public class TriggerLocation extends Trigger {
@Override @Override
Trigger fromJSON(String data) { Trigger fromJSON(String data) {
try { try {
log.debug("fromJSON: "+data);
JSONObject d = new JSONObject(data); JSONObject d = new JSONObject(data);
latitude.setValue(JsonHelper.safeGetDouble(d, "latitude")); latitude.setValue(JsonHelper.safeGetDouble(d, "latitude"));
longitude.setValue(JsonHelper.safeGetDouble(d, "longitude")); longitude.setValue(JsonHelper.safeGetDouble(d, "longitude"));
distance.setValue(JsonHelper.safeGetDouble(d, "distance")); distance.setValue(JsonHelper.safeGetDouble(d, "distance"));
name.setValue(JsonHelper.safeGetString(d, "name")); name.setValue(JsonHelper.safeGetString(d, "name"));
modeSelected.setValue(InputLocationMode.Mode.valueOf(JsonHelper.safeGetString(d, "mode"))); modeSelected.setValue(InputLocationMode.Mode.valueOf(JsonHelper.safeGetString(d, "mode")));
lastMode = modeSelected.getValue(); // load the asked mode as default
lastRun = JsonHelper.safeGetLong(d, "lastRun"); lastRun = JsonHelper.safeGetLong(d, "lastRun");
} catch (Exception e) { } catch (Exception e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
@ -128,7 +139,7 @@ public class TriggerLocation extends Trigger {
@Override @Override
public String friendlyDescription() { public String friendlyDescription() {
return MainApp.gs(R.string.locationis, modeSelected.getValue() + " " + name.getValue()); return MainApp.gs(R.string.locationis, MainApp.gs(modeSelected.getValue().getStringRes()) + " " + name.getValue());
} }
@Override @Override
@ -163,7 +174,6 @@ public class TriggerLocation extends Trigger {
} }
TriggerLocation setMode(InputLocationMode.Mode value) { TriggerLocation setMode(InputLocationMode.Mode value) {
modeSelected.setValue(value); modeSelected.setValue(value);
return this; return this;
} }
@ -181,4 +191,12 @@ public class TriggerLocation extends Trigger {
.build(root); .build(root);
} }
// Method to return the actual mode based on the current distance
InputLocationMode.Mode currentMode(double currentDistance){
log.debug("Updating current mode!");
if ( currentDistance <= this.distance.getValue() )
return INSIDE;
else
return InputLocationMode.Mode.OUTSIDE;
}
} }

View file

@ -1414,6 +1414,8 @@
<string name="location_mode">In/Out of defined area</string> <string name="location_mode">In/Out of defined area</string>
<string name="location_inside">inside</string> <string name="location_inside">inside</string>
<string name="location_outside">outside</string> <string name="location_outside">outside</string>
<string name="location_going_in">when you enter the area named</string>
<string name="location_going_out">when you leave the area named</string>
<string name="lastboluslabel">Last bolus ago</string> <string name="lastboluslabel">Last bolus ago</string>
<string name="lastboluscompared">Last bolus time %1$s %2$s min ago</string> <string name="lastboluscompared">Last bolus time %1$s %2$s min ago</string>
<string name="triggercoblabel">COB</string> <string name="triggercoblabel">COB</string>