diff --git a/pom.xml b/pom.xml
index 5d51842..f22fe69 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
4.0.0
de.srsoftware
web4rail
- 1.4.40
+ 1.4.41
Web4Rail
jar
Java Model Railway Control
diff --git a/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java b/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java
index 42e39f0..9e927ef 100644
--- a/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java
+++ b/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java
@@ -5,6 +5,7 @@ import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Vector;
import org.json.JSONObject;
@@ -38,10 +39,56 @@ public class Locomotive extends Car implements Constants{
private static final String FORWARD = "forward";
private static final String REVERSE = "reverse";
private static final String ACTION_MAPPING = "mapping";
+ private static final String FUNCTIONS = "functions";
+ private final HashMap> functions = new HashMap<>();
private int speed = 0;
private boolean f1,f2,f3,f4;
//private TreeMap cvs = new TreeMap();
private Decoder decoder;
+
+ private class Function{
+ private boolean directional;
+ private boolean reverse;
+ private boolean forward;
+ private String type;
+
+ public Function(String type, Params dirs) {
+ this.type = type;
+ for (Entry entry : dirs.entrySet()) {
+ boolean enabled = "on".equals(entry.getValue());
+ switch (entry.getKey()) {
+ case DIRECTIONAL:
+ directional = enabled;
+ break;
+ case FORWARD:
+ forward = enabled;
+ break;
+ case REVERSE:
+ reverse = enabled;
+ break;
+ default:
+ LOG.debug("unknwon direction {}",entry.getKey());
+ }
+ }
+ }
+
+ public boolean isDirectional() {
+ return directional;
+ }
+
+ public boolean isForward() {
+ return forward;
+ }
+
+ public boolean isReverse() {
+ return reverse;
+ }
+
+ @Override
+ public String toString() {
+ return type+"("+(forward?t("forward"):"")+(reverse?" "+t("reverse"):"")+(directional?" "+t("directional"):"").trim()+")";
+ }
+ }
public Locomotive(String name) {
super(name);
@@ -234,7 +281,7 @@ public class Locomotive extends Car implements Constants{
new Input(REALM, REALM_LOCO).hideIn(form);
new Input(ACTION, ACTION_MAPPING).hideIn(form);
new Input(ID,id()).hideIn(form);
- for (int i=0; i value : functions.values()) {
+ Function f = value.get(index);
+ if (isSet(f) && f.isDirectional()) return true;
+ }
+ return false;
+ }
+
+ private boolean isForward(int index) {
+ for (HashMap value : functions.values()) {
+ Function f = value.get(index);
+ if (isSet(f) && f.isForward()) return true;
+ }
+ return false;
+ }
+
+ private boolean isReverse(int index) {
+ for (HashMap value : functions.values()) {
+ Function f = value.get(index);
+ if (isSet(f) && f.isReverse()) return true;
+ }
+ return false;
+ }
+
+ private boolean isMapped(String type, int funNum) {
+ HashMap fun = functions.get(type);
+ return isSet(fun) && fun.containsKey(funNum);
+ }
+
+ private static String functionName(int index,String map,String key) {
+ return FUNCTIONS+"/"+index+"/"+map+"/"+key;
}
@Override
@@ -456,8 +533,34 @@ public class Locomotive extends Car implements Constants{
return properties();
}
+ private void updateFunction(int num, Params settings) {
+ LOG.debug("Settings for function {}: {}",num,settings);
+ Params dirs = settings.getParams(DIRECTION);
+ Params types = settings.getParams(TYPE);
+ for (String type : types.keySet()) {
+ boolean enabled = "on".equals(types.get(type));
+ HashMap funList = functions.get(type);
+ if (enabled && isNull(funList)) {
+ funList = new HashMap<>();
+ functions.put(type, funList);
+ }
+ if (enabled) {
+ funList.put(num, new Function(type,dirs));
+ } else {
+ if (isSet(funList)) {
+ funList.remove(num);
+ if (funList.isEmpty()) functions.remove(type);
+ }
+ }
+ }
+ }
+
private Object updateMapping(Params params) {
- // TODO Auto-generated method stub
+ if (params.containsKey(FUNCTIONS)) {
+ for (Entry entry : params.getParams(FUNCTIONS).entrySet()) {
+ updateFunction(Integer.parseInt(entry.getKey()),(Params) entry.getValue());
+ }
+ }
return properties();
}
}