Search in sources :

Example 1 with SiriDistanceExtension

use of org.onebusaway.transit_data_federation.siri.SiriDistanceExtension 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 SiriDistanceExtension

use of org.onebusaway.transit_data_federation.siri.SiriDistanceExtension in project onebusaway-application-modules by camsys.

the class SiriSupport method getMonitoredCallStructure.

private static MonitoredCallStructure getMonitoredCallStructure(StopBean stopBean, PresentationService presentationService, double distanceOfCallAlongTrip, double distanceOfVehicleFromCall, int visitNumber, int index, TimepointPredictionRecord prediction, boolean hasRealtimeData, long responseTimestamp, long scheduledArrivalTime) {
    MonitoredCallStructure monitoredCallStructure = new MonitoredCallStructure();
    monitoredCallStructure.setVisitNumber(BigInteger.valueOf(visitNumber));
    StopPointRefStructure stopPointRef = new StopPointRefStructure();
    stopPointRef.setValue(stopBean.getId());
    monitoredCallStructure.setStopPointRef(stopPointRef);
    NaturalLanguageStringStructure stopPoint = new NaturalLanguageStringStructure();
    stopPoint.setValue(stopBean.getName());
    monitoredCallStructure.setStopPointName(stopPoint);
    if (prediction != null) {
        if (!hasRealtimeData) {
            monitoredCallStructure.setExpectedArrivalTime(new Date(prediction.getTimepointScheduledTime()));
            monitoredCallStructure.setExpectedDepartureTime(new Date(prediction.getTimepointScheduledTime()));
        } else // do not allow predicted times to be less than ResponseTimestamp
        if (prediction.getTimepointPredictedArrivalTime() < responseTimestamp) {
            /*
				 * monitoredCall has less precision than onwardCall (date vs. timestamp)
				 * which results in a small amount of error when converting back to timestamp.
				 * Add a second here to prevent negative values from showing up in the UI 
				 * (actual precision of the value is 1 minute, so a second has little influence)
				 */
            monitoredCallStructure.setExpectedArrivalTime(new Date(responseTimestamp + 1000));
            monitoredCallStructure.setExpectedDepartureTime(new Date(responseTimestamp + 1000));
        } else {
            monitoredCallStructure.setExpectedArrivalTime(new Date(prediction.getTimepointPredictedArrivalTime()));
            monitoredCallStructure.setExpectedDepartureTime(new Date(prediction.getTimepointPredictedArrivalTime()));
        }
    } else if (!hasRealtimeData) {
        monitoredCallStructure.setExpectedArrivalTime(new Date(scheduledArrivalTime));
        monitoredCallStructure.setExpectedDepartureTime(new Date(scheduledArrivalTime));
    }
    // setting the scheduled arrival time.
    if (monitoredCallStructure.getExpectedArrivalTime() != null) {
        monitoredCallStructure.setAimedArrivalTime(new Date(scheduledArrivalTime));
    }
    // siri extensions
    SiriExtensionWrapper wrapper = new SiriExtensionWrapper();
    ExtensionsStructure distancesExtensions = new ExtensionsStructure();
    SiriDistanceExtension distances = new SiriDistanceExtension();
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setGroupingUsed(false);
    distances.setStopsFromCall(index);
    distances.setCallDistanceAlongRoute(NumberUtils.toDouble(df.format(distanceOfCallAlongTrip)));
    distances.setDistanceFromCall(NumberUtils.toDouble(df.format(distanceOfVehicleFromCall)));
    distances.setPresentableDistance(presentationService.getPresentableDistance(distances));
    long deviation = 0L;
    if (monitoredCallStructure.getExpectedArrivalTime() != null && monitoredCallStructure.getAimedArrivalTime() != null) {
        // get schedule deviation in milliseconds
        long deviationSeconds = monitoredCallStructure.getExpectedArrivalTime().getTime() - monitoredCallStructure.getAimedArrivalTime().getTime();
        deviation = Math.round(deviationSeconds / (1000.0 * 60.0));
    }
    wrapper.setDeviation(String.valueOf(deviation));
    wrapper.setDistances(distances);
    distancesExtensions.setAny(wrapper);
    monitoredCallStructure.setExtensions(distancesExtensions);
    return monitoredCallStructure;
}
Also used : NaturalLanguageStringStructure(uk.org.siri.siri.NaturalLanguageStringStructure) DecimalFormat(java.text.DecimalFormat) SiriExtensionWrapper(org.onebusaway.transit_data_federation.siri.SiriExtensionWrapper) SiriDistanceExtension(org.onebusaway.transit_data_federation.siri.SiriDistanceExtension) StopPointRefStructure(uk.org.siri.siri.StopPointRefStructure) ExtensionsStructure(uk.org.siri.siri.ExtensionsStructure) MonitoredCallStructure(uk.org.siri.siri.MonitoredCallStructure) Date(java.util.Date)

