Search in sources :

Example 16 with ArrivalAndDepartureBean

use of org.onebusaway.transit_data.model.ArrivalAndDepartureBean in project onebusaway-application-modules by camsys.

the class RealtimeServiceV2Impl method getMonitoredStopVisitsForStop.

@Override
public List<MonitoredStopVisitStructure> getMonitoredStopVisitsForStop(String stopId, int maximumOnwardCalls, DetailLevel detailLevel, long currentTime, List<AgencyAndId> routeIds, Map<Filters, String> filters) {
    List<MonitoredStopVisitStructure> output = new ArrayList<MonitoredStopVisitStructure>();
    String directionId = filters.get(Filters.DIRECTION_REF);
    int maximumStopVisits = SiriSupportV2.convertToNumeric(filters.get(Filters.MAX_STOP_VISITS), Integer.MAX_VALUE);
    Integer minimumStopVisitsPerLine = SiriSupportV2.convertToNumeric(filters.get(Filters.MIN_STOP_VISITS), null);
    Map<AgencyAndId, Integer> visitCountByLine = new HashMap<AgencyAndId, Integer>();
    int visitCount = 0;
    for (ArrivalAndDepartureBean adBean : getArrivalsAndDeparturesForStop(stopId, currentTime)) {
        TripStatusBean statusBeanForCurrentTrip = adBean.getTripStatus();
        TripBean tripBeanForAd = adBean.getTrip();
        if (statusBeanForCurrentTrip == null)
            continue;
        if (!_presentationService.include(statusBeanForCurrentTrip) || !_presentationService.include(adBean, statusBeanForCurrentTrip))
            continue;
        MonitoredStopVisitStructure stopVisit = new MonitoredStopVisitStructure();
        stopVisit.setRecordedAtTime(DateUtil.toXmlGregorianCalendar(statusBeanForCurrentTrip.getLastUpdateTime()));
        List<TimepointPredictionRecord> timePredictionRecords = null;
        timePredictionRecords = _transitDataService.getPredictionRecordsForTrip(AgencyAndId.convertFromString(stopId).getAgencyId(), statusBeanForCurrentTrip);
        MonitoredVehicleJourneyStructure mvjourney = new MonitoredVehicleJourneyStructure();
        stopVisit.setMonitoredVehicleJourney(mvjourney);
        SiriSupportV2.fillMonitoredVehicleJourney(mvjourney, tripBeanForAd, statusBeanForCurrentTrip, adBean.getStop(), OnwardCallsMode.STOP_MONITORING, _presentationService, _transitDataService, maximumOnwardCalls, timePredictionRecords, statusBeanForCurrentTrip.isPredicted(), detailLevel, currentTime, filters);
        // FILTERS
        AgencyAndId thisRouteId = AgencyAndIdLibrary.convertFromString(mvjourney.getLineRef().getValue());
        String thisDirectionId = mvjourney.getDirectionRef().getValue();
        if (routeIds.size() > 0 && !routeIds.contains(thisRouteId))
            continue;
        if (directionId != null && !thisDirectionId.equals(directionId))
            continue;
        // Monitored Stop Visits
        Map<String, MonitoredStopVisitStructure> visitsMap = new HashMap<String, MonitoredStopVisitStructure>();
        // visit count filters
        Integer visitCountForThisLine = visitCountByLine.get(thisRouteId);
        if (visitCountForThisLine == null) {
            visitCountForThisLine = 0;
        }
        if (visitCount >= maximumStopVisits) {
            if (minimumStopVisitsPerLine == null) {
                break;
            } else {
                if (visitCountForThisLine >= minimumStopVisitsPerLine) {
                    continue;
                }
            }
        }
        // unique stops filters
        if (stopVisit.getMonitoredVehicleJourney() == null || stopVisit.getMonitoredVehicleJourney().getVehicleRef() == null || StringUtils.isBlank(stopVisit.getMonitoredVehicleJourney().getVehicleRef().getValue())) {
            continue;
        } else {
            String visitKey = stopVisit.getMonitoredVehicleJourney().getVehicleRef().getValue();
            if (visitsMap.containsKey(stopVisit.getMonitoredVehicleJourney().getVehicleRef().getValue())) {
                if (stopVisit.getMonitoredVehicleJourney().getProgressStatus() == null) {
                    visitsMap.remove(visitKey);
                    visitsMap.put(visitKey, stopVisit);
                }
                continue;
            } else {
                visitsMap.put(stopVisit.getMonitoredVehicleJourney().getVehicleRef().getValue(), stopVisit);
            }
        }
        output.add(stopVisit);
        visitCount++;
        visitCountForThisLine++;
        visitCountByLine.put(thisRouteId, visitCountForThisLine);
    }
    Collections.sort(output, new Comparator<MonitoredStopVisitStructure>() {

        public int compare(MonitoredStopVisitStructure arg0, MonitoredStopVisitStructure arg1) {
            try {
                SiriExtensionWrapper wrapper0 = (SiriExtensionWrapper) arg0.getMonitoredVehicleJourney().getMonitoredCall().getExtensions().getAny();
                SiriExtensionWrapper wrapper1 = (SiriExtensionWrapper) arg1.getMonitoredVehicleJourney().getMonitoredCall().getExtensions().getAny();
                return wrapper0.getDistances().getDistanceFromCall().compareTo(wrapper1.getDistances().getDistanceFromCall());
            } catch (Exception e) {
                return -1;
            }
        }
    });
    return output;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SiriExtensionWrapper(org.onebusaway.transit_data_federation.siri.SiriExtensionWrapper) TripBean(org.onebusaway.transit_data.model.trips.TripBean) MonitoredStopVisitStructure(uk.org.siri.siri_2.MonitoredStopVisitStructure) ArrivalAndDepartureBean(org.onebusaway.transit_data.model.ArrivalAndDepartureBean) MonitoredVehicleJourneyStructure(uk.org.siri.siri_2.MonitoredVehicleJourneyStructure) TimepointPredictionRecord(org.onebusaway.realtime.api.TimepointPredictionRecord) TripStatusBean(org.onebusaway.transit_data.model.trips.TripStatusBean)

Example 17 with ArrivalAndDepartureBean

use of org.onebusaway.transit_data.model.ArrivalAndDepartureBean in project onebusaway-application-modules by camsys.

the class ArrivalsAndDeparturesAction method filterArrivalsAndDeparturesByRoute.

private void filterArrivalsAndDeparturesByRoute(String[] tokens) {
    // Filter by route
    Set<String> routes = new HashSet<String>();
    for (String token : tokens) {
        String[] routeNames = token.split(",");
        for (String routeName : routeNames) routes.add(routeName);
    }
    StopsWithArrivalsAndDeparturesBean result = _model.getResult();
    Iterator<ArrivalAndDepartureBean> it = result.getArrivalsAndDepartures().iterator();
    while (it.hasNext()) {
        ArrivalAndDepartureBean bean = it.next();
        TripBean trip = bean.getTrip();
        RouteBean route = trip.getRoute();
        String routeName = RoutePresenter.getNameForRoute(route);
        if (!routes.contains(routeName))
            it.remove();
    }
}
Also used : RouteBean(org.onebusaway.transit_data.model.RouteBean) StopsWithArrivalsAndDeparturesBean(org.onebusaway.transit_data.model.StopsWithArrivalsAndDeparturesBean) TripBean(org.onebusaway.transit_data.model.trips.TripBean) ArrivalAndDepartureBean(org.onebusaway.transit_data.model.ArrivalAndDepartureBean) HashSet(java.util.HashSet)

Example 18 with ArrivalAndDepartureBean

use of org.onebusaway.transit_data.model.ArrivalAndDepartureBean in project onebusaway-application-modules by camsys.

the class ScheduleAction method getModel.

@Override
public Body<ScheduleRoute> getModel() {
    Body<ScheduleRoute> body = new Body<ScheduleRoute>();
    List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
    if (this.isValid(body, routeIds)) {
        AgencyBean agency = _transitDataService.getAgency(agencyId);
        List<HashMap<String, HashSet<ScheduleStop>>> blockStopsMapList = new ArrayList<HashMap<String, HashSet<ScheduleStop>>>();
        blockStopsMapList.add(new HashMap<String, HashSet<ScheduleStop>>());
        blockStopsMapList.add(new HashMap<String, HashSet<ScheduleStop>>());
        blockStopsMapList.add(new HashMap<String, HashSet<ScheduleStop>>());
        blockStopsMapList.add(new HashMap<String, HashSet<ScheduleStop>>());
        // Get All the Stops for a specific route
        for (AgencyAndId routeId : routeIds) {
            String route = AgencyAndId.convertToString(routeId);
            StopsForRouteBean stopsForRoute = _service.getStopsForRoute(route);
            for (StopGroupingBean stopGroupingBean : stopsForRoute.getStopGroupings()) {
                for (StopGroupBean stopGroupBean : stopGroupingBean.getStopGroups()) {
                    // Weekday Trips
                    for (Long weekdayTime : DateUtil.getWeekdayDateTimes(agency.getTimezone())) {
                        ArrivalsAndDeparturesQueryBean query = new ArrivalsAndDeparturesQueryBean();
                        query.setTime(weekdayTime);
                        query.setMinutesBefore(0);
                        query.setMinutesAfter(MINUTES_IN_DAY);
                        StopsWithArrivalsAndDeparturesBean stopsWithArrivals = _service.getStopsWithArrivalsAndDepartures(stopGroupBean.getStopIds(), query);
                        for (ArrivalAndDepartureBean arrivalsAndDeparture : stopsWithArrivals.getArrivalsAndDepartures()) {
                            // Filter Arrivals and Departures By Route
                            if (arrivalsAndDeparture.getTrip().getRoute().getId().equals(route)) {
                                ScheduleStop scheduleStop = new ScheduleStop();
                                scheduleStop.setTag(getIdNoAgency(arrivalsAndDeparture.getStop().getId()));
                                scheduleStop.setEpochTime(arrivalsAndDeparture.getScheduledArrivalTime());
                                scheduleStop.setStopName(arrivalsAndDeparture.getStop().getName());
                                if (arrivalsAndDeparture.getTrip().getDirectionId().equals("0")) {
                                    addStopByBlockId(blockStopsMapList.get(0), arrivalsAndDeparture.getTrip().getBlockId(), scheduleStop);
                                } else if (arrivalsAndDeparture.getTrip().getDirectionId().equals("1")) {
                                    addStopByBlockId(blockStopsMapList.get(1), arrivalsAndDeparture.getTrip().getBlockId(), scheduleStop);
                                }
                            }
                        }
                    }
                    // Weekend Trips
                    for (Long weekendTime : DateUtil.getWeekendDateTimes(agency.getTimezone())) {
                        ArrivalsAndDeparturesQueryBean query = new ArrivalsAndDeparturesQueryBean();
                        query.setTime(weekendTime);
                        query.setMinutesBefore(0);
                        query.setMinutesAfter(MINUTES_IN_DAY);
                        StopsWithArrivalsAndDeparturesBean stopsWithArrivals = _service.getStopsWithArrivalsAndDepartures(stopGroupBean.getStopIds(), query);
                        for (ArrivalAndDepartureBean arrivalsAndDeparture : stopsWithArrivals.getArrivalsAndDepartures()) {
                            // Filter Arrivals and Departures By Route
                            if (arrivalsAndDeparture.getTrip().getRoute().getId().equals(route)) {
                                ScheduleStop scheduleStop = new ScheduleStop();
                                scheduleStop.setTag(getIdNoAgency(arrivalsAndDeparture.getStop().getId()));
                                scheduleStop.setEpochTime(arrivalsAndDeparture.getScheduledArrivalTime());
                                scheduleStop.setStopName(arrivalsAndDeparture.getStop().getName());
                                if (arrivalsAndDeparture.getTrip().getDirectionId().equals("0")) {
                                    addStopByBlockId(blockStopsMapList.get(2), arrivalsAndDeparture.getTrip().getBlockId(), scheduleStop);
                                } else if (arrivalsAndDeparture.getTrip().getDirectionId().equals("1")) {
                                    addStopByBlockId(blockStopsMapList.get(3), arrivalsAndDeparture.getTrip().getBlockId(), scheduleStop);
                                }
                            }
                        }
                    }
                }
            }
            // Routes
            for (int n = 0; n < blockStopsMapList.size(); n++) {
                HashMap<String, HashSet<ScheduleStop>> blockStopsMap = blockStopsMapList.get(n);
                ScheduleRoute scheduleRoute = new ScheduleRoute();
                scheduleRoute.setTitle(stopsForRoute.getRoute().getLongName());
                scheduleRoute.setDirection(Integer.toString(n % 2));
                scheduleRoute.setTag(getIdNoAgency(stopsForRoute.getRoute().getId()));
                scheduleRoute.setServiceClass(n < 3 ? "wkd" : "wkend");
                // Blocks
                for (Entry<String, HashSet<ScheduleStop>> entry : blockStopsMap.entrySet()) {
                    int tripStopTimeCounter = 0;
                    String blockId = entry.getKey();
                    HashSet<ScheduleStop> blockStops = entry.getValue();
                    ScheduleTableRow scheduleTr = new ScheduleTableRow(getIdNoAgency(blockId));
                    // Stop Times
                    for (ScheduleStop stop : blockStops) {
                        if (tripStopTimeCounter == 0) {
                            DisplayStop displayStop = new DisplayStop();
                            displayStop.setTag(stop.getTag());
                            displayStop.setValue(stop.getStopName());
                            scheduleRoute.getStops().add(displayStop);
                        }
                        // scheduleStop.setValue(value);
                        scheduleTr.getStops().add(stop);
                    }
                    scheduleRoute.getScheduleTableRow().add(scheduleTr);
                }
                body.getResponse().add(scheduleRoute);
            }
        }
    }
    return body;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) StopGroupBean(org.onebusaway.transit_data.model.StopGroupBean) ArrayList(java.util.ArrayList) ScheduleStop(org.onebusaway.nextbus.model.nextbus.ScheduleStop) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) StopsWithArrivalsAndDeparturesBean(org.onebusaway.transit_data.model.StopsWithArrivalsAndDeparturesBean) ArrivalsAndDeparturesQueryBean(org.onebusaway.transit_data.model.ArrivalsAndDeparturesQueryBean) ScheduleTableRow(org.onebusaway.nextbus.model.nextbus.ScheduleTableRow) DisplayStop(org.onebusaway.nextbus.model.nextbus.DisplayStop) Body(org.onebusaway.nextbus.model.nextbus.Body) AgencyBean(org.onebusaway.transit_data.model.AgencyBean) HashSet(java.util.HashSet) ArrivalAndDepartureBean(org.onebusaway.transit_data.model.ArrivalAndDepartureBean) StopGroupingBean(org.onebusaway.transit_data.model.StopGroupingBean) ScheduleRoute(org.onebusaway.nextbus.model.nextbus.ScheduleRoute)

