Search in sources :

Example 6 with Body

use of org.onebusaway.nextbus.model.nextbus.Body in project onebusaway-application-modules by camsys.

the class ScheduleAction method getModel.

@Override
public Body<ScheduleRoute> getModel() {
    Body<ScheduleRoute> body = new Body<ScheduleRoute>();
    List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
    if (this.isValid(body, routeIds)) {
        AgencyBean agency = _transitDataService.getAgency(agencyId);
        List<HashMap<String, HashSet<ScheduleStop>>> blockStopsMapList = new ArrayList<HashMap<String, HashSet<ScheduleStop>>>();
        blockStopsMapList.add(new HashMap<String, HashSet<ScheduleStop>>());
        blockStopsMapList.add(new HashMap<String, HashSet<ScheduleStop>>());
        blockStopsMapList.add(new HashMap<String, HashSet<ScheduleStop>>());
        blockStopsMapList.add(new HashMap<String, HashSet<ScheduleStop>>());
        // Get All the Stops for a specific route
        for (AgencyAndId routeId : routeIds) {
            String route = AgencyAndId.convertToString(routeId);
            StopsForRouteBean stopsForRoute = _service.getStopsForRoute(route);
            for (StopGroupingBean stopGroupingBean : stopsForRoute.getStopGroupings()) {
                for (StopGroupBean stopGroupBean : stopGroupingBean.getStopGroups()) {
                    // Weekday Trips
                    for (Long weekdayTime : DateUtil.getWeekdayDateTimes(agency.getTimezone())) {
                        ArrivalsAndDeparturesQueryBean query = new ArrivalsAndDeparturesQueryBean();
                        query.setTime(weekdayTime);
                        query.setMinutesBefore(0);
                        query.setMinutesAfter(MINUTES_IN_DAY);
                        StopsWithArrivalsAndDeparturesBean stopsWithArrivals = _service.getStopsWithArrivalsAndDepartures(stopGroupBean.getStopIds(), query);
                        for (ArrivalAndDepartureBean arrivalsAndDeparture : stopsWithArrivals.getArrivalsAndDepartures()) {
                            // Filter Arrivals and Departures By Route
                            if (arrivalsAndDeparture.getTrip().getRoute().getId().equals(route)) {
                                ScheduleStop scheduleStop = new ScheduleStop();
                                scheduleStop.setTag(getIdNoAgency(arrivalsAndDeparture.getStop().getId()));
                                scheduleStop.setEpochTime(arrivalsAndDeparture.getScheduledArrivalTime());
                                scheduleStop.setStopName(arrivalsAndDeparture.getStop().getName());
                                if (arrivalsAndDeparture.getTrip().getDirectionId().equals("0")) {
                                    addStopByBlockId(blockStopsMapList.get(0), arrivalsAndDeparture.getTrip().getBlockId(), scheduleStop);
                                } else if (arrivalsAndDeparture.getTrip().getDirectionId().equals("1")) {
                                    addStopByBlockId(blockStopsMapList.get(1), arrivalsAndDeparture.getTrip().getBlockId(), scheduleStop);
                                }
                            }
                        }
                    }
                    // Weekend Trips
                    for (Long weekendTime : DateUtil.getWeekendDateTimes(agency.getTimezone())) {
                        ArrivalsAndDeparturesQueryBean query = new ArrivalsAndDeparturesQueryBean();
                        query.setTime(weekendTime);
                        query.setMinutesBefore(0);
                        query.setMinutesAfter(MINUTES_IN_DAY);
                        StopsWithArrivalsAndDeparturesBean stopsWithArrivals = _service.getStopsWithArrivalsAndDepartures(stopGroupBean.getStopIds(), query);
                        for (ArrivalAndDepartureBean arrivalsAndDeparture : stopsWithArrivals.getArrivalsAndDepartures()) {
                            // Filter Arrivals and Departures By Route
                            if (arrivalsAndDeparture.getTrip().getRoute().getId().equals(route)) {
                                ScheduleStop scheduleStop = new ScheduleStop();
                                scheduleStop.setTag(getIdNoAgency(arrivalsAndDeparture.getStop().getId()));
                                scheduleStop.setEpochTime(arrivalsAndDeparture.getScheduledArrivalTime());
                                scheduleStop.setStopName(arrivalsAndDeparture.getStop().getName());
                                if (arrivalsAndDeparture.getTrip().getDirectionId().equals("0")) {
                                    addStopByBlockId(blockStopsMapList.get(2), arrivalsAndDeparture.getTrip().getBlockId(), scheduleStop);
                                } else if (arrivalsAndDeparture.getTrip().getDirectionId().equals("1")) {
                                    addStopByBlockId(blockStopsMapList.get(3), arrivalsAndDeparture.getTrip().getBlockId(), scheduleStop);
                                }
                            }
                        }
                    }
                }
            }
            // Routes
            for (int n = 0; n < blockStopsMapList.size(); n++) {
                HashMap<String, HashSet<ScheduleStop>> blockStopsMap = blockStopsMapList.get(n);
                ScheduleRoute scheduleRoute = new ScheduleRoute();
                scheduleRoute.setTitle(stopsForRoute.getRoute().getLongName());
                scheduleRoute.setDirection(Integer.toString(n % 2));
                scheduleRoute.setTag(getIdNoAgency(stopsForRoute.getRoute().getId()));
                scheduleRoute.setServiceClass(n < 3 ? "wkd" : "wkend");
                // Blocks
                for (Entry<String, HashSet<ScheduleStop>> entry : blockStopsMap.entrySet()) {
                    int tripStopTimeCounter = 0;
                    String blockId = entry.getKey();
                    HashSet<ScheduleStop> blockStops = entry.getValue();
                    ScheduleTableRow scheduleTr = new ScheduleTableRow(getIdNoAgency(blockId));
                    // Stop Times
                    for (ScheduleStop stop : blockStops) {
                        if (tripStopTimeCounter == 0) {
                            DisplayStop displayStop = new DisplayStop();
                            displayStop.setTag(stop.getTag());
                            displayStop.setValue(stop.getStopName());
                            scheduleRoute.getStops().add(displayStop);
                        }
                        // scheduleStop.setValue(value);
                        scheduleTr.getStops().add(stop);
                    }
                    scheduleRoute.getScheduleTableRow().add(scheduleTr);
                }
                body.getResponse().add(scheduleRoute);
            }
        }
    }
    return body;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) StopGroupBean(org.onebusaway.transit_data.model.StopGroupBean) ArrayList(java.util.ArrayList) ScheduleStop(org.onebusaway.nextbus.model.nextbus.ScheduleStop) StopsForRouteBean(org.onebusaway.transit_data.model.StopsForRouteBean) StopsWithArrivalsAndDeparturesBean(org.onebusaway.transit_data.model.StopsWithArrivalsAndDeparturesBean) ArrivalsAndDeparturesQueryBean(org.onebusaway.transit_data.model.ArrivalsAndDeparturesQueryBean) ScheduleTableRow(org.onebusaway.nextbus.model.nextbus.ScheduleTableRow) DisplayStop(org.onebusaway.nextbus.model.nextbus.DisplayStop) Body(org.onebusaway.nextbus.model.nextbus.Body) AgencyBean(org.onebusaway.transit_data.model.AgencyBean) HashSet(java.util.HashSet) ArrivalAndDepartureBean(org.onebusaway.transit_data.model.ArrivalAndDepartureBean) StopGroupingBean(org.onebusaway.transit_data.model.StopGroupingBean) ScheduleRoute(org.onebusaway.nextbus.model.nextbus.ScheduleRoute)

