separated decoder settings from locomotive
This commit is contained in:
@@ -1,27 +1,30 @@
|
||||
package de.srsoftware.web4rail.devices;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.tools.Tools;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Command;
|
||||
import de.srsoftware.web4rail.Command.Reply;
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
import de.srsoftware.web4rail.Protocol;
|
||||
import de.srsoftware.web4rail.moving.Locomotive;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Radio;
|
||||
import de.srsoftware.web4rail.tags.Select;
|
||||
import de.srsoftware.web4rail.tags.Table;
|
||||
import de.srsoftware.web4rail.tags.Window;
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
import de.srsoftware.web4rail.Protocol;
|
||||
|
||||
public class Decoder extends BaseClass implements Constants, Device {
|
||||
|
||||
@@ -38,12 +41,43 @@ public class Decoder extends BaseClass implements Constants, Device {
|
||||
private static final String TRACK = "track";
|
||||
private static final String VALUE = "val";
|
||||
private static final String CV = "cv";
|
||||
private String type;
|
||||
private Locomotive loco;
|
||||
|
||||
public static Object action(HashMap<String, String> params) {
|
||||
Decoder decoder = BaseClass.get(Id.from(params));
|
||||
switch (params.get(Constants.ACTION)) {
|
||||
case ACTION_DECOUPLE:
|
||||
return decoder.dismount();
|
||||
case ACTION_PROGRAM:
|
||||
return decoder.program(params);
|
||||
case ACTION_PROPS:
|
||||
return decoder.properties();
|
||||
case ACTION_UPDATE:
|
||||
return decoder.update(params);
|
||||
}
|
||||
|
||||
String message = BaseClass.t("Unknown action: {}",params.get(Constants.ACTION));
|
||||
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);
|
||||
return address;
|
||||
return isNull(address) ? 3 : address;
|
||||
}
|
||||
|
||||
public Button button() {
|
||||
return super.button(type(),Map.of(REALM,REALM_DECODER));
|
||||
}
|
||||
|
||||
public void init() {
|
||||
@@ -84,13 +118,14 @@ public class Decoder extends BaseClass implements Constants, Device {
|
||||
JSONObject json = super.json();
|
||||
json.put(CVS, cvs);
|
||||
json.put(PROTOCOL, proto);
|
||||
json.put(TYPE, type);
|
||||
return json;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Tag link(String... args) {
|
||||
Tools.notImplemented("Decoder.link(…)");
|
||||
return new Tag("span").content("[[Decoder.link() not implemented]]");
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -101,9 +136,21 @@ public class Decoder extends BaseClass implements Constants, Device {
|
||||
JSONObject jCvs = json.getJSONObject(CVS);
|
||||
for (String key : jCvs.keySet()) cvs.put(Integer.parseInt(key),jCvs.getInt(key));
|
||||
}
|
||||
if (json.has(TYPE)) type = json.getString(TYPE);
|
||||
return this;
|
||||
}
|
||||
|
||||
private Window program(HashMap<String, String> params) {
|
||||
String error = null;
|
||||
if (params.get(ACTION).equals(ACTION_PROGRAM)) try {
|
||||
int cv = Integer.parseInt(params.get(CV));
|
||||
int val = Integer.parseInt(params.get(VALUE));
|
||||
boolean pom = !params.get(MODE).equals(TRACK);
|
||||
error = program(cv,val,pom);
|
||||
} catch (NumberFormatException e) {}
|
||||
return properties(error);
|
||||
}
|
||||
|
||||
private String program(int cv,int val,boolean pom) {
|
||||
if (cv != 0) {
|
||||
if (val < 0) {
|
||||
@@ -132,7 +179,7 @@ public class Decoder extends BaseClass implements Constants, Device {
|
||||
Fieldset fieldset = new Fieldset(t("Programming")).id("props-cv");
|
||||
|
||||
Form form = new Form("cv-form");
|
||||
new Input(REALM,REALM_LOCO).hideIn(form);
|
||||
new Input(REALM,REALM_DECODER).hideIn(form);
|
||||
new Input(ID,id()).hideIn(form);
|
||||
new Input(ACTION,ACTION_PROGRAM).hideIn(form);
|
||||
|
||||
@@ -161,12 +208,14 @@ public class Decoder extends BaseClass implements Constants, Device {
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm, String... errorMessages) {
|
||||
formInputs.add(t("Type"),new Input(TYPE,type()));
|
||||
Tag div = new Tag("div");
|
||||
for (Protocol proto : Protocol.values()) {
|
||||
new Radio(PROTOCOL, proto.toString(), t(proto.toString()), proto == this.proto).addTo(div);
|
||||
}
|
||||
formInputs.add(t("Protocol"),div);
|
||||
formInputs.add(t("Address"),new Input(ADDRESS, address).numeric());
|
||||
formInputs.add(t("Address"),new Tag("span").content(""+address()));
|
||||
if (isSet(loco)) formInputs.add(t("Locomotive"),loco.button(loco.name()));
|
||||
postForm.add(programming());
|
||||
return super.properties(preForm, formInputs, postForm, errorMessages);
|
||||
}
|
||||
@@ -175,6 +224,25 @@ public class Decoder extends BaseClass implements Constants, Device {
|
||||
return proto;
|
||||
}
|
||||
|
||||
public static Select selector(boolean freeOnly) {
|
||||
Select selector = new Select(REALM_DECODER);
|
||||
List<Decoder> decoders = BaseClass.listElements(Decoder.class);
|
||||
selector.addOption(-1,t("no decoder"));
|
||||
selector.addOption(0,t("new decoder"));
|
||||
for (Decoder d: decoders) {
|
||||
if (freeOnly && isSet(d.loco)) continue;
|
||||
selector.addOption(d.id(), d);
|
||||
}
|
||||
return selector;
|
||||
}
|
||||
|
||||
public Decoder setLoco(Locomotive locomotive, boolean log) {
|
||||
loco = locomotive;
|
||||
if (log) addLogEntry(t("Mounted into \"{}\".",loco));
|
||||
locomotive.setDecoder(this,log);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setProtocol(Protocol proto) {
|
||||
this.proto = proto;
|
||||
}
|
||||
@@ -201,6 +269,22 @@ public class Decoder extends BaseClass implements Constants, Device {
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return type()+" ("+t("Address")+": "+address()+")";
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String type() {
|
||||
return isSet(type) ? type : t("Unknown decoder type");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window update(HashMap<String, String> params) {
|
||||
super.update(params);
|
||||
if (params.containsKey(TYPE)) type = params.get(TYPE);
|
||||
if (params.containsKey(Device.PROTOCOL)) setProtocol(Protocol.valueOf(params.get(Device.PROTOCOL)));
|
||||
return isSet(loco) ? loco.properties() : properties();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user