use of uk.org.siri.siri.MonitoredStopVisitStructure in project onebusaway-application-modules by camsys.
the class StopMonitoringAction method index.
public DefaultHttpHeaders index() throws IOException {
processGoogleAnalytics();
long responseTimestamp = getTime();
_realtimeService.setTime(responseTimestamp);
String directionId = _request.getParameter("DirectionRef");
// 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<String> agencyIds = new ArrayList<String>();
// Try to get the agency id passed by the user
String agencyId = _request.getParameter("OperatorRef");
if (agencyId != null) {
// The user provided an agency id so, use it
agencyIds.add(agencyId);
} else {
// They did not provide an agency id, so interpret that an any/all agencies.
Map<String, List<CoordinateBounds>> agencies = _transitDataService.getAgencyIdsWithCoverageArea();
agencyIds.addAll(agencies.keySet());
}
List<AgencyAndId> stopIds = new ArrayList<AgencyAndId>();
String stopIdsErrorString = "";
if (_request.getParameter("MonitoringRef") != null) {
try {
// If the user included an agency id as part of the stop id, ignore any OperatorRef arg
// or lack of OperatorRef arg and just use the included one.
AgencyAndId stopId = AgencyAndIdLibrary.convertFromString(_request.getParameter("MonitoringRef"));
if (isValidStop(stopId)) {
stopIds.add(stopId);
} else {
stopIdsErrorString += "No such stop: " + stopId.toString() + ".";
}
} catch (Exception e) {
// The user didn't provide an agency id in the MonitoringRef, so use our list of operator refs
for (String agency : agencyIds) {
AgencyAndId stopId = new AgencyAndId(agency, _request.getParameter("MonitoringRef"));
if (isValidStop(stopId)) {
stopIds.add(stopId);
} else {
stopIdsErrorString += "No such stop: " + stopId.toString() + ". ";
}
}
stopIdsErrorString = stopIdsErrorString.trim();
}
if (stopIds.size() > 0)
stopIdsErrorString = "";
} else {
stopIdsErrorString = "You must provide a MonitoringRef.";
}
List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
String routeIdsErrorString = "";
if (_request.getParameter("LineRef") != null) {
try {
// Same as above for stop id
AgencyAndId routeId = AgencyAndIdLibrary.convertFromString(_request.getParameter("LineRef"));
if (isValidRoute(routeId)) {
routeIds.add(routeId);
} else {
routeIdsErrorString += "No such route: " + routeId.toString() + ".";
}
} catch (Exception e) {
// Same as above for stop id
for (String agency : agencyIds) {
AgencyAndId routeId = new AgencyAndId(agency, _request.getParameter("LineRef"));
if (isValidRoute(routeId)) {
routeIds.add(routeId);
} else {
routeIdsErrorString += "No such route: " + routeId.toString() + ". ";
}
}
routeIdsErrorString = routeIdsErrorString.trim();
}
if (routeIds.size() > 0)
routeIdsErrorString = "";
}
String detailLevel = _request.getParameter("StopMonitoringDetailLevel");
int maximumOnwardCalls = 0;
if (detailLevel != null && detailLevel.equals("calls")) {
maximumOnwardCalls = Integer.MAX_VALUE;
try {
maximumOnwardCalls = Integer.parseInt(_request.getParameter("MaximumNumberOfCallsOnwards"));
} catch (NumberFormatException e) {
maximumOnwardCalls = Integer.MAX_VALUE;
}
}
int maximumStopVisits = Integer.MAX_VALUE;
try {
maximumStopVisits = Integer.parseInt(_request.getParameter("MaximumStopVisits"));
} catch (NumberFormatException e) {
maximumStopVisits = Integer.MAX_VALUE;
}
Integer minimumStopVisitsPerLine = null;
try {
minimumStopVisitsPerLine = Integer.parseInt(_request.getParameter("MinimumStopVisitsPerLine"));
} catch (NumberFormatException e) {
minimumStopVisitsPerLine = null;
}
// Monitored Stop Visits
List<MonitoredStopVisitStructure> visits = new ArrayList<MonitoredStopVisitStructure>();
Map<String, MonitoredStopVisitStructure> visitsMap = new HashMap<String, 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, responseTimestamp);
if (visitsForStop != null)
visits.addAll(visitsForStop);
}
List<MonitoredStopVisitStructure> filteredVisits = new ArrayList<MonitoredStopVisitStructure>();
Map<AgencyAndId, Integer> visitCountByLine = new HashMap<AgencyAndId, Integer>();
int visitCount = 0;
for (MonitoredStopVisitStructure visit : visits) {
MonitoredVehicleJourneyStructure journey = visit.getMonitoredVehicleJourney();
AgencyAndId thisRouteId = AgencyAndIdLibrary.convertFromString(journey.getLineRef().getValue());
String thisDirectionId = journey.getDirectionRef().getValue();
// user filtering
if (routeIds.size() > 0 && !routeIds.contains(thisRouteId))
continue;
if (thisDirectionId != null) {
if (directionId != null && !thisDirectionId.equals(directionId))
continue;
}
// 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 (visit.getMonitoredVehicleJourney() == null || visit.getMonitoredVehicleJourney().getVehicleRef() == null || StringUtils.isBlank(visit.getMonitoredVehicleJourney().getVehicleRef().getValue())) {
continue;
} else {
String visitKey = visit.getMonitoredVehicleJourney().getVehicleRef().getValue();
if (visitsMap.containsKey(visit.getMonitoredVehicleJourney().getVehicleRef().getValue())) {
if (visit.getMonitoredVehicleJourney().getProgressStatus() == null) {
visitsMap.remove(visitKey);
visitsMap.put(visitKey, visit);
}
continue;
} else {
visitsMap.put(visit.getMonitoredVehicleJourney().getVehicleRef().getValue(), visit);
}
}
filteredVisits.add(visit);
visitCount++;
visitCountForThisLine++;
visitCountByLine.put(thisRouteId, visitCountForThisLine);
}
visits = filteredVisits;
Exception error = null;
if (stopIds.size() == 0 || (_request.getParameter("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;
}
use of uk.org.siri.siri.MonitoredStopVisitStructure in project onebusaway-application-modules by camsys.
the class SearchResultFactoryImpl method getDisplayStringsByHeadsignForStopAndRouteAndDirection.
// stop view
private Map<String, List<StopOnRoute>> getDisplayStringsByHeadsignForStopAndRouteAndDirection(StopBean stopBean, RouteBean routeBean, StopGroupBean stopGroupBean) {
Map<String, List<StopOnRoute>> results = new HashMap<String, List<StopOnRoute>>();
// stop visits
List<MonitoredStopVisitStructure> visitList = _realtimeService.getMonitoredStopVisitsForStop(stopBean.getId(), 0, SystemTime.currentTimeMillis());
for (MonitoredStopVisitStructure visit : visitList) {
String routeId = visit.getMonitoredVehicleJourney().getLineRef().getValue();
if (!routeBean.getId().equals(routeId))
continue;
String directionId = visit.getMonitoredVehicleJourney().getDirectionRef().getValue();
if (directionId != null && !stopGroupBean.getId().equals(directionId))
continue;
// on detour?
MonitoredCallStructure monitoredCall = visit.getMonitoredVehicleJourney().getMonitoredCall();
if (monitoredCall == null)
continue;
if (!results.containsKey(visit.getMonitoredVehicleJourney().getDestinationName().getValue()))
results.put(visit.getMonitoredVehicleJourney().getDestinationName().getValue(), new ArrayList<StopOnRoute>());
if (results.get(visit.getMonitoredVehicleJourney().getDestinationName().getValue()).size() >= 3)
continue;
String distance = getPresentableDistance(visit.getMonitoredVehicleJourney(), visit.getRecordedAtTime().getTime(), true);
String timePrediction = getPresentableTime(visit.getMonitoredVehicleJourney(), visit.getRecordedAtTime().getTime(), true);
String vehicleId = null;
if (visit.getMonitoredVehicleJourney() != null && visit.getMonitoredVehicleJourney().getVehicleRef() != null) {
vehicleId = visit.getMonitoredVehicleJourney().getVehicleRef().getValue();
} else {
// insert an empty element so it aligns with distanceAways
vehicleId = "N/A";
}
List<String> distanceAways = new ArrayList<String>();
List<String> vehicleIds = new ArrayList<String>();
if (vehicleId.contains("_"))
vehicleId = vehicleId.split("_")[1];
vehicleIds.add(vehicleId);
if (timePrediction != null) {
distanceAways.add(timePrediction);
} else {
distanceAways.add(distance);
}
results.get(visit.getMonitoredVehicleJourney().getDestinationName().getValue()).add(new StopOnRoute(stopBean, distanceAways, visit.getMonitoredVehicleJourney().isMonitored(), vehicleIds));
}
return results;
}
use of uk.org.siri.siri.MonitoredStopVisitStructure 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;
}
use of uk.org.siri.siri.MonitoredStopVisitStructure in project onebusaway-application-modules by camsys.
the class RealtimeServiceImpl method getMonitoredStopVisitsForStop.
@Override
public List<MonitoredStopVisitStructure> getMonitoredStopVisitsForStop(String stopId, int maximumOnwardCalls, long currentTime) {
List<MonitoredStopVisitStructure> output = new ArrayList<MonitoredStopVisitStructure>();
for (ArrivalAndDepartureBean adBean : getArrivalsAndDeparturesForStop(stopId, currentTime)) {
TripStatusBean statusBeanForCurrentTrip = adBean.getTripStatus();
TripBean tripBeanForAd = adBean.getTrip();
final RouteBean routeBean = tripBeanForAd.getRoute();
if (statusBeanForCurrentTrip == null) {
_log.debug("status drop");
continue;
}
if (!_presentationService.include(statusBeanForCurrentTrip) || !_presentationService.include(adBean, statusBeanForCurrentTrip)) {
_log.debug("presentation drop for vehicle=" + statusBeanForCurrentTrip.getVehicleId());
continue;
}
if (!_transitDataService.stopHasRevenueServiceOnRoute((routeBean.getAgency() != null ? routeBean.getAgency().getId() : null), stopId, routeBean.getId(), adBean.getTrip().getDirectionId())) {
_log.debug("non reveunue drop");
continue;
}
// Filter out if the vehicle has realtime information and is ahead of current stop
if (statusBeanForCurrentTrip.isPredicted() && !(adBean.hasPredictedArrivalTime() || adBean.hasPredictedDepartureTime())) {
_log.debug("no realtime drop");
continue;
}
if (statusBeanForCurrentTrip.getVehicleId() != null) {
_log.debug("valid vehicle " + statusBeanForCurrentTrip.getVehicleId());
}
MonitoredStopVisitStructure stopVisit = new MonitoredStopVisitStructure();
// Check for Realtime Data
if (!statusBeanForCurrentTrip.isPredicted()) {
stopVisit.setRecordedAtTime(new Date(getTime()));
} else {
stopVisit.setRecordedAtTime(new Date(statusBeanForCurrentTrip.getLastUpdateTime()));
}
List<TimepointPredictionRecord> timePredictionRecords = null;
timePredictionRecords = createTimePredictionRecordsForStop(adBean, stopId);
stopVisit.setMonitoredVehicleJourney(new MonitoredVehicleJourneyStructure());
SiriSupport.fillMonitoredVehicleJourney(stopVisit.getMonitoredVehicleJourney(), tripBeanForAd, statusBeanForCurrentTrip, adBean.getStop(), OnwardCallsMode.STOP_MONITORING, _presentationService, _transitDataService, maximumOnwardCalls, timePredictionRecords, statusBeanForCurrentTrip.isPredicted(), currentTime, false);
output.add(stopVisit);
}
Collections.sort(output, new Comparator<MonitoredStopVisitStructure>() {
public int compare(MonitoredStopVisitStructure arg0, MonitoredStopVisitStructure arg1) {
try {
Date expectedArrival0 = arg0.getMonitoredVehicleJourney().getMonitoredCall().getExpectedArrivalTime();
Date expectedArrival1 = arg1.getMonitoredVehicleJourney().getMonitoredCall().getExpectedArrivalTime();
return expectedArrival0.compareTo(expectedArrival1);
} catch (Exception e) {
return -1;
}
}
});
return output;
}
use of uk.org.siri.siri.MonitoredStopVisitStructure in project onebusaway-application-modules by camsys.
the class StopForIdAction method execute.
@Override
public String execute() {
if (_stopId == null) {
return SUCCESS;
}
StopBean stop = _transitDataService.getStop(_stopId);
if (stop == null) {
return SUCCESS;
}
List<RouteAtStop> routesAtStop = new ArrayList<RouteAtStop>();
for (RouteBean routeBean : stop.getRoutes()) {
StopsForRouteBean stopsForRoute = _transitDataService.getStopsForRoute(routeBean.getId());
List<RouteDirection> routeDirections = new ArrayList<RouteDirection>();
List<StopGroupingBean> stopGroupings = stopsForRoute.getStopGroupings();
for (StopGroupingBean stopGroupingBean : stopGroupings) {
for (StopGroupBean stopGroupBean : stopGroupingBean.getStopGroups()) {
if (_transitDataService.stopHasRevenueServiceOnRoute((routeBean.getAgency() != null ? routeBean.getAgency().getId() : null), _stopId, routeBean.getId(), stopGroupBean.getId())) {
NameBean name = stopGroupBean.getName();
String type = name.getType();
if (!type.equals("destination"))
continue;
// filter out route directions that don't stop at this stop
if (!stopGroupBean.getStopIds().contains(_stopId))
continue;
Boolean hasUpcomingScheduledService = _transitDataService.stopHasUpcomingScheduledService((routeBean.getAgency() != null ? routeBean.getAgency().getId() : null), SystemTime.currentTimeMillis(), stop.getId(), routeBean.getId(), stopGroupBean.getId());
// if there are buses on route, always have "scheduled service"
Boolean routeHasVehiclesInService = true;
if (routeHasVehiclesInService) {
hasUpcomingScheduledService = true;
}
routeDirections.add(new RouteDirection(stopGroupBean, null, null, hasUpcomingScheduledService));
}
}
}
RouteAtStop routeAtStop = new RouteAtStop(routeBean, routeDirections);
routesAtStop.add(routeAtStop);
}
_result = new StopResult(stop, routesAtStop);
List<MonitoredStopVisitStructure> visits = _realtimeService.getMonitoredStopVisitsForStop(_stopId, 0, SystemTime.currentTimeMillis());
_response = generateSiriResponse(visits, AgencyAndIdLibrary.convertFromString(_stopId));
return SUCCESS;
}
Aggregations