Search in sources :

Example 1 with OnwardCallsStructure

use of uk.org.siri.siri.OnwardCallsStructure in project onebusaway-application-modules by camsys.

the class SiriSupport method fillOnwardCalls.

private static void fillOnwardCalls(MonitoredVehicleJourneyStructure monitoredVehicleJourney, BlockInstanceBean blockInstance, TripBean framedJourneyTripBean, TripStatusBean currentVehicleTripStatus, OnwardCallsMode onwardCallsMode, PresentationService presentationService, TransitDataService transitDataService, Map<String, TimepointPredictionRecord> stopLevelPredictions, int maximumOnwardCalls, boolean hasRealtimeData, long responseTimestamp) {
    String tripIdOfMonitoredCall = framedJourneyTripBean.getId();
    monitoredVehicleJourney.setOnwardCalls(new OnwardCallsStructure());
    // no need to go further if this is the case!
    if (maximumOnwardCalls == 0) {
        return;
    }
    List<BlockTripBean> blockTrips = blockInstance.getBlockConfiguration().getTrips();
    double distanceOfVehicleAlongBlock = 0;
    int blockTripStopsAfterTheVehicle = 0;
    int onwardCallsAdded = 0;
    boolean foundActiveTrip = false;
    for (int i = 0; i < blockTrips.size(); i++) {
        BlockTripBean blockTrip = blockTrips.get(i);
        if (!foundActiveTrip) {
            if (currentVehicleTripStatus.getActiveTrip().getId().equals(blockTrip.getTrip().getId())) {
                distanceOfVehicleAlongBlock += currentVehicleTripStatus.getDistanceAlongTrip();
                foundActiveTrip = true;
            } else {
                // so to get the size of this one, we have to look at the next.
                if (i + 1 < blockTrips.size()) {
                    distanceOfVehicleAlongBlock = blockTrips.get(i + 1).getDistanceAlongBlock();
                }
                // bus has already served this trip, so no need to go further
                continue;
            }
        }
        if (onwardCallsMode == OnwardCallsMode.STOP_MONITORING) {
            // always include onward calls for the trip the monitored call is on ONLY.
            if (!blockTrip.getTrip().getId().equals(tripIdOfMonitoredCall)) {
                continue;
            }
        }
        boolean foundMatch = false;
        HashMap<String, Integer> visitNumberForStopMap = new HashMap<String, Integer>();
        for (BlockStopTimeBean stopTime : blockTrip.getBlockStopTimes()) {
            int visitNumber = getVisitNumber(visitNumberForStopMap, stopTime.getStopTime().getStop());
            StopBean stop = stopTime.getStopTime().getStop();
            double distanceOfCallAlongTrip = stopTime.getDistanceAlongBlock() - blockTrip.getDistanceAlongBlock();
            double distanceOfVehicleFromCall = stopTime.getDistanceAlongBlock() - distanceOfVehicleAlongBlock;
            // on future trips, count always.
            if (currentVehicleTripStatus.getActiveTrip().getId().equals(blockTrip.getTrip().getId())) {
                if (!hasRealtimeData) {
                    if (stop.getId().equals(currentVehicleTripStatus.getNextStop().getId()))
                        foundMatch = true;
                    if (foundMatch) {
                        blockTripStopsAfterTheVehicle++;
                        ArrivalsAndDeparturesQueryBean query = new ArrivalsAndDeparturesQueryBean();
                        StopWithArrivalsAndDeparturesBean result = transitDataService.getStopWithArrivalsAndDepartures(stop.getId(), query);
                        // We can't assume the first result is the correct result
                        Collections.sort(result.getArrivalsAndDepartures(), new SortByTime());
                        if (result.getArrivalsAndDepartures().isEmpty()) {
                            // bad data?  abort!
                            continue;
                        }
                        ArrivalAndDepartureBean arrivalAndDeparture = result.getArrivalsAndDepartures().get(0);
                        distanceOfVehicleFromCall = arrivalAndDeparture.getDistanceFromStop();
                    // responseTimestamp = arrivalAndDeparture.getScheduledArrivalTime();
                    } else
                        continue;
                } else if (stopTime.getDistanceAlongBlock() >= distanceOfVehicleAlongBlock) {
                    blockTripStopsAfterTheVehicle++;
                } else {
                    // stop is behind the bus--no need to go further
                    continue;
                }
            // future trip--bus hasn't reached this trip yet, so count all stops
            } else {
                blockTripStopsAfterTheVehicle++;
            }
            monitoredVehicleJourney.getOnwardCalls().getOnwardCall().add(getOnwardCallStructure(stop, presentationService, distanceOfCallAlongTrip, distanceOfVehicleFromCall, visitNumber, blockTripStopsAfterTheVehicle - 1, stopLevelPredictions.get(stopTime.getStopTime().getStop().getId()), hasRealtimeData, responseTimestamp, (currentVehicleTripStatus.getServiceDate() + stopTime.getStopTime().getArrivalTime() * 1000)));
            onwardCallsAdded++;
            if (onwardCallsAdded >= maximumOnwardCalls) {
                return;
            }
        }
        // if we get here, we added our stops
        return;
    }
    return;
}
Also used : BlockTripBean(org.onebusaway.transit_data.model.blocks.BlockTripBean) StopWithArrivalsAndDeparturesBean(org.onebusaway.transit_data.model.StopWithArrivalsAndDeparturesBean) HashMap(java.util.HashMap) ArrivalsAndDeparturesQueryBean(org.onebusaway.transit_data.model.ArrivalsAndDeparturesQueryBean) BlockStopTimeBean(org.onebusaway.transit_data.model.blocks.BlockStopTimeBean) ArrivalAndDepartureBean(org.onebusaway.transit_data.model.ArrivalAndDepartureBean) BigInteger(java.math.BigInteger) OnwardCallsStructure(uk.org.siri.siri.OnwardCallsStructure) StopBean(org.onebusaway.transit_data.model.StopBean)

Aggregations

BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 ArrivalAndDepartureBean (org.onebusaway.transit_data.model.ArrivalAndDepartureBean)1 ArrivalsAndDeparturesQueryBean (org.onebusaway.transit_data.model.ArrivalsAndDeparturesQueryBean)1 StopBean (org.onebusaway.transit_data.model.StopBean)1 StopWithArrivalsAndDeparturesBean (org.onebusaway.transit_data.model.StopWithArrivalsAndDeparturesBean)1 BlockStopTimeBean (org.onebusaway.transit_data.model.blocks.BlockStopTimeBean)1 BlockTripBean (org.onebusaway.transit_data.model.blocks.BlockTripBean)1 OnwardCallsStructure (uk.org.siri.siri.OnwardCallsStructure)1