Search in sources :

Example 1 with DirectionRefStructure

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

the class SearchResultFactoryImplTest method runGetStopResult.

// Support methods
private StopResult runGetStopResult(List<ServiceAlertBean> serviceAlerts) {
    StopsForRouteBean stopsForRouteBean = mock(StopsForRouteBean.class);
    List<StopGroupingBean> stopGroupingBeans = new ArrayList<StopGroupingBean>();
    when(stopsForRouteBean.getStopGroupings()).thenReturn(stopGroupingBeans);
    StopGroupingBean stopGroupingBean = mock(StopGroupingBean.class);
    stopGroupingBeans.add(stopGroupingBean);
    List<StopGroupBean> stopGroups = new ArrayList<StopGroupBean>();
    StopGroupBean stopGroupBean = mock(StopGroupBean.class);
    stopGroups.add(stopGroupBean);
    when(stopGroupingBean.getStopGroups()).thenReturn(stopGroups);
    List<String> stopIds = new ArrayList<String>();
    when(stopGroupBean.getStopIds()).thenReturn(stopIds);
    NameBean nameBean = mock(NameBean.class);
    when(nameBean.getType()).thenReturn("destination");
    when(stopGroupBean.getName()).thenReturn(nameBean);
    List<String> stopGroupBeanStopIds = new ArrayList<String>();
    stopGroupBeanStopIds.add(TEST_STOP_ID);
    when(stopGroupBean.getStopIds()).thenReturn(stopGroupBeanStopIds);
    when(stopGroupBean.getId()).thenReturn(TEST_STOP_ID);
    stopIds.add(TEST_STOP_ID);
    List<RouteBean> routeBeans = new ArrayList<RouteBean>();
    routeBeans.add(createRouteBean());
    StopBean stopBean = mock(StopBean.class);
    when(stopBean.getId()).thenReturn(TEST_STOP_ID);
    when(stopBean.getRoutes()).thenReturn(routeBeans);
    List<MonitoredStopVisitStructure> monitoredStopVisits = new ArrayList<MonitoredStopVisitStructure>();
    MonitoredStopVisitStructure monitoredStopVisitStructure = mock(MonitoredStopVisitStructure.class);
    monitoredStopVisits.add(monitoredStopVisitStructure);
    MonitoredVehicleJourneyStructure monVehJourney = mock(MonitoredVehicleJourneyStructure.class);
    when(monitoredStopVisitStructure.getMonitoredVehicleJourney()).thenReturn(monVehJourney);
    when(monitoredStopVisitStructure.getRecordedAtTime()).thenReturn(new Date(TEST_TIME));
    LineRefStructure lineRefStructure = mock(LineRefStructure.class);
    when(monVehJourney.getLineRef()).thenReturn(lineRefStructure);
    when(lineRefStructure.getValue()).thenReturn(ROUTE_ID);
    DirectionRefStructure directionRef = mock(DirectionRefStructure.class);
    when(monVehJourney.getDirectionRef()).thenReturn(directionRef);
    when(directionRef.getValue()).thenReturn(TEST_STOP_ID);
    NaturalLanguageStringStructure natLangStrStructure = mock(NaturalLanguageStringStructure.class);
    when(natLangStrStructure.getValue()).thenReturn(TEST_DESTINATION_NAME);
    when(monVehJourney.getDestinationName()).thenReturn(natLangStrStructure);
    MonitoredCallStructure monCall = mock(MonitoredCallStructure.class);
    ExtensionsStructure extensions = mock(ExtensionsStructure.class);
    SiriExtensionWrapper siriExtensionWrapper = mock(SiriExtensionWrapper.class);
    SiriDistanceExtension distances = mock(SiriDistanceExtension.class);
    when(distances.getPresentableDistance()).thenReturn(TEST_PRESENTABLE_DISTANCE);
    when(siriExtensionWrapper.getDistances()).thenReturn(distances);
    when(extensions.getAny()).thenReturn(siriExtensionWrapper);
    when(monCall.getExtensions()).thenReturn(extensions);
    when(monVehJourney.getMonitoredCall()).thenReturn(monCall);
    when(_realtimeService.getMonitoredStopVisitsForStop(eq(TEST_STOP_ID), eq(0), anyLong())).thenReturn(monitoredStopVisits);
    when(_transitDataService.getStopsForRoute(anyString())).thenReturn(stopsForRouteBean);
    when(_realtimeService.getServiceAlertsForRouteAndDirection(ROUTE_ID, TEST_STOP_ID)).thenReturn(serviceAlerts);
    SearchResultFactoryImpl srf = new SearchResultFactoryImpl(_transitDataService, _realtimeService, _configurationService);
    Set<RouteBean> routeFilter = new HashSet<RouteBean>();
    StopResult result = (StopResult) srf.getStopResult(stopBean, routeFilter);
    return result;
}
Also used : NaturalLanguageStringStructure(uk.org.siri.siri.NaturalLanguageStringStructure) StopGroupBean(org.onebusaway.transit_data.model.StopGroupBean) ArrayList(java.util.ArrayList) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) Matchers.anyString(org.mockito.Matchers.anyString) ExtensionsStructure(uk.org.siri.siri.ExtensionsStructure) RouteBean(org.onebusaway.transit_data.model.RouteBean) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) SiriDistanceExtension(org.onebusaway.transit_data_federation.siri.SiriDistanceExtension) MonitoredCallStructure(uk.org.siri.siri.MonitoredCallStructure) HashSet(java.util.HashSet) SiriExtensionWrapper(org.onebusaway.transit_data_federation.siri.SiriExtensionWrapper) LineRefStructure(uk.org.siri.siri.LineRefStructure) MonitoredStopVisitStructure(uk.org.siri.siri.MonitoredStopVisitStructure) Date(java.util.Date) MonitoredVehicleJourneyStructure(uk.org.siri.siri.MonitoredVehicleJourneyStructure) StopGroupingBean(org.onebusaway.transit_data.model.StopGroupingBean) StopBean(org.onebusaway.transit_data.model.StopBean) NameBean(org.onebusaway.transit_data.model.NameBean) DirectionRefStructure(uk.org.siri.siri.DirectionRefStructure) StopResult(org.onebusaway.enterprise.webapp.actions.m.model.StopResult)

