Browse Source

re-implemented functions of locomotives

lookup-tables
Stephan Richter 4 years ago
parent
commit
dafcf651c3
  1. 2
      pom.xml
  2. 6
      src/main/java/de/srsoftware/web4rail/Constants.java
  3. 3
      src/main/java/de/srsoftware/web4rail/actions/SwitchFunction.java
  4. 66
      src/main/java/de/srsoftware/web4rail/devices/Decoder.java
  5. 83
      src/main/java/de/srsoftware/web4rail/devices/Function.java
  6. 138
      src/main/java/de/srsoftware/web4rail/moving/Locomotive.java
  7. 63
      src/main/java/de/srsoftware/web4rail/moving/Train.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.42</version>
<version>1.4.43</version>
<name>Web4Rail</name>
<packaging>jar</packaging>
<description>Java Model Railway Control</description>

6
src/main/java/de/srsoftware/web4rail/Constants.java

@ -33,10 +33,7 @@ public interface Constants { @@ -33,10 +33,7 @@ public interface Constants {
public static final String ACTION_START = "start";
public static final String ACTION_STOP = "stop";
public static final String ACTION_TIMES = "update_times";
public static final String ACTION_TOGGLE_F1 = "f1";
public static final String ACTION_TOGGLE_F2 = "f2";
public static final String ACTION_TOGGLE_F3 = "f3";
public static final String ACTION_TOGGLE_F4 = "f4";
public static final String ACTION_TOGGLE_FUNCTION = "function";
public static final String ACTION_TOGGLE_SHUNTING = "shunt";
public static final String ACTION_TURN = "turn";
public static final String ACTION_UPDATE = "update";
@ -66,6 +63,7 @@ public interface Constants { @@ -66,6 +63,7 @@ public interface Constants {
public static final String DESTINATION = "destination";
public static final String DISABLED = "disabled";
public static final String DIRECTION = "direction";
public static final String FUNCTION = "function";
public static final String GITHUB_URL = "https://github.com/srsoftware-de/Web4Rail";
public static final String ID = "id";
public static final String LOCKED = "locked";

3
src/main/java/de/srsoftware/web4rail/actions/SwitchFunction.java

@ -31,7 +31,8 @@ public class SwitchFunction extends Action { @@ -31,7 +31,8 @@ public class SwitchFunction extends Action {
if (isNull(context) || isNull(context.train())) return false;
switch (effect) {
case TOGGLE:
context.train().toggleFunction(function);
// context.train().toggleFunction(function);
// TODO
return true;
case ON:
context.train().setFunction(function, true);

66
src/main/java/de/srsoftware/web4rail/devices/Decoder.java

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
package de.srsoftware.web4rail.devices;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -45,6 +46,9 @@ public class Decoder extends BaseClass implements Constants, Device { @@ -45,6 +46,9 @@ public class Decoder extends BaseClass implements Constants, Device {
private String type;
private Locomotive loco;
private int numFunctions = 2;
private HashSet<Integer> enabledFunctions = new HashSet<>();
private int step;
private boolean reverse;
public static Object action(Params params) {
Decoder decoder = BaseClass.get(Id.from(params));
@ -63,6 +67,16 @@ public class Decoder extends BaseClass implements Constants, Device { @@ -63,6 +67,16 @@ public class Decoder extends BaseClass implements Constants, Device {
return (BaseClass.isNull(decoder)) ? message : decoder.properties(message);
}
@Override
public int address() {
if (isNull(address)) address = cvs.get(CV_ADDR);
return isNull(address) ? 3 : address;
}
public Button button() {
return super.button(type(),Map.of(REALM,REALM_DECODER));
}
private Window dismount() {
if (isNull(loco)) return properties();
Locomotive locomotive = loco;
@ -71,15 +85,11 @@ public class Decoder extends BaseClass implements Constants, Device { @@ -71,15 +85,11 @@ public class Decoder extends BaseClass implements Constants, Device {
addLogEntry(t("Removed decoder from \"{}\".",locomotive));
return locomotive.properties();
}
@Override
public int address() {
if (isNull(address)) address = cvs.get(CV_ADDR);
return isNull(address) ? 3 : address;
}
public Button button() {
return super.button(type(),Map.of(REALM,REALM_DECODER));
public StringBuilder functions() {
StringBuilder sb = new StringBuilder();
for (int i=1; i<=numFunctions; i++) sb.append(" ").append(isEnabled(i)?1:0);
return sb;
}
public void init() {
@ -115,6 +125,11 @@ public class Decoder extends BaseClass implements Constants, Device { @@ -115,6 +125,11 @@ public class Decoder extends BaseClass implements Constants, Device {
init = true;
}
public boolean isEnabled(int function) {
return enabledFunctions.contains(function);
}
@Override
public JSONObject json() {
JSONObject json = super.json();
@ -146,10 +161,6 @@ public class Decoder extends BaseClass implements Constants, Device { @@ -146,10 +161,6 @@ public class Decoder extends BaseClass implements Constants, Device {
return numFunctions ;
}
private Window program(Params params) {
String error = null;
if (ACTION_PROGRAM.equals(params.get(ACTION))) try {
@ -281,6 +292,11 @@ public class Decoder extends BaseClass implements Constants, Device { @@ -281,6 +292,11 @@ public class Decoder extends BaseClass implements Constants, Device {
return "";
}
public void toggleFunction(Integer index) {
if (!enabledFunctions.remove(index)) enabledFunctions.add(index);
queue();
}
@Override
public String toString() {
return type()+" ("+t("Address")+": "+address()+")";
@ -296,17 +312,25 @@ public class Decoder extends BaseClass implements Constants, Device { @@ -296,17 +312,25 @@ public class Decoder extends BaseClass implements Constants, Device {
super.update(params);
if (params.containsKey(TYPE)) type = params.getString(TYPE);
if (params.containsKey(Device.PROTOCOL)) setProtocol(Protocol.valueOf(params.getString(Device.PROTOCOL)));
if (params.containsKey(NUM_FUNCTIONS)) numFunctions = params.getInt(NUM_FUNCTIONS);
return isSet(loco) ? loco.properties() : properties();
}
public void queue(double speed, boolean reverse) {
step = (int)(speed*proto.steps);
this.reverse = reverse;
queue();
}
public void queue() {
init();
plan.queue(new Command("SET {} GL "+address()+" "+(reverse ? 0 : 1)+" "+step+" "+protocol().steps+functions()) {
return isSet(loco) ? loco.properties() : properties();
@Override
public void onFailure(Reply reply) {
super.onFailure(reply);
plan.stream(t("Failed to send command to {}: {}",this,reply.message()));
}
});
}
}

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

@ -1,7 +1,6 @@ @@ -1,7 +1,6 @@
package de.srsoftware.web4rail.devices;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import org.json.JSONArray;
@ -10,10 +9,9 @@ import org.slf4j.Logger; @@ -10,10 +9,9 @@ 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{
public class Function extends BaseClass{
protected static final Logger LOG = LoggerFactory.getLogger(Function.class);
public static final String DIRECTIONAL = "directional";
@ -25,30 +23,18 @@ public class Function implements Constants{ @@ -25,30 +23,18 @@ public class Function implements Constants{
private boolean reverse;
private boolean forward;
private String type;
private String name;
public Function(String type, Params dirs) {
public Function(String type, String name, Params dirs) {
this.type = type;
this.name = name;
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 Function(JSONObject json) {
if (json.has(NAME)) name = json.getString(NAME);
if (json.has(TYPE)) type = json.getString(TYPE);
if (json.has(DIRECTION)) json.getJSONArray(DIRECTION).forEach(o -> setDirection(o.toString(), true));
}
public boolean isDirectional() {
@ -62,21 +48,22 @@ public class Function implements Constants{ @@ -62,21 +48,22 @@ public class Function implements Constants{
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);
public JSONObject json() {
JSONArray directions = new JSONArray();
if (directional) directions.put(DIRECTIONAL);
if (forward) directions.put(FORWARD);
if (reverse) directions.put(REVERSE);
JSONObject json = new JSONObject();
if (!directions.isEmpty()) json.put(DIRECTION, directions);
json.put(NAME, name);
if (isSet(type)) json.put(TYPE, type);
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()) {
@ -89,4 +76,32 @@ public class Function implements Constants{ @@ -89,4 +76,32 @@ public class Function implements Constants{
}
return json;
}
public String name() {
return name;
}
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);
}
}
@Override
public String toString() {
return type+"("+(forward?BaseClass.t("forward"):"")+(reverse?" "+BaseClass.t("reverse"):"")+(directional?" "+BaseClass.t("directional"):"").trim()+")";
}
}

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

@ -12,7 +12,6 @@ import org.json.JSONObject; @@ -12,7 +12,6 @@ import org.json.JSONObject;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.BaseClass;
import de.srsoftware.web4rail.Command;
import de.srsoftware.web4rail.Constants;
import de.srsoftware.web4rail.Params;
import de.srsoftware.web4rail.Plan;
@ -40,7 +39,6 @@ public class Locomotive extends Car implements Constants{ @@ -40,7 +39,6 @@ public class Locomotive extends Car implements Constants{
private static final String FUNCTIONS = "functions";
private final HashMap<String,HashMap<Integer,Function>> functions = new HashMap<>();
private int speed = 0;
private boolean f1,f2,f3,f4;
//private TreeMap<Integer,Integer> cvs = new TreeMap<Integer, Integer>();
private Decoder decoder;
@ -73,14 +71,8 @@ public class Locomotive extends Car implements Constants{ @@ -73,14 +71,8 @@ public class Locomotive extends Car implements Constants{
return loco.faster(-Train.defaultSpeedStep);
case ACTION_STOP:
return loco.stop();
case ACTION_TOGGLE_F1:
return loco.toggleFunction(1);
case ACTION_TOGGLE_F2:
return loco.toggleFunction(2);
case ACTION_TOGGLE_F3:
return loco.toggleFunction(3);
case ACTION_TOGGLE_F4:
return loco.toggleFunction(4);
case ACTION_TOGGLE_FUNCTION:
return loco.toggleFunction(params);
case ACTION_TURN:
return loco.turn();
case ACTION_UPDATE:
@ -116,26 +108,17 @@ public class Locomotive extends Car implements Constants{ @@ -116,26 +108,17 @@ public class Locomotive extends Car implements Constants{
Train train = null;
Locomotive loco = null;
int maxSpeed = 0;
boolean fun1=false,fun2=false,fun3=false,fun4=false;
String id = null;
if (locoOrTrain instanceof Locomotive) {
loco = (Locomotive) locoOrTrain;
realm = REALM_LOCO;
speed = loco.speed;
fun1 = loco.f1;
fun2 = loco.f2;
fun3 = loco.f3;
fun4 = loco.f4;
maxSpeed = loco.orientation ? loco.maxSpeedForward : loco.maxSpeedReverse;
id = "loco_"+loco.id();
} else if (locoOrTrain instanceof Train) {
train = (Train)locoOrTrain;
realm = REALM_TRAIN;
speed = train.speed;
fun1 = train.getFunction(1);
fun2 = train.getFunction(2);
fun3 = train.getFunction(3);
fun4 = train.getFunction(4);
maxSpeed = train.maxSpeed();
id = "train_"+train.id();
} else return null;
@ -187,27 +170,26 @@ public class Locomotive extends Car implements Constants{ @@ -187,27 +170,26 @@ public class Locomotive extends Car implements Constants{
}
direction.addTo(fieldset);
Tag functions = new Tag("p");
params.put(ACTION, ACTION_TOGGLE_F1);
Button b1 = new Button(t("F1"),params);
if (fun1) b1.clazz("active");
b1.addTo(functions);
Tag functions = new Tag("p");
params.put(ACTION, ACTION_TOGGLE_F2);
Button b2 = new Button(t("F2"),params);
if (fun2) b2.clazz("active");
b2.addTo(functions);
if (isSet(loco) && isSet(loco.decoder)) {
for (int i = 1; i<=loco.decoder.numFunctions(); i++) {
params.put(ACTION, ACTION_TOGGLE_FUNCTION);
params.put(FUNCTION,i);
Button btn = new Button(loco.functionName(i),params);
if (loco.decoder.isEnabled(i)) btn.clazz("active");
btn.addTo(functions);
}
}
params.put(ACTION, ACTION_TOGGLE_F3);
Button b3 = new Button(t("F3"),params);
if (fun3) b3.clazz("active");
b3.addTo(functions);
if (isSet(train)) {
params.put(ACTION, HEADLIGHT);
//headlight = new Button(t("Headlight"), params);
}
params.put(ACTION, ACTION_TOGGLE_F4);
Button b4 = new Button(t("F4"),params);
if (fun4) b4.clazz("active");
b4.addTo(functions);
functions.addTo(fieldset);
if (isSet(train)) {
@ -255,11 +237,26 @@ public class Locomotive extends Car implements Constants{ @@ -255,11 +237,26 @@ public class Locomotive extends Car implements Constants{
new Checkbox(functionName(index,DIRECTION,Function.REVERSE), t("reverse"), isReverse(index), true).addTo(dir);
Table table = new Table();
table.addHead(t("Type"),t("Direction"));
table.addRow(t("Name"),new Input(functionName(index,NAME), functionName(index)));
table.addHead(t("Type"),t("Direction"));
table.addRow(type,dir);
return table.addTo(mapping);
}
private String functionName(int index) {
for (HashMap<Integer, Function> value : functions.values()) {
Function f = value.get(index);
if (isSet(f)) return f.name();
}
return "F"+index;
}
private static String functionName(Object...parts) {
StringBuilder sb = new StringBuilder(FUNCTIONS);
for (Object part : parts) sb.append("/"+part);
return sb.toString();
}
private boolean isDirectional(int index) {
for (HashMap<Integer, Function> value : functions.values()) {
Function f = value.get(index);
@ -289,10 +286,6 @@ public class Locomotive extends Car implements Constants{ @@ -289,10 +286,6 @@ public class Locomotive extends Car implements Constants{
return isSet(fun) && fun.containsKey(funNum);
}
private static String functionName(int index,String map,String key) {
return FUNCTIONS+"/"+index+"/"+map+"/"+key;
}
@Override
public JSONObject json() {
JSONObject json = super.json();
@ -333,7 +326,7 @@ public class Locomotive extends Car implements Constants{ @@ -333,7 +326,7 @@ public class Locomotive extends Car implements Constants{
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()));
for (String idx : map.keySet()) funMap.put(Integer.parseInt(idx), new Function(map.getJSONObject(idx)));
}
}
@ -391,48 +384,12 @@ public class Locomotive extends Car implements Constants{ @@ -391,48 +384,12 @@ public class Locomotive extends Car implements Constants{
this.decoder = null;
}
}
private void queue() {
if (isNull(decoder)) return;
int step = decoder.protocol().steps * speed / (maxSpeedForward == 0 ? 100 : maxSpeedForward);
decoder.init();
plan.queue(new Command("SET {} GL "+decoder.address()+" "+(orientation == Car.FORWARD ? 0 : 1)+" "+step+" "+decoder.protocol().steps+" "+(f1?1:0)+" "+(f2?1:0)+" "+(f3?1:0)+" "+(f4?1:0)) {
@Override
public void onFailure(Reply reply) {
super.onFailure(reply);
plan.stream(t("Failed to send command to {}: {}",this,reply.message()));
}
});
}
public void setDecoder(Decoder newDecoder, boolean log) {
decoder = newDecoder;
if (log) addLogEntry(t("Mounted decoder \"{}\".",decoder));
}
public String setFunction(int num, boolean active) {
switch (num) {
case 1:
f1 = active;
break;
case 2:
f2 = active;
break;
case 3:
f3 = active;
break;
case 4:
f4 = active;
break;
default:
return t("Unknown function: {}",num);
}
queue();
return t("{} F{}",t(active?"Activated":"Deavtivated"),num);
}
/**
* Sets the speed of the locomotive to the given velocity in [plan.speedUnit]s
* @param newSpeed
@ -445,8 +402,8 @@ public class Locomotive extends Car implements Constants{ @@ -445,8 +402,8 @@ public class Locomotive extends Car implements Constants{
if (speed > maxSpeedForward && maxSpeedForward > 0) speed = maxSpeed();
if (speed < 0) speed = 0;
queue();
//plan.stream(t("Speed of {} set to {} {}.",this,speed,BaseClass.speedUnit));
double step = 1.0 * speed / (maxSpeedForward == 0 ? 100 : maxSpeedForward);
decoder.queue(step,orientation != FORWARD);
}
return properties();
@ -459,18 +416,12 @@ public class Locomotive extends Car implements Constants{ @@ -459,18 +416,12 @@ public class Locomotive extends Car implements Constants{
return properties();
}
Object toggleFunction(int f) {
switch (f) {
case 1:
return setFunction(1, !f1);
case 2:
return setFunction(2, !f2);
case 3:
return setFunction(3, !f3);
case 4:
return setFunction(4, !f4);
}
return t("Unknown function: {}",f);
Object toggleFunction(Params params) {
Integer index = params.getInt(FUNCTION);
if (isNull(index)) return t("No function number provided!");
if (isNull(decoder)) return t("{} has no decoder!",this);
decoder.toggleFunction(index);
return t("Unknown function: {}",params);
}
public Object turn() {
@ -504,6 +455,7 @@ public class Locomotive extends Car implements Constants{ @@ -504,6 +455,7 @@ public class Locomotive extends Car implements Constants{
LOG.debug("Settings for function {}: {}",num,settings);
Params dirs = settings.getParams(DIRECTION);
Params types = settings.getParams(TYPE);
String name = settings.getString(NAME);
for (String type : types.keySet()) {
boolean enabled = "on".equals(types.get(type));
HashMap<Integer, Function> funList = functions.get(type);
@ -512,7 +464,7 @@ public class Locomotive extends Car implements Constants{ @@ -512,7 +464,7 @@ public class Locomotive extends Car implements Constants{
functions.put(type, funList);
}
if (enabled) {
funList.put(num, new Function(type,dirs));
funList.put(num, new Function(type, name, dirs)); // TODO
} else {
if (isSet(funList)) {
funList.remove(num);

63
src/main/java/de/srsoftware/web4rail/moving/Train.java

@ -81,7 +81,6 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -81,7 +81,6 @@ public class Train extends BaseClass implements Comparable<Train> {
public static final char SHUNTING_FLAG = '¥';
private HashSet<String> tags = new HashSet<String>();
private boolean f1,f2,f3,f4;
private Block currentBlock,destination = null;
HashSet<Tile> trace = new HashSet<Tile>();
@ -124,14 +123,6 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -124,14 +123,6 @@ public class Train extends BaseClass implements Comparable<Train> {
return train.connect(params);
case ACTION_DROP:
return train.dropCar(params);
case ACTION_TOGGLE_F1:
return train.toggleFunction(1);
case ACTION_TOGGLE_F2:
return train.toggleFunction(2);
case ACTION_TOGGLE_F3:
return train.toggleFunction(3);
case ACTION_TOGGLE_F4:
return train.toggleFunction(4);
case ACTION_FASTER10:
return train.faster(Train.defaultSpeedStep);
case ACTION_MOVE:
@ -536,21 +527,6 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -536,21 +527,6 @@ public class Train extends BaseClass implements Comparable<Train> {
return setSpeed(speed+steps);
}
public boolean getFunction(int num) {
switch (num) {
case 1:
return f1;
case 2:
return f2;
case 3:
return f3;
case 4:
return f4;
default:
return false;
}
}
private boolean hasLoco() {
for (Car c:cars) {
if (c instanceof Locomotive) return true;
@ -938,29 +914,7 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -938,29 +914,7 @@ public class Train extends BaseClass implements Comparable<Train> {
}
public Object setFunction(int num, boolean active) {
switch (num) {
case 1:
f1 = active;
break;
case 2:
f2 = active;
break;
case 3:
f3 = active;
break;
case 4:
f4 = active;
break;
default:
return t("Unknown function: {}",num);
}
for (Car car : cars) {
if (car instanceof Locomotive) {
Locomotive loco = (Locomotive) car;
loco.setFunction(num,active);
if (active) break;
}
}
// TODO
return properties();
}
@ -1094,21 +1048,6 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -1094,21 +1048,6 @@ public class Train extends BaseClass implements Comparable<Train> {
return list;
}
public Object toggleFunction(int f) {
switch (f) {
case 1:
return setFunction(1, !f1);
case 2:
return setFunction(2, !f2);
case 3:
return setFunction(3, !f3);
case 4:
return setFunction(4, !f4);
}
return t("Unknown function: {}",f);
}
@Override
public String toString() {
return name();

Loading…
Cancel
Save