Example 3 with SiriDistanceExtension

use of org.onebusaway.transit_data_federation.siri.SiriDistanceExtension in project onebusaway-application-modules by camsys.

the class SearchResultFactoryImpl method getPresentableDistance.

private String getPresentableDistance(MonitoredVehicleJourneyStructure journey, long updateTime, boolean isStopContext) {
    MonitoredCallStructure monitoredCall = journey.getMonitoredCall();
    SiriExtensionWrapper wrapper = (SiriExtensionWrapper) monitoredCall.getExtensions().getAny();
    SiriDistanceExtension distanceExtension = wrapper.getDistances();
    String message = "";
    String distance = "<strong>" + distanceExtension.getPresentableDistance() + "</strong>";
    NaturalLanguageStringStructure progressStatus = journey.getProgressStatus();
    // at terminal label only appears in stop results
    if (isStopContext && progressStatus != null && progressStatus.getValue().contains("layover")) {
        if (journey.getOriginAimedDepartureTime() != null) {
            DateFormat formatter = DateFormat.getTimeInstance(DateFormat.SHORT);
            if (journey.getOriginAimedDepartureTime().getTime() < new Date().getTime()) {
                message += "at terminal";
            } else {
                message += "at terminal, scheduled to depart " + formatter.format(journey.getOriginAimedDepartureTime());
            }
        } else {
            message += "at terminal";
        }
    } else if (isStopContext && progressStatus != null && progressStatus.getValue().contains("prevTrip")) {
        message += "+ scheduled layover at terminal";
    }
    int staleTimeout = _configurationService.getConfigurationValueAsInteger("display.staleTimeout", 120);
    long age = (SystemTime.currentTimeMillis() - updateTime) / 1000;
    if (age > staleTimeout) {
        if (message.length() > 0) {
            message += ", ";
        }
        message += "old data";
    }
    if (message.length() > 0)
        return distance + " (" + message + ")";
    else
        return distance;
}
Also used : NaturalLanguageStringStructure(uk.org.siri.siri.NaturalLanguageStringStructure) DateFormat(java.text.DateFormat) SiriExtensionWrapper(org.onebusaway.transit_data_federation.siri.SiriExtensionWrapper) SiriDistanceExtension(org.onebusaway.transit_data_federation.siri.SiriDistanceExtension) MonitoredCallStructure(uk.org.siri.siri.MonitoredCallStructure) Date(java.util.Date)

Example 4 with SiriDistanceExtension

use of org.onebusaway.transit_data_federation.siri.SiriDistanceExtension in project onebusaway-application-modules by camsys.

the class SearchResultFactoryImpl method getPresentableTime.

private String getPresentableTime(MonitoredVehicleJourneyStructure journey, long updateTime, boolean isStopContext) {
    NaturalLanguageStringStructure progressStatus = journey.getProgressStatus();
    MonitoredCallStructure monitoredCall = journey.getMonitoredCall();
    if (!isStopContext) {
        return null;
    }
    int staleTimeout = _configurationService.getConfigurationValueAsInteger("display.staleTimeout", 120);
    long age = (SystemTime.currentTimeMillis() - updateTime) / 1000;
    if (age > staleTimeout) {
        return null;
    }
    if (monitoredCall.getExpectedArrivalTime() != null) {
        long predictedArrival = monitoredCall.getExpectedArrivalTime().getTime();
        SiriExtensionWrapper wrapper = (SiriExtensionWrapper) monitoredCall.getExtensions().getAny();
        SiriDistanceExtension distanceExtension = wrapper.getDistances();
        String distance = distanceExtension.getPresentableDistance();
        double minutes = Math.floor((predictedArrival - updateTime) / 60 / 1000);
        String timeString = Math.round(minutes) + " minute" + ((Math.abs(minutes) != 1) ? "s" : "");
        if (progressStatus != null && progressStatus.getValue().contains("prevTrip")) {
            return timeString;
        } else {
            return "<strong>" + timeString + "</strong>" + ", " + distance;
        }
    }
    return null;
}
Also used : NaturalLanguageStringStructure(uk.org.siri.siri.NaturalLanguageStringStructure) SiriExtensionWrapper(org.onebusaway.transit_data_federation.siri.SiriExtensionWrapper) SiriDistanceExtension(org.onebusaway.transit_data_federation.siri.SiriDistanceExtension) MonitoredCallStructure(uk.org.siri.siri.MonitoredCallStructure)

Example 5 with SiriDistanceExtension

