Search in sources :

Example 41 with RollingStock

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;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) RouteLocation(jmri.jmrit.operations.routes.RouteLocation) RollingStock(jmri.jmrit.operations.rollingstock.RollingStock) BufferedWriter(java.io.BufferedWriter) Car(jmri.jmrit.operations.rollingstock.cars.Car) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Engine(jmri.jmrit.operations.rollingstock.engines.Engine) Route(jmri.jmrit.operations.routes.Route) PrintWriter(java.io.PrintWriter) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 42 with RollingStock

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));
}
Also used : Track(jmri.jmrit.operations.locations.Track) RollingStock(jmri.jmrit.operations.rollingstock.RollingStock)

Example 43 with RollingStock

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));
}
Also used : Track(jmri.jmrit.operations.locations.Track) RollingStock(jmri.jmrit.operations.rollingstock.RollingStock)

Aggregations

RollingStock (jmri.jmrit.operations.rollingstock.RollingStock)43 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)13 Car (jmri.jmrit.operations.rollingstock.cars.Car)13 Route (jmri.jmrit.operations.routes.Route)11 RouteLocation (jmri.jmrit.operations.routes.RouteLocation)11 Track (jmri.jmrit.operations.locations.Track)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)5 Location (jmri.jmrit.operations.locations.Location)5 Engine (jmri.jmrit.operations.rollingstock.engines.Engine)5 Train (jmri.jmrit.operations.trains.Train)5 BufferedWriter (java.io.BufferedWriter)4 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 PrintWriter (java.io.PrintWriter)4 Dimension (java.awt.Dimension)2 JPanel (javax.swing.JPanel)2 XmlFile (jmri.jmrit.XmlFile)2 HardcopyWriter (jmri.util.davidflanagan.HardcopyWriter)2