Search in sources :

Example 1 with VehicleActivityStructure

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

the class VehicleMonitoringV2Action method index.

public DefaultHttpHeaders index() throws IOException {
    long currentTimestamp = getTime();
    processGoogleAnalytics();
    // _monitoringActionSupport.setupGoogleAnalytics(_request, _configurationService);
    _realtimeService.setTime(currentTimestamp);
    String detailLevelParam = _request.getParameter(VEHICLE_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 vehicleRef = _request.getParameter(VEHICLE_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 = "";
    /*
     * 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<String> agencyIds = processAgencyIds(agencyId);
    List<AgencyAndId> vehicleIds = processVehicleIds(vehicleRef, agencyIds);
    routeIdsErrorString = processRouteIds(lineRef, routeIds, agencyIds);
    int maximumOnwardCalls = 0;
    if (detailLevel.equals(DetailLevel.CALLS)) {
        maximumOnwardCalls = SiriSupportV2.convertToNumeric(maxOnwardCallsParam, Integer.MAX_VALUE);
    }
    String gaLabel = null;
    // *** CASE 1: single vehicle, ignore any other filters
    if (vehicleIds.size() > 0) {
        gaLabel = vehicleRef;
        List<VehicleActivityStructure> activities = new ArrayList<VehicleActivityStructure>();
        try {
            for (AgencyAndId vehicleId : vehicleIds) {
                VehicleActivityStructure activity = _realtimeService.getVehicleActivityForVehicle(vehicleId.toString(), maximumOnwardCalls, detailLevel, currentTimestamp);
                if (activity != null) {
                    activities.add(activity);
                }
            }
        } catch (Exception e) {
            _log.info(e.getMessage(), e);
        }
        // No vehicle id validation, so we pass null for error
        _response = generateSiriResponse(activities, null, null, currentTimestamp);
    // *** CASE 2: by route, using direction id, if provided
    } else if (lineRef != null) {
        gaLabel = lineRef;
        List<VehicleActivityStructure> activities = new ArrayList<VehicleActivityStructure>();
        for (AgencyAndId routeId : routeIds) {
            List<VehicleActivityStructure> activitiesForRoute = _realtimeService.getVehicleActivityForRoute(routeId.toString(), directionId, maximumOnwardCalls, detailLevel, currentTimestamp);
            if (activitiesForRoute != null) {
                activities.addAll(activitiesForRoute);
            }
        }
        if (vehicleIds.size() > 0) {
            List<VehicleActivityStructure> filteredActivities = new ArrayList<VehicleActivityStructure>();
            for (VehicleActivityStructure activity : activities) {
                MonitoredVehicleJourneyStructure journey = activity.getMonitoredVehicleJourney();
                AgencyAndId thisVehicleId = AgencyAndIdLibrary.convertFromString(journey.getVehicleRef().getValue());
                // user filtering
                if (!vehicleIds.contains(thisVehicleId))
                    continue;
                filteredActivities.add(activity);
            }
            activities = filteredActivities;
        }
        Exception error = null;
        if (lineRef != null && routeIds.size() == 0) {
            error = new Exception(routeIdsErrorString.trim());
        }
        _response = generateSiriResponse(activities, routeIds, error, currentTimestamp);
    // *** CASE 3: all vehicles
    } else {
        try {
            gaLabel = "All Vehicles";
            // int hashKey = _siriCacheService.hash(maximumOnwardCalls, agencyIds, _type);
            List<VehicleActivityStructure> activities = new ArrayList<VehicleActivityStructure>();
            // if (!_siriCacheService.containsKey(hashKey)) {
            for (String agency : agencyIds) {
                ListBean<VehicleStatusBean> vehicles = _transitDataService.getAllVehiclesForAgency(agency, currentTimestamp);
                for (VehicleStatusBean v : vehicles.getList()) {
                    VehicleActivityStructure activity = _realtimeService.getVehicleActivityForVehicle(v.getVehicleId(), maximumOnwardCalls, detailLevel, currentTimestamp);
                    if (activity != null) {
                        activities.add(activity);
                    }
                }
            }
            // There is no input (route id) to validate, so pass null error
            _response = generateSiriResponse(activities, null, null, currentTimestamp);
        // _siriCacheService.store(hashKey, getVehicleMonitoring());
        // } else {
        // _cachedResponse = _siriCacheService.retrieve(hashKey);
        // }
        } catch (Exception e) {
            _log.error("vm all broke:", e);
            throw new RuntimeException(e);
        }
    }
    try {
        this._servletResponse.getWriter().write(getVehicleMonitoring());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) ListBean(org.onebusaway.transit_data.model.ListBean) IOException(java.io.IOException) IOException(java.io.IOException) VehicleStatusBean(org.onebusaway.transit_data.model.VehicleStatusBean) MonitoredVehicleJourneyStructure(uk.org.siri.siri_2.MonitoredVehicleJourneyStructure) DetailLevel(org.onebusaway.api.actions.siri.model.DetailLevel) ArrayList(java.util.ArrayList) List(java.util.List) VehicleActivityStructure(uk.org.siri.siri_2.VehicleActivityStructure)

Example 2 with VehicleActivityStructure

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

the class VehicleMonitoringV2Action method generateSiriResponse.

/**
 * Generate a siri response for a set of VehicleActivities
 *
 * @param routeId
 */
private Siri generateSiriResponse(List<VehicleActivityStructure> activities, List<AgencyAndId> routeIds, Exception error, long currentTimestamp) {
    VehicleMonitoringDeliveryStructure vehicleMonitoringDelivery = new VehicleMonitoringDeliveryStructure();
    vehicleMonitoringDelivery.setResponseTimestamp(DateUtil.toXmlGregorianCalendar(currentTimestamp));
    ServiceDelivery serviceDelivery = new ServiceDelivery();
    serviceDelivery.setResponseTimestamp(DateUtil.toXmlGregorianCalendar(currentTimestamp));
    serviceDelivery.getVehicleMonitoringDelivery().add(vehicleMonitoringDelivery);
    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);
        vehicleMonitoringDelivery.setErrorCondition(errorConditionStructure);
    } else {
        Calendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTimeInMillis(currentTimestamp);
        gregorianCalendar.add(Calendar.MINUTE, 1);
        vehicleMonitoringDelivery.setValidUntil(DateUtil.toXmlGregorianCalendar(gregorianCalendar.getTimeInMillis()));
        vehicleMonitoringDelivery.getVehicleActivity().addAll(activities);
        _serviceAlertsHelper.addSituationExchangeToServiceDelivery(serviceDelivery, activities, _transitDataService, routeIds);
        _serviceAlertsHelper.addGlobalServiceAlertsToServiceDelivery(serviceDelivery, _realtimeService);
    }
    Siri siri = new Siri();
    siri.setServiceDelivery(serviceDelivery);
    return siri;
}
Also used : OtherErrorStructure(uk.org.siri.siri_2.OtherErrorStructure) VehicleMonitoringDeliveryStructure(uk.org.siri.siri_2.VehicleMonitoringDeliveryStructure) 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 VehicleActivityStructure

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

the class RealtimeServiceV2Impl method getVehicleActivityForVehicle.

@Override
public VehicleActivityStructure getVehicleActivityForVehicle(String vehicleId, int maximumOnwardCalls, DetailLevel detailLevel, long currentTime) {
    TripForVehicleQueryBean query = new TripForVehicleQueryBean();
    query.setTime(new Date(currentTime));
    query.setVehicleId(vehicleId);
    TripDetailsInclusionBean inclusion = new TripDetailsInclusionBean();
    inclusion.setIncludeTripStatus(true);
    inclusion.setIncludeTripBean(true);
    query.setInclusion(inclusion);
    TripDetailsBean tripDetailsForCurrentTrip = _transitDataService.getTripDetailsForVehicleAndTime(query);
    if (tripDetailsForCurrentTrip != null) {
        if (!_presentationService.include(tripDetailsForCurrentTrip.getStatus()))
            return null;
        VehicleActivityStructure output = new VehicleActivityStructure();
        output.setRecordedAtTime(DateUtil.toXmlGregorianCalendar(tripDetailsForCurrentTrip.getStatus().getLastUpdateTime()));
        List<TimepointPredictionRecord> timePredictionRecords = null;
        timePredictionRecords = _transitDataService.getPredictionRecordsForTrip(AgencyAndId.convertFromString(vehicleId).getAgencyId(), tripDetailsForCurrentTrip.getStatus());
        output.setMonitoredVehicleJourney(new MonitoredVehicleJourney());
        SiriSupportV2.fillMonitoredVehicleJourney(output.getMonitoredVehicleJourney(), tripDetailsForCurrentTrip.getTrip(), tripDetailsForCurrentTrip.getStatus(), null, OnwardCallsMode.VEHICLE_MONITORING, _presentationService, _transitDataService, maximumOnwardCalls, timePredictionRecords, tripDetailsForCurrentTrip.getStatus().isPredicted(), detailLevel, currentTime, null);
        return output;
    }
    return null;
}
Also used : TripForVehicleQueryBean(org.onebusaway.transit_data.model.trips.TripForVehicleQueryBean) TimepointPredictionRecord(org.onebusaway.realtime.api.TimepointPredictionRecord) TripDetailsInclusionBean(org.onebusaway.transit_data.model.trips.TripDetailsInclusionBean) VehicleActivityStructure(uk.org.siri.siri_2.VehicleActivityStructure) Date(java.util.Date) TripDetailsBean(org.onebusaway.transit_data.model.trips.TripDetailsBean) MonitoredVehicleJourney(uk.org.siri.siri_2.VehicleActivityStructure.MonitoredVehicleJourney)

Example 4 with VehicleActivityStructure

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

the class ServiceAlertsHelperV2 method addSituationExchangeToServiceDelivery.

/*
  public void addSituationExchangeToServiceDelivery(ServiceDelivery serviceDelivery,
      List<VehicleActivityStructure> activities,
      TransitDataService transitDataService) {
    
    addSituationExchangeToServiceDelivery(serviceDelivery, activities, transitDataService, null);
  }
  */
public void addSituationExchangeToServiceDelivery(ServiceDelivery serviceDelivery, List<VehicleActivityStructure> activities, TransitDataService transitDataService, List<AgencyAndId> routeIds) {
    if (activities == null)
        return;
    Map<String, PtSituationElementStructure> ptSituationElements = new HashMap<String, PtSituationElementStructure>();
    for (VehicleActivityStructure activity : activities) {
        if (activity.getMonitoredVehicleJourney() != null) {
            addSituationElement(transitDataService, ptSituationElements, activity.getMonitoredVehicleJourney().getSituationRef());
        }
    }
    addPtSituationElementsToServiceDelivery(serviceDelivery, ptSituationElements);
    if (routeIds == null)
        return;
    List<ServiceAlertBean> serviceAlerts = new ArrayList<ServiceAlertBean>();
    for (AgencyAndId routeId : routeIds) {
        SituationQueryBean query = new SituationQueryBean();
        SituationQueryBean.AffectsBean affects = new SituationQueryBean.AffectsBean();
        query.getAffects().add(affects);
        affects.setRouteId(routeId.toString());
        ListBean<ServiceAlertBean> serviceAlertsForRoute = transitDataService.getServiceAlerts(query);
        if (serviceAlertsForRoute != null) {
            serviceAlerts.addAll(serviceAlertsForRoute.getList());
        }
    }
    if (serviceAlerts.size() == 0)
        return;
    addSituationExchangeToServiceDelivery(serviceDelivery, serviceAlerts);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PtSituationElementStructure(uk.org.siri.siri_2.PtSituationElementStructure) SituationAffectsBean(org.onebusaway.transit_data.model.service_alerts.SituationAffectsBean) SituationQueryBean(org.onebusaway.transit_data.model.service_alerts.SituationQueryBean) VehicleActivityStructure(uk.org.siri.siri_2.VehicleActivityStructure) ServiceAlertBean(org.onebusaway.transit_data.model.service_alerts.ServiceAlertBean)

Example 5 with VehicleActivityStructure

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

the class RealtimeServiceV2Impl method getVehicleActivityForRoute.

/**
 * SIRI METHODS
 */
@Override
public List<VehicleActivityStructure> getVehicleActivityForRoute(String routeId, String directionId, int maximumOnwardCalls, DetailLevel detailLevel, long currentTime) {
    List<VehicleActivityStructure> output = new ArrayList<VehicleActivityStructure>();
    ListBean<TripDetailsBean> trips = getAllTripsForRoute(routeId, currentTime);
    for (TripDetailsBean tripDetails : trips.getList()) {
        // filter out interlined routes
        if (routeId != null && !tripDetails.getTrip().getRoute().getId().equals(routeId))
            continue;
        // filtered out by user
        if (directionId != null && !tripDetails.getTrip().getDirectionId().equals(directionId))
            continue;
        if (!_presentationService.include(tripDetails.getStatus()))
            continue;
        VehicleActivityStructure activity = new VehicleActivityStructure();
        activity.setRecordedAtTime(DateUtil.toXmlGregorianCalendar(tripDetails.getStatus().getLastUpdateTime()));
        List<TimepointPredictionRecord> timePredictionRecords = null;
        timePredictionRecords = _transitDataService.getPredictionRecordsForTrip(AgencyAndId.convertFromString(routeId).getAgencyId(), tripDetails.getStatus());
        activity.setMonitoredVehicleJourney(new MonitoredVehicleJourney());
        SiriSupportV2.fillMonitoredVehicleJourney(activity.getMonitoredVehicleJourney(), tripDetails.getTrip(), tripDetails.getStatus(), null, OnwardCallsMode.VEHICLE_MONITORING, _presentationService, _transitDataService, maximumOnwardCalls, timePredictionRecords, tripDetails.getStatus().isPredicted(), detailLevel, currentTime, null);
        output.add(activity);
    }
    Collections.sort(output, new Comparator<VehicleActivityStructure>() {

        public int compare(VehicleActivityStructure arg0, VehicleActivityStructure 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 : ArrayList(java.util.ArrayList) SiriExtensionWrapper(org.onebusaway.transit_data_federation.siri.SiriExtensionWrapper) TimepointPredictionRecord(org.onebusaway.realtime.api.TimepointPredictionRecord) VehicleActivityStructure(uk.org.siri.siri_2.VehicleActivityStructure) TripDetailsBean(org.onebusaway.transit_data.model.trips.TripDetailsBean) MonitoredVehicleJourney(uk.org.siri.siri_2.VehicleActivityStructure.MonitoredVehicleJourney)

Aggregations

VehicleActivityStructure (uk.org.siri.siri_2.VehicleActivityStructure)4 ArrayList (java.util.ArrayList)3 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 TimepointPredictionRecord (org.onebusaway.realtime.api.TimepointPredictionRecord)2 TripDetailsBean (org.onebusaway.transit_data.model.trips.TripDetailsBean)2 MonitoredVehicleJourney (uk.org.siri.siri_2.VehicleActivityStructure.MonitoredVehicleJourney)2 IOException (java.io.IOException)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 HashMap (java.util.HashMap)1 List (java.util.List)1 DetailLevel (org.onebusaway.api.actions.siri.model.DetailLevel)1 ListBean (org.onebusaway.transit_data.model.ListBean)1 VehicleStatusBean (org.onebusaway.transit_data.model.VehicleStatusBean)1 ServiceAlertBean (org.onebusaway.transit_data.model.service_alerts.ServiceAlertBean)1 SituationAffectsBean (org.onebusaway.transit_data.model.service_alerts.SituationAffectsBean)1 SituationQueryBean (org.onebusaway.transit_data.model.service_alerts.SituationQueryBean)1 TripDetailsInclusionBean (org.onebusaway.transit_data.model.trips.TripDetailsInclusionBean)1 TripForVehicleQueryBean (org.onebusaway.transit_data.model.trips.TripForVehicleQueryBean)1