Example 7 with Body

use of org.onebusaway.nextbus.model.nextbus.Body in project onebusaway-application-modules by camsys.

the class ScheduleHorizAction method getModel.

public Body<List<ScheduleRoute>> getModel() {
    Body<List<ScheduleRoute>> body = new Body<List<ScheduleRoute>>();
    List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
    if (isValid(body, routeIds)) {
        String serviceUrl = getServiceUrl() + agencyId + SCHEDULE_COMMAND + "?";
        String route = "r=" + getIdNoAgency(routeId);
        String uri = serviceUrl + route + "&format=" + REQUEST_TYPE;
        try {
            int timeout = _configUtil.getHttpTimeoutSeconds();
            JsonArray scheduleJson = _httpUtil.getJsonObject(uri, timeout).getAsJsonArray("schedule");
            Type listType = new TypeToken<List<ScheduleRoute>>() {
            }.getType();
            List<ScheduleRoute> schedules = new Gson().fromJson(scheduleJson, listType);
            modifyJSONObject(schedules);
            body.getResponse().add(schedules);
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    }
    return body;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) ScheduleRoute(org.onebusaway.nextbus.model.transiTime.ScheduleRoute) ArrayList(java.util.ArrayList) List(java.util.List) Body(org.onebusaway.nextbus.model.nextbus.Body)

Example 8 with Body

use of org.onebusaway.nextbus.model.nextbus.Body in project onebusaway-application-modules by camsys.

the class VehicleLocationsAction method getModel.

@Override
public Body<Vehicle> getModel() {
    Body<Vehicle> body = new Body<Vehicle>();
    List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
    List<String> agencies = new ArrayList<String>();
    if (isValid(body)) {
        agencies.add(agencyId);
        processRouteIds(routeId, routeIds, agencies, body, false);
        // Valid Route Specified
        if (routeIds.size() > 0) {
            for (AgencyAndId routeId : routeIds) {
                body.getResponse().addAll(getVehiclesForRoute(routeId.toString(), getAllTripsForRoute(routeId.toString(), getT())));
            }
        } else // Invalid Route Specified, return results for first 100 results for agency
        {
            body.getResponse().addAll(getVehiclesForRoute(null, getAllTripsForAgency(agencyId, getT())));
        }
        body.setLastTime(new LastTime(SystemTime.currentTimeMillis()));
    }
    return body;
}
Also used : Vehicle(org.onebusaway.nextbus.model.nextbus.Vehicle) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) Body(org.onebusaway.nextbus.model.nextbus.Body) LastTime(org.onebusaway.nextbus.model.nextbus.LastTime)

