started to re-implement actions with promises
This commit is contained in:
@@ -155,9 +155,10 @@ public abstract class Tile implements Constants{
|
||||
return this;
|
||||
}
|
||||
|
||||
public void lock(Route route) throws IOException {
|
||||
this.route = route;
|
||||
plan.place(this);
|
||||
public Tile lock(Route lockingRoute) throws IOException {
|
||||
if (route != null && route != lockingRoute) throw new IllegalStateException(this.toString());
|
||||
route = lockingRoute;
|
||||
return plan.place(this);
|
||||
}
|
||||
|
||||
public Plan plan() {
|
||||
|
||||
@@ -3,10 +3,12 @@ package de.srsoftware.web4rail.tiles;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.ControlUnit.Reply;
|
||||
import de.srsoftware.web4rail.Device;
|
||||
import de.srsoftware.web4rail.Protocol;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
@@ -35,29 +37,8 @@ public abstract class Turnout extends Tile implements Device{
|
||||
@Override
|
||||
public Object click() throws IOException {
|
||||
LOG.debug("Turnout.click()");
|
||||
Object o = super.click();
|
||||
if (address != 0 && !initialized) {
|
||||
String p = null;
|
||||
switch (protocol) {
|
||||
case DCC14:
|
||||
case DCC27:
|
||||
case DCC28:
|
||||
case DCC128:
|
||||
p = "N";
|
||||
break;
|
||||
case MOTO:
|
||||
p = "M";
|
||||
break;
|
||||
case SELECTRIX:
|
||||
p = "S";
|
||||
break;
|
||||
default:
|
||||
p = "P";
|
||||
}
|
||||
plan.queue("INIT {} GA "+address+" "+p);
|
||||
initialized = true;
|
||||
}
|
||||
return o;
|
||||
init();
|
||||
return super.click();
|
||||
}
|
||||
|
||||
protected void init() {
|
||||
@@ -119,7 +100,7 @@ public abstract class Turnout extends Tile implements Device{
|
||||
return state;
|
||||
}
|
||||
|
||||
public abstract void state(State newState) throws IOException;
|
||||
public abstract CompletableFuture<Reply> state(State newState) throws IOException;
|
||||
|
||||
@Override
|
||||
public Tag tag(Map<String, Object> replacements) throws IOException {
|
||||
|
||||
@@ -3,8 +3,10 @@ package de.srsoftware.web4rail.tiles;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.ControlUnit.Reply;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
|
||||
public class Turnout3E extends Turnout{
|
||||
@@ -29,8 +31,9 @@ public class Turnout3E extends Turnout{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void state(State newState) throws IOException {
|
||||
public CompletableFuture<Reply> state(State newState) throws IOException {
|
||||
// TODO Auto-generated method stub
|
||||
LOG.warn("Turnout3E.state({}) not implemented, yet!",newState);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@ package de.srsoftware.web4rail.tiles;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.ControlUnit.Reply;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
@@ -18,7 +20,13 @@ public class TurnoutL extends Turnout {
|
||||
Object o = super.click();
|
||||
if (route != null) {
|
||||
plan.stream(t("{} is locked by {}!",this,route));
|
||||
} else state(state == State.STRAIGHT ? State.LEFT : State.STRAIGHT);
|
||||
} else {
|
||||
CompletableFuture<Reply> promise = state(state == State.STRAIGHT ? State.LEFT : State.STRAIGHT);
|
||||
promise.exceptionally(ex -> {
|
||||
LOG.warn("Failed to toggle turnout: ",ex);
|
||||
throw new RuntimeException(ex);
|
||||
}).thenAccept(reply -> LOG.debug("Success: {}",reply));
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
@@ -45,21 +53,25 @@ public class TurnoutL extends Turnout {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void state(State newState) throws IOException {
|
||||
public CompletableFuture<Reply> state(State newState) throws IOException {
|
||||
init();
|
||||
LOG.debug("Setting {} to {}",this,newState);
|
||||
int p = 0;
|
||||
LOG.debug("Requesting to set {} to {}",this,newState);
|
||||
CompletableFuture<Reply> result;
|
||||
switch (newState) {
|
||||
case LEFT:
|
||||
p = portB;
|
||||
result = plan.queue("SET {} GA "+address+" "+portB+" 1 "+delay);
|
||||
break;
|
||||
case STRAIGHT:
|
||||
p = portA;
|
||||
result = plan.queue("SET {} GA "+address+" "+portA+" 1 "+delay);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
if (p != 0) plan.queue("SET {} GA "+address+" "+p+" 1 "+delay);
|
||||
state = newState;
|
||||
plan.stream("place "+tag(null));
|
||||
return result.thenApply(reply -> {
|
||||
LOG.debug("{} received {}",TurnoutL.this,reply);
|
||||
if (!reply.is(200)) throw new RuntimeException(reply.message());
|
||||
state = newState;
|
||||
return reply;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@ package de.srsoftware.web4rail.tiles;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.ControlUnit.Reply;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
@@ -45,21 +47,24 @@ public class TurnoutR extends Turnout {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void state(State newState) throws IOException {
|
||||
public CompletableFuture<Reply> state(State newState) throws IOException {
|
||||
init();
|
||||
LOG.debug("Setting {} to {}",this,newState);
|
||||
int p = 0;
|
||||
CompletableFuture<Reply> result;
|
||||
switch (newState) {
|
||||
case RIGHT:
|
||||
p = portB;
|
||||
result = plan.queue("SET {} GA "+address+" "+portB+" 1 "+delay);
|
||||
break;
|
||||
case STRAIGHT:
|
||||
p = portA;
|
||||
result = plan.queue("SET {} GA "+address+" "+portA+" 1 "+delay);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
if (p != 0) plan.queue("SET {} GA "+address+" "+p+" 1 "+delay);
|
||||
state = newState;
|
||||
plan.stream("place "+tag(null));
|
||||
return result.thenApply(reply -> {
|
||||
LOG.debug("{} received {}",reply);
|
||||
if (reply.is(200)) state = newState;
|
||||
return reply;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user