@ -12,7 +12,6 @@ import java.util.HashSet;
import java.util.Map.Entry ;
import java.util.Map.Entry ;
import java.util.Random ;
import java.util.Random ;
import java.util.Vector ;
import java.util.Vector ;
import java.util.concurrent.CompletableFuture ;
import org.json.JSONObject ;
import org.json.JSONObject ;
import org.slf4j.Logger ;
import org.slf4j.Logger ;
@ -22,6 +21,7 @@ import de.keawe.tools.translations.Translation;
import de.srsoftware.tools.Tag ;
import de.srsoftware.tools.Tag ;
import de.srsoftware.web4rail.Application ;
import de.srsoftware.web4rail.Application ;
import de.srsoftware.web4rail.Constants ;
import de.srsoftware.web4rail.Constants ;
import de.srsoftware.web4rail.Plan ;
import de.srsoftware.web4rail.Plan.Direction ;
import de.srsoftware.web4rail.Plan.Direction ;
import de.srsoftware.web4rail.Route ;
import de.srsoftware.web4rail.Route ;
import de.srsoftware.web4rail.Window ;
import de.srsoftware.web4rail.Window ;
@ -99,6 +99,8 @@ public class Train implements Constants {
public int speed = 0 ;
public int speed = 0 ;
private Autopilot autopilot = null ;
private Autopilot autopilot = null ;
private Plan plan ;
public Train ( Locomotive loco ) {
public Train ( Locomotive loco ) {
this ( loco , null ) ;
this ( loco , null ) ;
}
}
@ -110,7 +112,7 @@ public class Train implements Constants {
trains . put ( id , this ) ;
trains . put ( id , this ) ;
}
}
public static Object action ( HashMap < String , String > params ) throws IOException {
public static Object action ( HashMap < String , String > params , Plan plan ) throws IOException {
String action = params . get ( ACTION ) ;
String action = params . get ( ACTION ) ;
if ( action = = null ) return t ( "No action passed to Train.action!" ) ;
if ( action = = null ) return t ( "No action passed to Train.action!" ) ;
if ( ! params . containsKey ( Train . ID ) ) {
if ( ! params . containsKey ( Train . ID ) ) {
@ -118,7 +120,7 @@ public class Train implements Constants {
case ACTION_PROPS :
case ACTION_PROPS :
return manager ( ) ;
return manager ( ) ;
case ACTION_ADD :
case ACTION_ADD :
return create ( params ) ;
return create ( params , plan ) ;
}
}
return t ( "No train id passed!" ) ;
return t ( "No train id passed!" ) ;
}
}
@ -140,10 +142,10 @@ public class Train implements Constants {
return t ( "Unknown action: {}" , params . get ( ACTION ) ) ;
return t ( "Unknown action: {}" , params . get ( ACTION ) ) ;
}
}
private static Object create ( HashMap < String , String > params ) {
private static Object create ( HashMap < String , String > params , Plan plan ) {
Locomotive loco = ( Locomotive ) Locomotive . get ( params . get ( Train . LOCO_ID ) ) ;
Locomotive loco = ( Locomotive ) Locomotive . get ( params . get ( Train . LOCO_ID ) ) ;
if ( loco = = null ) return t ( "unknown locomotive: {}" , params . get ( ID ) ) ;
if ( loco = = null ) return t ( "unknown locomotive: {}" , params . get ( ID ) ) ;
Train train = new Train ( loco ) ;
Train train = new Train ( loco ) . plan ( plan ) ;
if ( params . containsKey ( NAME ) ) train . name ( params . get ( NAME ) ) ;
if ( params . containsKey ( NAME ) ) train . name ( params . get ( NAME ) ) ;
return train ;
return train ;
}
}
@ -215,7 +217,7 @@ public class Train implements Constants {
return trains . values ( ) ;
return trains . values ( ) ;
}
}
public static void loadAll ( String filename ) throws IOException {
public static void loadAll ( String filename , Plan plan ) throws IOException {
BufferedReader file = new BufferedReader ( new FileReader ( filename ) ) ;
BufferedReader file = new BufferedReader ( new FileReader ( filename ) ) ;
String line = file . readLine ( ) ;
String line = file . readLine ( ) ;
while ( line ! = null ) {
while ( line ! = null ) {
@ -224,18 +226,19 @@ public class Train implements Constants {
long id = json . getLong ( ID ) ;
long id = json . getLong ( ID ) ;
Train train = new Train ( null , id ) ;
Train train = new Train ( null , id ) ;
train . load ( json ) ;
train . load ( json ) . plan ( plan ) ;
line = file . readLine ( ) ;
line = file . readLine ( ) ;
}
}
file . close ( ) ;
file . close ( ) ;
}
}
private void load ( JSONObject json ) {
private Train load ( JSONObject json ) {
pushPull = json . getBoolean ( PUSH_PULL ) ;
pushPull = json . getBoolean ( PUSH_PULL ) ;
if ( json . has ( NAME ) ) name = json . getString ( NAME ) ;
if ( json . has ( NAME ) ) name = json . getString ( NAME ) ;
for ( Object id : json . getJSONArray ( CARS ) ) add ( Car . get ( ( String ) id ) ) ;
for ( Object id : json . getJSONArray ( CARS ) ) add ( Car . get ( ( String ) id ) ) ;
for ( Object id : json . getJSONArray ( LOCOS ) ) add ( ( Locomotive ) Car . get ( ( String ) id ) ) ;
for ( Object id : json . getJSONArray ( LOCOS ) ) add ( ( Locomotive ) Car . get ( ( String ) id ) ) ;
return this ;
}
}
public static Object manager ( ) {
public static Object manager ( ) {
@ -278,6 +281,11 @@ public class Train implements Constants {
return result ;
return result ;
}
}
private Train plan ( Plan plan ) {
this . plan = plan ;
return this ;
}
public Tag props ( ) {
public Tag props ( ) {
Window window = new Window ( "train-properties" , t ( "Properties of {}" , getClass ( ) . getSimpleName ( ) ) ) ;
Window window = new Window ( "train-properties" , t ( "Properties of {}" , getClass ( ) . getSimpleName ( ) ) ) ;
@ -310,6 +318,9 @@ public class Train implements Constants {
actions . addTo ( list ) ;
actions . addTo ( list ) ;
}
}
if ( route ! = null ) {
new Tag ( "li" ) . content ( t ( "Current route: {}" , route ) ) . addTo ( list ) ;
}
if ( direction ! = null ) new Tag ( "li" ) . content ( t ( "Direction: heading {}" , direction ) ) . addTo ( list ) ;
if ( direction ! = null ) new Tag ( "li" ) . content ( t ( "Direction: heading {}" , direction ) ) . addTo ( list ) ;
@ -332,8 +343,8 @@ public class Train implements Constants {
this . speed = v ;
this . speed = v ;
}
}
public CompletableFuture < String > start ( ) throws IOException {
public String start ( ) throws IOException {
if ( block = = null ) return CompletableFuture . failedFuture ( new RuntimeException ( t ( "{} not in a block" , this ) ) ) ;
if ( block = = null ) return t ( "{} not in a block" , this ) ;
if ( route ! = null ) route . unlock ( ) . setSignals ( Signal . STOP ) ;
if ( route ! = null ) route . unlock ( ) . setSignals ( Signal . STOP ) ;
HashSet < Route > routes = block . routes ( ) ;
HashSet < Route > routes = block . routes ( ) ;
Vector < Route > availableRoutes = new Vector < Route > ( ) ;
Vector < Route > availableRoutes = new Vector < Route > ( ) ;
@ -352,9 +363,10 @@ public class Train implements Constants {
availableRoutes . add ( rt ) ;
availableRoutes . add ( rt ) ;
}
}
Random rand = new Random ( ) ;
Random rand = new Random ( ) ;
if ( availableRoutes . isEmpty ( ) ) return CompletableFuture . failedFuture ( new RuntimeException ( t ( "No free routes from {}" , block ) ) ) ;
if ( availableRoutes . isEmpty ( ) ) return t ( "No free routes from {}" , block ) ;
route = availableRoutes . get ( rand . nextInt ( availableRoutes . size ( ) ) ) ;
route = availableRoutes . get ( rand . nextInt ( availableRoutes . size ( ) ) ) ;
return route . lock ( this ) . thenApply ( reply - > {
route . lock ( this ) . thenApply ( reply - > {
try {
try {
route . setSignals ( null ) ;
route . setSignals ( null ) ;
if ( direction ! = route . startDirection ) turn ( ) ;
if ( direction ! = route . startDirection ) turn ( ) ;
@ -363,7 +375,13 @@ public class Train implements Constants {
} catch ( Exception e ) {
} catch ( Exception e ) {
throw new RuntimeException ( e ) ;
throw new RuntimeException ( e ) ;
}
}
} ) . thenAccept ( message - > plan . stream ( message ) )
. exceptionally ( ex - > {
plan . stream ( ex . getMessage ( ) ) ;
throw new RuntimeException ( ex ) ;
} ) ;
} ) ;
return t ( "Trying to start {}" , this ) ;
}
}
private Object stop ( ) {
private Object stop ( ) {