minor bugfixes + more debug logging

This commit is contained in:
Stephan Richter
2021-03-29 20:09:15 +02:00
parent da2ad054ed
commit 0dde9693d1
11 changed files with 66 additions and 27 deletions

View File

@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.srsoftware</groupId>
<artifactId>web4rail</artifactId>
<version>1.4.4</version>
<version>1.4.5</version>
<name>Web4Rail</name>
<packaging>jar</packaging>
<description>Java Model Railway Control</description>

View File

@@ -9,7 +9,7 @@
</encoder>
<filter class="de.srsoftware.web4rail.ThreadFilter">
<level>DEBUG</level>
<keywords>Brake, Contact, Feed, Route, Train, e</keywords>
<keywords>Brake, Contact, Feed, Route, Train</keywords>
</filter>
</appender>

View File

@@ -8,7 +8,6 @@ import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
@@ -70,7 +69,7 @@ public abstract class BaseClass implements Constants{
private Contact contact;
private Direction direction;
private Integer waitTime;
List<EventListener> invalidationListeners = new LinkedList<>();
EventListener invalidationListener = null;
public Context(BaseClass object) {
setMain(object);
@@ -149,7 +148,7 @@ public abstract class BaseClass implements Constants{
public void invalidate() {
setMain(null);
invalidationListeners.forEach(EventListener::fire);
if (isSet(invalidationListener)) invalidationListener.fire();
}
public boolean invalidated() {
@@ -157,7 +156,7 @@ public abstract class BaseClass implements Constants{
}
public void onInvalidate(EventListener listener) {
invalidationListeners.add(listener);
invalidationListener = listener;
}

View File

@@ -281,6 +281,7 @@ public class Plan extends BaseClass{
}
tile.position(x, y);
if (tile instanceof TileWithShadow) ((TileWithShadow)tile).placeShadows();
tile.parent(this);
place(tile);
return t("Added {}",tile.getClass().getSimpleName());
}

View File

@@ -374,6 +374,9 @@ public class Route extends BaseClass {
}
public Route dropNextPreparedRoute() {
if (isSet(nextRoutePrepper)) nextRoutePrepper.stop();
nextRoutePrepper = null;
try {
return nextPreparedRoute;
} finally {
@@ -387,6 +390,7 @@ public class Route extends BaseClass {
public void finish(Train train) {
LOG.debug("{}.finish()",this);
context.onInvalidate(null); // do in invalidate context of route's nextRoute
context.invalidate();
train.endRoute(this);
setSignals(Signal.RED);
@@ -690,8 +694,10 @@ public class Route extends BaseClass {
nextRoutePrepper.onRoutePrepared(() -> {
nextPreparedRoute = nextRoutePrepper.route();
nextRoutePrepper = null;
LOG.debug("prepared route after {}: {}",this,nextPreparedRoute);
});
nextRoutePrepper.onFail(() -> {
LOG.debug("preparing route next to {} failed, resetting.",this);
Route rt = nextRoutePrepper.route(); // Nachfolgeroute kann ja schon reserviert sein, oder gar schon teilweise vorbereitet!
if (isSet(rt)) rt.resetIgnoring(this); // angefangene Route freigeben ohne Teile der aktuellen Route freizugeben
nextRoutePrepper = null;
@@ -843,10 +849,16 @@ public class Route extends BaseClass {
public boolean start() {
LOG.debug("{}.start()",this);
if (isNull(context) || context.invalidated()) return false;
if (isNull(context) || context.invalidated()) {
LOG.debug("Invalid context: {}",context);
return false;
}
Train train = context.train();
if (isNull(train)) return false; // can't set route's train to null
if (isNull(train)) {
LOG.debug("Context does not contain train: {}",context);
return false; // can't set route's train to null
}
train.setRoute(this); // set new train
triggeredContacts.clear();
@@ -856,7 +868,10 @@ public class Route extends BaseClass {
if (isSet(startActions)) {
context.route(this);
String cause = this+".start("+train.name()+")";
if (!startActions.fire(context,cause)) return false; // start actions failed
if (!startActions.fire(context,cause)) {
LOG.debug("Failed to execute {}",startActions);
return false; // start actions failed
}
}
context.waitTime(endBlock.getWaitTime(train, endDirection).random());

View File

@@ -12,7 +12,6 @@ public class BrakeStart extends Action {
public boolean fire(Context context,Object cause) {
if (isNull(context.train())) return false;
context.train().startBrake();
LOG.debug("Started brake process...");
return true;
}
}

View File

@@ -443,6 +443,7 @@ public class Train extends BaseClass implements Comparable<Train> {
}
public void endRoute(Route endedRoute) {
LOG.debug("{}.endRoute({})",this,endedRoute);
BrakeProcess brake = endBrake();
direction = endedRoute.endDirection; // muss vor der Auswertung des Destination-Tags stehen!
Block endBlock = endedRoute.endBlock();
@@ -494,6 +495,7 @@ public class Train extends BaseClass implements Comparable<Train> {
if (isSet(brake)) brake.updateTime();
Integer waitTime = route.waitTime();
nextPreparedRoute = route.dropNextPreparedRoute();
if (isSet(nextPreparedRoute)) LOG.debug("nextPreparedRoute is now {}",nextPreparedRoute);
if ((!autopilot)|| isNull(nextPreparedRoute) || (isSet(waitTime) && waitTime > 0)) setSpeed(0);
route = null;
endBlock.setTrain(this);
@@ -632,13 +634,14 @@ public class Train extends BaseClass implements Comparable<Train> {
public void afterLoad() {
if (json.has(TRACE)) json.getJSONArray(TRACE).forEach(elem -> {
Tile tile = plan.get(new Id(elem.toString()), false);
tile.setTrain(Train.this);
tile.setTrain(Train.this);
trace.add(tile);
});
if (json.has(BLOCK)) {// do not move this up! during set, other fields will be referenced!
currentBlock = (Block) plan.get(Id.from(json, BLOCK), false);
if (isSet(currentBlock)) {
currentBlock.setTrain(Train.this);
trace.add(currentBlock);
// currentBlock.add(Train.this, direction);
}
}
@@ -969,21 +972,31 @@ public class Train extends BaseClass implements Comparable<Train> {
public String start(boolean auto) {
LOG.debug("{}.start({})",this,auto?"auto":"");
autopilot |= auto;
if (isSet(nextPreparedRoute) && nextPreparedRoute.start()) {
nextPreparedRoute = null;
return null;
if (isSet(nextPreparedRoute)) {
LOG.debug("starting nextPreparedRoute: {}",nextPreparedRoute);
if (nextPreparedRoute.start()) {
LOG.debug("dropped nextPreparedRoute (was {})",nextPreparedRoute);
nextPreparedRoute = null;
return null;
} else {
LOG.debug("was not able to start {}", nextPreparedRoute);
}
}
if (isSet(routePrepper)) return t("Already searching route for {}",this);
routePrepper = new RoutePrepper(new Context(this).block(currentBlock).direction(direction));
routePrepper.onRoutePrepared(() -> {
routePrepper.route().start();
Route newRoute = routePrepper.route();
LOG.debug("prepared route {} for {}",newRoute,this);
newRoute.start();
routePrepper = null;
plan.stream(t("Started {}",Train.this));
});
routePrepper.onFail(() -> {
LOG.debug("preparing route for {} failed, resetting.",this);
Route failedRoute = routePrepper.route();
routePrepper = null;
if (isSet(failedRoute)) failedRoute.reset();
@@ -1005,7 +1018,10 @@ public class Train extends BaseClass implements Comparable<Train> {
public void startBrake() {
LOG.debug("{}.startBrake()",this);
if (autopilot && isSet(nextPreparedRoute)) return;
if (autopilot && isSet(nextPreparedRoute)) {
LOG.debug("not braking, because autopilot is active and next roue is prepared, already");
return;
}
brake = new BrakeProcess(this);
}

View File

@@ -40,8 +40,8 @@ public class BrakeProcess extends BaseClass implements Runnable{
long dist = 0;
int s = startSpeed;
while (s > Train.defaultEndSpeed) {
s -= SPEED_STEP;
dist += s*ts;
s -= SPEED_STEP;
}
LOG.debug("Estimated distamce with {} ms timestep: {}",ts,dist);
return dist;
@@ -56,6 +56,7 @@ public class BrakeProcess extends BaseClass implements Runnable{
@Override
public void run() {
timeStep = train.route().brakeTime(train.brakeId());
LOG.debug("{}.run() with timestep = {} ms",this,timeStep);
if (timeStep == null) timeStep = defaultTimeStep;
startSpeed = train.speed;
lastTime = timestamp();
@@ -100,7 +101,7 @@ public class BrakeProcess extends BaseClass implements Runnable{
if (!newTimeStep.equals(timeStep)) {
route.brakeTime(brakeId,newTimeStep);
calculated = calcDistance(newTimeStep);
LOG.debug("Corrected brake timestep for {} @ {} from {} to {} ms.",train,route,timeStep,newTimeStep);
LOG.debug("Corrected brake timestep from {} to {} ms for {} @ {}.",timeStep,newTimeStep,train,route);
LOG.debug("Differemce from estimated distance: {} ({}%)",distance-calculated,100*(distance-calculated)/(float)distance);
}
}

View File

@@ -22,9 +22,10 @@ public class RoutePrepper extends BaseClass implements Runnable{
private List<EventListener> failListeners = new LinkedList<>();
private List<EventListener> foundListeners = new LinkedList<>();
private List<EventListener> lockedListeners = new LinkedList<>();
private List<EventListener> preparedListeners= new LinkedList<>();
private EventListener preparedListener = null;
public RoutePrepper(Context c) {
LOG.debug("new RoutePrepper({})",c);
List<String> errors = new LinkedList<>();
if (isNull(c.train())) errors.add(t("No train in context for {}",getClass().getSimpleName()));
if (isNull(c.block())) errors.add(t("No block in context for {}",getClass().getSimpleName()));
@@ -182,7 +183,7 @@ public class RoutePrepper extends BaseClass implements Runnable{
}
public void onRoutePrepared(EventListener l) {
preparedListeners.add(l);
preparedListener = l;
}
@@ -196,7 +197,7 @@ public class RoutePrepper extends BaseClass implements Runnable{
if (!route.reserveFor(context)) return fail();
notify(lockedListeners);
if (!route.prepareAndLock()) return fail();
notify(preparedListeners);
if (isSet(preparedListener)) preparedListener.fire();
return true;
}
@@ -207,6 +208,7 @@ public class RoutePrepper extends BaseClass implements Runnable{
@Override
public void run() {
LOG.debug("{}.run()",this);
prepareRoute();
}
@@ -217,4 +219,9 @@ public class RoutePrepper extends BaseClass implements Runnable{
public void stop() {
context.invalidate();
}
@Override
public String toString() {
return getClass().getSimpleName()+"("+context+")";
}
}

View File

@@ -200,7 +200,9 @@ public abstract class Block extends StretchableTile{
@Override
protected HashSet<String> classes() {
HashSet<String> classes = super.classes();
if (!parkedTrains.isEmpty()) classes.add(OCCUPIED);
if (!isNull(occupyingTrain()) || !parkedTrains.isEmpty()) classes.add(OCCUPIED);
if (!isNull(lockingTrain())) classes.add(LOCKED);
if (isDisabled()) classes.add(DISABLED);
return classes;
}
@@ -456,8 +458,8 @@ public abstract class Block extends StretchableTile{
if (isNull(replacements)) replacements = new HashMap<String, Object>();
replacements.put("%text%",name);
Vector<String> trainNames = new Vector<String>();
Train lockingTrain = lockingTrain();
if (isSet(lockingTrain) /*&& !parkedTrains.contains(lockingTrain)*/) trainNames.add(lockingTrain.directedName());
Train lockingTrain = occupyingTrain();
if (isSet(lockingTrain)) trainNames.add(lockingTrain.directedName());
for (Train train: parkedTrains) trainNames.add(train.directedName());
if (!trainNames.isEmpty())replacements.put("%text%",String.join(" | ", trainNames));
Tag tag = super.tag(replacements);

View File

@@ -120,11 +120,10 @@ public abstract class Tile extends BaseClass implements Comparable<Tile> {
return new Id(x + "-" + y);
}
private static void inflate(String clazz, JSONObject json,
Plan plan) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException, IOException {
private static void inflate(String clazz, JSONObject json, Plan plan) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException, IOException {
clazz = Tile.class.getName().replace(".Tile", "." + clazz);
Tile tile = (Tile) Tile.class.getClassLoader().loadClass(clazz).getDeclaredConstructor().newInstance();
tile.load(json).register();
tile.load(json).parent(plan).register();
if (tile instanceof TileWithShadow) ((TileWithShadow) tile).placeShadows();
plan.place(tile);
}