Example 19 with ArrivalAndDepartureBean

use of org.onebusaway.transit_data.model.ArrivalAndDepartureBean in project onebusaway-application-modules by camsys.

the class ArrivalsAndDeparturesBeanServiceImpl method getArrivalsAndDeparturesByStopId.

/**
 **
 * {@link ArrivalsAndDeparturesBeanService} Interface
 ***
 */
@Override
public List<ArrivalAndDepartureBean> getArrivalsAndDeparturesByStopId(AgencyAndId stopId, ArrivalsAndDeparturesQueryBean query) {
    StopEntry stop = _transitGraphDao.getStopEntryForId(stopId, true);
    long time = query.getTime();
    int minutesBefore = Math.max(query.getMinutesBefore(), query.getFrequencyMinutesBefore());
    int minutesAfter = Math.max(query.getMinutesAfter(), query.getFrequencyMinutesAfter());
    long fromTime = time - minutesBefore * 60 * 1000;
    long toTime = time + minutesAfter * 60 * 1000;
    long nonFrequencyFromTime = time - query.getMinutesBefore() * 60 * 1000;
    long nonFrequencyToTime = time + query.getMinutesAfter() * 60 * 1000;
    long frequencyFromTime = time - query.getFrequencyMinutesBefore() * 60 * 1000;
    long frequencyToTime = time + query.getFrequencyMinutesAfter() * 60 * 1000;
    TargetTime target = new TargetTime(time, time);
    List<ArrivalAndDepartureInstance> instances = _arrivalAndDepartureService.getArrivalsAndDeparturesForStopInTimeRange(stop, target, fromTime, toTime);
    List<ArrivalAndDepartureBean> beans = new ArrayList<ArrivalAndDepartureBean>();
    Map<AgencyAndId, StopBean> stopBeanCache = new HashMap<AgencyAndId, StopBean>();
    for (ArrivalAndDepartureInstance instance : instances) {
        FrequencyEntry frequency = instance.getFrequency();
        long from = frequency != null ? frequencyFromTime : nonFrequencyFromTime;
        long to = frequency != null ? frequencyToTime : nonFrequencyToTime;
        if (!isArrivalAndDepartureInRange(instance, from, to))
            continue;
        ArrivalAndDepartureBean bean = getStopTimeInstanceAsBean(time, instance, stopBeanCache);
        applyBlockLocationToBean(instance, bean, time);
        Boolean isNegativeScheduledArrivalsEnabled = _gtfsRealtimeNegativeArrivals.getShowNegativeScheduledArrivalByAgencyId(instance.getBlockTrip().getTrip().getId().getAgencyId());
        if (isNegativeScheduledArrivalsEnabled != null && !isNegativeScheduledArrivalsEnabled && bean.getNumberOfStopsAway() < 0 && bean.getPredictedArrivalTime() <= 0)
            continue;
        applySituationsToBean(time, instance, bean);
        beans.add(bean);
    }
    Collections.sort(beans, new ArrivalAndDepartureComparator());
    return beans;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FrequencyEntry(org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry) TargetTime(org.onebusaway.transit_data_federation.model.TargetTime) ArrivalAndDepartureBean(org.onebusaway.transit_data.model.ArrivalAndDepartureBean) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) ArrivalAndDepartureInstance(org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance) StopBean(org.onebusaway.transit_data.model.StopBean)

