use of org.onebusaway.api.actions.siri.model.DetailLevel 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;
}
use of org.onebusaway.api.actions.siri.model.DetailLevel in project onebusaway-application-modules by camsys.
the class StopPointsV2Action method index.
public DefaultHttpHeaders index() throws IOException {
long responseTimestamp = getTime();
processGoogleAnalytics();
_realtimeService.setTime(responseTimestamp);
boolean useLineRefOnly = false;
Boolean upcomingServiceAllStops = null;
CoordinateBounds bounds = null;
boolean validBoundDistance = true;
// User Parameters
String boundingBox = _request.getParameter(BOUNDING_BOX);
String circle = _request.getParameter(CIRCLE);
String lineRef = _request.getParameter(LINE_REF);
String directionId = _request.getParameter(DIRECTION_REF);
String agencyId = _request.getParameter(OPERATOR_REF);
String hasUpcomingScheduledService = _request.getParameter(UPCOMING_SCHEDULED_SERVICE);
String detailLevelParam = _request.getParameter(STOP_POINTS_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;
}
// Error Strings
String routeIdsErrorString = "";
String boundsErrorString = "";
/*
* 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 = processAgencyIds(agencyId);
List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
routeIdsErrorString = processRouteIds(lineRef, routeIds, agencyIds);
// Calculate Bounds
try {
if (StringUtils.isNotBlank(circle)) {
bounds = getCircleBounds(circle);
if (bounds != null && !isValidBoundsDistance(bounds, MAX_BOUNDS_RADIUS)) {
boundsErrorString += "Provided values exceed allowed search radius of " + MAX_BOUNDS_RADIUS + "m ";
validBoundDistance = false;
}
} else if (StringUtils.isNotBlank(boundingBox)) {
bounds = getBoxBounds(boundingBox);
if (bounds != null && !isValidBoundBoxDistance(bounds, MAX_BOUNDS_DISTANCE)) {
boundsErrorString += "Provided values exceed allowed search distance of " + MAX_BOUNDS_DISTANCE + "m ";
validBoundDistance = false;
}
}
} catch (NumberFormatException nfe) {
boundsErrorString += ERROR_NON_NUMERIC;
}
// Check for case where only LineRef was provided
if (bounds == null) {
if (routeIds.size() > 0) {
useLineRefOnly = true;
} else {
boundsErrorString += ERROR_REQUIRED_PARAMS;
}
}
// Setup Filters
Map<Filters, String> filters = new HashMap<Filters, String>();
filters.put(Filters.DIRECTION_REF, directionId);
filters.put(Filters.UPCOMING_SCHEDULED_SERVICE, hasUpcomingScheduledService);
// Annotated Stop Points
List<AnnotatedStopPointStructure> stopPoints = new ArrayList<AnnotatedStopPointStructure>();
Map<Boolean, List<AnnotatedStopPointStructure>> stopPointsMap;
// Error Handler
Exception error = null;
if ((bounds == null && !useLineRefOnly) || (_request.getParameter(LINE_REF) != null && routeIds.size() == 0) || !validBoundDistance) {
String errorString = (boundsErrorString + " " + routeIdsErrorString).trim();
error = new Exception(errorString);
} else {
if (useLineRefOnly) {
stopPointsMap = _realtimeService.getAnnotatedStopPointStructures(agencyIds, routeIds, detailLevel, responseTimestamp, filters);
} else {
stopPointsMap = _realtimeService.getAnnotatedStopPointStructures(bounds, agencyIds, routeIds, detailLevel, responseTimestamp, filters);
}
for (Map.Entry<Boolean, List<AnnotatedStopPointStructure>> entry : stopPointsMap.entrySet()) {
if (entry.getValue().size() > 0)
upcomingServiceAllStops = entry.getKey();
stopPoints.addAll(entry.getValue());
}
}
_response = generateSiriResponse(stopPoints, upcomingServiceAllStops, error, responseTimestamp);
try {
this._servletResponse.getWriter().write(getStopPoints());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
use of org.onebusaway.api.actions.siri.model.DetailLevel 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;
}
use of org.onebusaway.api.actions.siri.model.DetailLevel in project onebusaway-application-modules by camsys.
the class RealtimeServiceTest method testStopPointsByBounds.
@Test
public void testStopPointsByBounds() throws Exception {
// MOCKS
// Coordinate Bounds
CoordinateBounds bounds = new CoordinateBounds(Double.parseDouble("47.612813"), Double.parseDouble("-122.339662"), Double.parseDouble("47.608813"), Double.parseDouble("-122.337662"));
// Stops For Bounds
StopsBean stopsBean = new StopsBean();
stopsBean.setStops(stops);
when(transitDataService.getRouteForId("1_100194")).thenReturn(routeBean);
when(transitDataService.getStops(any(SearchQueryBean.class))).thenReturn(stopsBean);
when(transitDataService.getStopsForRoute("1_100194")).thenReturn(stopsForRouteBean);
when(transitDataService.stopHasUpcomingScheduledService(anyString(), anyLong(), anyString(), anyString(), anyString())).thenReturn(true);
// EXPECTED
LineDirectionStructure lds = new LineDirectionStructure();
DirectionRefStructure drs = new DirectionRefStructure();
LineRefStructure lrs = new LineRefStructure();
lds.setDirectionRef(drs);
lds.setLineRef(lrs);
drs.setValue("0");
lrs.setValue("1_100194");
LocationStructure ls = new LocationStructure();
BigDecimal lat = new BigDecimal(47.610813);
BigDecimal lon = new BigDecimal(-122.338662);
ls.setLatitude(lat.setScale(6, BigDecimal.ROUND_HALF_DOWN));
ls.setLongitude(lon.setScale(6, BigDecimal.ROUND_HALF_DOWN));
NaturalLanguageStringStructure stopName = new NaturalLanguageStringStructure();
stopName.setValue("3rd Ave & Pine St");
List<NaturalLanguageStringStructure> stopNames = new ArrayList<NaturalLanguageStringStructure>();
stopNames.add(stopName);
StopPointRefStructure stopPointRef = new StopPointRefStructure();
stopPointRef.setValue("1_430");
Boolean monitored = true;
AnnotatedStopPointStructure mockStopPoint = new AnnotatedStopPointStructure();
mockStopPoint.setLines(new AnnotatedStopPointStructure.Lines());
mockStopPoint.getLines().getLineRefOrLineDirection().add(lds);
mockStopPoint.setLocation(ls);
mockStopPoint.getStopName().add(stopName);
mockStopPoint.setStopPointRef(stopPointRef);
mockStopPoint.setMonitored(monitored);
// REALTIME ARGUMENTS
// Agency Ids
List<String> agencyIds = new ArrayList<String>();
String agencyId = "1";
agencyIds.add(agencyId);
// Route Ids
List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
AgencyAndId routeId = AgencyAndIdLibrary.convertFromString("1_100194");
routeIds.add(routeId);
// Detail Level
DetailLevel detailLevel = DetailLevel.NORMAL;
// Time
long time = System.currentTimeMillis();
// Filters
Map<Filters, String> filters = new HashMap<Filters, String>();
Map<Boolean, List<AnnotatedStopPointStructure>> actualResult = realtimeService.getAnnotatedStopPointStructures(bounds, agencyIds, routeIds, detailLevel, time, filters);
AnnotatedStopPointStructure actualStopPoint = actualResult.get(true).get(0);
assertTrue(isEqual(mockStopPoint, actualStopPoint));
}
use of org.onebusaway.api.actions.siri.model.DetailLevel in project onebusaway-application-modules by camsys.
the class RealtimeServiceTest method testStopPointsByRoute.
@Test
public void testStopPointsByRoute() throws Exception {
when(transitDataService.getRouteForId("1_100194")).thenReturn(routeBean);
when(transitDataService.getStopsForRoute("1_100194")).thenReturn(stopsForRouteBean);
when(transitDataService.stopHasUpcomingScheduledService(anyString(), anyLong(), anyString(), anyString(), anyString())).thenReturn(true);
LineDirectionStructure lds = new LineDirectionStructure();
DirectionRefStructure drs = new DirectionRefStructure();
LineRefStructure lrs = new LineRefStructure();
lds.setDirectionRef(drs);
lds.setLineRef(lrs);
drs.setValue("0");
lrs.setValue("1_100194");
LocationStructure ls = new LocationStructure();
BigDecimal lat = new BigDecimal(47.610813);
BigDecimal lon = new BigDecimal(-122.338662);
ls.setLatitude(lat.setScale(6, BigDecimal.ROUND_HALF_DOWN));
ls.setLongitude(lon.setScale(6, BigDecimal.ROUND_HALF_DOWN));
NaturalLanguageStringStructure stopName = new NaturalLanguageStringStructure();
stopName.setValue("3rd Ave & Pine St");
List<NaturalLanguageStringStructure> stopNames = new ArrayList<NaturalLanguageStringStructure>();
stopNames.add(stopName);
StopPointRefStructure stopPointRef = new StopPointRefStructure();
stopPointRef.setValue("1_430");
Boolean monitored = true;
AnnotatedStopPointStructure mockStopPoint = new AnnotatedStopPointStructure();
mockStopPoint.setLines(new AnnotatedStopPointStructure.Lines());
mockStopPoint.getLines().getLineRefOrLineDirection().add(lds);
mockStopPoint.setLocation(ls);
mockStopPoint.getStopName().add(stopName);
mockStopPoint.setStopPointRef(stopPointRef);
mockStopPoint.setMonitored(monitored);
// REALTIME ARGUMENTS
// Agency Ids
List<String> agencyIds = new ArrayList<String>();
String agencyId = "1";
agencyIds.add(agencyId);
// Route Ids
List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
AgencyAndId routeId = AgencyAndIdLibrary.convertFromString("1_100194");
routeIds.add(routeId);
// Detail Level
DetailLevel detailLevel = DetailLevel.NORMAL;
// Time
long time = System.currentTimeMillis();
// Filters
Map<Filters, String> filters = new HashMap<Filters, String>();
Map<Boolean, List<AnnotatedStopPointStructure>> actualResult = realtimeService.getAnnotatedStopPointStructures(agencyIds, routeIds, detailLevel, time, filters);
AnnotatedStopPointStructure actualStopPoint = actualResult.get(true).get(0);
assertTrue(isEqual(mockStopPoint, actualStopPoint));
}
Aggregations