completed conditional actions

This commit is contained in:
Stephan Richter
2020-10-30 00:18:21 +01:00
parent e199482c07
commit 3858f7e36d
9 changed files with 65 additions and 11 deletions

View File

@@ -122,7 +122,7 @@ public class ControlUnit extends Thread implements Constants{
restart();
return t("Control unit (re)started.");
case ACTION_EMERGENCY:
power = true;
return emergency();
case ACTION_POWER:
return togglePower();
case ACTION_PROPS:
@@ -134,6 +134,11 @@ public class ControlUnit extends Thread implements Constants{
return t("Unknown action: {}",params.get(ACTION));
}
public Object emergency() {
power = true;
return togglePower();
}
public Object properties() {
Window win = new Window("cu-props", t("Properties of the control unit"));
Form form = new Form();

View File

@@ -29,7 +29,7 @@ public abstract class Action implements Constants {
public Route route = null;
public Train train = null;
public Context(Contact c) {
public Context(Contact c) {
contact = c;
route = contact.route();
if (route == null) return;
@@ -56,7 +56,7 @@ public abstract class Action implements Constants {
protected Tag link(int actionId, String context) {
Map<String, String> props = Map.of(REALM,REALM_ACTIONS,ID,actionId+"/"+id,ACTION,ACTION_PROPS,CONTEXT,context);
String action = "request("+(new JSONObject(props).toString().replace("\"", "'"))+")";
return new Tag("span").content(toString()).attr("onclick", action);
return new Tag("span").content(toString()+NBSP).attr("onclick", action);
}
public static Action load(JSONObject json) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {

View File

@@ -56,11 +56,14 @@ public class ActionList extends Vector<Action> implements Constants{
new Input(CONTEXT,context).hideIn(typeForm);
Select select = new Select(TYPE);
List<Class<? extends Action>> classes = List.of(
ConditionalAction.class,
SpeedReduction.class,
SetSignalsToStop.class,
FinishRoute.class,
TurnTrain.class,
ConditionalAction.class);
StopAuto.class,
PowerOff.class
);
for (Class<? extends Action> clazz : classes) select.addOption(clazz.getSimpleName());
select.addTo(new Label("Action type:")).addTo(typeForm);
return new Button(t("Create action"),"return submitForm('"+formId+"');").addTo(typeForm).addTo(win);
@@ -87,7 +90,13 @@ public class ActionList extends Vector<Action> implements Constants{
break;
case "TurnTrain":
add(new TurnTrain());
break;
break;
case "StopAuto":
add(new StopAuto());
break;
case "PowerOff":
add(new PowerOff());
break;
default:
actionTypeForm(win,context);
new Tag("span").content(t("Unknown action type: {}",type)).addTo(win);
@@ -117,6 +126,10 @@ public class ActionList extends Vector<Action> implements Constants{
}
props.put(ACTION, ACTION_DROP);
new Button("-",props).addTo(act);
if (action instanceof ConditionalAction) {
ConditionalAction ca = (ConditionalAction) action;
ca.children().addTo(act, context);
}
act.addTo(ul);
first = false;
}

View File

@@ -19,9 +19,15 @@ public class ConditionalAction extends Action {
private Vector<Condition> conditions = new Vector<Condition>();
private ActionList actions = new ActionList();
private Tag actionsForm(HashMap<String, String> params) {
Fieldset fieldset = new Fieldset(t("Actions"));
actions.addTo(fieldset, params.get(CONTEXT));
return fieldset;
}
private Tag conditionForm(HashMap<String, String> params) {
Fieldset fieldset = new Fieldset("Conditions");
Fieldset fieldset = new Fieldset(t("Conditions"));
if (!conditions.isEmpty()) {
Tag list = new Tag("ul");
@@ -63,6 +69,7 @@ public class ConditionalAction extends Action {
public Window properties(HashMap<String, String> params) {
Window win = super.properties(params);
conditionForm(params).addTo(win);
actionsForm(params).addTo(win);
return win;
}
@@ -92,4 +99,8 @@ public class ConditionalAction extends Action {
}
return super.update(params);
}
public ActionList children() {
return actions;
}
}

View File

@@ -0,0 +1,10 @@
package de.srsoftware.web4rail.actions;
public class PowerOff extends Action{
@Override
public boolean fire(Context context) {
context.contact.plan().controlUnit().emergency();
return false;
}
}

View File

@@ -0,0 +1,16 @@
package de.srsoftware.web4rail.actions;
import java.io.IOException;
public class StopAuto extends Action {
@Override
public boolean fire(Context context) throws IOException {
if (context.train != null) {
context.train.quitAutopilot();
return true;
}
return false;
}
}

View File

@@ -17,8 +17,7 @@ public class TrainSelect extends Condition {
@Override
public boolean fulfilledBy(Context context) {
// TODO Auto-generated method stub
return false;
return context.train == train;
}
@Override

View File

@@ -367,7 +367,7 @@ public class Train implements Constants {
return window;
}
private Object quitAutopilot() {
public Object quitAutopilot() {
if (autopilot != null) {
autopilot.stop = true;
autopilot = null;