Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/mato/MapiArrivalRequest.java
/* | |||||
BusTO - Backend components | |||||
Copyright (C) 2021 Fabio Mazza | |||||
This program is free software: you can redistribute it and/or modify | |||||
it under the terms of the GNU General Public License as published by | |||||
the Free Software Foundation, either version 3 of the License, or | |||||
(at your option) any later version. | |||||
This program is distributed in the hope that it will be useful, | |||||
but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
GNU General Public License for more details. | |||||
You should have received a copy of the GNU General Public License | |||||
along with this program. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
package it.reyboz.bustorino.backend.mato; | package it.reyboz.bustorino.backend.mato; | ||||
import android.util.Log; | import android.util.Log; | ||||
import androidx.annotation.Nullable; | import androidx.annotation.Nullable; | ||||
import com.android.volley.AuthFailureError; | import com.android.volley.AuthFailureError; | ||||
import com.android.volley.NetworkResponse; | import com.android.volley.NetworkResponse; | ||||
import com.android.volley.Response; | import com.android.volley.Response; | ||||
import com.android.volley.VolleyError; | import com.android.volley.VolleyError; | ||||
import com.android.volley.toolbox.HttpHeaderParser; | import com.android.volley.toolbox.HttpHeaderParser; | ||||
import org.json.JSONArray; | import org.json.JSONArray; | ||||
import org.json.JSONException; | import org.json.JSONException; | ||||
import org.json.JSONObject; | import org.json.JSONObject; | ||||
import java.nio.charset.StandardCharsets; | import java.nio.charset.StandardCharsets; | ||||
import java.util.Date; | import java.util.Date; | ||||
import java.util.HashMap; | import java.util.HashMap; | ||||
import java.util.Map; | import java.util.Map; | ||||
import java.util.concurrent.atomic.AtomicReference; | |||||
import it.reyboz.bustorino.backend.Fetcher; | |||||
import it.reyboz.bustorino.backend.Palina; | import it.reyboz.bustorino.backend.Palina; | ||||
public class MapiArrivalRequest extends MapiVolleyRequest<Palina> { | public class MapiArrivalRequest extends MapiVolleyRequest<Palina> { | ||||
private final String stopName; | private final String stopName; | ||||
private final Date startingTime; | private final Date startingTime; | ||||
private final int timeRange, numberOfDepartures; | private final int timeRange, numberOfDepartures; | ||||
private final AtomicReference<Fetcher.Result> reqRes; | |||||
public MapiArrivalRequest(String stopName, Date startingTime, int timeRange, | public MapiArrivalRequest(String stopName, Date startingTime, int timeRange, | ||||
int numberOfDepartures, | int numberOfDepartures, | ||||
AtomicReference<Fetcher.Result> res, | |||||
Response.Listener<Palina> listener, | Response.Listener<Palina> listener, | ||||
@Nullable Response.ErrorListener errorListener) { | @Nullable Response.ErrorListener errorListener) { | ||||
super(MatoAPIFetcher.QueryType.ARRIVALS, listener, errorListener); | super(MatoAPIFetcher.QueryType.ARRIVALS, listener, errorListener); | ||||
this.stopName = stopName; | this.stopName = stopName; | ||||
this.startingTime = startingTime; | this.startingTime = startingTime; | ||||
this.timeRange = timeRange; | this.timeRange = timeRange; | ||||
this.numberOfDepartures = numberOfDepartures; | this.numberOfDepartures = numberOfDepartures; | ||||
this.reqRes = res; | |||||
} | |||||
public MapiArrivalRequest(String stopName, Date startingTime, int timeRange, | |||||
int numberOfDepartures, | |||||
Response.Listener<Palina> listener, | |||||
@Nullable Response.ErrorListener errorListener) { | |||||
this(stopName, startingTime, timeRange, numberOfDepartures, | |||||
new AtomicReference<>(), listener, errorListener); | |||||
} | } | ||||
@Nullable | @Nullable | ||||
@Override | @Override | ||||
public byte[] getBody() throws AuthFailureError { | public byte[] getBody() throws AuthFailureError { | ||||
JSONObject variables = new JSONObject(); | JSONObject variables = new JSONObject(); | ||||
JSONObject data = new JSONObject(); | JSONObject data = new JSONObject(); | ||||
try { | try { | ||||
data.put("operationName","AllStopsDirect"); | data.put("operationName","AllStopsDirect"); | ||||
variables.put("name", stopName); | variables.put("name", stopName); | ||||
variables.put("startTime", (long) startingTime.getTime()/1000); | variables.put("startTime", (long) startingTime.getTime()/1000); | ||||
variables.put("timeRange", timeRange); | variables.put("timeRange", timeRange); | ||||
variables.put("numberOfDepartures", numberOfDepartures); | variables.put("numberOfDepartures", numberOfDepartures); | ||||
data.put("variables", variables); | data.put("variables", variables); | ||||
data.put("query", MatoAPIFetcher.QUERY_ARRIVALS); | data.put("query", MatoQueries.QUERY_ARRIVALS); | ||||
} catch (JSONException e) { | } catch (JSONException e) { | ||||
e.printStackTrace(); | e.printStackTrace(); | ||||
throw new AuthFailureError("Error with JSON enconding",e); | throw new AuthFailureError("Error with JSON enconding",e); | ||||
} | } | ||||
String requestBody = data.toString(); | String requestBody = data.toString(); | ||||
Log.d("MapiArrivalBusTO", "Request variables: "+ variables); | Log.d("MapiArrivalBusTO", "Request variables: "+ variables); | ||||
return requestBody.getBytes(); | return requestBody.getBytes(); | ||||
} | } | ||||
@Override | @Override | ||||
protected Response<Palina> parseNetworkResponse(NetworkResponse response) { | protected Response<Palina> parseNetworkResponse(NetworkResponse response) { | ||||
if(response.statusCode != 200) | if(response.statusCode != 200) { | ||||
reqRes.set(Fetcher.Result.SERVER_ERROR); | |||||
return Response.error(new VolleyError("Response Error Code "+response.statusCode)); | return Response.error(new VolleyError("Response Error Code " + response.statusCode)); | ||||
} | |||||
final String stringResponse = new String(response.data); | final String stringResponse = new String(response.data); | ||||
Palina p = null; | Palina p = null; | ||||
try { | try { | ||||
JSONObject data = new JSONObject(stringResponse).getJSONObject("data"); | JSONObject data = new JSONObject(stringResponse).getJSONObject("data"); | ||||
JSONArray allStopsFound = data.getJSONArray("stops"); | JSONArray allStopsFound = data.getJSONArray("stops"); | ||||
boolean haveManyResults = allStopsFound.length() > 1; | boolean stopFound = false; | ||||
for (int i=0; i<allStopsFound.length(); i++){ | for (int i=0; i<allStopsFound.length(); i++){ | ||||
final JSONObject currentObj = allStopsFound.getJSONObject(i); | final JSONObject currentObj = allStopsFound.getJSONObject(i); | ||||
p = MatoAPIFetcher.Companion.parseStopJSON(currentObj); | p = MatoAPIFetcher.Companion.parseStopJSON(currentObj); | ||||
if (haveManyResults){ | if (p.gtfsID != null) { | ||||
//check we got the right one | if(p.gtfsID.contains("gtt:")){ | ||||
if (p.gtfsID == null){ | |||||
continue; | |||||
} else if(p.gtfsID.contains("gtt:")){ | |||||
//valid stop | //valid stop | ||||
stopFound = true; | |||||
break; | break; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
if (!stopFound){ | |||||
Log.w("MapiArrival-Busto", "No stop found: "+p); | |||||
reqRes.set(Fetcher.Result.NOT_FOUND); | |||||
return Response.error(new VolleyError("Stop not found")); | |||||
} | |||||
} catch (JSONException e) { | } catch (JSONException e) { | ||||
e.printStackTrace(); | e.printStackTrace(); | ||||
Log.e("BusTO:MapiRequest", "Error parsing JSON: "+stringResponse); | Log.e("BusTO:MapiRequest", "Error parsing JSON: "+stringResponse); | ||||
reqRes.set(Fetcher.Result.PARSER_ERROR); | |||||
return Response.error(new VolleyError("Error parsing the response in JSON", | return Response.error(new VolleyError("Error parsing the response in JSON", | ||||
e)); | e)); | ||||
} | } | ||||
reqRes.set(Fetcher.Result.OK); | |||||
return Response.success(p, HttpHeaderParser.parseCacheHeaders(response)); | return Response.success(p, HttpHeaderParser.parseCacheHeaders(response)); | ||||
} | } | ||||
public class StopNotFoundError extends VolleyError{ | |||||
@Nullable | public StopNotFoundError(String message) { | ||||
@Override | super(message); | ||||
protected Map<String, String> getParams() throws AuthFailureError { | } | ||||
return new HashMap<>(); | |||||
public StopNotFoundError() { | |||||
super(); | |||||
} | |||||
} | } | ||||
} | } |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register