Browse Source

implemented saving of function settings for locomotives

lookup-tables
Stephan Richter 4 years ago
parent
commit
aa9c87776d
  1. 2
      pom.xml
  2. 92
      src/main/java/de/srsoftware/web4rail/devices/Function.java
  3. 71
      src/main/java/de/srsoftware/web4rail/moving/Locomotive.java

2
pom.xml

@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.srsoftware</groupId>
<artifactId>web4rail</artifactId>
<version>1.4.41</version>
<version>1.4.42</version>
<name>Web4Rail</name>
<packaging>jar</packaging>
<description>Java Model Railway Control</description>

92
src/main/java/de/srsoftware/web4rail/devices/Function.java

@ -0,0 +1,92 @@ @@ -0,0 +1,92 @@
package de.srsoftware.web4rail.devices;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.srsoftware.web4rail.BaseClass;
import de.srsoftware.web4rail.Constants;
import de.srsoftware.web4rail.Params;
public class Function implements Constants{
protected static final Logger LOG = LoggerFactory.getLogger(Function.class);
public static final String DIRECTIONAL = "directional";
public static final String FORWARD = "forward";
public static final String REVERSE = "reverse";
private boolean directional;
private boolean reverse;
private boolean forward;
private String type;
public Function(String type, Params dirs) {
this.type = type;
for (Entry<String, Object> entry : dirs.entrySet()) setDirection(entry.getKey(), "on".equals(entry.getValue()));
}
public Function(List<Object> list) {
for (Object item : list) setDirection(item.toString(), true);
}
private void setDirection(String key,boolean value) {
switch (key) {
case DIRECTIONAL:
directional = value;
break;
case FORWARD:
forward = value;
break;
case REVERSE:
reverse = value;
break;
default:
LOG.debug("unknwon direction {}",key);
}
}
public boolean isDirectional() {
return directional;
}
public boolean isForward() {
return forward;
}
public boolean isReverse() {
return reverse;
}
public JSONArray json() {
JSONArray json = new JSONArray();
if (directional) json.put(DIRECTIONAL);
if (forward) json.put(FORWARD);
if (reverse) json.put(REVERSE);
return json;
}
@Override
public String toString() {
return type+"("+(forward?BaseClass.t("forward"):"")+(reverse?" "+BaseClass.t("reverse"):"")+(directional?" "+BaseClass.t("directional"):"").trim()+")";
}
public static JSONObject json(HashMap<String, HashMap<Integer, Function>> functions) {
JSONObject json = new JSONObject();
for (Entry<String, HashMap<Integer, Function>> entry : functions.entrySet()) {
HashMap<Integer, Function> map = entry.getValue();
if (map.isEmpty()) continue;
String type = entry.getKey();
JSONObject list = new JSONObject();
for (Integer idx : map.keySet()) list.put(idx.toString(), map.get(idx).json());
json.put(type, list);
}
return json;
}
}

71
src/main/java/de/srsoftware/web4rail/moving/Locomotive.java

@ -17,6 +17,7 @@ import de.srsoftware.web4rail.Constants; @@ -17,6 +17,7 @@ import de.srsoftware.web4rail.Constants;
import de.srsoftware.web4rail.Params;
import de.srsoftware.web4rail.Plan;
import de.srsoftware.web4rail.devices.Decoder;
import de.srsoftware.web4rail.devices.Function;
import de.srsoftware.web4rail.tags.Button;
import de.srsoftware.web4rail.tags.Checkbox;
import de.srsoftware.web4rail.tags.Fieldset;
@ -29,15 +30,12 @@ import de.srsoftware.web4rail.tags.Window; @@ -29,15 +30,12 @@ import de.srsoftware.web4rail.tags.Window;
import de.srsoftware.web4rail.tiles.Block;
public class Locomotive extends Car implements Constants{
public static final String LOCOMOTIVE = "locomotive";
private static final String HEADLIGHT = "headlight";
private static final String TAILLIGHT = "taillight";
private static final String DIRECTIONAL = "directional";
private static final String INTERIOR_LIGHT = "interior";
private static final String COUPLER = "coupler";
private static final String FORWARD = "forward";
private static final String REVERSE = "reverse";
public static final String LOCOMOTIVE = "locomotive";
private static final String ACTION_MAPPING = "mapping";
private static final String FUNCTIONS = "functions";
private final HashMap<String,HashMap<Integer,Function>> functions = new HashMap<>();
@ -46,50 +44,6 @@ public class Locomotive extends Car implements Constants{ @@ -46,50 +44,6 @@ public class Locomotive extends Car implements Constants{
//private TreeMap<Integer,Integer> cvs = new TreeMap<Integer, Integer>();
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<String, Object> 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);
}
@ -296,9 +250,9 @@ public class Locomotive extends Car implements Constants{ @@ -296,9 +250,9 @@ public class Locomotive extends Car implements Constants{
new Checkbox(functionName(index,TYPE,COUPLER),t("Coupler"),isMapped(COUPLER,index), true).addTo(type);
Tag dir = new Tag("div");
new Checkbox(functionName(index,DIRECTION,DIRECTIONAL), t("directional"), isDirectional(index), true).addTo(dir);
new Checkbox(functionName(index,DIRECTION,FORWARD), t("forward"), isForward(index), true).addTo(dir);
new Checkbox(functionName(index,DIRECTION,REVERSE), t("reverse"), isReverse(index), true).addTo(dir);
new Checkbox(functionName(index,DIRECTION,Function.DIRECTIONAL), t("directional"), isDirectional(index), true).addTo(dir);
new Checkbox(functionName(index,DIRECTION,Function.FORWARD), t("forward"), isForward(index), true).addTo(dir);
new Checkbox(functionName(index,DIRECTION,Function.REVERSE), t("reverse"), isReverse(index), true).addTo(dir);
Table table = new Table();
table.addHead(t("Type"),t("Direction"));
@ -345,6 +299,7 @@ public class Locomotive extends Car implements Constants{ @@ -345,6 +299,7 @@ public class Locomotive extends Car implements Constants{
JSONObject loco = new JSONObject();
json.put(LOCOMOTIVE, loco);
if (isSet(decoder)) loco.put(Decoder.DECODER,decoder.json());
if (functions.size()>0) loco.put(FUNCTIONS,Function.json(functions));
return json;
}
@ -366,10 +321,22 @@ public class Locomotive extends Car implements Constants{ @@ -366,10 +321,22 @@ public class Locomotive extends Car implements Constants{
}
if (isSet(decoder)) decoder.setLoco(this,false);
if (loco.has(FUNCTIONS)) loadFunctions(loco);
}
return this;
}
private void loadFunctions(JSONObject loco) {
JSONObject json = loco.getJSONObject(FUNCTIONS);
for (String type : json.keySet()) {
JSONObject map = json.getJSONObject(type);
HashMap<Integer, Function> funMap = functions.get(type);
if (isNull(funMap)) functions.put(type, funMap = new HashMap<>());
for (String idx : map.keySet()) funMap.put(Integer.parseInt(idx), new Function(map.getJSONArray(idx).toList()));
}
}
public static Window manager() {
Window win = new Window("loco-manager", t("Locomotive manager"));
new Tag("h4").content(t("known locomotives")).addTo(win);

Loading…
Cancel
Save