Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/mato/MatoAPIFetcher.kt
Show All 13 Lines | BusTO - Backend components | ||||
You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
along with this program. If not, see <http://www.gnu.org/licenses/>. | 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.content.Context | import android.content.Context | ||||
import android.util.Log | import android.util.Log | ||||
import com.android.volley.DefaultRetryPolicy | |||||
import com.android.volley.toolbox.RequestFuture | import com.android.volley.toolbox.RequestFuture | ||||
import it.reyboz.bustorino.BuildConfig | import it.reyboz.bustorino.BuildConfig | ||||
import it.reyboz.bustorino.backend.* | import it.reyboz.bustorino.backend.* | ||||
import it.reyboz.bustorino.data.gtfs.GtfsAgency | |||||
import it.reyboz.bustorino.data.gtfs.GtfsFeed | |||||
import it.reyboz.bustorino.data.gtfs.GtfsRoute | |||||
import it.reyboz.bustorino.data.gtfs.MatoPattern | |||||
import org.json.JSONArray | |||||
import org.json.JSONException | |||||
import org.json.JSONObject | import org.json.JSONObject | ||||
import java.util.* | import java.util.* | ||||
import java.util.concurrent.ExecutionException | import java.util.concurrent.ExecutionException | ||||
import java.util.concurrent.TimeUnit | import java.util.concurrent.TimeUnit | ||||
import java.util.concurrent.TimeoutException | import java.util.concurrent.TimeoutException | ||||
import java.util.concurrent.atomic.AtomicReference | import java.util.concurrent.atomic.AtomicReference | ||||
import kotlin.collections.ArrayList | |||||
open class MatoAPIFetcher(val minNumPassaggi: Int) : ArrivalsFetcher { | open class MatoAPIFetcher(val minNumPassaggi: Int) : ArrivalsFetcher { | ||||
var appContext: Context? = null | var appContext: Context? = null | ||||
set(value) { | set(value) { | ||||
field = value!!.applicationContext | field = value!!.applicationContext | ||||
} | } | ||||
constructor(): this(2) | constructor(): this(2) | ||||
Show All 14 Lines | override fun ReadArrivalTimesAll(stopID: String?, res: AtomicReference<Fetcher.Result>?): Palina { | ||||
numMinutes += 20 | numMinutes += 20 | ||||
val future = RequestFuture.newFuture<Palina>() | val future = RequestFuture.newFuture<Palina>() | ||||
val request = MapiArrivalRequest(stopID, now, numMinutes * 60, numDepartures, res, future, future) | val request = MapiArrivalRequest(stopID, now, numMinutes * 60, numDepartures, res, future, future) | ||||
if (appContext == null || res == null) { | if (appContext == null || res == null) { | ||||
Log.e("BusTO:MatoAPIFetcher", "ERROR: Given null context or null result ref") | Log.e("BusTO:MatoAPIFetcher", "ERROR: Given null context or null result ref") | ||||
return Palina(stopID) | return Palina(stopID) | ||||
} | } | ||||
val requestQueue = NetworkVolleyManager.getInstance(appContext).requestQueue | val requestQueue = NetworkVolleyManager.getInstance(appContext).requestQueue | ||||
request.setTag(getVolleyReqTag(QueryType.ARRIVALS)) | request.setTag(getVolleyReqTag(MatoQueries.QueryType.ARRIVALS)) | ||||
requestQueue.add(request) | requestQueue.add(request) | ||||
try { | try { | ||||
val palinaResult = future.get(5, TimeUnit.SECONDS) | val palinaResult = future.get(5, TimeUnit.SECONDS) | ||||
if (palinaResult!=null) { | if (palinaResult!=null) { | ||||
if (BuildConfig.DEBUG) | if (BuildConfig.DEBUG) | ||||
for (r in palinaResult.queryAllRoutes()){ | for (r in palinaResult.queryAllRoutes()){ | ||||
Log.d(DEBUG_TAG, "route " + r.gtfsId + " has " + r.passaggi.size + " passaggi: "+ r.passaggiToString) | Log.d(DEBUG_TAG, "route " + r.gtfsId + " has " + r.passaggi.size + " passaggi: "+ r.passaggiToString) | ||||
Show All 30 Lines | companion object{ | ||||
const val DEBUG_TAG = "BusTO:MatoAPIFetcher" | const val DEBUG_TAG = "BusTO:MatoAPIFetcher" | ||||
val REQ_PARAMETERS = mapOf( | val REQ_PARAMETERS = mapOf( | ||||
"Content-Type" to "application/json; charset=utf-8", | "Content-Type" to "application/json; charset=utf-8", | ||||
"DNT" to "1", | "DNT" to "1", | ||||
"Host" to "mapi.5t.torino.it") | "Host" to "mapi.5t.torino.it") | ||||
fun getVolleyReqTag(type: QueryType): String{ | private val longRetryPolicy = DefaultRetryPolicy(10000,5,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT) | ||||
fun getVolleyReqTag(type: MatoQueries.QueryType): String{ | |||||
return when (type){ | return when (type){ | ||||
QueryType.ALL_STOPS -> VOLLEY_TAG +"_AllStops" | MatoQueries.QueryType.ALL_STOPS -> VOLLEY_TAG +"_AllStops" | ||||
QueryType.ARRIVALS -> VOLLEY_TAG+"_Arrivals" | MatoQueries.QueryType.ARRIVALS -> VOLLEY_TAG+"_Arrivals" | ||||
MatoQueries.QueryType.FEEDS -> VOLLEY_TAG +"_Feeds" | |||||
MatoQueries.QueryType.ROUTES -> VOLLEY_TAG +"_AllRoutes" | |||||
MatoQueries.QueryType.PATTERNS_FOR_ROUTES -> VOLLEY_TAG + "_PatternsForRoute" | |||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Get stops from the MatoAPI, set [res] accordingly | * Get stops from the MatoAPI, set [res] accordingly | ||||
*/ | */ | ||||
fun getAllStopsGTT(context: Context, res: AtomicReference<Fetcher.Result>?): List<Palina>{ | fun getAllStopsGTT(context: Context, res: AtomicReference<Fetcher.Result>?): List<Palina>{ | ||||
val requestQueue = NetworkVolleyManager.getInstance(context).requestQueue | val requestQueue = NetworkVolleyManager.getInstance(context).requestQueue | ||||
val future = RequestFuture.newFuture<List<Palina>>() | val future = RequestFuture.newFuture<List<Palina>>() | ||||
val request = VolleyAllStopsRequest(future, future) | val request = VolleyAllStopsRequest(future, future) | ||||
request.tag = getVolleyReqTag(QueryType.ALL_STOPS) | request.tag = getVolleyReqTag(MatoQueries.QueryType.ALL_STOPS) | ||||
request.retryPolicy = longRetryPolicy | |||||
requestQueue.add(request) | requestQueue.add(request) | ||||
var palinaList:List<Palina> = mutableListOf() | var palinaList:List<Palina> = mutableListOf() | ||||
try { | try { | ||||
palinaList = future.get(60, TimeUnit.SECONDS) | palinaList = future.get(120, TimeUnit.SECONDS) | ||||
res?.set(Fetcher.Result.OK) | res?.set(Fetcher.Result.OK) | ||||
}catch (e: InterruptedException) { | }catch (e: InterruptedException) { | ||||
e.printStackTrace() | e.printStackTrace() | ||||
res?.set(Fetcher.Result.PARSER_ERROR) | res?.set(Fetcher.Result.PARSER_ERROR) | ||||
} catch (e: ExecutionException) { | } catch (e: ExecutionException) { | ||||
e.printStackTrace() | e.printStackTrace() | ||||
res?.set(Fetcher.Result.SERVER_ERROR) | res?.set(Fetcher.Result.SERVER_ERROR) | ||||
▲ Show 20 Lines • Show All 115 Lines • ▼ Show 20 Lines | companion object{ | ||||
fun makeRequestParameters(requestName:String, variables: JSONObject, query: String): JSONObject{ | fun makeRequestParameters(requestName:String, variables: JSONObject, query: String): JSONObject{ | ||||
val data = JSONObject() | val data = JSONObject() | ||||
data.put("operationName", requestName) | data.put("operationName", requestName) | ||||
data.put("variables", variables) | data.put("variables", variables) | ||||
data.put("query", query) | data.put("query", query) | ||||
return data | return data | ||||
} | } | ||||
fun getFeedsAndAgencies(context: Context, res: AtomicReference<Fetcher.Result>?): | |||||
Pair<List<GtfsFeed>, ArrayList<GtfsAgency>> { | |||||
val requestQueue = NetworkVolleyManager.getInstance(context).requestQueue | |||||
val future = RequestFuture.newFuture<JSONObject>() | |||||
val request = MatoVolleyJSONRequest(MatoQueries.QueryType.FEEDS, JSONObject(), future, future) | |||||
request.setRetryPolicy(longRetryPolicy) | |||||
request.tag = getVolleyReqTag(MatoQueries.QueryType.FEEDS) | |||||
requestQueue.add(request) | |||||
val feeds = ArrayList<GtfsFeed>() | |||||
val agencies = ArrayList<GtfsAgency>() | |||||
var outObj = "" | |||||
try { | |||||
val resObj = future.get(120,TimeUnit.SECONDS) | |||||
outObj = resObj.toString(1) | |||||
val feedsJSON = resObj.getJSONArray("feeds") | |||||
for (i in 0 until feedsJSON.length()){ | |||||
val resTup = ResponseParsing.parseFeedJSON(feedsJSON.getJSONObject(i)) | |||||
feeds.add(resTup.first) | |||||
agencies.addAll(resTup.second) | |||||
} | } | ||||
enum class QueryType { | |||||
ARRIVALS, ALL_STOPS | } catch (e: InterruptedException) { | ||||
e.printStackTrace() | |||||
res?.set(Fetcher.Result.PARSER_ERROR) | |||||
} catch (e: ExecutionException) { | |||||
e.printStackTrace() | |||||
res?.set(Fetcher.Result.SERVER_ERROR) | |||||
} catch (e: TimeoutException) { | |||||
res?.set(Fetcher.Result.CONNECTION_ERROR) | |||||
e.printStackTrace() | |||||
} catch (e: JSONException){ | |||||
e.printStackTrace() | |||||
res?.set(Fetcher.Result.PARSER_ERROR) | |||||
Log.e(DEBUG_TAG, "Downloading feeds: $outObj") | |||||
} | |||||
return Pair(feeds,agencies) | |||||
} | |||||
fun getRoutes(context: Context, res: AtomicReference<Fetcher.Result>?): | |||||
ArrayList<GtfsRoute>{ | |||||
val requestQueue = NetworkVolleyManager.getInstance(context).requestQueue | |||||
val future = RequestFuture.newFuture<JSONObject>() | |||||
val params = JSONObject() | |||||
params.put("feeds","gtt") | |||||
val request = MatoVolleyJSONRequest(MatoQueries.QueryType.ROUTES, params, future, future) | |||||
request.tag = getVolleyReqTag(MatoQueries.QueryType.ROUTES) | |||||
request.retryPolicy = longRetryPolicy | |||||
requestQueue.add(request) | |||||
val routes = ArrayList<GtfsRoute>() | |||||
var outObj = "" | |||||
try { | |||||
val resObj = future.get(120,TimeUnit.SECONDS) | |||||
outObj = resObj.toString(1) | |||||
val routesJSON = resObj.getJSONArray("routes") | |||||
for (i in 0 until routesJSON.length()){ | |||||
val route = ResponseParsing.parseRouteJSON(routesJSON.getJSONObject(i)) | |||||
routes.add(route) | |||||
} | |||||
} catch (e: InterruptedException) { | |||||
e.printStackTrace() | |||||
res?.set(Fetcher.Result.PARSER_ERROR) | |||||
} catch (e: ExecutionException) { | |||||
e.printStackTrace() | |||||
res?.set(Fetcher.Result.SERVER_ERROR) | |||||
} catch (e: TimeoutException) { | |||||
res?.set(Fetcher.Result.CONNECTION_ERROR) | |||||
e.printStackTrace() | |||||
} catch (e: JSONException){ | |||||
e.printStackTrace() | |||||
res?.set(Fetcher.Result.PARSER_ERROR) | |||||
Log.e(DEBUG_TAG, "Downloading feeds: $outObj") | |||||
} | |||||
return routes | |||||
} | |||||
fun getPatternsWithStops(context: Context, routesGTFSIds: ArrayList<String>, res: AtomicReference<Fetcher.Result>?): ArrayList<MatoPattern>{ | |||||
val requestQueue = NetworkVolleyManager.getInstance(context).requestQueue | |||||
val future = RequestFuture.newFuture<JSONObject>() | |||||
val params = JSONObject() | |||||
for (r in routesGTFSIds){ | |||||
if(r.isEmpty()) routesGTFSIds.remove(r) | |||||
} | |||||
val routes = JSONArray(routesGTFSIds) | |||||
params.put("routes",routes) | |||||
val request = MatoVolleyJSONRequest(MatoQueries.QueryType.PATTERNS_FOR_ROUTES, params, future, future) | |||||
request.retryPolicy = longRetryPolicy | |||||
request.tag = getVolleyReqTag(MatoQueries.QueryType.PATTERNS_FOR_ROUTES) | |||||
requestQueue.add(request) | |||||
val patterns = ArrayList<MatoPattern>() | |||||
//var outObj = "" | |||||
try { | |||||
val resObj = future.get(60,TimeUnit.SECONDS) | |||||
//outObj = resObj.toString(1) | |||||
val routesJSON = resObj.getJSONArray("routes") | |||||
for (i in 0 until routesJSON.length()){ | |||||
val patternList = ResponseParsing.parseRoutePatternsStopsJSON(routesJSON.getJSONObject(i)) | |||||
patterns.addAll(patternList) | |||||
} | |||||
} catch (e: InterruptedException) { | |||||
e.printStackTrace() | |||||
res?.set(Fetcher.Result.PARSER_ERROR) | |||||
} catch (e: ExecutionException) { | |||||
e.printStackTrace() | |||||
res?.set(Fetcher.Result.SERVER_ERROR) | |||||
} catch (e: TimeoutException) { | |||||
res?.set(Fetcher.Result.CONNECTION_ERROR) | |||||
e.printStackTrace() | |||||
} catch (e: JSONException){ | |||||
e.printStackTrace() | |||||
res?.set(Fetcher.Result.PARSER_ERROR) | |||||
//Log.e(DEBUG_TAG, "Downloading feeds: $outObj") | |||||
} | |||||
/* | |||||
var numRequests = 0 | |||||
for(routeName in routesGTFSIds){ | |||||
if (!routeName.isEmpty()) numRequests++ | |||||
} | |||||
val countDownForRequests = CountDownLatch(numRequests) | |||||
val lockSave = ReentrantLock() | |||||
//val countDownFor | |||||
for (routeName in routesGTFSIds){ | |||||
val pars = JSONObject() | |||||
pars.put("") | |||||
} | |||||
val goodResponseListener = Response.Listener<JSONObject> { } | |||||
val errorResponseListener = Response.ErrorListener { } | |||||
*/ | |||||
return patterns | |||||
} | |||||
} | } | ||||
} | } | ||||
No newline at end of file | No newline at end of file |
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