re-implemented functions of locomotives

This commit is contained in:
Stephan Richter
2021-06-17 14:21:41 +02:00
parent aa9c87776d
commit dafcf651c3
7 changed files with 151 additions and 222 deletions

View File

@@ -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 {
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,15 +67,6 @@ public class Decoder extends BaseClass implements Constants, Device {
return (BaseClass.isNull(decoder)) ? message : decoder.properties(message);
}
private Window dismount() {
if (isNull(loco)) return properties();
Locomotive locomotive = loco;
locomotive.removeDecoder(this);
loco = null;
addLogEntry(t("Removed decoder from \"{}\".",locomotive));
return locomotive.properties();
}
@Override
public int address() {
if (isNull(address)) address = cvs.get(CV_ADDR);
@@ -82,6 +77,21 @@ public class Decoder extends BaseClass implements Constants, Device {
return super.button(type(),Map.of(REALM,REALM_DECODER));
}
private Window dismount() {
if (isNull(loco)) return properties();
Locomotive locomotive = loco;
locomotive.removeDecoder(this);
loco = null;
addLogEntry(t("Removed decoder from \"{}\".",locomotive));
return locomotive.properties();
}
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() {
if (init) return;
String proto = null;
@@ -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 {
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 {
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 {
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()) {
@Override
public void onFailure(Reply reply) {
super.onFailure(reply);
plan.stream(t("Failed to send command to {}: {}",this,reply.message()));
}
});
}
}

View File

@@ -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;
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,14 +23,62 @@ 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);
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() {
return directional;
}
public boolean isForward() {
return forward;
}
public boolean isReverse() {
return 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;
}
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;
}
public String name() {
return name;
}
private void setDirection(String key,boolean value) {
@@ -50,26 +96,6 @@ public class Function implements Constants{
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() {
@@ -77,16 +103,5 @@ public class Function implements Constants{
}
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;
}
}