Example 20 with ArrivalAndDepartureBean

use of org.onebusaway.transit_data.model.ArrivalAndDepartureBean in project onebusaway-application-modules by camsys.

the class ArrivalsAndDeparturesForRouteAction method execute.

@Override
public String execute() throws Exception {
    Set<String> routeIds = getRouteIdsForMatchingRoutes();
    List<ArrivalAndDepartureBean> arrivals = new ArrayList<ArrivalAndDepartureBean>();
    StopsWithArrivalsAndDeparturesBean m = _model.getResult();
    for (ArrivalAndDepartureBean pab : m.getArrivalsAndDepartures()) {
        RouteBean route = pab.getTrip().getRoute();
        if (routeIds.contains(route.getId()) || _route.equals(route.getShortName())) {
            arrivals.add(pab);
        }
    }
    m = new StopsWithArrivalsAndDeparturesBean(m.getStops(), arrivals, m.getNearbyStops(), m.getSituations());
    _model.setResult(m);
    return SUCCESS;
}
Also used : RouteBean(org.onebusaway.transit_data.model.RouteBean) ArrayList(java.util.ArrayList) StopsWithArrivalsAndDeparturesBean(org.onebusaway.transit_data.model.StopsWithArrivalsAndDeparturesBean) ArrivalAndDepartureBean(org.onebusaway.transit_data.model.ArrivalAndDepartureBean)

