use of jmri.jmrit.operations.rollingstock.RollingStock in project JMRI by JMRI.
the class TrainCsvSwitchLists method buildSwitchList.
/**
* builds a csv file containing the switch list for a location
* @param location The Location requesting a switch list.
*
* @return File
*/
@SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", justification = "CarManager only provides Car Objects")
public File buildSwitchList(Location location) {
// create csv switch list file
File file = TrainManagerXml.instance().createCsvSwitchListFile(location.getName());
PrintWriter fileOut = null;
try {
fileOut = new // NOI18N
PrintWriter(// NOI18N
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")), // NOI18N
true);
} catch (IOException e) {
log.error("Can not open CSV switch list file: {}", file.getName());
return null;
}
// build header
addLine(fileOut, HEADER);
// this is a switch list
addLine(fileOut, SWL);
addLine(fileOut, RN + ESC + Setup.getRailroadName() + ESC);
addLine(fileOut, LN + ESC + splitString(location.getName()) + ESC);
addLine(fileOut, PRNTR + ESC + location.getDefaultPrinterName() + ESC);
addLine(fileOut, SWLC + ESC + location.getSwitchListComment() + ESC);
// add location comment
if (Setup.isPrintLocationCommentsEnabled() && !location.getComment().equals(Location.NONE)) {
// location comment can have multiple lines
// NOI18N
String[] comments = location.getComment().split(NEW_LINE);
for (String comment : comments) {
addLine(fileOut, LC + ESC + comment + ESC);
}
}
addLine(fileOut, VT + getDate(true));
// get a list of trains sorted by arrival time
List<Train> trains = TrainManager.instance().getTrainsArrivingThisLocationList(location);
for (Train train : trains) {
if (!train.isBuilt()) {
// train wasn't built so skip
continue;
}
if (!Setup.isSwitchListRealTime() && train.getSwitchListStatus().equals(Train.PRINTED)) {
// already printed this train
continue;
}
int pickupCars = 0;
int dropCars = 0;
int stops = 1;
boolean trainDone = false;
List<Car> carList = CarManager.instance().getByTrainDestinationList(train);
List<Engine> enginesList = EngineManager.instance().getByTrainBlockingList(train);
// does the train stop once or more at this location?
Route route = train.getRoute();
if (route == null) {
// no route for this train
continue;
}
List<RouteLocation> routeList = route.getLocationsBySequenceList();
RouteLocation rlPrevious = null;
// need to know where in the route we are for the various comments
for (RouteLocation rl : routeList) {
if (!splitString(rl.getName()).equals(splitString(location.getName()))) {
rlPrevious = rl;
continue;
}
String expectedArrivalTime = train.getExpectedArrivalTime(rl);
if (expectedArrivalTime.equals(Train.ALREADY_SERVICED)) {
trainDone = true;
}
// if it terminates at this location
if (stops == 1) {
// newLine(fileOut);
addLine(fileOut, TN + train.getName());
addLine(fileOut, TM + train.getDescription());
if (train.isTrainEnRoute()) {
addLine(fileOut, TIR);
addLine(fileOut, ETE + expectedArrivalTime);
} else {
addLine(fileOut, DL + splitString(splitString(train.getTrainDepartsName())));
addLine(fileOut, DT + train.getDepartureTime());
if (rl == train.getRoute().getDepartsRouteLocation() && routeList.size() > 1) {
addLine(fileOut, TD + splitString(rl.getName()) + DEL + rl.getTrainDirectionString());
}
if (rl != train.getRoute().getDepartsRouteLocation()) {
addLine(fileOut, ETA + expectedArrivalTime);
addLine(fileOut, TA + splitString(rl.getName()) + DEL + rl.getTrainDirectionString());
}
}
if (rl == train.getRoute().getTerminatesRouteLocation()) {
addLine(fileOut, TT + splitString(rl.getName()));
}
}
if (stops > 1) {
// Print visit number, etc. only if previous location wasn't the same
if (rlPrevious == null || !splitString(rl.getName()).equals(splitString(rlPrevious.getName()))) {
// After the first time a train stops at a location provide:
// if the train has started its route
// the arrival time or relative time if the train has started its route
// the train's direction when it arrives
// if it terminate at this location
addLine(fileOut, VN + stops);
if (train.isTrainEnRoute()) {
addLine(fileOut, ETE + expectedArrivalTime);
} else {
addLine(fileOut, ETA + expectedArrivalTime);
}
addLine(fileOut, TA + splitString(rl.getName()) + DEL + rl.getTrainDirectionString());
if (rl == train.getRoute().getTerminatesRouteLocation()) {
addLine(fileOut, TT + splitString(rl.getName()));
}
} else {
// don't bump stop count, same location
stops--;
// Does the train change direction?
if (rl.getTrainDirection() != rlPrevious.getTrainDirection()) {
addLine(fileOut, TDC + rl.getTrainDirectionString());
}
}
}
rlPrevious = rl;
// add route comment
if (!rl.getComment().equals(RouteLocation.NONE)) {
addLine(fileOut, RLC + ESC + rl.getComment() + ESC);
}
// engine change or helper service?
checkForEngineOrCabooseChange(fileOut, train, rl);
// go through the list of engines and determine if the engine departs here
for (Engine engine : enginesList) {
if (engine.getRouteLocation() == rl && engine.getTrack() != null) {
fileOutCsvEngine(fileOut, engine, PL);
}
}
// block pick up cars by destination
for (RouteLocation rld : routeList) {
for (Car car : carList) {
if (car.getRouteLocation() == rl && car.getTrack() != null && car.getRouteDestination() == rld) {
pickupCars++;
int count = 0;
if (car.isUtility()) {
count = countPickupUtilityCars(carList, car, !IS_MANIFEST);
if (count == 0) {
// already done this set of utility cars
continue;
}
}
fileOutCsvCar(fileOut, car, PC, count);
}
}
}
for (Engine engine : enginesList) {
if (engine.getRouteDestination() == rl) {
fileOutCsvEngine(fileOut, engine, SL);
}
}
// now do car set outs
for (Car car : carList) {
if (car.getRouteDestination() == rl) {
dropCars++;
int count = 0;
if (car.isUtility()) {
count = countSetoutUtilityCars(carList, car, !LOCAL, !IS_MANIFEST);
if (count == 0) {
// already done this set of utility cars
continue;
}
}
fileOutCsvCar(fileOut, car, SC, count);
}
}
stops++;
if (rl != train.getRoute().getTerminatesRouteLocation()) {
addLine(fileOut, TL + train.getTrainLength(rl) + DEL + train.getNumberEmptyCarsInTrain(rl) + DEL + train.getNumberCarsInTrain(rl));
addLine(fileOut, TW + train.getTrainWeight(rl));
}
}
if (trainDone && pickupCars == 0 && dropCars == 0) {
addLine(fileOut, TDONE);
} else if (stops > 1) {
if (pickupCars == 0) {
addLine(fileOut, NCPU);
}
if (dropCars == 0) {
addLine(fileOut, NCSO);
}
// done with this train
addLine(fileOut, TEND + train.getName());
}
}
// done with switch list
addLine(fileOut, END);
// now list hold cars
List<RollingStock> rsByLocation = CarManager.instance().getByLocationList();
List<Car> carList = new ArrayList<Car>();
for (RollingStock rs : rsByLocation) {
if (rs.getLocation() != null && splitString(rs.getLocation().getName()).equals(splitString(location.getName())) && rs.getRouteLocation() == null) {
carList.add((Car) rs);
}
}
// list utility cars by quantity
clearUtilityCarTypes();
for (Car car : carList) {
int count = 0;
if (car.isUtility()) {
count = countPickupUtilityCars(carList, car, !IS_MANIFEST);
if (count == 0) {
// already done this set of utility cars
continue;
}
}
fileOutCsvCar(fileOut, car, HOLD, count);
}
// done with hold cars
addLine(fileOut, END);
// Are there any cars that need to be found?
listCarsLocationUnknown(fileOut);
fileOut.flush();
fileOut.close();
location.setStatus(Location.CSV_GENERATED);
return file;
}
use of jmri.jmrit.operations.rollingstock.RollingStock in project JMRI by JMRI.
the class CarManagerTest method testListCarsOnTrack.
public void testListCarsOnTrack() {
resetCarManager();
CarManager manager = CarManager.instance();
Track l1t1 = l1.getTrackByName("A", Track.SPUR);
List<RollingStock> carList = manager.getList(l1t1);
Assert.assertEquals("Number of Cars on track", 1, carList.size());
Assert.assertTrue("c1 in car list on track", carList.contains(c1));
Assert.assertFalse("c2 not in car list on track", carList.contains(c2));
Assert.assertFalse("c3 not in car list on track", carList.contains(c3));
}
use of jmri.jmrit.operations.rollingstock.RollingStock in project JMRI by JMRI.
the class EngineManagerTest method testListEnginesOnTrack.
public void testListEnginesOnTrack() {
resetEngineManager();
EngineManager manager = EngineManager.instance();
Track l1t1 = l1.getTrackByName("A", Track.SPUR);
List<RollingStock> engineList = manager.getList(l1t1);
Assert.assertEquals("Number of Engines on track", 1, engineList.size());
Assert.assertTrue("e1 in engine list on track", engineList.contains(e1));
Assert.assertFalse("e2 not in engine list on track", engineList.contains(e2));
Assert.assertFalse("e3 not in engine list on track", engineList.contains(e3));
}
Aggregations