Example 9 with Body

use of org.onebusaway.nextbus.model.nextbus.Body in project onebusaway-application-modules by camsys.

the class PredictionsAction method getModel.

public Body<Predictions> getModel() {
    Body<Predictions> body = new Body<Predictions>();
    List<AgencyAndId> stopIds = new ArrayList<AgencyAndId>();
    List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
    if (isValid(body, stopIds, routeIds)) {
        String serviceUrl = getServiceUrl() + agencyId + PREDICTIONS_COMMAND + "?";
        String routeStop = "";
        for (AgencyAndId routeId : routeIds) {
            routeStop += "rs=" + getIdNoAgency(routeId.toString()) + "|" + getIdNoAgency(stopId) + "&";
        }
        String uri = serviceUrl + routeStop + "format=" + REQUEST_TYPE;
        _log.info(uri);
        try {
            int timeout = _configUtil.getHttpTimeoutSeconds();
            JsonArray predictionsJson = _httpUtil.getJsonObject(uri, timeout).getAsJsonArray("predictions");
            Type listType = new TypeToken<List<Predictions>>() {
            }.getType();
            List<Predictions> predictions = new Gson().fromJson(predictionsJson, listType);
            modifyJSONObject(predictions);
            body.getResponse().addAll(predictions);
        } catch (Exception e) {
            body.getErrors().add(new BodyError("No valid results found."));
            _log.error(e.getMessage());
        }
    }
    return body;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) BodyError(org.onebusaway.nextbus.model.nextbus.BodyError) Predictions(org.onebusaway.nextbus.model.transiTime.Predictions) ArrayList(java.util.ArrayList) List(java.util.List) Body(org.onebusaway.nextbus.model.nextbus.Body)

Aggregations

Body (org.onebusaway.nextbus.model.nextbus.Body)9 ArrayList (java.util.ArrayList)7 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)6 Gson (com.google.gson.Gson)3 JsonArray (com.google.gson.JsonArray)3 Type (java.lang.reflect.Type)3 List (java.util.List)3 BodyError (org.onebusaway.nextbus.model.nextbus.BodyError)3 ServiceException (org.onebusaway.exceptions.ServiceException)2 DisplayRoute (org.onebusaway.nextbus.model.nextbus.DisplayRoute)2 DisplayStop (org.onebusaway.nextbus.model.nextbus.DisplayStop)2 Predictions (org.onebusaway.nextbus.model.transiTime.Predictions)2 RouteBean (org.onebusaway.transit_data.model.RouteBean)2 StopGroupBean (org.onebusaway.transit_data.model.StopGroupBean)2 StopGroupingBean (org.onebusaway.transit_data.model.StopGroupingBean)2 StopsForRouteBean (org.onebusaway.transit_data.model.StopsForRouteBean)2 AlphanumComparator (org.onebusaway.util.comparators.AlphanumComparator)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)1