Aggregations

ArrivalAndDepartureBean (org.onebusaway.transit_data.model.ArrivalAndDepartureBean)21 ArrayList (java.util.ArrayList)14 StopBean (org.onebusaway.transit_data.model.StopBean)9 TripBean (org.onebusaway.transit_data.model.trips.TripBean)8 HashMap (java.util.HashMap)7 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)6 RouteBean (org.onebusaway.transit_data.model.RouteBean)6 HashSet (java.util.HashSet)4 AgencyBean (org.onebusaway.transit_data.model.AgencyBean)4 StopsWithArrivalsAndDeparturesBean (org.onebusaway.transit_data.model.StopsWithArrivalsAndDeparturesBean)4 Date (java.util.Date)3 ArrivalsAndDeparturesQueryBean (org.onebusaway.transit_data.model.ArrivalsAndDeparturesQueryBean)3 ServiceAlertBean (org.onebusaway.transit_data.model.service_alerts.ServiceAlertBean)3 TripStatusBean (org.onebusaway.transit_data.model.trips.TripStatusBean)3 ArrivalAndDepartureInstance (org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance)3 ArrivalAndDepartureComparator (org.onebusaway.presentation.impl.ArrivalAndDepartureComparator)2 TimepointPredictionRecord (org.onebusaway.realtime.api.TimepointPredictionRecord)2 StopWithArrivalsAndDeparturesBean (org.onebusaway.transit_data.model.StopWithArrivalsAndDeparturesBean)2 TargetTime (org.onebusaway.transit_data_federation.model.TargetTime)2 BlockStopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)2