Implementierung des neuen Routen-Algorithmus weitgehend abgeschlossen.
Was noch fehlt ist der Brems-Prozessor; außerdem muss der neue Code getestet werden.
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.3.70</version>
|
||||
<version>1.3.71</version>
|
||||
<name>Web4Rail</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Java Model Railway Control</description>
|
||||
|
||||
@@ -542,7 +542,7 @@ public abstract class BaseClass implements Constants{
|
||||
|
||||
public void sleep(long ms) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
Thread.sleep(ms);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import org.slf4j.LoggerFactory;
|
||||
* @author Stephan Richter, SRSoftware
|
||||
*
|
||||
*/
|
||||
public class Command {
|
||||
public class Command extends BaseClass {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Command.class);
|
||||
private String command;
|
||||
@@ -143,11 +143,9 @@ public class Command {
|
||||
*/
|
||||
public Reply reply(int timeout) throws TimeoutException {
|
||||
int counter = 0;
|
||||
while (reply == null) try {
|
||||
while (reply == null) {
|
||||
if (counter++ > timeout) timeout();
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException e) {
|
||||
LOG.warn("wait() interrupted!",e);
|
||||
sleep(10);
|
||||
}
|
||||
return reply;
|
||||
}
|
||||
|
||||
@@ -300,7 +300,7 @@ public class Plan extends BaseClass{
|
||||
return win;
|
||||
}
|
||||
|
||||
Thread analyzer = new Thread() {
|
||||
new Thread(Application.threadName("Plan.Analyzer")) {
|
||||
public void run() {
|
||||
Vector<Route> newRoutes = new Vector<Route>();
|
||||
for (Block block : BaseClass.listElements(Block.class)) {
|
||||
@@ -317,9 +317,7 @@ public class Plan extends BaseClass{
|
||||
|
||||
stream(t("Found {} routes.",newRoutes.size()));
|
||||
}
|
||||
};
|
||||
analyzer.setName(Application.threadName("Plan.Analyzer"));
|
||||
analyzer.start();
|
||||
}.start();
|
||||
|
||||
return t("Analyzing plan...");
|
||||
}
|
||||
@@ -652,7 +650,7 @@ public class Plan extends BaseClass{
|
||||
*/
|
||||
public Tile place(Tile tile) {
|
||||
try {
|
||||
tile.parent(this);
|
||||
// tile.parent(this);
|
||||
tile.register();
|
||||
stream("place "+tile.tag(null));
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.io.BufferedWriter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@@ -36,6 +37,7 @@ import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Table;
|
||||
import de.srsoftware.web4rail.tags.Window;
|
||||
import de.srsoftware.web4rail.threads.BrakeProcess;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.BlockContact;
|
||||
import de.srsoftware.web4rail.tiles.Contact;
|
||||
@@ -92,7 +94,7 @@ public class Route extends BaseClass {
|
||||
public Direction startDirection;
|
||||
private HashSet<Contact> triggeredContacts = new HashSet<>();
|
||||
|
||||
private Route nextRoute;
|
||||
private Route nextPreparedRoute;
|
||||
|
||||
public Route() {
|
||||
conditions = new ConditionList();
|
||||
@@ -227,7 +229,9 @@ public class Route extends BaseClass {
|
||||
}
|
||||
|
||||
public Integer brakeTime(String brakeId) {
|
||||
return brakeTimes.get(brakeId);
|
||||
Integer result = brakeTimes.get(brakeId);
|
||||
Collection<Integer> values = brakeTimes.values();
|
||||
return values.isEmpty() ? BrakeProcess.defaultTimeStep : values.stream().mapToInt(Integer::intValue).sum()/values.size();
|
||||
}
|
||||
|
||||
public void brakeTime(String brakeId, Integer newTimeStep) {
|
||||
@@ -280,19 +284,19 @@ public class Route extends BaseClass {
|
||||
Contact nextToLastContact = contacts.get(contacts.size()-2);
|
||||
String trigger = nextToLastContact.trigger();
|
||||
add(trigger,new BrakeStart(this));
|
||||
add(trigger,new PreserveRoute(this));
|
||||
|
||||
int count = 0;
|
||||
for (int i=0;i<contacts.size();i++) { // chose second contact, that is not a BlockContact
|
||||
Contact contact = contacts.get(i);
|
||||
if (contact instanceof BlockContact) continue;
|
||||
if (count++<1) continue;
|
||||
|
||||
trigger = contact.trigger(); // second contact, that is not a BlockContact
|
||||
for (Signal signal : signals) add(trigger,new SetSignal(this).set(signal).to(Signal.RED));
|
||||
if (count++==1) { // second contact, that is not a BlockContact:
|
||||
for (Signal signal : signals) add(contact.trigger(),new SetSignal(this).set(signal).to(Signal.RED));
|
||||
break;
|
||||
}
|
||||
}
|
||||
add(trigger,new PreserveRoute(this));
|
||||
|
||||
}
|
||||
if (!contacts.isEmpty()) add(contacts.lastElement().trigger(), new FinishRoute(this));
|
||||
for (Entry<Turnout, Turnout.State> entry : turnouts.entrySet()) {
|
||||
Turnout turnout = entry.getKey();
|
||||
@@ -370,6 +374,14 @@ public class Route extends BaseClass {
|
||||
for (String brakeId : brakeIds) brakeTimes.remove(brakeId);
|
||||
}
|
||||
|
||||
public Route dropNextPreparedRoute() {
|
||||
try {
|
||||
return nextPreparedRoute;
|
||||
} finally {
|
||||
nextPreparedRoute = null;
|
||||
}
|
||||
}
|
||||
|
||||
public Block endBlock() {
|
||||
return endBlock;
|
||||
}
|
||||
@@ -406,8 +418,8 @@ public class Route extends BaseClass {
|
||||
return sb.toString().trim();
|
||||
}
|
||||
|
||||
public Route getNextRoute() {
|
||||
return nextRoute;
|
||||
public Route getNextPreparedRoute() {
|
||||
return nextPreparedRoute;
|
||||
}
|
||||
|
||||
public Id id() {
|
||||
@@ -781,8 +793,8 @@ public class Route extends BaseClass {
|
||||
if (lastTile instanceof Turnout) addTurnout((Turnout) lastTile,state);
|
||||
}
|
||||
|
||||
public void setNextRoute(Route nextRoute) {
|
||||
this.nextRoute = nextRoute;
|
||||
public void setNextPreparedRoute(Route route) {
|
||||
nextPreparedRoute = route;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -29,7 +29,6 @@ public class PreserveRoute extends Action {
|
||||
LOG.debug("Not preserving route, as train needs to stop for {} ms at {}!",waitTime,endBlock);
|
||||
return false; // train is expected to wait in next block.
|
||||
}
|
||||
|
||||
return train.reserveRouteAfter(route);
|
||||
return isSet(route.getNextPreparedRoute()) || train.reserveRouteAfter(route);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,7 @@ public class SetTurnout extends Action {
|
||||
if (isNull(turnout)) return false;
|
||||
if (!turnout.state(state).succeeded()) return false;
|
||||
if (turnout.address() == 0) return true;
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
sleep(1000);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -37,6 +37,8 @@ import de.srsoftware.web4rail.tags.Select;
|
||||
import de.srsoftware.web4rail.tags.Table;
|
||||
import de.srsoftware.web4rail.tags.Window;
|
||||
import de.srsoftware.web4rail.threads.RouteManager.Callback;
|
||||
import de.srsoftware.web4rail.threads.BrakeProcess;
|
||||
import de.srsoftware.web4rail.threads.DelayedExecution;
|
||||
import de.srsoftware.web4rail.threads.RouteManager;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.Contact;
|
||||
@@ -94,7 +96,9 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
private HashSet<Tile> stuckTrace = null;
|
||||
|
||||
private Route nextRoute;
|
||||
private Route nextPreparedRoute;
|
||||
|
||||
private BrakeProcess brake;
|
||||
|
||||
public static Object action(HashMap<String, String> params, Plan plan) throws IOException {
|
||||
String action = params.get(ACTION);
|
||||
@@ -404,10 +408,21 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
trace.clear();
|
||||
}
|
||||
|
||||
private BrakeProcess endBrake() {
|
||||
if (isNull(brake)) return null;
|
||||
try {
|
||||
return brake.end();
|
||||
} finally {
|
||||
brake = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void endRoute(Block endBlock, Direction endDirection) {
|
||||
setSpeed(0);
|
||||
nextRoute = route.getNextRoute();
|
||||
BrakeProcess brake = endBrake();
|
||||
if (isSet(brake)) brake.updateTime();
|
||||
Integer waitTime = route.waitTime();
|
||||
nextPreparedRoute = route.dropNextPreparedRoute();
|
||||
if (isNull(nextPreparedRoute) || (isSet(waitTime) && waitTime > 0)) setSpeed(0);
|
||||
route = null;
|
||||
direction = endDirection;
|
||||
endBlock.add(this, direction);
|
||||
@@ -415,8 +430,18 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
trace.add(endBlock);
|
||||
stuckTrace = null;
|
||||
if (autopilot) {
|
||||
if (isSet(waitTime) && waitTime > 0) sleep(waitTime);
|
||||
if (isNull(waitTime) || waitTime == 0) {
|
||||
start(false);
|
||||
} else if (isSet(waitTime) && waitTime > 0) {
|
||||
new DelayedExecution(waitTime,this) {
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
if (autopilot) Train.this.start(false);
|
||||
}
|
||||
};
|
||||
plan.stream(t("{} waiting {} secs",this,(int)(waitTime/1000)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -447,6 +472,11 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hasNextPreparedRoute() {
|
||||
return isSet(nextPreparedRoute) || (isSet(route) && isSet(route.getNextPreparedRoute()));
|
||||
}
|
||||
|
||||
|
||||
public Train heading(Direction dir) {
|
||||
LOG.debug("{}.heading({})",this,dir);
|
||||
direction = dir;
|
||||
@@ -460,7 +490,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
public boolean isStoppable() {
|
||||
if (speed > 0) return true;
|
||||
if (isSet(routeManager) && routeManager.isSearching()) return true;
|
||||
if (isSet(routeManager) && routeManager.isActive()) return true;
|
||||
if (isSet(route)) return true;
|
||||
return false;
|
||||
}
|
||||
@@ -689,6 +719,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
public String quitAutopilot() {
|
||||
if (isSet(routeManager)) routeManager.quit();
|
||||
autopilot = false;
|
||||
return t("Autopilot already was disabled!");
|
||||
}
|
||||
|
||||
@@ -724,19 +755,19 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
return tags().iterator();
|
||||
}
|
||||
|
||||
public boolean reserveRouteAfter(Route r) {
|
||||
LOG.debug("reserveRouteAfter({})",r);
|
||||
public boolean reserveRouteAfter(Route route) {
|
||||
LOG.debug("reserveRouteAfter({})",route);
|
||||
if (isNull(routeManager)) routeManager = new RouteManager();
|
||||
Context newContext = new Context(this).block(r.endBlock()).direction(r.endDirection);
|
||||
Context newContext = new Context(this).block(route.endBlock()).direction(route.endDirection);
|
||||
if (routeManager.isSearching()) return false;
|
||||
routeManager.setContext(newContext);
|
||||
return (routeManager.setCallback(new Callback() {
|
||||
|
||||
routeManager.setCallback(new Callback() {
|
||||
@Override
|
||||
public void routePrepared(Route route) {
|
||||
r.setNextRoute(route);
|
||||
public void routePrepared(Route nextRoute) {
|
||||
route.setNextPreparedRoute(nextRoute);
|
||||
}
|
||||
}));
|
||||
});
|
||||
return routeManager.prepareRoute();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -869,9 +900,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
} else if (remaining.name.length()+car.name().length()<30){
|
||||
remaining.name += ", "+car.name();
|
||||
}
|
||||
} else {
|
||||
LOG.debug("Skipping {}",cars.get(i));
|
||||
}
|
||||
} else LOG.debug("Skipping {}",cars.get(i));
|
||||
}
|
||||
if (remaining.cars.isEmpty()) return false;
|
||||
remaining.direction = this.direction;
|
||||
@@ -885,8 +914,8 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
public String start(boolean auto) {
|
||||
autopilot |= auto;
|
||||
if (isSet(nextRoute) && nextRoute.start()) {
|
||||
nextRoute = null;
|
||||
if (isSet(nextPreparedRoute) && nextPreparedRoute.start()) {
|
||||
nextPreparedRoute = null;
|
||||
return null;
|
||||
}
|
||||
if (isNull(routeManager)) routeManager = new RouteManager();
|
||||
@@ -899,7 +928,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
}
|
||||
};
|
||||
|
||||
if (routeManager.isSearching()) { // es wird bereits eine Anschlussroute gesucht
|
||||
if (routeManager.isActive()) { // es wird bereits eine Anschlussroute gesucht
|
||||
// in diesem Fall muss bloß dass Callback des Route-Managers aktualisiert werden
|
||||
routeManager.setCallback(callback);
|
||||
} else { // routeManager nicht aktiv →> neuen Context setzen und starten
|
||||
@@ -916,9 +945,14 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
for (Train train : BaseClass.listElements(Train.class)) LOG.info(train.start(true));
|
||||
}
|
||||
|
||||
public void startBrake() {}
|
||||
public void startBrake() {
|
||||
LOG.debug("{}.startBrake()",this);
|
||||
if (isSet(nextPreparedRoute)) return;
|
||||
brake = new BrakeProcess(this);
|
||||
}
|
||||
|
||||
public Window stopNow() {
|
||||
endBrake();
|
||||
setSpeed(0);
|
||||
quitAutopilot();
|
||||
if (isSet(route)) {
|
||||
@@ -994,8 +1028,6 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
}
|
||||
|
||||
public Context updateTrace(Context context) {
|
||||
// TOSO: neu implementieren!
|
||||
// Beachten: Route aus Context, plan.freeBehindTrain
|
||||
LOG.debug("updateTrace({})",context);
|
||||
Tile from = context.tile();
|
||||
if (isNull(from)) from = context.contact();
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package de.srsoftware.web4rail.threads;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
|
||||
public class BrakeProcess extends BaseClass implements Runnable{
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(BrakeProcess.class);
|
||||
|
||||
public static int defaultTimeStep = 500;
|
||||
private Train train;
|
||||
private int targetSpeed = Train.defaultEndSpeed;
|
||||
boolean ended = false;
|
||||
long distance = 0;
|
||||
private int startSpeed;
|
||||
private int lastSpeed;
|
||||
private long lastTime;
|
||||
|
||||
public BrakeProcess(Train train) {
|
||||
this.train = train;
|
||||
new Thread(this, Application.threadName(this)).start();
|
||||
}
|
||||
|
||||
public BrakeProcess end() {
|
||||
LOG.debug("{}.end()",this);
|
||||
ended = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Integer delay = train.route().brakeTime(train.brakeId());
|
||||
startSpeed = train.speed;
|
||||
lastTime = timestamp();
|
||||
while (!train.hasNextPreparedRoute()) {
|
||||
sleep(delay);
|
||||
lastSpeed = train.speed;
|
||||
updateDistance();
|
||||
if (lastSpeed > targetSpeed) lastSpeed -= 10;
|
||||
if (lastSpeed < targetSpeed && (ended = true)) lastSpeed = targetSpeed;
|
||||
if (ended) break;
|
||||
if (lastSpeed != train.speed) train.setSpeed(lastSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateDistance() {
|
||||
long newTime = timestamp();
|
||||
distance += (newTime-lastTime)*lastSpeed;
|
||||
lastTime = newTime;
|
||||
LOG.debug("measured distance: {} units",distance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName()+"("+train+")";
|
||||
}
|
||||
|
||||
public void updateTime() {
|
||||
updateDistance();
|
||||
LOG.debug("updateTime(): start speed was {} {}.",startSpeed,BaseClass.speedUnit);
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
@@ -300,7 +300,7 @@ public class ControlUnit extends Thread implements Constants{
|
||||
private void startInfoThread() {
|
||||
infoSocket = commandSocket; // handshake läuft immer über commandSocket und commandScanner
|
||||
infoScanner = commandScanner;
|
||||
Thread infoThread = new Thread() {
|
||||
new Thread(Application.threadName("CU.InfoThread")) {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -318,14 +318,12 @@ public class ControlUnit extends Thread implements Constants{
|
||||
case FEEDBACK:
|
||||
int addr = Integer.parseInt(parts[5]);
|
||||
boolean active = !parts[6].equals("0");
|
||||
Thread thread = new Thread() {
|
||||
new Thread(Application.threadName("CU.FeedBack("+addr+")")) {
|
||||
@Override
|
||||
public void run() {
|
||||
plan.sensor(addr,active);
|
||||
}
|
||||
};
|
||||
thread.setName(Application.threadName("CU.FeedBack("+addr+")"));
|
||||
thread.start();
|
||||
}.start();
|
||||
case ACESSORY:
|
||||
break;
|
||||
default:
|
||||
@@ -351,9 +349,7 @@ public class ControlUnit extends Thread implements Constants{
|
||||
public String toString() {
|
||||
return "CU.InfoThread";
|
||||
}
|
||||
};
|
||||
infoThread.setName(Application.threadName("CU.InfoThread"));
|
||||
infoThread.start();
|
||||
}.start();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,9 +10,8 @@ public abstract class DelayedExecution extends Thread {
|
||||
}
|
||||
|
||||
public DelayedExecution(int delay,Object cause) {
|
||||
super(Application.threadName(cause));
|
||||
this.delay = delay;
|
||||
|
||||
setName(Application.threadName(cause));
|
||||
start();
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.util.Vector;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
@@ -18,17 +19,21 @@ import de.srsoftware.web4rail.tiles.Tile;
|
||||
|
||||
public class RouteManager extends BaseClass {
|
||||
|
||||
private enum State{
|
||||
IDLE,SEARCHING,PREPARING
|
||||
}
|
||||
|
||||
public static abstract class Callback {
|
||||
public abstract void routePrepared(Route route);
|
||||
}
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(RouteManager.class);
|
||||
private Context ctx;
|
||||
private State state;
|
||||
private Callback callback;
|
||||
private boolean searching;
|
||||
|
||||
public RouteManager() {
|
||||
searching = false;
|
||||
state =State.IDLE;
|
||||
}
|
||||
|
||||
private static TreeMap<Integer, List<Route>> availableRoutes(Context context, HashSet<Route> visitedRoutes) {
|
||||
@@ -153,55 +158,63 @@ public class RouteManager extends BaseClass {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return state != State.IDLE;
|
||||
}
|
||||
|
||||
public boolean isSearching() {
|
||||
return state == State.SEARCHING;
|
||||
}
|
||||
|
||||
public boolean prepareRoute() {
|
||||
try {
|
||||
if (isNull(ctx) || ctx.invalidated()) return false;
|
||||
state = State.SEARCHING;
|
||||
Route route = chooseRoute(ctx);
|
||||
if (isNull(route)) return false;
|
||||
ctx.route(route);
|
||||
if (!route.reserveFor(ctx)) {
|
||||
route.reset();
|
||||
return false;
|
||||
}
|
||||
state = State.PREPARING;
|
||||
if (!route.prepareAndLock()) {
|
||||
route.reset();
|
||||
return false;
|
||||
}
|
||||
if (isNull(route) || isNull(callback)) return false;
|
||||
callback.routePrepared(route);
|
||||
return true;
|
||||
} finally {
|
||||
state = State.IDLE;
|
||||
}
|
||||
}
|
||||
|
||||
public void quit() {
|
||||
LOG.debug("{}.quit", this);
|
||||
callback = null;
|
||||
if (isSet(ctx)) ctx.invalidate();
|
||||
}
|
||||
|
||||
public Route prepareRoute(Context context) {
|
||||
if (isNull(context) || context.invalidated()) return null;
|
||||
Route route = chooseRoute(context);
|
||||
if (isNull(route)) return null;
|
||||
context.route(route);
|
||||
if (!route.reserveFor(context)) {
|
||||
route.reset();
|
||||
return null;
|
||||
}
|
||||
if (!route.prepareAndLock()) {
|
||||
route.reset();
|
||||
return null;
|
||||
}
|
||||
return route;
|
||||
}
|
||||
|
||||
public void setContext(Context context) {
|
||||
ctx = context;
|
||||
}
|
||||
|
||||
public boolean setCallback(Callback callback) {
|
||||
if (ctx.invalidated()) return false;
|
||||
this.callback = callback;
|
||||
if (searching) return false; // search already running, do not start again!
|
||||
searching = true;
|
||||
Route route = prepareRoute(ctx);
|
||||
searching = false;
|
||||
if (isNull(route) || isNull(callback)) return false;
|
||||
callback.routePrepared(route);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isSearching() {
|
||||
return searching;
|
||||
public void setCallback(Callback callback) {
|
||||
if (!ctx.invalidated()) this.callback = callback;
|
||||
}
|
||||
|
||||
public void start(Callback callback) {
|
||||
Thread thread = new Thread() {
|
||||
public void run() {
|
||||
setCallback(callback);
|
||||
new Thread(Application.threadName(this)) {
|
||||
public void run() {
|
||||
prepareRoute();
|
||||
};
|
||||
};
|
||||
thread.setName(getClass().getSimpleName() + "(" + ctx.train() + ")");
|
||||
thread.start();
|
||||
}.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + "(" + ctx.train() + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ public abstract class Block extends StretchableTile{
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return trains.toString();
|
||||
return trains.stream().map(t -> t+"→"+directionOf(t)).reduce((s1,s2) -> s1+", "+s2).orElse(t("empty"));
|
||||
}
|
||||
}
|
||||
private static final String ALLOW_TURN = "allowTurn";
|
||||
@@ -192,6 +192,7 @@ public abstract class Block extends StretchableTile{
|
||||
private Vector<WaitTime> waitTimes = new Vector<WaitTime>();
|
||||
|
||||
public void add(Train train,Direction direction) {
|
||||
if (isNull(train)) return;
|
||||
train.register();
|
||||
trains.add(train,direction);
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class Contact extends Tile{
|
||||
boolean aborted = false;
|
||||
|
||||
public OffTimer() {
|
||||
setName(Application.threadName("OffTimer("+Contact.this+")"));
|
||||
super(Application.threadName("OffTimer("+Contact.this+")"));
|
||||
start();
|
||||
}
|
||||
|
||||
@@ -112,7 +112,12 @@ public class Contact extends Tile{
|
||||
|
||||
@Override
|
||||
public Object click(boolean shift) throws IOException {
|
||||
if (!shift) trigger(200);
|
||||
if (!shift) new Thread(Application.threadName(this)) {
|
||||
@Override
|
||||
public void run() {
|
||||
trigger(200);
|
||||
}
|
||||
}.start();
|
||||
return super.click(shift);
|
||||
}
|
||||
|
||||
|
||||
@@ -164,7 +164,7 @@ public class Switch extends Tile{
|
||||
public void state(boolean newState) {
|
||||
state = newState;
|
||||
|
||||
Thread thread = new Thread() {
|
||||
new Thread(Application.threadName(this)) {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -173,9 +173,7 @@ public class Switch extends Tile{
|
||||
actionsOn.fire(context,Switch.this);
|
||||
} else actionsOff.fire(context,Switch.this);
|
||||
}
|
||||
};
|
||||
thread.setName(Application.threadName(this));
|
||||
thread.start();
|
||||
}.start();
|
||||
stream();
|
||||
}
|
||||
|
||||
|
||||
@@ -161,6 +161,7 @@ public abstract class Turnout extends Tile implements Device{
|
||||
public Reply state(State newState) {
|
||||
if (is(Status.LOCKED,Status.OCCUPIED) && newState != state) return new Reply(415, t("{} locked by {}!",this,train()));
|
||||
if (address == 0) {
|
||||
sleep(300);
|
||||
state = newState;
|
||||
plan.place(this);
|
||||
return new Reply(200,"OK");
|
||||
|
||||
Reference in New Issue
Block a user