Search in sources :

Example 1 with Predictions

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

the class PredictionsForMultiStopsAction method getModel.

public Body<Predictions> getModel() {
    Body<Predictions> body = new Body<Predictions>();
    if (isValid(body)) {
        String serviceUrl = getServiceUrl() + agencyId + PREDICTIONS_COMMAND + "?";
        String routeStopIds = getStopParams();
        String uri = serviceUrl + routeStopIds + "format=" + REQUEST_TYPE;
        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."));
        }
    }
    return body;
}
Also used : Gson(com.google.gson.Gson) ServiceException(org.onebusaway.exceptions.ServiceException) 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)

Example 2 with Predictions

use of org.onebusaway.nextbus.model.transiTime.Predictions 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

Gson (com.google.gson.Gson)2 JsonArray (com.google.gson.JsonArray)2 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Body (org.onebusaway.nextbus.model.nextbus.Body)2 BodyError (org.onebusaway.nextbus.model.nextbus.BodyError)2 Predictions (org.onebusaway.nextbus.model.transiTime.Predictions)2 ServiceException (org.onebusaway.exceptions.ServiceException)1 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1