Search in sources :

Example 1 with MonitoredStopVisitStructure

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

the class StopMonitoringV2Action method index.

/*
  @Override
  public String execute() {
  */
public DefaultHttpHeaders index() throws IOException {
    long responseTimestamp = getTime();
    // _monitoringActionSupport.setupGoogleAnalytics(_request,
    // _configurationService);
    processGoogleAnalytics();
    _realtimeService.setTime(responseTimestamp);
    String detailLevelParam = _request.getParameter(STOP_MONITORING_DETAIL_LEVEL);
    // get the detail level parameter or set it to default if not specified
    DetailLevel detailLevel;
    if (DetailLevel.contains(detailLevelParam)) {
        detailLevel = DetailLevel.valueOf(detailLevelParam.toUpperCase());
    } else {
        detailLevel = DetailLevel.NORMAL;
    }
    // User Parameters
    String lineRef = _request.getParameter(LINE_REF);
    String monitoringRef = _request.getParameter(MONITORING_REF);
    String directionId = _request.getParameter(DIRECTION_REF);
    String agencyId = _request.getParameter(OPERATOR_REF);
    String maxOnwardCallsParam = _request.getParameter(MAX_ONWARD_CALLS);
    String maxStopVisitsParam = _request.getParameter(MAX_STOP_VISITS);
    String minStopVisitsParam = _request.getParameter(MIN_STOP_VISITS);
    // Error Strings
    String routeIdsErrorString = "";
    String stopIdsErrorString = "";
    /* 
     * We need to support the user providing no agency id which means 'all
    agencies'. So, this array will hold a single agency if the user provides it or
    all agencies if the user provides none. We'll iterate over them later while
    querying for vehicles and routes
    */
    List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
    List<AgencyAndId> stopIds = new ArrayList<AgencyAndId>();
    List<String> agencyIds = processAgencyIds(agencyId);
    stopIdsErrorString = processStopIds(monitoringRef, stopIds, agencyIds);
    routeIdsErrorString = processRouteIds(lineRef, routeIds, agencyIds);
    int maximumOnwardCalls = 0;
    if (detailLevel.equals(DetailLevel.CALLS)) {
        maximumOnwardCalls = SiriSupportV2.convertToNumeric(maxOnwardCallsParam, Integer.MAX_VALUE);
    }
    // Google Analytics code used by nyc
    // if (_monitoringActionSupport
    // .canReportToGoogleAnalytics(_configurationService)) {
    // _monitoringActionSupport.reportToGoogleAnalytics(_request,
    // "Stop Monitoring", StringUtils.join(stopIds, ","),
    // _configurationService);
    // }
    // Setup Filters
    Map<Filters, String> filters = new HashMap<Filters, String>();
    filters.put(Filters.DIRECTION_REF, directionId);
    filters.put(Filters.MAX_STOP_VISITS, maxStopVisitsParam);
    filters.put(Filters.MIN_STOP_VISITS, minStopVisitsParam);
    // Monitored Stop Visits
    List<MonitoredStopVisitStructure> visits = new ArrayList<MonitoredStopVisitStructure>();
    for (AgencyAndId stopId : stopIds) {
        if (!stopId.hasValues())
            continue;
        // Stop ids can only be valid here because we only added valid ones
        // to stopIds.
        List<MonitoredStopVisitStructure> visitsForStop = _realtimeService.getMonitoredStopVisitsForStop(stopId.toString(), maximumOnwardCalls, detailLevel, responseTimestamp, routeIds, filters);
        if (visitsForStop != null)
            visits.addAll(visitsForStop);
    }
    Exception error = null;
    if (stopIds.size() == 0 || (lineRef != null && routeIds.size() == 0)) {
        String errorString = (stopIdsErrorString + " " + routeIdsErrorString).trim();
        error = new Exception(errorString);
    }
    _response = generateSiriResponse(visits, stopIds, error, responseTimestamp);
    try {
        this._servletResponse.getWriter().write(getStopMonitoring());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MonitoredStopVisitStructure(uk.org.siri.siri_2.MonitoredStopVisitStructure) IOException(java.io.IOException) Filters(org.onebusaway.api.actions.siri.impl.SiriSupportV2.Filters) DetailLevel(org.onebusaway.api.actions.siri.model.DetailLevel)

Example 2 with MonitoredStopVisitStructure

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

the class StopMonitoringV2Action method generateSiriResponse.

private Siri generateSiriResponse(List<MonitoredStopVisitStructure> visits, List<AgencyAndId> stopIds, Exception error, long responseTimestamp) {
    StopMonitoringDeliveryStructure stopMonitoringDelivery = new StopMonitoringDeliveryStructure();
    stopMonitoringDelivery.setResponseTimestamp(DateUtil.toXmlGregorianCalendar(responseTimestamp));
    ServiceDelivery serviceDelivery = new ServiceDelivery();
    serviceDelivery.setResponseTimestamp(DateUtil.toXmlGregorianCalendar(responseTimestamp));
    serviceDelivery.getStopMonitoringDelivery().add(stopMonitoringDelivery);
    if (error != null) {
        ServiceDeliveryErrorConditionStructure errorConditionStructure = new ServiceDeliveryErrorConditionStructure();
        ErrorDescriptionStructure errorDescriptionStructure = new ErrorDescriptionStructure();
        errorDescriptionStructure.setValue(error.getMessage());
        OtherErrorStructure otherErrorStructure = new OtherErrorStructure();
        otherErrorStructure.setErrorText(error.getMessage());
        errorConditionStructure.setDescription(errorDescriptionStructure);
        errorConditionStructure.setOtherError(otherErrorStructure);
        stopMonitoringDelivery.setErrorCondition(errorConditionStructure);
    } else {
        Calendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTimeInMillis(responseTimestamp);
        gregorianCalendar.add(Calendar.MINUTE, 1);
        stopMonitoringDelivery.setValidUntil(DateUtil.toXmlGregorianCalendar(gregorianCalendar.getTimeInMillis()));
        stopMonitoringDelivery.getMonitoredStopVisit().addAll(visits);
        serviceDelivery.setResponseTimestamp(DateUtil.toXmlGregorianCalendar(responseTimestamp));
        _serviceAlertsHelper.addSituationExchangeToSiriForStops(serviceDelivery, visits, _transitDataService, stopIds);
        _serviceAlertsHelper.addGlobalServiceAlertsToServiceDelivery(serviceDelivery, _realtimeService);
    }
    Siri siri = new Siri();
    siri.setServiceDelivery(serviceDelivery);
    return siri;
}
Also used : OtherErrorStructure(uk.org.siri.siri_2.OtherErrorStructure) StopMonitoringDeliveryStructure(uk.org.siri.siri_2.StopMonitoringDeliveryStructure) ServiceDeliveryErrorConditionStructure(uk.org.siri.siri_2.ServiceDeliveryErrorConditionStructure) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) Siri(uk.org.siri.siri_2.Siri) ServiceDelivery(uk.org.siri.siri_2.ServiceDelivery) ErrorDescriptionStructure(uk.org.siri.siri_2.ErrorDescriptionStructure)

Example 3 with MonitoredStopVisitStructure

use of uk.org.siri.siri_2.MonitoredStopVisitStructure 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 4 with MonitoredStopVisitStructure

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

the class ServiceAlertsHelperV2 method addSituationExchangeToSiriForStops.

public void addSituationExchangeToSiriForStops(ServiceDelivery serviceDelivery, List<MonitoredStopVisitStructure> visits, TransitDataService transitDataService, List<AgencyAndId> stopIds) {
    Map<String, PtSituationElementStructure> ptSituationElements = new HashMap<String, PtSituationElementStructure>();
    for (MonitoredStopVisitStructure visit : visits) {
        if (visit.getMonitoredVehicleJourney() != null)
            addSituationElement(transitDataService, ptSituationElements, visit.getMonitoredVehicleJourney().getSituationRef());
    }
    if (stopIds != null && stopIds.size() > 0) {
        for (AgencyAndId stopId : stopIds) {
            String stopIdString = stopId.toString();
            // First get service alerts for the stop
            SituationQueryBean query = new SituationQueryBean();
            List<String> stopIdStrings = new ArrayList<String>();
            stopIdStrings.add(stopIdString);
            SituationQueryBean.AffectsBean affects = new SituationQueryBean.AffectsBean();
            query.getAffects().add(affects);
            affects.setStopId(stopIdString);
            addFromQuery(transitDataService, ptSituationElements, query);
            // Now also add service alerts for (route+direction)s of the stop
            query = new SituationQueryBean();
            StopBean stopBean = transitDataService.getStop(stopIdString);
            List<RouteBean> routes = stopBean.getRoutes();
            for (RouteBean route : routes) {
                StopsForRouteBean stopsForRoute = transitDataService.getStopsForRoute(route.getId());
                List<StopGroupingBean> stopGroupings = stopsForRoute.getStopGroupings();
                for (StopGroupingBean stopGrouping : stopGroupings) {
                    if (!stopGrouping.getType().equalsIgnoreCase("direction"))
                        continue;
                    for (StopGroupBean stopGroup : stopGrouping.getStopGroups()) {
                        handleStopGroupBean(stopIdString, query, route, stopGroup);
                    }
                }
            }
            addFromQuery(transitDataService, ptSituationElements, query);
        }
    }
    addPtSituationElementsToServiceDelivery(serviceDelivery, ptSituationElements);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) StopGroupBean(org.onebusaway.transit_data.model.StopGroupBean) ArrayList(java.util.ArrayList) PtSituationElementStructure(uk.org.siri.siri_2.PtSituationElementStructure) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) MonitoredStopVisitStructure(uk.org.siri.siri_2.MonitoredStopVisitStructure) SituationAffectsBean(org.onebusaway.transit_data.model.service_alerts.SituationAffectsBean) RouteBean(org.onebusaway.transit_data.model.RouteBean) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) StopGroupingBean(org.onebusaway.transit_data.model.StopGroupingBean) SituationQueryBean(org.onebusaway.transit_data.model.service_alerts.SituationQueryBean) StopBean(org.onebusaway.transit_data.model.StopBean)

