Search in sources :

Example 1 with VehicleLastKnownRecord

use of org.onebusaway.admin.model.json.VehicleLastKnownRecord in project onebusaway-application-modules by camsys.

the class VehicleStatusServiceImpl method getLastKnownRecordData.

private VehicleLastKnownRecord getLastKnownRecordData(String vehicleId) {
    List<VehicleLastKnownRecord> lastKnownRecords = new ArrayList<VehicleLastKnownRecord>();
    String operationalAPIHost = null;
    try {
        operationalAPIHost = configurationService.getConfigurationValueAsString("operational-api.host", DEFAULT_OPERATIONAL_API_HOST);
    } catch (RemoteConnectFailureException e) {
        log.error("Failed retrieving operational API host from TDM. Setting to default value");
        operationalAPIHost = DEFAULT_OPERATIONAL_API_HOST;
    }
    String url = buildURL(operationalAPIHost, "/record/last-known/vehicle/" + vehicleId);
    log.info("making request for : " + url);
    String lastknownContent = remoteConnectionService.getContent(url);
    lastknownContent = lastknownContent.replace(System.getProperty("line.separator"), "");
    String json = extractJsonArrayString(lastknownContent);
    try {
        JSONArray lastKnownContentArray = new JSONArray("[" + json + "]");
        for (int i = 0; i < lastKnownContentArray.length(); i++) {
            VehicleLastKnownRecord lastKnownRecord = convertToObject(lastKnownContentArray.getString(i), VehicleLastKnownRecord.class);
            // lastknownrecord can be null if no data is returned by web service call
            if (lastKnownRecord != null) {
                lastKnownRecords.add(lastKnownRecord);
            }
        }
    } catch (JSONException e) {
        log.error("Error parsing json content : " + e);
        e.printStackTrace();
    }
    if (!lastKnownRecords.isEmpty())
        return lastKnownRecords.get(0);
    return null;
}
Also used : RemoteConnectFailureException(org.springframework.remoting.RemoteConnectFailureException) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) VehicleLastKnownRecord(org.onebusaway.admin.model.json.VehicleLastKnownRecord)

Example 2 with VehicleLastKnownRecord

use of org.onebusaway.admin.model.json.VehicleLastKnownRecord in project onebusaway-application-modules by camsys.

the class VehicleStatusServiceImpl method getVehicleStatus.

@Override
public List<VehicleStatus> getVehicleStatus(boolean loadNew) {
    List<VehicleStatus> vehicleStatusRecords = null;
    // Load new data only if asked explicitly
    if (loadNew) {
        VehicleStatusBuilder builder = new VehicleStatusBuilder();
        // get last known record data from operational API
        List<VehicleLastKnownRecord> vehicleLastKnownRecords = getLastKnownRecordData();
        // get vehicle pipo data
        Map<String, VehiclePullout> vehiclePullouts = getPulloutData();
        vehicleStatusRecords = new ArrayList<VehicleStatus>();
        // Build vehicle status objects by getting the required fields from both collections
        for (VehicleLastKnownRecord lastknownRecord : vehicleLastKnownRecords) {
            VehiclePullout pullout = vehiclePullouts.get(lastknownRecord.getVehicleId());
            VehicleStatus vehicleStatus = builder.buildVehicleStatus(pullout, lastknownRecord);
            vehicleStatusRecords.add(vehicleStatus);
        }
        // Add these records to the cache
        cache.add(vehicleStatusRecords);
    } else {
        // return data from the cache to improve performance
        vehicleStatusRecords = cache.fetch();
    }
    return vehicleStatusRecords;
}
Also used : VehicleStatusBuilder(org.onebusaway.admin.util.VehicleStatusBuilder) VehiclePullout(org.onebusaway.admin.model.json.VehiclePullout) VehicleLastKnownRecord(org.onebusaway.admin.model.json.VehicleLastKnownRecord) VehicleStatus(org.onebusaway.admin.model.ui.VehicleStatus)

Example 3 with VehicleLastKnownRecord

use of org.onebusaway.admin.model.json.VehicleLastKnownRecord in project onebusaway-application-modules by camsys.

the class VehicleStatusServiceImpl method getLastKnownRecordData.

private List<VehicleLastKnownRecord> getLastKnownRecordData() {
    List<VehicleLastKnownRecord> lastKnownRecords = new ArrayList<VehicleLastKnownRecord>();
    String operationalAPIHost = null;
    try {
        operationalAPIHost = configurationService.getConfigurationValueAsString("operational-api.host", DEFAULT_OPERATIONAL_API_HOST);
    } catch (RemoteConnectFailureException e) {
        log.error("Failed retrieving operational API host from TDM. Setting to default value");
        operationalAPIHost = DEFAULT_OPERATIONAL_API_HOST;
    }
    String url = buildURL(operationalAPIHost, "/record/last-known/list");
    log.debug("making request for : " + url);
    String lastknownContent = remoteConnectionService.getContent(url);
    lastknownContent = lastknownContent.replace(System.getProperty("line.separator"), "");
    String json = extractJsonArrayString(lastknownContent);
    try {
        JSONArray lastKnownContentArray = new JSONArray("[" + json + "]");
        for (int i = 0; i < lastKnownContentArray.length(); i++) {
            VehicleLastKnownRecord lastKnownRecord = convertToObject(lastKnownContentArray.getString(i), VehicleLastKnownRecord.class);
            // lastknownrecord can be null if no data is returned by web service call
            if (lastKnownRecord != null) {
                lastKnownRecords.add(lastKnownRecord);
            }
        }
    } catch (JSONException e) {
        log.error("Error parsing json content : " + e);
        e.printStackTrace();
    }
    return lastKnownRecords;
}
Also used : RemoteConnectFailureException(org.springframework.remoting.RemoteConnectFailureException) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) VehicleLastKnownRecord(org.onebusaway.admin.model.json.VehicleLastKnownRecord)

Example 4 with VehicleLastKnownRecord

use of org.onebusaway.admin.model.json.VehicleLastKnownRecord in project onebusaway-application-modules by camsys.

the class VehicleStatusServiceImpl method getVehicleDetail.

@Override
public VehicleDetail getVehicleDetail(String vehicleId) {
    VehicleDetailBuilder builder = new VehicleDetailBuilder();
    VehicleLastKnownRecord lastKnownRecord = getLastKnownRecordData(vehicleId);
    if (lastKnownRecord != null) {
        VehiclePullout pullout = getPulloutData(vehicleId);
        String headSign = getHeadSign(lastKnownRecord.getDestinationSignCode());
        String inferredHeadSign = getHeadSign(lastKnownRecord.getInferredDSC());
        return builder.buildVehicleDetail(pullout, lastKnownRecord, headSign, inferredHeadSign);
    }
    return null;
}
Also used : VehicleDetailBuilder(org.onebusaway.admin.util.VehicleDetailBuilder) VehiclePullout(org.onebusaway.admin.model.json.VehiclePullout) VehicleLastKnownRecord(org.onebusaway.admin.model.json.VehicleLastKnownRecord)

Aggregations

VehicleLastKnownRecord (org.onebusaway.admin.model.json.VehicleLastKnownRecord)4 ArrayList (java.util.ArrayList)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 VehiclePullout (org.onebusaway.admin.model.json.VehiclePullout)2 RemoteConnectFailureException (org.springframework.remoting.RemoteConnectFailureException)2 VehicleStatus (org.onebusaway.admin.model.ui.VehicleStatus)1 VehicleDetailBuilder (org.onebusaway.admin.util.VehicleDetailBuilder)1 VehicleStatusBuilder (org.onebusaway.admin.util.VehicleStatusBuilder)1