Example 2 with DirectionRefStructure

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

the class SiriSupport method fillMonitoredVehicleJourney.

/**
 * NOTE: The tripDetails bean here may not be for the trip the vehicle is currently on
 * in the case of A-D for stop!
 */
@SuppressWarnings("unused")
public static void fillMonitoredVehicleJourney(MonitoredVehicleJourneyStructure monitoredVehicleJourney, TripBean framedJourneyTripBean, TripStatusBean currentVehicleTripStatus, StopBean monitoredCallStopBean, OnwardCallsMode onwardCallsMode, PresentationService presentationService, TransitDataService transitDataService, int maximumOnwardCalls, List<TimepointPredictionRecord> stopLevelPredictions, boolean hasRealtimeData, long responseTimestamp, boolean showRawLocation) {
    BlockInstanceBean blockInstance = transitDataService.getBlockInstance(currentVehicleTripStatus.getActiveTrip().getBlockId(), currentVehicleTripStatus.getServiceDate());
    List<BlockTripBean> blockTrips = blockInstance.getBlockConfiguration().getTrips();
    if (monitoredCallStopBean == null) {
        monitoredCallStopBean = currentVehicleTripStatus.getNextStop();
    }
    // ///////////
    LineRefStructure lineRef = new LineRefStructure();
    lineRef.setValue(framedJourneyTripBean.getRoute().getId());
    monitoredVehicleJourney.setLineRef(lineRef);
    OperatorRefStructure operatorRef = new OperatorRefStructure();
    operatorRef.setValue(framedJourneyTripBean.getRoute().getId().split("_")[0]);
    monitoredVehicleJourney.setOperatorRef(operatorRef);
    DirectionRefStructure directionRef = new DirectionRefStructure();
    directionRef.setValue(framedJourneyTripBean.getDirectionId());
    monitoredVehicleJourney.setDirectionRef(directionRef);
    NaturalLanguageStringStructure routeShortName = new NaturalLanguageStringStructure();
    String shortName = framedJourneyTripBean.getRoute().getShortName();
    if (shortName == null) {
        shortName = framedJourneyTripBean.getRoute().getId().split("_")[1];
    }
    if (!isBlank(currentVehicleTripStatus.getActiveTrip().getRouteShortName())) {
        // look for an override like an express desginator
        routeShortName.setValue(currentVehicleTripStatus.getActiveTrip().getRouteShortName());
    } else {
        routeShortName.setValue(shortName);
    }
    monitoredVehicleJourney.setPublishedLineName(routeShortName);
    JourneyPatternRefStructure journeyPattern = new JourneyPatternRefStructure();
    journeyPattern.setValue(framedJourneyTripBean.getShapeId());
    monitoredVehicleJourney.setJourneyPatternRef(journeyPattern);
    NaturalLanguageStringStructure headsign = new NaturalLanguageStringStructure();
    headsign.setValue(framedJourneyTripBean.getTripHeadsign());
    monitoredVehicleJourney.setDestinationName(headsign);
    VehicleRefStructure vehicleRef = new VehicleRefStructure();
    if (currentVehicleTripStatus.getVehicleId() == null) {
        String tripId = framedJourneyTripBean.getId();
        String blockId = framedJourneyTripBean.getBlockId();
        String directionId = framedJourneyTripBean.getDirectionId();
        String vehicleIdHash = Integer.toString((tripId + blockId + directionId).hashCode());
        String agencyName = tripId.split("_")[0];
        String vehicleId = agencyName + "_" + vehicleIdHash;
        vehicleRef.setValue(vehicleId);
    } else {
        vehicleRef.setValue(currentVehicleTripStatus.getVehicleId());
    }
    monitoredVehicleJourney.setVehicleRef(vehicleRef);
    monitoredVehicleJourney.getVehicleMode().add(toVehicleMode(currentVehicleTripStatus.getVehicleType()));
    monitoredVehicleJourney.setMonitored(currentVehicleTripStatus.isPredicted());
    monitoredVehicleJourney.setBearing((float) currentVehicleTripStatus.getOrientation());
    monitoredVehicleJourney.setProgressRate(getProgressRateForPhaseAndStatus(currentVehicleTripStatus.getStatus(), currentVehicleTripStatus.getPhase()));
    // origin-destination
    for (int i = 0; i < blockTrips.size(); i++) {
        BlockTripBean blockTrip = blockTrips.get(i);
        if (blockTrip.getTrip().getId().equals(framedJourneyTripBean.getId())) {
            List<BlockStopTimeBean> stops = blockTrip.getBlockStopTimes();
            JourneyPlaceRefStructure origin = new JourneyPlaceRefStructure();
            origin.setValue(stops.get(0).getStopTime().getStop().getId());
            monitoredVehicleJourney.setOriginRef(origin);
            StopBean lastStop = stops.get(stops.size() - 1).getStopTime().getStop();
            DestinationRefStructure dest = new DestinationRefStructure();
            dest.setValue(lastStop.getId());
            monitoredVehicleJourney.setDestinationRef(dest);
            break;
        }
    }
    // framed journey
    FramedVehicleJourneyRefStructure framedJourney = new FramedVehicleJourneyRefStructure();
    DataFrameRefStructure dataFrame = new DataFrameRefStructure();
    dataFrame.setValue(String.format("%1$tY-%1$tm-%1$td", currentVehicleTripStatus.getServiceDate()));
    framedJourney.setDataFrameRef(dataFrame);
    framedJourney.setDatedVehicleJourneyRef(framedJourneyTripBean.getId());
    monitoredVehicleJourney.setFramedVehicleJourneyRef(framedJourney);
    // location
    // if vehicle is detected to be on detour, use actual lat/lon, not snapped location.
    LocationStructure location = new LocationStructure();
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(6);
    // if we want to show the raw location AND we have realtime or if its on detour, show actual location
    if ((showRawLocation && currentVehicleTripStatus.getLastKnownLocation() != null) || (presentationService.isOnDetour(currentVehicleTripStatus))) {
        location.setLatitude(new BigDecimal(df.format(currentVehicleTripStatus.getLastKnownLocation().getLat())));
        location.setLongitude(new BigDecimal(df.format(currentVehicleTripStatus.getLastKnownLocation().getLon())));
    } else {
        // show snapped location
        if (currentVehicleTripStatus.getLocation() != null) {
            location.setLatitude(new BigDecimal(df.format(currentVehicleTripStatus.getLocation().getLat())));
            location.setLongitude(new BigDecimal(df.format(currentVehicleTripStatus.getLocation().getLon())));
        }
    }
    monitoredVehicleJourney.setVehicleLocation(location);
    // progress status
    List<String> progressStatuses = new ArrayList<String>();
    if (presentationService.isInLayover(currentVehicleTripStatus)) {
        progressStatuses.add("layover");
    }
    // "prevTrip" really means not on the framedvehiclejourney trip
    if (!framedJourneyTripBean.getId().equals(currentVehicleTripStatus.getActiveTrip().getId())) {
        progressStatuses.add("prevTrip");
    }
    if (!progressStatuses.isEmpty()) {
        NaturalLanguageStringStructure progressStatus = new NaturalLanguageStringStructure();
        progressStatus.setValue(StringUtils.join(progressStatuses, ","));
        monitoredVehicleJourney.setProgressStatus(progressStatus);
    }
    // block ref
    if (presentationService.isBlockLevelInference(currentVehicleTripStatus)) {
        BlockRefStructure blockRef = new BlockRefStructure();
        blockRef.setValue(framedJourneyTripBean.getBlockId());
        monitoredVehicleJourney.setBlockRef(blockRef);
    }
    // scheduled depature time
    if (presentationService.isBlockLevelInference(currentVehicleTripStatus) && (presentationService.isInLayover(currentVehicleTripStatus) || !framedJourneyTripBean.getId().equals(currentVehicleTripStatus.getActiveTrip().getId()))) {
        BlockStopTimeBean originDepartureStopTime = null;
        for (int t = 0; t < blockTrips.size(); t++) {
            BlockTripBean thisTrip = blockTrips.get(t);
            BlockTripBean nextTrip = null;
            if (t + 1 < blockTrips.size()) {
                nextTrip = blockTrips.get(t + 1);
            }
            if (thisTrip.getTrip().getId().equals(currentVehicleTripStatus.getActiveTrip().getId())) {
                // just started new trip
                if (currentVehicleTripStatus.getDistanceAlongTrip() < (0.5 * currentVehicleTripStatus.getTotalDistanceAlongTrip())) {
                    originDepartureStopTime = thisTrip.getBlockStopTimes().get(0);
                // at end of previous trip
                } else {
                    if (nextTrip != null) {
                        originDepartureStopTime = nextTrip.getBlockStopTimes().get(0);
                    }
                }
                break;
            }
        }
        if (originDepartureStopTime != null) {
            Date departureTime = new Date(currentVehicleTripStatus.getServiceDate() + (originDepartureStopTime.getStopTime().getDepartureTime() * 1000));
            monitoredVehicleJourney.setOriginAimedDepartureTime(departureTime);
        }
    }
    Map<String, TimepointPredictionRecord> stopIdToPredictionRecordMap = new HashMap<String, TimepointPredictionRecord>();
    // (build map of vehicle IDs to TPRs)
    if (stopLevelPredictions != null) {
        for (TimepointPredictionRecord tpr : stopLevelPredictions) {
            stopIdToPredictionRecordMap.put(AgencyAndId.convertToString(tpr.getTimepointId()), tpr);
        }
    }
    // monitored call
    if (!presentationService.isOnDetour(currentVehicleTripStatus))
        fillMonitoredCall(monitoredVehicleJourney, blockInstance, currentVehicleTripStatus, monitoredCallStopBean, presentationService, transitDataService, stopIdToPredictionRecordMap, hasRealtimeData, responseTimestamp);
    // onward calls
    if (!presentationService.isOnDetour(currentVehicleTripStatus))
        fillOnwardCalls(monitoredVehicleJourney, blockInstance, framedJourneyTripBean, currentVehicleTripStatus, onwardCallsMode, presentationService, transitDataService, stopIdToPredictionRecordMap, maximumOnwardCalls, hasRealtimeData, responseTimestamp);
    // situations
    fillSituations(monitoredVehicleJourney, currentVehicleTripStatus);
    return;
}
Also used : NaturalLanguageStringStructure(uk.org.siri.siri.NaturalLanguageStringStructure) BlockTripBean(org.onebusaway.transit_data.model.blocks.BlockTripBean) DestinationRefStructure(uk.org.siri.siri.DestinationRefStructure) HashMap(java.util.HashMap) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) LocationStructure(uk.org.siri.siri.LocationStructure) BlockStopTimeBean(org.onebusaway.transit_data.model.blocks.BlockStopTimeBean) DataFrameRefStructure(uk.org.siri.siri.DataFrameRefStructure) OperatorRefStructure(uk.org.siri.siri.OperatorRefStructure) JourneyPlaceRefStructure(uk.org.siri.siri.JourneyPlaceRefStructure) FramedVehicleJourneyRefStructure(uk.org.siri.siri.FramedVehicleJourneyRefStructure) JourneyPatternRefStructure(uk.org.siri.siri.JourneyPatternRefStructure) BlockRefStructure(uk.org.siri.siri.BlockRefStructure) LineRefStructure(uk.org.siri.siri.LineRefStructure) BigDecimal(java.math.BigDecimal) Date(java.util.Date) BlockInstanceBean(org.onebusaway.transit_data.model.blocks.BlockInstanceBean) TimepointPredictionRecord(org.onebusaway.realtime.api.TimepointPredictionRecord) StopBean(org.onebusaway.transit_data.model.StopBean) DirectionRefStructure(uk.org.siri.siri.DirectionRefStructure) VehicleRefStructure(uk.org.siri.siri.VehicleRefStructure)