Aggregations

ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)3 MonitoredStopVisitStructure (uk.org.siri.siri_2.MonitoredStopVisitStructure)3 IOException (java.io.IOException)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 Filters (org.onebusaway.api.actions.siri.impl.SiriSupportV2.Filters)1 DetailLevel (org.onebusaway.api.actions.siri.model.DetailLevel)1 TimepointPredictionRecord (org.onebusaway.realtime.api.TimepointPredictionRecord)1 ArrivalAndDepartureBean (org.onebusaway.transit_data.model.ArrivalAndDepartureBean)1 RouteBean (org.onebusaway.transit_data.model.RouteBean)1 StopBean (org.onebusaway.transit_data.model.StopBean)1 StopGroupBean (org.onebusaway.transit_data.model.StopGroupBean)1 StopGroupingBean (org.onebusaway.transit_data.model.StopGroupingBean)1 StopsForRouteBean (org.onebusaway.transit_data.model.StopsForRouteBean)1 SituationAffectsBean (org.onebusaway.transit_data.model.service_alerts.SituationAffectsBean)1 SituationQueryBean (org.onebusaway.transit_data.model.service_alerts.SituationQueryBean)1 TripBean (org.onebusaway.transit_data.model.trips.TripBean)1 TripStatusBean (org.onebusaway.transit_data.model.trips.TripStatusBean)1