working on autorouting and turnout code

This commit is contained in:
Stephan Richter
2020-10-20 01:32:16 +02:00
parent 6acbb9769b
commit f5dbb0916a
10 changed files with 111 additions and 50 deletions

View File

@@ -274,7 +274,7 @@ public abstract class Tile implements Constants{
.id((x!=-1 && y!=-1)?(id()):(getClass().getSimpleName()))
.clazz(classes())
.size(100,100)
.attr("name", getClass().getSimpleName())
.attr("name", getClass().getSimpleName())
.attr("viewbox", "0 0 "+width+" "+height);
if (x>-1) style="left: "+(30*x)+"px; top: "+(30*y)+"px;";
if (len()>1) style+=" width: "+(30*len())+"px;";
@@ -322,10 +322,16 @@ public abstract class Tile implements Constants{
.content("?")
.addTo(svg);
}
String title = title();
if (title!=null) new Tag("title").content(title()).addTo(svg);
return svg;
}
public String title() {
return null;
}
@Override
public String toString() {
return t("{}({},{})",getClass().getSimpleName(),x,y) ;

View File

@@ -24,9 +24,10 @@ public abstract class Turnout extends Tile implements Device{
private Protocol protocol = Protocol.DCC128;
protected int address = 0;
protected int portA = 0, portB = 0;
protected int portA = 0, portB = 1;
protected int delay = 400;
protected boolean initialized;
protected boolean initialized = false;
protected boolean error = false;
public enum State{
LEFT,STRAIGHT,RIGHT,UNDEF;
@@ -36,11 +37,21 @@ public abstract class Turnout extends Tile implements Device{
@Override
public Object click() throws IOException {
LOG.debug("Turnout.click()");
LOG.debug(getClass().getSimpleName()+".click()");
init();
return super.click();
}
public void error(Reply reply) {
this.error = true;
try {
plan.stream(tag(null).toString());
} catch (IOException e) {
LOG.error("Was not able to stream: ",e);
}
throw new RuntimeException(reply.message());
}
protected void init() {
if (!initialized) {
plan.queue("INIT {} GA "+address+" "+proto());
@@ -52,7 +63,7 @@ public abstract class Turnout extends Tile implements Device{
public JSONObject json() {
JSONObject json = super.json();
if (portA != 0) json.put(PORT_A, portA);
if (portB != 0) json.put(PORT_B, portB);
if (portB != 1) json.put(PORT_B, portB);
if (address != 0) json.put(ADDRESS, address);
json.put(PROTOCOL, protocol);
return json;
@@ -101,14 +112,28 @@ public abstract class Turnout extends Tile implements Device{
}
public abstract CompletableFuture<Reply> state(State newState) throws IOException;
public void success() {
this.error = false;
try {
plan.stream(tag(null).toString());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public Tag tag(Map<String, Object> replacements) throws IOException {
Tag tag = super.tag(replacements);
tag.clazz(tag.get("class")+(" "+state).toLowerCase());
tag.clazz(tag.get("class")+(" "+state).toLowerCase()+(error?" error":""));
return tag;
}
@Override
public String title() {
return getClass().getSimpleName()+t("(Address: {}, Ports {} and {})",address,portA,portB);
}
public void toggle() {
state = State.STRAIGHT;
}

View File

@@ -16,7 +16,6 @@ public class TurnoutL extends Turnout {
@Override
public Object click() throws IOException {
LOG.debug("TurnoutL.click()");
Object o = super.click();
if (route != null) {
plan.stream(t("{} is locked by {}!",this,route));
@@ -45,13 +44,6 @@ public class TurnoutL extends Turnout {
return form;
}
@Override
public Tile update(HashMap<String, String> params) throws IOException {
if (params.containsKey(STRAIGHT)) portA = Integer.parseInt(params.get(STRAIGHT));
if (params.containsKey(LEFT)) portB = Integer.parseInt(params.get(LEFT));
return super.update(params);
}
@Override
public CompletableFuture<Reply> state(State newState) throws IOException {
init();
@@ -68,10 +60,18 @@ public class TurnoutL extends Turnout {
throw new IllegalStateException();
}
return result.thenApply(reply -> {
LOG.debug("{} received {}",TurnoutL.this,reply);
if (!reply.is(200)) throw new RuntimeException(reply.message());
LOG.debug("{} received {}",getClass().getSimpleName(),reply);
if (!reply.is(200)) error(reply);
state = newState;
success();
return reply;
});
}
@Override
public Tile update(HashMap<String, String> params) throws IOException {
if (params.containsKey(STRAIGHT)) portA = Integer.parseInt(params.get(STRAIGHT));
if (params.containsKey(LEFT)) portB = Integer.parseInt(params.get(LEFT));
return super.update(params);
}
}

View File

@@ -16,7 +16,6 @@ public class TurnoutR extends Turnout {
@Override
public Object click() throws IOException {
LOG.debug("Turnoutr.click()");
Object o = super.click();
if (route != null) {
plan.stream(t("{} is locked by {}!",this,route));
@@ -34,8 +33,8 @@ public class TurnoutR extends Turnout {
break;
}
}
new Input(STRAIGHT, portA).addTo(new Label(t("Straight port"))).addTo(fieldset);
new Input(RIGHT, portB).addTo(new Label(t("Right port"))).addTo(fieldset);
new Input(STRAIGHT, portA).numeric().addTo(new Label(t("Straight port"))).addTo(fieldset);
new Input(RIGHT, portB).numeric().addTo(new Label(t("Right port"))).addTo(fieldset);
return form;
}
@@ -62,8 +61,10 @@ public class TurnoutR extends Turnout {
throw new IllegalStateException();
}
return result.thenApply(reply -> {
LOG.debug("{} received {}",reply);
if (reply.is(200)) state = newState;
LOG.debug("{} received {}",getClass().getSimpleName(),reply);
if (!reply.is(200)) error(reply);
state = newState;
success();
return reply;
});
}