fixed actions boken by function mapping implementation
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>1.5.2</version>
|
<version>1.5.3</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>
|
||||||
|
|||||||
@@ -2,14 +2,16 @@ abort : abbrechen
|
|||||||
Aborting route allocation... : Routen-Reservierung wird abgebrochen...
|
Aborting route allocation... : Routen-Reservierung wird abgebrochen...
|
||||||
Accessory : Zubehör
|
Accessory : Zubehör
|
||||||
Action : Aktion
|
Action : Aktion
|
||||||
|
Action fired : Aktion ausgeführt
|
||||||
|
Action failed : Aktion fehlgeschlagen
|
||||||
Actions : Aktionen
|
Actions : Aktionen
|
||||||
Actions (On) : Aktionen (Ein)
|
Actions (On) : Aktionen (Ein)
|
||||||
Actions (Off) : Aktionen (Aus)
|
Actions (Off) : Aktionen (Aus)
|
||||||
Actions and contacts : Aktionen und Kontakte
|
Actions and contacts : Aktionen und Kontakte
|
||||||
Action type : Aktions-Typ
|
|
||||||
Actions in case conditions are fulfilled : Aktionen, wenn Bedingungen erfüllt sind
|
Actions in case conditions are fulfilled : Aktionen, wenn Bedingungen erfüllt sind
|
||||||
Actions in case conditions are <em>not</em> fulfilled : Aktionen, wenn Bedingungen <em>nicht</em> erfüllt sind
|
Actions in case conditions are <em>not</em> fulfilled : Aktionen, wenn Bedingungen <em>nicht</em> erfüllt sind
|
||||||
Actions will only fire, if all conditions are fullfilled. : Aktionen werden nur ausgeführt, wenn alle Bedingungen erfüllt sind.
|
Actions will only fire, if all conditions are fullfilled. : Aktionen werden nur ausgeführt, wenn alle Bedingungen erfüllt sind.
|
||||||
|
Action type : Aktions-Typ
|
||||||
ActivateRoute : Route aktivieren
|
ActivateRoute : Route aktivieren
|
||||||
add : hinzufügen
|
add : hinzufügen
|
||||||
add command for {} : Kommando für {} hinzufügen
|
add command for {} : Kommando für {} hinzufügen
|
||||||
@@ -408,6 +410,7 @@ Task name must not be empty : Aufgaben-Name darf nicht leer sein
|
|||||||
Task type : Aufgabe
|
Task type : Aufgabe
|
||||||
Text to display on clients : Text, welcher auf den Clients angezeigt werden soll
|
Text to display on clients : Text, welcher auf den Clients angezeigt werden soll
|
||||||
Text to show on display : Text, welcher in der Anzeige dargestellt werden soll
|
Text to show on display : Text, welcher in der Anzeige dargestellt werden soll
|
||||||
|
test : testen
|
||||||
Tile(s) : Kachel(n)
|
Tile(s) : Kachel(n)
|
||||||
Tile(s) moved. : Kachel(n) verschoben.
|
Tile(s) moved. : Kachel(n) verschoben.
|
||||||
Timeout : maximale Wartezeit5
|
Timeout : maximale Wartezeit5
|
||||||
|
|||||||
@@ -148,7 +148,7 @@ public abstract class Action extends BaseClass {
|
|||||||
@Override
|
@Override
|
||||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm,String...errors) {
|
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm,String...errors) {
|
||||||
formInputs.add(t("Edit json"),button(t("export"), Map.of(ACTION, ACTION_SAVE)));
|
formInputs.add(t("Edit json"),button(t("export"), Map.of(ACTION, ACTION_SAVE)));
|
||||||
|
formInputs.add("",button(t("test"), Map.of(ACTION,ACTION_START)));
|
||||||
return super.properties(preForm, formInputs, postForm,errors);
|
return super.properties(preForm, formInputs, postForm,errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ 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.Window;
|
import de.srsoftware.web4rail.tags.Window;
|
||||||
|
import de.srsoftware.web4rail.tiles.Tile;
|
||||||
|
|
||||||
public class ActionList extends Action implements Iterable<Action>{
|
public class ActionList extends Action implements Iterable<Action>{
|
||||||
static final Logger LOG = LoggerFactory.getLogger(ActionList.class);
|
static final Logger LOG = LoggerFactory.getLogger(ActionList.class);
|
||||||
@@ -239,6 +240,8 @@ public class ActionList extends Action implements Iterable<Action>{
|
|||||||
return action.properties();
|
return action.properties();
|
||||||
case ACTION_SAVE:
|
case ACTION_SAVE:
|
||||||
return action.jsonImportExport(params);
|
return action.jsonImportExport(params);
|
||||||
|
case ACTION_START:
|
||||||
|
return start(action);
|
||||||
case ACTION_UPDATE:
|
case ACTION_UPDATE:
|
||||||
return action.update(params);
|
return action.update(params);
|
||||||
}
|
}
|
||||||
@@ -265,6 +268,18 @@ public class ActionList extends Action implements Iterable<Action>{
|
|||||||
super.removeChild(child);
|
super.removeChild(child);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Window start(Action action) {
|
||||||
|
BaseClass ctx = action.parent();
|
||||||
|
while (isSet(ctx) && !(ctx instanceof Tile) && isSet(ctx.parent())) ctx = ctx.parent();
|
||||||
|
Context startContext = new Context(ctx);
|
||||||
|
if (ctx instanceof Tile) {
|
||||||
|
Tile tile = (Tile) ctx;
|
||||||
|
startContext.train(tile.lockingTrain());
|
||||||
|
}
|
||||||
|
String message = action.fire(startContext) ? t("Action fired") : t("Action failed");
|
||||||
|
return action.properties(message);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return actions.isEmpty() ? "[no actions]" : actions.toString();
|
return actions.isEmpty() ? "[no actions]" : actions.toString();
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
package de.srsoftware.web4rail.actions;
|
package de.srsoftware.web4rail.actions;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import de.srsoftware.tools.Tag;
|
import de.srsoftware.tools.Tag;
|
||||||
import de.srsoftware.web4rail.BaseClass;
|
import de.srsoftware.web4rail.BaseClass;
|
||||||
import de.srsoftware.web4rail.Params;
|
import de.srsoftware.web4rail.Params;
|
||||||
|
import de.srsoftware.web4rail.functions.CustomFunction;
|
||||||
|
import de.srsoftware.web4rail.functions.Function;
|
||||||
import de.srsoftware.web4rail.tags.Fieldset;
|
import de.srsoftware.web4rail.tags.Fieldset;
|
||||||
import de.srsoftware.web4rail.tags.Radio;
|
import de.srsoftware.web4rail.tags.Radio;
|
||||||
import de.srsoftware.web4rail.tags.Select;
|
import de.srsoftware.web4rail.tags.Select;
|
||||||
@@ -20,7 +24,8 @@ public class SwitchFunction extends Action {
|
|||||||
private static final int ON = 1;
|
private static final int ON = 1;
|
||||||
private static final int OFF = 0;
|
private static final int OFF = 0;
|
||||||
|
|
||||||
private int function = 1,effect=-1;
|
private int effect=-1;
|
||||||
|
private String function = "["+t("Select function")+"]";
|
||||||
|
|
||||||
public SwitchFunction(BaseClass parent) {
|
public SwitchFunction(BaseClass parent) {
|
||||||
super(parent);
|
super(parent);
|
||||||
@@ -31,8 +36,7 @@ public class SwitchFunction extends Action {
|
|||||||
if (isNull(context) || isNull(context.train())) return false;
|
if (isNull(context) || isNull(context.train())) return false;
|
||||||
switch (effect) {
|
switch (effect) {
|
||||||
case TOGGLE:
|
case TOGGLE:
|
||||||
// context.train().toggleFunction(function);
|
context.train().toggleFunction(function);
|
||||||
// TODO
|
|
||||||
return true;
|
return true;
|
||||||
case ON:
|
case ON:
|
||||||
context.train().setFunction(function, true);
|
context.train().setFunction(function, true);
|
||||||
@@ -56,18 +60,31 @@ public class SwitchFunction extends Action {
|
|||||||
@Override
|
@Override
|
||||||
public Action load(JSONObject json) {
|
public Action load(JSONObject json) {
|
||||||
super.load(json);
|
super.load(json);
|
||||||
if (json.has(EFFECT)) effect = json.getInt(EFFECT);
|
try {
|
||||||
if (json.has(FUNCTION)) function = json.getInt(FUNCTION);
|
if (json.has(EFFECT)) effect = json.getInt(EFFECT);
|
||||||
|
if (json.has(FUNCTION)) function = json.getString(FUNCTION);
|
||||||
|
} catch(JSONException je) {
|
||||||
|
LOG.warn("Was not able to load function!",je);
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm,String...errors) {
|
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm,String...errors) {
|
||||||
|
|
||||||
Select selector = new Select(FUNCTION);
|
Select selector = Function.selector(function);
|
||||||
for (int i=1; i<5;i++) {
|
Vector<Tag> options = selector.children();
|
||||||
Tag option = selector.addOption(i,t("F"+i));
|
for (Tag option : options) { // remove unconfigured CustomFunction
|
||||||
if (function == i) option.attr("selected", "selected");
|
if (CustomFunction.class.getSimpleName().equals(option.get("value"))) {
|
||||||
|
options.remove(option);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
List<CustomFunction> customFunctions = BaseClass.listElements(CustomFunction.class);
|
||||||
|
for (CustomFunction cf : customFunctions) { // add configured custom functions
|
||||||
|
String cfName = cf.name();
|
||||||
|
Tag option = selector.addOption(cfName);
|
||||||
|
if (function.equals(cfName)) option.attr("selected", "selected");
|
||||||
}
|
}
|
||||||
formInputs.add(t("Function"), selector);
|
formInputs.add(t("Function"), selector);
|
||||||
|
|
||||||
@@ -84,22 +101,19 @@ public class SwitchFunction extends Action {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
switch (effect) {
|
switch (effect) {
|
||||||
case TOGGLE:
|
case TOGGLE:
|
||||||
return t("toggle {}","F"+function);
|
return t("toggle {}",function);
|
||||||
case ON:
|
case ON:
|
||||||
return t("enable {}","F"+function);
|
return t("enable {}",function);
|
||||||
case OFF:
|
case OFF:
|
||||||
return t("disable {}","F"+function);
|
return t("disable {}",function);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object update(Params params) {
|
protected Object update(Params params) {
|
||||||
String fn = params.getString(FUNCTION);
|
String fn = params.getString(Function.SELECTOR);
|
||||||
if (isSet(fn)) {
|
if (isSet(fn) && !fn.isEmpty()) function = t(fn);
|
||||||
function = Integer.parseInt(fn);
|
|
||||||
if (function < 1 || function > 4) function = 1;
|
|
||||||
}
|
|
||||||
String effect = params.getString(EFFECT);
|
String effect = params.getString(EFFECT);
|
||||||
if (isSet(effect)) switch (effect) {
|
if (isSet(effect)) switch (effect) {
|
||||||
case "1":
|
case "1":
|
||||||
|
|||||||
@@ -1,5 +1,49 @@
|
|||||||
package de.srsoftware.web4rail.functions;
|
package de.srsoftware.web4rail.functions;
|
||||||
|
|
||||||
public class CustomFunction extends Function {
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import de.srsoftware.web4rail.Params;
|
||||||
|
import de.srsoftware.web4rail.devices.Decoder;
|
||||||
|
import de.srsoftware.web4rail.tags.Fieldset;
|
||||||
|
import de.srsoftware.web4rail.tags.Input;
|
||||||
|
import de.srsoftware.web4rail.tags.Label;
|
||||||
|
|
||||||
|
public class CustomFunction extends Function {
|
||||||
|
private String customName = super.name();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Fieldset form(Decoder decoder) {
|
||||||
|
Fieldset fieldset = super.form(decoder);
|
||||||
|
String prefix = "functions/"+id()+"/";
|
||||||
|
new Input(prefix+NAME, customName)
|
||||||
|
.addTo(new Label(t("Name")))
|
||||||
|
.addTo(fieldset);
|
||||||
|
return fieldset;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JSONObject json() {
|
||||||
|
JSONObject json = super.json();
|
||||||
|
json.put(NAME, customName);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CustomFunction load(JSONObject json) {
|
||||||
|
super.load(json);
|
||||||
|
if (json.has(NAME)) customName = json.getString(NAME);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String name() {
|
||||||
|
return customName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object update(Params params) {
|
||||||
|
String newName = params.getString(NAME);
|
||||||
|
if (isSet(newName) && !newName.isEmpty()) customName = newName;
|
||||||
|
return super.update(params);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import de.srsoftware.web4rail.tags.Select;
|
|||||||
|
|
||||||
public abstract class Function extends BaseClass{
|
public abstract class Function extends BaseClass{
|
||||||
|
|
||||||
public static final String NEW = "new_function";
|
public static final String SELECTOR = "selected_fun";
|
||||||
private static final String PACKAGE = Function.class.getPackageName();
|
private static final String PACKAGE = Function.class.getPackageName();
|
||||||
private static final String INDEX = "index";
|
private static final String INDEX = "index";
|
||||||
static final String FORWARD = "forward";
|
static final String FORWARD = "forward";
|
||||||
@@ -93,15 +93,21 @@ public abstract class Function extends BaseClass{
|
|||||||
return t(type());
|
return t(type());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Tag selector() {
|
public static Select selector() {
|
||||||
Select selector = new Select(NEW);
|
return selector(null);
|
||||||
selector.addOption("", t("Select function"));
|
}
|
||||||
|
|
||||||
|
public static Select selector(String preselect) {
|
||||||
|
Select selector = new Select(SELECTOR);
|
||||||
|
selector.addOption("", "["+t("Select function")+"]");
|
||||||
|
|
||||||
for (Class<? extends Function> fun : List.of(HeadLight.class,TailLight.class,InteriorLight.class,Coupler.class,CustomFunction.class)) {
|
for (Class<? extends Function> fun : List.of(HeadLight.class,TailLight.class,InteriorLight.class,Coupler.class,CustomFunction.class)) {
|
||||||
String className = fun.getSimpleName();
|
String className = fun.getSimpleName();
|
||||||
selector.addOption(className,t(className));
|
String name = t(className);
|
||||||
|
Tag option = selector.addOption(className,name);
|
||||||
|
if (name.equals(t(preselect))) option.attr("selected", "selected");
|
||||||
}
|
}
|
||||||
|
selector.children().sort((c1,c2) -> c1.children().toString().compareToIgnoreCase(c2.children().toString()));
|
||||||
return selector;
|
return selector;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +121,7 @@ public abstract class Function extends BaseClass{
|
|||||||
return name()+"("+decoderFunction+"="+enabled+")";
|
return name()+"("+decoderFunction+"="+enabled+")";
|
||||||
}
|
}
|
||||||
|
|
||||||
private String type() {
|
protected String type() {
|
||||||
return getClass().getSimpleName();
|
return getClass().getSimpleName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -398,8 +398,8 @@ public class Locomotive extends Car implements Constants{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Object updateMapping(Params params) {
|
private Object updateMapping(Params params) {
|
||||||
if (params.containsKey(Function.NEW)) {
|
if (params.containsKey(Function.SELECTOR)) {
|
||||||
String type = params.getString(Function.NEW);
|
String type = params.getString(Function.SELECTOR);
|
||||||
if (!type.isEmpty()) {
|
if (!type.isEmpty()) {
|
||||||
Function newFun = Function.create(type);
|
Function newFun = Function.create(type);
|
||||||
if (isSet(newFun)) functions.add(newFun.register());
|
if (isSet(newFun)) functions.add(newFun.register());
|
||||||
|
|||||||
@@ -533,7 +533,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean functionEnabled(String name) {
|
public boolean functionEnabled(String name) {
|
||||||
return locos().flatMap(
|
return locos().flatMap(
|
||||||
loco -> loco.functions().stream().filter(f -> f.enabled() && f.name().equals(name))
|
loco -> loco.functions().stream().filter(f -> f.enabled() && f.name().equals(name))
|
||||||
).findAny().isPresent();
|
).findAny().isPresent();
|
||||||
}
|
}
|
||||||
@@ -1064,18 +1064,24 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object toggleFunction(Params params) {
|
public String toggleFunction(String name) {
|
||||||
String name = params.getString(FUNCTION);
|
if (isNull(name)) return t("No function name passed to toggleFunction(…)");
|
||||||
String error = isNull(name) ? t("No function name passed to toggleFunction(…)") : null;
|
return setFunction(name,!functionEnabled(name));
|
||||||
boolean enable = !functionEnabled(name);
|
}
|
||||||
|
|
||||||
|
public String setFunction(String name,boolean enable) {
|
||||||
|
if (isNull(name)) return t("No function name passed to toggleFunction(…)");
|
||||||
|
LOG.debug("Setting function \"{}\" to {}",name,enable);
|
||||||
locos().forEach(loco -> {
|
locos().forEach(loco -> {
|
||||||
Function any = null;
|
Function any = null;
|
||||||
for (Function f : loco.functions(name)) {
|
for (Function f : loco.functions(name)) any = f.setState(enable);
|
||||||
any = f.setState(enable);
|
|
||||||
}
|
|
||||||
if (isSet(any)) loco.decoder().queue();
|
if (isSet(any)) loco.decoder().queue();
|
||||||
});
|
});
|
||||||
return properties(error);
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object toggleFunction(Params params) {
|
||||||
|
return properties(toggleFunction(params.getString(FUNCTION)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user