improved actions and conditions code,
added TrainLength condition, added FreeStartBlock action
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>de.srsoftware</groupId>
|
<groupId>de.srsoftware</groupId>
|
||||||
<artifactId>web4rail</artifactId>
|
<artifactId>web4rail</artifactId>
|
||||||
<version>0.9.6</version>
|
<version>0.9.7</version>
|
||||||
<name>Web4Rail</name>
|
<name>Web4Rail</name>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<description>Java Model Railway Control</description>
|
<description>Java Model Railway Control</description>
|
||||||
|
|||||||
@@ -1,17 +1,29 @@
|
|||||||
Actions : Aktionen
|
Actions : Aktionen
|
||||||
|
Action type\: : Aktions-Typ
|
||||||
Added {} : {} hinzugefügt
|
Added {} : {} hinzugefügt
|
||||||
|
add action : Aktion hinzufügen
|
||||||
|
Add condition : Bedingung hinzufügen
|
||||||
Add tile : Kachel hinzufügen
|
Add tile : Kachel hinzufügen
|
||||||
Analyze plan : Plan analysieren
|
Analyze plan : Plan analysieren
|
||||||
Apply : Übernehmen
|
Apply : Übernehmen
|
||||||
|
ConditionalAction : bedingte Aktion
|
||||||
Conditions : Bedingungen
|
Conditions : Bedingungen
|
||||||
Contacts and actions : Kontakte und Aktionen
|
Actions and contacts : Aktionen und Kontakte
|
||||||
|
Create action : Aktion erzeugen
|
||||||
|
DelayedAction : verzögerte Aktion
|
||||||
|
delete route : Route löschen
|
||||||
Destination\: {} from {} : Ziel: {} von {}
|
Destination\: {} from {} : Ziel: {} von {}
|
||||||
|
Emergency : Notfall
|
||||||
|
FinishRoute : Route abschließen
|
||||||
|
FreeStartBlock : Start-Block freigeben
|
||||||
inverted : invertiert
|
inverted : invertiert
|
||||||
length\: : Länge:
|
length\: : Länge:
|
||||||
|
Maximum train length\: : maximale Zug-Länge
|
||||||
name\: : Name:
|
name\: : Name:
|
||||||
Origin and destination : Start und Ziel
|
Origin and destination : Start und Ziel
|
||||||
Origin\: {} to {} : Start: {} nach {}
|
Origin\: {} to {} : Start: {} nach {}
|
||||||
Plan saved as "{}". : Plan als „{}“ gespeichert.
|
Plan saved as "{}". : Plan als „{}“ gespeichert.
|
||||||
|
PowerOff : Strom abschalten
|
||||||
Properties : Eigenschaften
|
Properties : Eigenschaften
|
||||||
Properties of {} : Eigenschaften von {}
|
Properties of {} : Eigenschaften von {}
|
||||||
Properties of {} @ ({},{}) : Eigenschaften von {} @ ({},{})
|
Properties of {} @ ({},{}) : Eigenschaften von {} @ ({},{})
|
||||||
@@ -19,6 +31,19 @@ Reduce speed to {} km/h : Geschwindigkeit auf {} km/h reduzieren
|
|||||||
Routes using this tile\: : Fahrstraßen, die diesen Abschnitt verwenden:
|
Routes using this tile\: : Fahrstraßen, die diesen Abschnitt verwenden:
|
||||||
save : speichern
|
save : speichern
|
||||||
Save plan : Plan speichern
|
Save plan : Plan speichern
|
||||||
|
SetRelay : Relais schalten
|
||||||
|
SetSignalsToStop : Signale auf Halt stellen
|
||||||
|
SetSpeed : Geschwindigkeit ändern
|
||||||
|
Setup actions : Aktivierungs-Aktionen
|
||||||
Signals : Signale
|
Signals : Signale
|
||||||
|
StopAuto : Automatikmodus abschalten
|
||||||
|
Train\: : Zug:
|
||||||
|
train is longer than {} : Zug ist länger als {}
|
||||||
|
train is shorter than {} : Zug ist kürzer als {}
|
||||||
|
Trains\: : Züge:
|
||||||
|
TrainLength : Zuglänge
|
||||||
|
TrainSelect : Zug-Auswahl
|
||||||
|
Turn allowed : Wenden erlaubt
|
||||||
Turnouts : Weichen
|
Turnouts : Weichen
|
||||||
|
TurnTrain : Fahrtrichtung umkehren
|
||||||
Unknown action\: {} : Unbekannte Aktion: {}
|
Unknown action\: {} : Unbekannte Aktion: {}
|
||||||
@@ -26,15 +26,14 @@ import de.srsoftware.web4rail.actions.Action.Context;
|
|||||||
import de.srsoftware.web4rail.actions.ActionList;
|
import de.srsoftware.web4rail.actions.ActionList;
|
||||||
import de.srsoftware.web4rail.actions.ActivateRoute;
|
import de.srsoftware.web4rail.actions.ActivateRoute;
|
||||||
import de.srsoftware.web4rail.actions.FinishRoute;
|
import de.srsoftware.web4rail.actions.FinishRoute;
|
||||||
|
import de.srsoftware.web4rail.actions.FreeStartBlock;
|
||||||
import de.srsoftware.web4rail.actions.SetSignalsToStop;
|
import de.srsoftware.web4rail.actions.SetSignalsToStop;
|
||||||
import de.srsoftware.web4rail.actions.SetSpeed;
|
import de.srsoftware.web4rail.actions.SetSpeed;
|
||||||
import de.srsoftware.web4rail.conditions.Condition;
|
import de.srsoftware.web4rail.conditions.Condition;
|
||||||
import de.srsoftware.web4rail.conditions.TrainSelect;
|
|
||||||
import de.srsoftware.web4rail.moving.Train;
|
import de.srsoftware.web4rail.moving.Train;
|
||||||
import de.srsoftware.web4rail.tags.Button;
|
import de.srsoftware.web4rail.tags.Button;
|
||||||
import de.srsoftware.web4rail.tags.Form;
|
import de.srsoftware.web4rail.tags.Form;
|
||||||
import de.srsoftware.web4rail.tags.Input;
|
import de.srsoftware.web4rail.tags.Input;
|
||||||
import de.srsoftware.web4rail.tags.Select;
|
|
||||||
import de.srsoftware.web4rail.tiles.Block;
|
import de.srsoftware.web4rail.tiles.Block;
|
||||||
import de.srsoftware.web4rail.tiles.Contact;
|
import de.srsoftware.web4rail.tiles.Contact;
|
||||||
import de.srsoftware.web4rail.tiles.Shadow;
|
import de.srsoftware.web4rail.tiles.Shadow;
|
||||||
@@ -77,6 +76,7 @@ public class Route implements Constants{
|
|||||||
private static final String ACTION_LISTS = "action_lists";
|
private static final String ACTION_LISTS = "action_lists";
|
||||||
private static final String ROUTES = "routes";
|
private static final String ROUTES = "routes";
|
||||||
private static final String CONDITIONS = "conditions";
|
private static final String CONDITIONS = "conditions";
|
||||||
|
private static final String DROP_CONDITION = "drop_condition";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* process commands from the client
|
* process commands from the client
|
||||||
@@ -103,10 +103,26 @@ public class Route implements Constants{
|
|||||||
return route.properties(params);
|
return route.properties(params);
|
||||||
case ACTION_UPDATE:
|
case ACTION_UPDATE:
|
||||||
return route.update(params,plan);
|
return route.update(params,plan);
|
||||||
|
case DROP_CONDITION:
|
||||||
|
return route.dropCodition(params);
|
||||||
}
|
}
|
||||||
return t("Unknown action: {}",params.get(ACTION));
|
return t("Unknown action: {}",params.get(ACTION));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Object dropCodition(HashMap<String, String> params) {
|
||||||
|
String condId = params.get(REALM_CONDITION);
|
||||||
|
if (condId != null) {
|
||||||
|
int cid = Integer.parseInt(condId);
|
||||||
|
for (Condition condition : conditions) {
|
||||||
|
if (condition.id() == cid) {
|
||||||
|
conditions.remove(condition);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return properties(params);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Route wurde von Zug betreten
|
* Route wurde von Zug betreten
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
@@ -167,18 +183,16 @@ public class Route implements Constants{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addCondition(String type) {
|
|
||||||
switch (type) {
|
|
||||||
case "TrainSelect":
|
|
||||||
conditions.add(new TrainSelect());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addConditionsTo(Window win) {
|
private void addConditionsTo(Window win) {
|
||||||
new Tag("h4").content(t("Conditions")).addTo(win);
|
new Tag("h4").content(t("Conditions")).addTo(win);
|
||||||
if (!conditions.isEmpty()) {
|
if (!conditions.isEmpty()) {
|
||||||
Tag list = new Tag("ul");
|
Tag list = new Tag("ul");
|
||||||
for (Condition condition : conditions) condition.link("li",REALM_ROUTE+":"+id).addTo(list);
|
for (Condition condition : conditions) {
|
||||||
|
Tag li = new Tag("li");
|
||||||
|
condition.link("span",REALM_ROUTE+":"+id).addTo(li);
|
||||||
|
Map<String, Object> params = Map.of(REALM,REALM_ROUTE,ID,id(),ACTION,DROP_CONDITION,REALM_CONDITION,condition.id());
|
||||||
|
new Button(t("delete"), params).addTo(li).addTo(list);
|
||||||
|
}
|
||||||
list.addTo(win);
|
list.addTo(win);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,19 +201,16 @@ public class Route implements Constants{
|
|||||||
new Input(ID,id()).hideIn(form);
|
new Input(ID,id()).hideIn(form);
|
||||||
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
||||||
|
|
||||||
Select select = new Select(REALM_CONDITION);
|
Condition.selector().addTo(form);
|
||||||
List<Class<? extends Condition>> classes = List.of(TrainSelect.class);
|
|
||||||
for (Class<? extends Condition> clazz : classes) select.addOption(clazz.getSimpleName());
|
|
||||||
select.addTo(form);
|
|
||||||
new Button(t("Add condition"),form).addTo(form).addTo(win);
|
new Button(t("Add condition"),form).addTo(form).addTo(win);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addContactsTo(Window win) {
|
private void addContactsTo(Window win) {
|
||||||
if (!contacts.isEmpty()) {
|
if (!contacts.isEmpty()) {
|
||||||
new Tag("h4").content(t("Actions and Contacts")).addTo(win);
|
new Tag("h4").content(t("Actions and contacts")).addTo(win);
|
||||||
Tag list = new Tag("ol");
|
Tag list = new Tag("ol");
|
||||||
|
|
||||||
Tag setup = new Tag("li").content("Setup actions");
|
Tag setup = new Tag("li").content(t("Setup actions"));
|
||||||
setupActions.addTo(setup, context());
|
setupActions.addTo(setup, context());
|
||||||
setup.addTo(list);
|
setup.addTo(list);
|
||||||
for (Contact c : contacts) {
|
for (Contact c : contacts) {
|
||||||
@@ -277,12 +288,13 @@ public class Route implements Constants{
|
|||||||
if (contacts.size()>1) { // mindestens 2 Kontakte: erster Kontakt aktiviert Block, vorletzter Kontakt leitet Bremsung ein
|
if (contacts.size()>1) { // mindestens 2 Kontakte: erster Kontakt aktiviert Block, vorletzter Kontakt leitet Bremsung ein
|
||||||
add(contacts.firstElement().trigger(),new ActivateRoute());
|
add(contacts.firstElement().trigger(),new ActivateRoute());
|
||||||
Contact nextToLastContact = contacts.get(contacts.size()-2);
|
Contact nextToLastContact = contacts.get(contacts.size()-2);
|
||||||
add(nextToLastContact.trigger(),new SetSpeed(30));
|
add(nextToLastContact.trigger(),new SetSpeed().speed(30));
|
||||||
add(nextToLastContact.trigger(),new SetSignalsToStop());
|
add(nextToLastContact.trigger(),new SetSignalsToStop());
|
||||||
}
|
}
|
||||||
if (!contacts.isEmpty()) {
|
if (!contacts.isEmpty()) {
|
||||||
Contact lastContact = contacts.lastElement();
|
Contact lastContact = contacts.lastElement();
|
||||||
add(lastContact.trigger(), new SetSpeed(0));
|
add(lastContact.trigger(), new SetSpeed());
|
||||||
|
add(lastContact.trigger(), new FreeStartBlock());
|
||||||
add(lastContact.trigger(), new FinishRoute());
|
add(lastContact.trigger(), new FinishRoute());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -313,7 +325,6 @@ public class Route implements Constants{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void finish() throws IOException {
|
public void finish() throws IOException {
|
||||||
startBlock.train(null);
|
|
||||||
train.route = null;
|
train.route = null;
|
||||||
unlock();
|
unlock();
|
||||||
endBlock.train(train.heading(endDirection.inverse()));
|
endBlock.train(train.heading(endDirection.inverse()));
|
||||||
@@ -332,6 +343,11 @@ public class Route implements Constants{
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Route freeStartBlock() throws IOException {
|
||||||
|
startBlock.train(null);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
private String generateName() {
|
private String generateName() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (int i=0; i<path.size();i++) {
|
for (int i=0; i<path.size();i++) {
|
||||||
@@ -599,9 +615,9 @@ public class Route implements Constants{
|
|||||||
String name = params.get(NAME);
|
String name = params.get(NAME);
|
||||||
if (name != null) name(name);
|
if (name != null) name(name);
|
||||||
|
|
||||||
String condition = params.get(REALM_CONDITION);
|
Condition condition = Condition.create(params.get(REALM_CONDITION));
|
||||||
if (condition != null) {
|
if (condition != null) {
|
||||||
addCondition(condition);
|
conditions.add(condition);
|
||||||
return properties(params);
|
return properties(params);
|
||||||
}
|
}
|
||||||
String message = t("{} updated.",this);
|
String message = t("{} updated.",this);
|
||||||
|
|||||||
@@ -2,7 +2,10 @@ package de.srsoftware.web4rail.actions;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -16,11 +19,14 @@ import de.srsoftware.web4rail.Plan;
|
|||||||
import de.srsoftware.web4rail.Route;
|
import de.srsoftware.web4rail.Route;
|
||||||
import de.srsoftware.web4rail.Window;
|
import de.srsoftware.web4rail.Window;
|
||||||
import de.srsoftware.web4rail.moving.Train;
|
import de.srsoftware.web4rail.moving.Train;
|
||||||
|
import de.srsoftware.web4rail.tags.Label;
|
||||||
|
import de.srsoftware.web4rail.tags.Select;
|
||||||
import de.srsoftware.web4rail.tiles.Contact;
|
import de.srsoftware.web4rail.tiles.Contact;
|
||||||
|
|
||||||
public abstract class Action implements Constants {
|
public abstract class Action implements Constants {
|
||||||
private static final HashMap<Integer,Action> actions = new HashMap<Integer, Action>();
|
private static final HashMap<Integer,Action> actions = new HashMap<Integer, Action>();
|
||||||
public static final Logger LOG = LoggerFactory.getLogger(Action.class);
|
public static final Logger LOG = LoggerFactory.getLogger(Action.class);
|
||||||
|
private static final String PREFIX = Action.class.getPackageName();
|
||||||
protected int id;
|
protected int id;
|
||||||
|
|
||||||
public static class Context {
|
public static class Context {
|
||||||
@@ -47,31 +53,20 @@ public abstract class Action implements Constants {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Action create(String type) {
|
public static Action create(String type) {
|
||||||
switch (type) { // TODO: das kann man mit Reflection generischer lösen
|
try {
|
||||||
case "ConditionalAction":
|
return (Action) Class.forName(PREFIX+"."+type).getDeclaredConstructor().newInstance();
|
||||||
return new ConditionalAction();
|
} catch (Exception e) {
|
||||||
case "FinishRoute":
|
e.printStackTrace();
|
||||||
return new FinishRoute();
|
|
||||||
case "SetSignalsToStop":
|
|
||||||
return new SetSignalsToStop();
|
|
||||||
case "SetSpeed":
|
|
||||||
return new SetSpeed(0);
|
|
||||||
case "TurnTrain":
|
|
||||||
return new TurnTrain();
|
|
||||||
case "StopAuto":
|
|
||||||
return new StopAuto();
|
|
||||||
case "PowerOff":
|
|
||||||
return new PowerOff();
|
|
||||||
case "SetRelay":
|
|
||||||
return new SetRelay();
|
|
||||||
case "DelayedAction":
|
|
||||||
return new DelayedAction();
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract boolean fire(Context context) throws IOException;
|
public abstract boolean fire(Context context) throws IOException;
|
||||||
|
|
||||||
|
public static Action get(int actionId) {
|
||||||
|
return actions.get(actionId);
|
||||||
|
}
|
||||||
|
|
||||||
public int id() {
|
public int id() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
@@ -86,30 +81,23 @@ public abstract class Action implements Constants {
|
|||||||
return new Tag("span").content(toString()+NBSP).attr("onclick", action);
|
return new Tag("span").content(toString()+NBSP).attr("onclick", action);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Action load(JSONObject json) {
|
public static List<Class<? extends Action>> list() {
|
||||||
String clazz = json.getString(TYPE);
|
return List.of(
|
||||||
switch (clazz) { // TODO: das kann generisch mittels Reflection implementiert werden!
|
ConditionalAction.class,
|
||||||
case "ActivateRoute":
|
SetSpeed.class,
|
||||||
return new ActivateRoute();
|
SetSignalsToStop.class,
|
||||||
case "ConditionalAction":
|
FreeStartBlock.class,
|
||||||
return ConditionalAction.load(json);
|
FinishRoute.class,
|
||||||
case "FinishRoute":
|
TurnTrain.class,
|
||||||
return new FinishRoute();
|
StopAuto.class,
|
||||||
case "PowerOff":
|
PowerOff.class,
|
||||||
return new PowerOff();
|
SetRelay.class,
|
||||||
case "SetSignalsToStop":
|
DelayedAction.class
|
||||||
return new SetSignalsToStop();
|
);
|
||||||
case "SetSpeed":
|
|
||||||
return SetSpeed.load(json);
|
|
||||||
case "SetRelay":
|
|
||||||
return SetRelay.load(json);
|
|
||||||
case "TurnTrain":
|
|
||||||
return new TurnTrain();
|
|
||||||
case "DelayedAction":
|
|
||||||
return DelayedAction.load(json);
|
|
||||||
}
|
}
|
||||||
LOG.error("Found unknwon action \"{}\" in json!",clazz);
|
|
||||||
return null;
|
public Action load(JSONObject json) {
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Window properties(HashMap<String, String> params) {
|
public Window properties(HashMap<String, String> params) {
|
||||||
@@ -129,7 +117,16 @@ public abstract class Action implements Constants {
|
|||||||
return t("Nothing changed");
|
return t("Nothing changed");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Action get(int actionId) {
|
public static Tag selector() {
|
||||||
return actions.get(actionId);
|
Select select = new Select(TYPE);
|
||||||
|
TreeMap<String, String> names = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
|
||||||
|
|
||||||
|
for (Class<? extends Action> clazz : Action.list()) {
|
||||||
|
String s = t(clazz.getSimpleName());
|
||||||
|
names.put(s, clazz.getSimpleName());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Entry<String, String> entry : names.entrySet()) select.addOption(entry.getValue(), entry.getKey());
|
||||||
|
return select.addTo(new Label(t("Action type:")+NBSP));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,8 +21,6 @@ import de.srsoftware.web4rail.actions.Action.Context;
|
|||||||
import de.srsoftware.web4rail.tags.Button;
|
import de.srsoftware.web4rail.tags.Button;
|
||||||
import de.srsoftware.web4rail.tags.Form;
|
import de.srsoftware.web4rail.tags.Form;
|
||||||
import de.srsoftware.web4rail.tags.Input;
|
import de.srsoftware.web4rail.tags.Input;
|
||||||
import de.srsoftware.web4rail.tags.Label;
|
|
||||||
import de.srsoftware.web4rail.tags.Select;
|
|
||||||
|
|
||||||
public class ActionList extends Vector<Action> implements Constants{
|
public class ActionList extends Vector<Action> implements Constants{
|
||||||
|
|
||||||
@@ -55,20 +53,7 @@ public class ActionList extends Vector<Action> implements Constants{
|
|||||||
new Input(ID,id).hideIn(typeForm);
|
new Input(ID,id).hideIn(typeForm);
|
||||||
new Input(ACTION,ACTION_ADD).hideIn(typeForm);
|
new Input(ACTION,ACTION_ADD).hideIn(typeForm);
|
||||||
new Input(CONTEXT,context).hideIn(typeForm);
|
new Input(CONTEXT,context).hideIn(typeForm);
|
||||||
Select select = new Select(TYPE);
|
Action.selector().addTo(typeForm);
|
||||||
List<Class<? extends Action>> classes = List.of(
|
|
||||||
ConditionalAction.class,
|
|
||||||
SetSpeed.class,
|
|
||||||
SetSignalsToStop.class,
|
|
||||||
FinishRoute.class,
|
|
||||||
TurnTrain.class,
|
|
||||||
StopAuto.class,
|
|
||||||
PowerOff.class,
|
|
||||||
SetRelay.class,
|
|
||||||
DelayedAction.class
|
|
||||||
);
|
|
||||||
for (Class<? extends Action> clazz : classes) select.addOption(clazz.getSimpleName());
|
|
||||||
select.addTo(new Label(t("Action type:")+NBSP)).addTo(typeForm);
|
|
||||||
return new Button(t("Create action"),typeForm).addTo(typeForm).addTo(win);
|
return new Button(t("Create action"),typeForm).addTo(typeForm).addTo(win);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,8 +146,9 @@ public class ActionList extends Vector<Action> implements Constants{
|
|||||||
ActionList actionList = new ActionList();
|
ActionList actionList = new ActionList();
|
||||||
for (Object o : list) {
|
for (Object o : list) {
|
||||||
if (o instanceof JSONObject) {
|
if (o instanceof JSONObject) {
|
||||||
Action action = Action.load((JSONObject) o);
|
JSONObject json = (JSONObject) o;
|
||||||
if (action != null) actionList.add(action);
|
Action action = Action.create(json.getString(TYPE));
|
||||||
|
if (action != null) actionList.add(action.load(json));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return actionList;
|
return actionList;
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package de.srsoftware.web4rail.actions;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
@@ -11,12 +10,10 @@ import org.json.JSONObject;
|
|||||||
import de.srsoftware.tools.Tag;
|
import de.srsoftware.tools.Tag;
|
||||||
import de.srsoftware.web4rail.Window;
|
import de.srsoftware.web4rail.Window;
|
||||||
import de.srsoftware.web4rail.conditions.Condition;
|
import de.srsoftware.web4rail.conditions.Condition;
|
||||||
import de.srsoftware.web4rail.conditions.TrainSelect;
|
|
||||||
import de.srsoftware.web4rail.tags.Button;
|
import de.srsoftware.web4rail.tags.Button;
|
||||||
import de.srsoftware.web4rail.tags.Fieldset;
|
import de.srsoftware.web4rail.tags.Fieldset;
|
||||||
import de.srsoftware.web4rail.tags.Form;
|
import de.srsoftware.web4rail.tags.Form;
|
||||||
import de.srsoftware.web4rail.tags.Input;
|
import de.srsoftware.web4rail.tags.Input;
|
||||||
import de.srsoftware.web4rail.tags.Select;
|
|
||||||
|
|
||||||
public class ConditionalAction extends Action {
|
public class ConditionalAction extends Action {
|
||||||
|
|
||||||
@@ -50,10 +47,7 @@ public class ConditionalAction extends Action {
|
|||||||
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
||||||
new Input(CONTEXT,params.get(CONTEXT)).hideIn(form);
|
new Input(CONTEXT,params.get(CONTEXT)).hideIn(form);
|
||||||
|
|
||||||
Select select = new Select(REALM_CONDITION);
|
Condition.selector().addTo(form);
|
||||||
List<Class<? extends Condition>> classes = List.of(TrainSelect.class);
|
|
||||||
for (Class<? extends Condition> clazz : classes) select.addOption(clazz.getSimpleName());
|
|
||||||
select.addTo(form);
|
|
||||||
return new Button(t("Add condition"),form).addTo(form).addTo(fieldset);
|
return new Button(t("Add condition"),form).addTo(form).addTo(fieldset);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,16 +69,17 @@ public class ConditionalAction extends Action {
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ConditionalAction load(JSONObject json) {
|
@Override
|
||||||
ConditionalAction action = new ConditionalAction();
|
public Action load(JSONObject json) {
|
||||||
|
super.load(json);
|
||||||
for (Object o : json.getJSONArray(CONDITIONS)) {
|
for (Object o : json.getJSONArray(CONDITIONS)) {
|
||||||
if (o instanceof JSONObject) {
|
if (o instanceof JSONObject) {
|
||||||
Condition condition = Condition.load((JSONObject)o);
|
Condition condition = Condition.load((JSONObject)o);
|
||||||
if (condition != null) action.conditions.add(condition);
|
if (condition != null) conditions.add(condition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
action.actions = ActionList.load(json.getJSONArray(ACTIONS));
|
actions = ActionList.load(json.getJSONArray(ACTIONS));
|
||||||
return action;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -109,16 +104,9 @@ public class ConditionalAction extends Action {
|
|||||||
@Override
|
@Override
|
||||||
protected Object update(HashMap<String, String> params) {
|
protected Object update(HashMap<String, String> params) {
|
||||||
String conditionClass = params.get(REALM_CONDITION);
|
String conditionClass = params.get(REALM_CONDITION);
|
||||||
if (conditionClass != null) {
|
Condition condition = Condition.create(conditionClass);
|
||||||
switch (conditionClass) {
|
if (condition == null) return t("Unknown type of condition: {}",conditionClass);
|
||||||
case "TrainSelect":
|
conditions.add(condition);
|
||||||
conditions.add(new TrainSelect());
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
return t("Unknown type of condition: {}",conditionClass);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return super.update(params);
|
return super.update(params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,11 +70,11 @@ public class DelayedAction extends Action {
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DelayedAction load(JSONObject json) {
|
public DelayedAction load(JSONObject json) {
|
||||||
DelayedAction action = new DelayedAction();
|
super.load(json);
|
||||||
action.delay = json.getInt(DELAY);
|
delay = json.getInt(DELAY);
|
||||||
if (json.has(ACTIONS)) action.actions = ActionList.load(json.getJSONArray(ACTIONS));
|
if (json.has(ACTIONS)) actions = ActionList.load(json.getJSONArray(ACTIONS));
|
||||||
return action;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package de.srsoftware.web4rail.actions;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class FreeStartBlock extends Action {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean fire(Context context) throws IOException {
|
||||||
|
context.route.freeStartBlock();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,14 +39,15 @@ public class SetRelay extends Action {
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SetRelay load(JSONObject json) {
|
@Override
|
||||||
|
public Action load(JSONObject json) {
|
||||||
|
super.load(json);
|
||||||
String relayId = json.getString(RELAY);
|
String relayId = json.getString(RELAY);
|
||||||
SetRelay result = new SetRelay();
|
|
||||||
if (relayId != null) {
|
if (relayId != null) {
|
||||||
result.relay = Relay.get(relayId);
|
relay = Relay.get(relayId);
|
||||||
result.state = json.getBoolean(Relay.STATE);
|
state = json.getBoolean(Relay.STATE);
|
||||||
}
|
}
|
||||||
return result;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -14,12 +14,7 @@ import de.srsoftware.web4rail.tags.Label;
|
|||||||
public class SetSpeed extends Action{
|
public class SetSpeed extends Action{
|
||||||
|
|
||||||
public static final String MAX_SPEED = "max_speed";
|
public static final String MAX_SPEED = "max_speed";
|
||||||
private int maxSpeed = -1;
|
private int maxSpeed = 0;
|
||||||
|
|
||||||
public SetSpeed(int kmh) {
|
|
||||||
super();
|
|
||||||
maxSpeed = kmh;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean fire(Context context) {
|
public boolean fire(Context context) {
|
||||||
@@ -37,9 +32,12 @@ public class SetSpeed extends Action{
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SetSpeed load(JSONObject json) {
|
@Override
|
||||||
int s = json.getInt(MAX_SPEED);
|
public Action load(JSONObject json) {
|
||||||
return new SetSpeed(s);
|
super.load(json);
|
||||||
|
maxSpeed = json.getInt(MAX_SPEED);
|
||||||
|
return this;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -62,6 +60,11 @@ public class SetSpeed extends Action{
|
|||||||
return t("Reduce speed to {} km/h",maxSpeed);
|
return t("Reduce speed to {} km/h",maxSpeed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SetSpeed speed(int kmh) {
|
||||||
|
maxSpeed = kmh;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object update(HashMap<String, String> params) {
|
protected Object update(HashMap<String, String> params) {
|
||||||
LOG.debug("update: {}",params);
|
LOG.debug("update: {}",params);
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
package de.srsoftware.web4rail.conditions;
|
package de.srsoftware.web4rail.conditions;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -18,10 +21,13 @@ import de.srsoftware.web4rail.tags.Button;
|
|||||||
import de.srsoftware.web4rail.tags.Checkbox;
|
import de.srsoftware.web4rail.tags.Checkbox;
|
||||||
import de.srsoftware.web4rail.tags.Form;
|
import de.srsoftware.web4rail.tags.Form;
|
||||||
import de.srsoftware.web4rail.tags.Input;
|
import de.srsoftware.web4rail.tags.Input;
|
||||||
|
import de.srsoftware.web4rail.tags.Label;
|
||||||
|
import de.srsoftware.web4rail.tags.Select;
|
||||||
|
|
||||||
public abstract class Condition implements Constants {
|
public abstract class Condition implements Constants {
|
||||||
public static final Logger LOG = LoggerFactory.getLogger(Condition.class);
|
public static final Logger LOG = LoggerFactory.getLogger(Condition.class);
|
||||||
private static final String INVERTED = "inverted";
|
private static final String INVERTED = "inverted";
|
||||||
|
private static final String PREFIX = Condition.class.getPackageName();
|
||||||
private static HashMap<Integer, Condition> conditions = new HashMap<Integer, Condition>();
|
private static HashMap<Integer, Condition> conditions = new HashMap<Integer, Condition>();
|
||||||
public abstract boolean fulfilledBy(Context context);
|
public abstract boolean fulfilledBy(Context context);
|
||||||
public boolean inverted = false;
|
public boolean inverted = false;
|
||||||
@@ -55,6 +61,15 @@ public abstract class Condition implements Constants {
|
|||||||
return t("Unknown action: {}",action);
|
return t("Unknown action: {}",action);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Condition create(String type) {
|
||||||
|
try {
|
||||||
|
return (Condition) Class.forName(PREFIX+"."+type).getDeclaredConstructor().newInstance();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public JSONObject json() {
|
public JSONObject json() {
|
||||||
JSONObject json = new JSONObject().put(TYPE, getClass().getSimpleName());
|
JSONObject json = new JSONObject().put(TYPE, getClass().getSimpleName());
|
||||||
if (inverted) json.put(INVERTED, true);
|
if (inverted) json.put(INVERTED, true);
|
||||||
@@ -63,12 +78,7 @@ public abstract class Condition implements Constants {
|
|||||||
|
|
||||||
public static Condition load(JSONObject json) {
|
public static Condition load(JSONObject json) {
|
||||||
String type = json.getString(TYPE);
|
String type = json.getString(TYPE);
|
||||||
Condition condition = null;
|
Condition condition = Condition.create(type);
|
||||||
switch (type) {
|
|
||||||
case "TrainSelect":
|
|
||||||
condition = TrainSelect.load(json);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (condition != null) condition.inverted = json.has(INVERTED) && json.getBoolean(INVERTED);
|
if (condition != null) condition.inverted = json.has(INVERTED) && json.getBoolean(INVERTED);
|
||||||
return condition;
|
return condition;
|
||||||
}
|
}
|
||||||
@@ -95,6 +105,23 @@ public abstract class Condition implements Constants {
|
|||||||
return win;
|
return win;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Tag selector() {
|
||||||
|
Select select = new Select(REALM_CONDITION);
|
||||||
|
TreeMap<String, String> names = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
|
||||||
|
|
||||||
|
for (Class<? extends Condition> clazz : list()) {
|
||||||
|
String s = t(clazz.getSimpleName());
|
||||||
|
names.put(s, clazz.getSimpleName());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Entry<String, String> entry : names.entrySet()) select.addOption(entry.getValue(), entry.getKey());
|
||||||
|
return select.addTo(new Label(t("Action type:")+NBSP));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<Class<? extends Condition>> list() {
|
||||||
|
return List.of(TrainSelect.class,TrainLength.class);
|
||||||
|
}
|
||||||
|
|
||||||
public static String t(String text, Object...fills) {
|
public static String t(String text, Object...fills) {
|
||||||
return Translation.get(Application.class, text, fills);
|
return Translation.get(Application.class, text, fills);
|
||||||
}
|
}
|
||||||
@@ -108,4 +135,8 @@ public abstract class Condition implements Constants {
|
|||||||
inverted = "on".equals(params.get(INVERTED));
|
inverted = "on".equals(params.get(INVERTED));
|
||||||
return t("updated {}.",this);
|
return t("updated {}.",this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int id() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package de.srsoftware.web4rail.conditions;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import de.srsoftware.tools.Tag;
|
||||||
|
import de.srsoftware.web4rail.Window;
|
||||||
|
import de.srsoftware.web4rail.actions.Action.Context;
|
||||||
|
import de.srsoftware.web4rail.tags.Input;
|
||||||
|
import de.srsoftware.web4rail.tags.Label;
|
||||||
|
|
||||||
|
public class TrainLength extends Condition {
|
||||||
|
|
||||||
|
private static final String MAX_LENGTH = "max_length";
|
||||||
|
private int maxLength = 0;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean fulfilledBy(Context context) {
|
||||||
|
return context.train.length() < maxLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JSONObject json() {
|
||||||
|
return super.json().put(MAX_LENGTH, maxLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TrainLength load(JSONObject json) {
|
||||||
|
TrainLength tl = new TrainLength();
|
||||||
|
if (json.has(MAX_LENGTH)) tl.maxLength = json.getInt(MAX_LENGTH);
|
||||||
|
return tl;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Tag propForm(HashMap<String, String> params) {
|
||||||
|
return new Input(MAX_LENGTH, maxLength).addTo(new Label(t("Maximum train length:")+NBSP)).addTo(super.propForm(params));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return t(inverted ? "train is longer than {}" : "train is shorter than {}",maxLength) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object update(HashMap<String, String> params) {
|
||||||
|
if (params.containsKey(MAX_LENGTH)) try {
|
||||||
|
int ml = Integer.parseInt(params.get(MAX_LENGTH));
|
||||||
|
if (ml < 1) throw new NumberFormatException(t("length must be larger than zero!"));
|
||||||
|
maxLength = ml;
|
||||||
|
} catch (NumberFormatException nfe) {
|
||||||
|
Window win = properties(params);
|
||||||
|
win.children().insertElementAt(new Tag("div").content(nfe.getMessage()),1);
|
||||||
|
return win;
|
||||||
|
}
|
||||||
|
return super.update(params);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -152,19 +152,6 @@ public class Train implements Constants {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Object create(HashMap<String, String> params, Plan plan) {
|
|
||||||
Locomotive loco = (Locomotive) Locomotive.get(params.get(Train.LOCO_ID));
|
|
||||||
if (loco == null) return t("unknown locomotive: {}",params.get(ID));
|
|
||||||
Train train = new Train(loco).plan(plan);
|
|
||||||
if (params.containsKey(NAME)) train.name(params.get(NAME));
|
|
||||||
return train;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Train name(String newName) {
|
|
||||||
this.name = newName;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void add(Car car) {
|
public void add(Car car) {
|
||||||
if (car == null) return;
|
if (car == null) return;
|
||||||
if (car instanceof Locomotive) {
|
if (car instanceof Locomotive) {
|
||||||
@@ -189,6 +176,14 @@ public class Train implements Constants {
|
|||||||
this.block = block;
|
this.block = block;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Object create(HashMap<String, String> params, Plan plan) {
|
||||||
|
Locomotive loco = (Locomotive) Locomotive.get(params.get(Train.LOCO_ID));
|
||||||
|
if (loco == null) return t("unknown locomotive: {}",params.get(ID));
|
||||||
|
Train train = new Train(loco).plan(plan);
|
||||||
|
if (params.containsKey(NAME)) train.name(params.get(NAME));
|
||||||
|
return train;
|
||||||
|
}
|
||||||
|
|
||||||
public static Train get(int id) {
|
public static Train get(int id) {
|
||||||
return trains.get(id);
|
return trains.get(id);
|
||||||
}
|
}
|
||||||
@@ -321,6 +316,11 @@ public class Train implements Constants {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Train name(String newName) {
|
||||||
|
this.name = newName;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
private Train plan(Plan plan) {
|
private Train plan(Plan plan) {
|
||||||
this.plan = plan;
|
this.plan = plan;
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ public abstract class Block extends StretchableTile{
|
|||||||
new Checkbox(ALLOW_TURN,t("Turn allowed"),turnAllowed).addTo(new Tag("p")).addTo(form);
|
new Checkbox(ALLOW_TURN,t("Turn allowed"),turnAllowed).addTo(new Tag("p")).addTo(form);
|
||||||
|
|
||||||
Select select = Train.selector(train, null);
|
Select select = Train.selector(train, null);
|
||||||
select.addTo(new Label(t("Trains:")+" ")).addTo(new Tag("p")).addTo(form);
|
select.addTo(new Label(t("Trains:")+NBSP)).addTo(new Tag("p")).addTo(form);
|
||||||
|
|
||||||
return form;
|
return form;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user