Example 3 with DirectionRefStructure

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

the class ServiceAlertsHelper method handleAffects.

/*
  @SuppressWarnings("unused")
  private void handlReasons(PtSituationElementStructure ptSituation,
      ServiceAlertBean serviceAlert) {
    throw new RuntimeException("handleReasons not implemented");
  }
*/
private void handleAffects(ServiceAlertBean serviceAlert, PtSituationElementStructure ptSituation) {
    if (serviceAlert.getAllAffects() == null)
        return;
    boolean hasOperators = false;
    AffectsScopeStructure affectsStructure = new AffectsScopeStructure();
    VehicleJourneys vehicleJourneys = new VehicleJourneys();
    for (SituationAffectsBean affects : serviceAlert.getAllAffects()) {
        String agencyId = affects.getAgencyId();
        if (agencyId != null) {
            Operators operators = new Operators();
            if (StringUtils.equals(agencyId, "__ALL_OPERATORS__")) {
                operators.setAllOperators("");
            } else {
                AffectedOperatorStructure affectedOperator = new AffectedOperatorStructure();
                affectedOperator.setOperatorName(createDefaultedTextStructure(agencyId));
                operators.getAffectedOperator().add(affectedOperator);
            }
            affectsStructure.setOperators(operators);
            hasOperators = true;
        }
        // LineRef
        String routeId = affects.getRouteId();
        String directionId = affects.getDirectionId();
        String stopId = affects.getStopId();
        if (!StringUtils.isBlank(routeId)) {
            AffectedVehicleJourneyStructure affectedVehicleJourneyStructure = new AffectedVehicleJourneyStructure();
            LineRefStructure lineRefStructure = new LineRefStructure();
            lineRefStructure.setValue(routeId);
            affectedVehicleJourneyStructure.setLineRef(lineRefStructure);
            if (!StringUtils.isBlank(directionId)) {
                DirectionRefStructure directionRefStructure = new DirectionRefStructure();
                directionRefStructure.setValue(directionId);
                affectedVehicleJourneyStructure.setDirectionRef(directionRefStructure);
            }
            vehicleJourneys.getAffectedVehicleJourney().add(affectedVehicleJourneyStructure);
        }
        // add support for stop level alerts
        if (!StringUtils.isBlank(stopId)) {
            AffectedStopPointStructure stop = new AffectedStopPointStructure();
            StopPointRefStructure stopRef = new StopPointRefStructure();
            stopRef.setValue(stopId);
            stop.setStopPointRef(stopRef);
            if (affectsStructure.getStopPoints() == null) {
                affectsStructure.setStopPoints(new AffectsScopeStructure.StopPoints());
            }
            affectsStructure.getStopPoints().getAffectedStopPoint().add(stop);
        }
    }
    if (vehicleJourneys.getAffectedVehicleJourney().size() > 0) {
        affectsStructure.setVehicleJourneys(vehicleJourneys);
    }
    if ((vehicleJourneys.getAffectedVehicleJourney().size() > 0) || hasOperators) {
        ptSituation.setAffects(affectsStructure);
    }
}
Also used : Operators(uk.org.siri.siri.AffectsScopeStructure.Operators) AffectedVehicleJourneyStructure(uk.org.siri.siri.AffectedVehicleJourneyStructure) AffectedOperatorStructure(uk.org.siri.siri.AffectedOperatorStructure) LineRefStructure(uk.org.siri.siri.LineRefStructure) AffectsScopeStructure(uk.org.siri.siri.AffectsScopeStructure) SituationAffectsBean(org.onebusaway.transit_data.model.service_alerts.SituationAffectsBean) VehicleJourneys(uk.org.siri.siri.AffectsScopeStructure.VehicleJourneys) StopPointRefStructure(uk.org.siri.siri.StopPointRefStructure) DirectionRefStructure(uk.org.siri.siri.DirectionRefStructure) AffectedStopPointStructure(uk.org.siri.siri.AffectedStopPointStructure)

Aggregations

DirectionRefStructure (uk.org.siri.siri.DirectionRefStructure)3 LineRefStructure (uk.org.siri.siri.LineRefStructure)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 StopBean (org.onebusaway.transit_data.model.StopBean)2 NaturalLanguageStringStructure (uk.org.siri.siri.NaturalLanguageStringStructure)2 BigDecimal (java.math.BigDecimal)1 DecimalFormat (java.text.DecimalFormat)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Matchers.anyString (org.mockito.Matchers.anyString)1 StopResult (org.onebusaway.enterprise.webapp.actions.m.model.StopResult)1 TimepointPredictionRecord (org.onebusaway.realtime.api.TimepointPredictionRecord)1 NameBean (org.onebusaway.transit_data.model.NameBean)1 RouteBean (org.onebusaway.transit_data.model.RouteBean)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 BlockInstanceBean (org.onebusaway.transit_data.model.blocks.BlockInstanceBean)1 BlockStopTimeBean (org.onebusaway.transit_data.model.blocks.BlockStopTimeBean)1