use of org.onebusaway.transit_data_federation.siri.SiriDistanceExtension in project onebusaway-application-modules by camsys.

the class SiriSupport method getOnwardCallStructure.

private static OnwardCallStructure getOnwardCallStructure(StopBean stopBean, PresentationService presentationService, double distanceOfCallAlongTrip, double distanceOfVehicleFromCall, int visitNumber, int index, TimepointPredictionRecord prediction, boolean hasRealtimeData, long responseTimestamp, long scheduledArrivalTime) {
    OnwardCallStructure onwardCallStructure = new OnwardCallStructure();
    onwardCallStructure.setVisitNumber(BigInteger.valueOf(visitNumber));
    StopPointRefStructure stopPointRef = new StopPointRefStructure();
    stopPointRef.setValue(stopBean.getId());
    onwardCallStructure.setStopPointRef(stopPointRef);
    if (stopBean.getCode() != null) {
        // Agency's prefer stop code display in UI, so override platform name for this use
        NaturalLanguageStringStructure platform = new NaturalLanguageStringStructure();
        platform.setValue(stopBean.getCode());
        onwardCallStructure.setArrivalPlatformName(platform);
    }
    NaturalLanguageStringStructure stopPoint = new NaturalLanguageStringStructure();
    stopPoint.setValue(stopBean.getName());
    onwardCallStructure.setStopPointName(stopPoint);
    if (prediction != null) {
        if (prediction.getTimepointPredictedArrivalTime() < responseTimestamp) {
            onwardCallStructure.setExpectedArrivalTime(new Date(responseTimestamp));
            onwardCallStructure.setExpectedDepartureTime(new Date(responseTimestamp));
        } else {
            onwardCallStructure.setExpectedArrivalTime(new Date(prediction.getTimepointPredictedArrivalTime()));
            onwardCallStructure.setExpectedDepartureTime(new Date(prediction.getTimepointPredictedDepartureTime()));
        }
    } else if (!hasRealtimeData) {
        _log.debug("using arrival time of " + new Date(scheduledArrivalTime));
        onwardCallStructure.setExpectedArrivalTime(new Date(scheduledArrivalTime));
        onwardCallStructure.setExpectedDepartureTime(new Date(scheduledArrivalTime));
    }
    // siri extensions
    SiriExtensionWrapper wrapper = new SiriExtensionWrapper();
    ExtensionsStructure distancesExtensions = new ExtensionsStructure();
    SiriDistanceExtension distances = new SiriDistanceExtension();
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setGroupingUsed(false);
    distances.setStopsFromCall(index);
    distances.setCallDistanceAlongRoute(NumberUtils.toDouble(df.format(distanceOfCallAlongTrip)));
    distances.setDistanceFromCall(NumberUtils.toDouble(df.format(distanceOfVehicleFromCall)));
    distances.setPresentableDistance(presentationService.getPresentableDistance(distances));
    wrapper.setDistances(distances);
    distancesExtensions.setAny(wrapper);
    onwardCallStructure.setExtensions(distancesExtensions);
    return onwardCallStructure;
}
Also used : NaturalLanguageStringStructure(uk.org.siri.siri.NaturalLanguageStringStructure) DecimalFormat(java.text.DecimalFormat) SiriExtensionWrapper(org.onebusaway.transit_data_federation.siri.SiriExtensionWrapper) SiriDistanceExtension(org.onebusaway.transit_data_federation.siri.SiriDistanceExtension) StopPointRefStructure(uk.org.siri.siri.StopPointRefStructure) OnwardCallStructure(uk.org.siri.siri.OnwardCallStructure) ExtensionsStructure(uk.org.siri.siri.ExtensionsStructure) Date(java.util.Date)

Aggregations

SiriDistanceExtension (org.onebusaway.transit_data_federation.siri.SiriDistanceExtension)5 SiriExtensionWrapper (org.onebusaway.transit_data_federation.siri.SiriExtensionWrapper)5 NaturalLanguageStringStructure (uk.org.siri.siri.NaturalLanguageStringStructure)5 Date (java.util.Date)4 MonitoredCallStructure (uk.org.siri.siri.MonitoredCallStructure)4 ExtensionsStructure (uk.org.siri.siri.ExtensionsStructure)3 DecimalFormat (java.text.DecimalFormat)2 StopPointRefStructure (uk.org.siri.siri.StopPointRefStructure)2 DateFormat (java.text.DateFormat)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Matchers.anyString (org.mockito.Matchers.anyString)1 StopResult (org.onebusaway.enterprise.webapp.actions.m.model.StopResult)1 NameBean (org.onebusaway.transit_data.model.NameBean)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 DirectionRefStructure (uk.org.siri.siri.DirectionRefStructure)1