trigger works

This commit is contained in:
Roumen Georgiev 2019-12-03 16:49:08 +02:00
parent 2fa3805030
commit aae2e21bef
2 changed files with 41 additions and 19 deletions

View file

@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.general.automation.triggers;
import android.location.Location; import android.location.Location;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import com.google.common.base.Optional; import com.google.common.base.Optional;
@ -35,17 +36,17 @@ import info.nightscout.androidaps.utils.T;
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);
private static final String modeEntered = "entered";
private static final String modeLeft = "left";
private static final ArrayList<InputOption> modes = new ArrayList<>(Arrays.asList( private static final ArrayList<InputOption> modes = new ArrayList<>(Arrays.asList(
new InputOption(R.string.location_entered, modeEntered), new InputOption(R.string.location_inside, MainApp.gs(R.string.location_inside)),
new InputOption(R.string.location_left, modeLeft) new InputOption(R.string.location_outside, MainApp.gs(R.string.location_outside))
)); ));
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"));
InputSelect mode = new InputSelect(modes); // Default mode selected is 1 - inside area
InputSelect modeSelected = new InputSelect(modes);
InputString name = new InputString(); InputString name = new InputString();
private Runnable buttonAction = () -> { private Runnable buttonAction = () -> {
@ -66,7 +67,7 @@ public class TriggerLocation extends Trigger {
latitude = new InputDouble(triggerLocation.latitude); latitude = new InputDouble(triggerLocation.latitude);
longitude = new InputDouble(triggerLocation.longitude); longitude = new InputDouble(triggerLocation.longitude);
distance = new InputDouble(triggerLocation.distance); distance = new InputDouble(triggerLocation.distance);
mode = new InputSelect(triggerLocation.mode); modeSelected = new InputSelect(modes);
lastRun = triggerLocation.lastRun; lastRun = triggerLocation.lastRun;
name = triggerLocation.name; name = triggerLocation.name;
} }
@ -84,9 +85,8 @@ 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 (((stringToMode(modeSelected.getValue()) == 1d) && (calculatedDistance < distance.getValue())) ||
if ((mode.equals(modeEntered) && calculatedDistance < distance.getValue()) || ((stringToMode(modeSelected.getValue()) == 2d) && (calculatedDistance > distance.getValue()))) {
(mode.equals(modeLeft) && calculatedDistance > distance.getValue())) {
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;
@ -104,7 +104,7 @@ public class TriggerLocation extends Trigger {
data.put("longitude", longitude.getValue()); data.put("longitude", longitude.getValue());
data.put("distance", distance.getValue()); data.put("distance", distance.getValue());
data.put("name", name.getValue()); data.put("name", name.getValue());
data.put("mode", mode.getValue()); data.put("mode", stringToMode(modeSelected.getValue()));
data.put("lastRun", lastRun); data.put("lastRun", lastRun);
o.put("data", data); o.put("data", data);
} catch (JSONException e) { } catch (JSONException e) {
@ -121,7 +121,7 @@ public class TriggerLocation extends Trigger {
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"));
mode.setValue(JsonHelper.safeGetString(d, "mode", modeEntered)); modeSelected.setValue(modeToString(JsonHelper.safeGetDouble(d, "mode")));
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);
@ -136,7 +136,7 @@ public class TriggerLocation extends Trigger {
@Override @Override
public String friendlyDescription() { public String friendlyDescription() {
return MainApp.gs(R.string.locationis, name.getValue()); return MainApp.gs(R.string.locationis, modeSelected.getValue() + " " + name.getValue());
} }
@Override @Override
@ -170,8 +170,8 @@ public class TriggerLocation extends Trigger {
return this; return this;
} }
TriggerLocation setMode(String value) { TriggerLocation setMode(double value) {
mode.setValue(value); modeSelected.setValue(modeToString(value));
return this; return this;
} }
@ -183,8 +183,30 @@ public class TriggerLocation extends Trigger {
.add(new LabelWithElement(MainApp.gs(R.string.latitude_short), "", latitude)) .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.longitude_short), "", longitude))
.add(new LabelWithElement(MainApp.gs(R.string.distance_short), "", distance)) .add(new LabelWithElement(MainApp.gs(R.string.distance_short), "", distance))
.add(new LabelWithElement(MainApp.gs(R.string.location_mode), "", mode)) .add(new LabelWithElement(MainApp.gs(R.string.location_mode), "", modeSelected))
.add(new InputButton(MainApp.gs(R.string.currentlocation), buttonAction), LocationService.getLastLocation() != null) .add(new InputButton(MainApp.gs(R.string.currentlocation), buttonAction), LocationService.getLastLocation() != null)
.build(root); .build(root);
} }
public double stringToMode(String selected) {
if (selected == null)
return 1d;
if (selected.equals(MainApp.gs(R.string.location_inside)))
return 1d;
else if (selected.equals(MainApp.gs(R.string.location_outside)))
return 2d;
else return 0d;
}
public String modeToString(double selectedMode) {
if (selectedMode == 1d)
return MainApp.gs(R.string.location_inside);
else if (selectedMode == 2d)
return MainApp.gs(R.string.location_outside);
else
return MainApp.gs(R.string.location_mode);
}
} }

View file

@ -1410,10 +1410,10 @@
<string name="longitude_short">Lon:</string> <string name="longitude_short">Lon:</string>
<string name="distance_short">Dist [m]:</string> <string name="distance_short">Dist [m]:</string>
<string name="name_short">Name:</string> <string name="name_short">Name:</string>
<string name="locationis">Location is %1$s</string> <string name="locationis">Location is %1$s %2$s</string>
<string name="location_mode">On location</string> <string name="location_mode">In/Out of defined area</string>
<string name="location_entered">entered</string> <string name="location_inside">inside</string>
<string name="location_left">left</string> <string name="location_outside">outside</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>