reservation of next route now possible before route start
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>web4rail</artifactId>
|
||||
<version>1.2.20</version>
|
||||
<version>1.2.21</version>
|
||||
<name>Web4Rail</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Java Model Railway Control</description>
|
||||
|
||||
@@ -127,6 +127,7 @@ public abstract class BaseClass implements Constants{
|
||||
sb.append("(");
|
||||
sb.append(t("Train: {}",train));
|
||||
if (isSet(direction)) sb.append(", "+t("Direction: {}",direction));
|
||||
if (isSet(block)) sb.append(", "+t("Block: {}",block));
|
||||
if (isSet(route)) sb.append(", "+t("Route: {}",route));
|
||||
if (isSet(contact)) sb.append(", "+t("Contact: {}",contact));
|
||||
sb.append(")");
|
||||
|
||||
@@ -42,7 +42,6 @@ import de.srsoftware.web4rail.tiles.Shadow;
|
||||
import de.srsoftware.web4rail.tiles.Signal;
|
||||
import de.srsoftware.web4rail.tiles.Tile;
|
||||
import de.srsoftware.web4rail.tiles.Turnout;
|
||||
import de.srsoftware.web4rail.tiles.Turnout.State;
|
||||
/**
|
||||
* A route is a vector of tiles that leads from one block to another.
|
||||
*
|
||||
@@ -50,6 +49,10 @@ import de.srsoftware.web4rail.tiles.Turnout.State;
|
||||
*
|
||||
*/
|
||||
public class Route extends BaseClass implements Comparable<Route>{
|
||||
|
||||
public enum State {
|
||||
FREE, LOCKED, PREPARED, STARTED;
|
||||
}
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Route.class);
|
||||
|
||||
private static final String ACTIONS = "actions";
|
||||
@@ -64,6 +67,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
static final String PATH = "path";
|
||||
static final String SIGNALS = "signals";
|
||||
static final String TURNOUTS = "turnouts";
|
||||
private State state = State.FREE;
|
||||
|
||||
private static final String ROUTE_START = "route_start";
|
||||
|
||||
@@ -125,7 +129,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
timestamp = new Date().getTime();
|
||||
if (train.speed == 0) aborted = true;
|
||||
while (train.speed > ENDSPEED) {
|
||||
if (aborted) break;
|
||||
if (aborted || train.nextRoutePrepared()) break;
|
||||
train.setSpeed(train.speed - 5);
|
||||
try {
|
||||
sleep(timeStep);
|
||||
@@ -145,7 +149,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
public Direction endDirection;
|
||||
private Vector<Tile> path;
|
||||
private Vector<Signal> signals;
|
||||
public Train train;
|
||||
private Train train;
|
||||
private HashMap<String,ActionList> triggeredActions = new HashMap<String, ActionList>();
|
||||
private HashMap<Turnout,Turnout.State> turnouts;
|
||||
private Block startBlock = null;
|
||||
@@ -309,14 +313,14 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
signals.add(signal);
|
||||
}
|
||||
|
||||
void addTurnout(Turnout t, State s) {
|
||||
void addTurnout(Turnout t, Turnout.State s) {
|
||||
turnouts.put(t, s);
|
||||
}
|
||||
|
||||
private Fieldset turnouts() {
|
||||
Fieldset win = new Fieldset(t("Turnouts"));
|
||||
Tag list = new Tag("ul");
|
||||
for (Entry<Turnout, State> entry : turnouts.entrySet()) {
|
||||
for (Entry<Turnout, Turnout.State> entry : turnouts.entrySet()) {
|
||||
Turnout turnout = entry.getKey();
|
||||
Plan.addLink(turnout, turnout+": "+t(entry.getValue().toString()), list);
|
||||
}
|
||||
@@ -351,7 +355,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
}
|
||||
|
||||
public void brakeStart() {
|
||||
if (isNull(train)) return;
|
||||
if (isNull(train)) return;
|
||||
brakeProcessor = new BrakeProcessor(this,train);
|
||||
}
|
||||
|
||||
@@ -453,8 +457,9 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
|
||||
public boolean fireSetupActions(Context context) {
|
||||
ActionList setupActions = triggeredActions.get(ROUTE_SETUP);
|
||||
if (isNull(setupActions)) return true;
|
||||
return setupActions.fire(context);
|
||||
if (isSet(setupActions) && !setupActions.fire(context)) return false;
|
||||
state = State.PREPARED;
|
||||
return true;
|
||||
}
|
||||
|
||||
private String generateName() {
|
||||
@@ -499,7 +504,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
json.put(SIGNALS, signalIds);
|
||||
|
||||
JSONArray turnouts = new JSONArray();
|
||||
for (Entry<Turnout, State> entry : this.turnouts.entrySet()) {
|
||||
for (Entry<Turnout, Turnout.State> entry : this.turnouts.entrySet()) {
|
||||
Turnout t = entry.getKey();
|
||||
turnouts.put(new JSONObject(Map.of(Turnout.ID,t.id().toString(),Turnout.STATE,entry.getValue())));
|
||||
}
|
||||
@@ -703,9 +708,9 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!success) for (Tile tile :alreadyLocked) {
|
||||
tile.setRoute(null);
|
||||
}
|
||||
if (success) {
|
||||
state = State.LOCKED;
|
||||
} else for (Tile tile :alreadyLocked) tile.setRoute(null);
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -801,6 +806,7 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
train = null;
|
||||
}
|
||||
triggeredContacts.clear();
|
||||
state = State.FREE;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -818,8 +824,8 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
file.close();
|
||||
}
|
||||
|
||||
public void setLast(State state) {
|
||||
if (isNull(state) || state == State.UNDEF) return;
|
||||
public void setLast(Turnout.State state) {
|
||||
if (isNull(state) || state == Turnout.State.UNDEF) return;
|
||||
Tile lastTile = path.lastElement();
|
||||
if (lastTile instanceof Turnout) addTurnout((Turnout) lastTile,state);
|
||||
}
|
||||
@@ -833,9 +839,9 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
|
||||
public boolean setTurnouts() {
|
||||
Turnout turnout = null;
|
||||
for (Entry<Turnout, State> entry : turnouts.entrySet()) try {
|
||||
for (Entry<Turnout, Turnout.State> entry : turnouts.entrySet()) try {
|
||||
turnout = entry.getKey();
|
||||
State targetVal = entry.getValue();
|
||||
Turnout.State targetVal = entry.getValue();
|
||||
if (!turnout.state(targetVal).succeeded()) return false;
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
@@ -851,6 +857,21 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
String[] parts = name().split("-");
|
||||
return parts[0].trim()+"–"+parts[parts.length-1].trim();
|
||||
}
|
||||
|
||||
public Route.State state(){
|
||||
return state;
|
||||
}
|
||||
|
||||
public boolean start(Train newTrain) {
|
||||
if (isNull(newTrain)) return false; // can't set route's train to null
|
||||
if (isSet(train)) {
|
||||
if (newTrain != train) return false; // can't alter route's train
|
||||
} else train = newTrain; // set new train
|
||||
ActionList startActions = triggeredActions.get(ROUTE_START);
|
||||
if (isSet(startActions) && !startActions.fire(new Context(this).train(train))) return false; // start actions failed
|
||||
state = State.STARTED;
|
||||
return true;
|
||||
}
|
||||
|
||||
public Block startBlock() {
|
||||
return startBlock;
|
||||
@@ -874,15 +895,8 @@ public class Route extends BaseClass implements Comparable<Route>{
|
||||
if (isSet(train)) train.addToTrace(trace);
|
||||
}
|
||||
|
||||
public boolean train(Train newTrain) {
|
||||
if (isSet(train) && newTrain != train) return false;
|
||||
train = newTrain;
|
||||
if (isSet(train)) {
|
||||
ActionList startActions = triggeredActions.get(ROUTE_START);
|
||||
if (isNull(startActions)) return true;
|
||||
return startActions.fire(new Context(this).train(train));
|
||||
}
|
||||
return true;
|
||||
public Train train() {
|
||||
return train;
|
||||
}
|
||||
|
||||
public Route unlock() throws IOException {
|
||||
|
||||
@@ -496,6 +496,10 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
this.name = newName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean nextRoutePrepared() {
|
||||
return isSet(nextRoute) && nextRoute.state() == Route.State.PREPARED;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
@@ -696,11 +700,11 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
if (isNull(route)) return t("No free routes from {}",currentBlock);
|
||||
if (!route.lock()) return t("Was not able to lock {}",route);
|
||||
if (!route.setTurnouts()) error = t("Was not able to set all turnouts!");
|
||||
if (isNull(error) && !route.fireSetupActions(context)) error = t("Was not able to fire all setup actions of route!");
|
||||
if (isNull(error) && !route.fireSetupActions(context.route(route))) error = t("Was not able to fire all setup actions of route!");
|
||||
}
|
||||
if (direction != route.startDirection) turn();
|
||||
|
||||
if (isNull(error) && !route.train(this)) error = t("Was not able to assign {} to {}!",this,route);
|
||||
if (isNull(error) && !route.start(this)) error = t("Was not able to assign {} to {}!",this,route);
|
||||
if (isSet(error)) {
|
||||
route.reset();
|
||||
route = null;
|
||||
|
||||
@@ -115,7 +115,7 @@ public abstract class Tile extends BaseClass implements Comparable<Tile>{
|
||||
|
||||
public boolean isFreeFor(Train newTrain) {
|
||||
if (disabled) return false;
|
||||
if (isSet(route) && route.train != newTrain) return false;
|
||||
if (isSet(route) && isSet(route.train()) && route.train() != newTrain) return false;
|
||||
if (isSet(train) && train != newTrain) return false;
|
||||
return true;
|
||||
}
|
||||
@@ -223,7 +223,9 @@ public abstract class Tile extends BaseClass implements Comparable<Tile>{
|
||||
|
||||
if (isTrack) {
|
||||
formInputs.add(t("Length"),new Input(LENGTH,length).numeric().addTo(new Tag("span")).content(NBSP+lengthUnit));
|
||||
formInputs.add(t("State"),new Checkbox(DISABLED, t("disabled"), disabled));
|
||||
Checkbox checkbox = new Checkbox(DISABLED, t("disabled"),disabled);
|
||||
if (disabled) checkbox.clazz("disabled");
|
||||
formInputs.add(t("State"),checkbox);
|
||||
}
|
||||
|
||||
List<Direction> pd = possibleDirections();
|
||||
|
||||
Reference in New Issue
Block a user