diff --git a/src/it/reyboz/bustorino/backend/ArrivalsFetcher.java b/src/it/reyboz/bustorino/backend/ArrivalsFetcher.java index ed9d368..a346efc 100644 --- a/src/it/reyboz/bustorino/backend/ArrivalsFetcher.java +++ b/src/it/reyboz/bustorino/backend/ArrivalsFetcher.java @@ -1,50 +1,50 @@ /* BusTO (backend components) Copyright (C) 2016 Ludovico Pavesi 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 . */ package it.reyboz.bustorino.backend; // "arrivals" è più usato di "transit" o simili, e chi sono io per mettermi a dibattere con gli inglesi? -import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; public interface ArrivalsFetcher extends Fetcher { - /** - * Reads arrival times from a (hopefully) real-time source, e.g. the GTT website. - * Don't call this in UI thread! - * - * @param stopID stop ID, in normalized form. - * @param routeID route ID, in normalized form. - * @param res result code (will be set by this method) - * @return arrival times - * @see it.reyboz.bustorino.backend.Fetcher.resultCodes - * @see FiveTNormalizer - */ - Palina ReadArrivalTimesRoute(String stopID, String routeID, AtomicInteger res); +// /** +// * Reads arrival times from a (hopefully) real-time source, e.g. the GTT website. +// * Don't call this in UI thread! +// * +// * @param stopID stop ID, in normalized form. +// * @param routeID route ID, in normalized form. +// * @param res result code (will be set by this method) +// * @return arrival times +// * @see it.reyboz.bustorino.backend.Fetcher.result +// * @see FiveTNormalizer +// */ +// Palina ReadArrivalTimesRoute(String stopID, String routeID, AtomicReference res); /** * Reads arrival times from a (hopefully) real-time source, e.g. the GTT website. * Don't call this in UI thread! * * @param routeID route ID, in normalized form. * @param res result code (will be set by this method) * @return arrival times - * @see it.reyboz.bustorino.backend.Fetcher.resultCodes + * @see it.reyboz.bustorino.backend.Fetcher.result * @see FiveTNormalizer */ - Palina ReadArrivalTimesAll(String routeID, AtomicInteger res); + Palina ReadArrivalTimesAll(String routeID, AtomicReference res); } diff --git a/src/it/reyboz/bustorino/backend/Fetcher.java b/src/it/reyboz/bustorino/backend/Fetcher.java index c76a065..2be8299 100644 --- a/src/it/reyboz/bustorino/backend/Fetcher.java +++ b/src/it/reyboz/bustorino/backend/Fetcher.java @@ -1,44 +1,43 @@ /* BusTO (backend components) Copyright (C) 2016 Ludovico Pavesi 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 . */ package it.reyboz.bustorino.backend; import java.util.concurrent.atomic.AtomicInteger; public interface Fetcher { /** - * Status codes, convert to int with resultCodes.OK.ordinal() and the like. + * Status codes. * - * NULL: still haven't started * OK: got a response, parsed correctly, obtained some data * CLIENT_OFFLINE: can't connect to the internet * SERVER_OFFLINE: the server is down for maintenance (apparently sometimes happens to the 5T website and they put a message on the home page, that can be parsed to set this state) * SERVER_ERROR: the server replied anything other than HTTP 200, basically * PARSER_ERROR: the server replied something that can't be parsed, probably it's not the data we're looking for (e.g. "PHP: Fatal Error") * NO_RESULTS: like OK, but there's an empty array (no stops\routes\"passaggi" found) */ - enum resultCodes { - NULL, OK, CLIENT_OFFLINE, SERVER_OFFLINE, SERVER_ERROR, PARSER_ERROR, NOT_FOUND, EMPTY_RESULT_SET + enum result { + OK, CLIENT_OFFLINE, SERVER_OFFLINE, SERVER_ERROR, PARSER_ERROR, NOT_FOUND, EMPTY_RESULT_SET } // moved to AsyncFetcher since shouldn't be reused on different runs /* * Reading this before doing anything is a bad idea, probably. */ //AtomicInteger result = new AtomicInteger(resultCodes.NULL.ordinal()); // how does this even work in an interface is a mystery to me. } diff --git a/src/it/reyboz/bustorino/backend/GTTJSONFetcher.java b/src/it/reyboz/bustorino/backend/GTTJSONFetcher.java index c4dcf93..f5ac694 100644 --- a/src/it/reyboz/bustorino/backend/GTTJSONFetcher.java +++ b/src/it/reyboz/bustorino/backend/GTTJSONFetcher.java @@ -1,119 +1,134 @@ /* BusTO (backend components) Copyright (C) 2016 Ludovico Pavesi 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 . */ package it.reyboz.bustorino.backend; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Scanner; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; public class GTTJSONFetcher implements ArrivalsFetcher { - @Override - public Palina ReadArrivalTimesRoute(String stopID, String routeID, AtomicInteger res) { - return this.ReadArrivalTimesAll(routeID, res); // TODO: finish implementation - } +// @Override +// public Palina ReadArrivalTimesRoute(String stopID, String routeID, AtomicReference res) { +// return this.ReadArrivalTimesAll(routeID, res); // TODO: implement this when it's needed (never?) +// } - @Override - public Palina ReadArrivalTimesAll(String routeID, AtomicInteger res) { - HttpURLConnection urlConnection; + @Override @NonNull + public Palina ReadArrivalTimesAll(String routeID, AtomicReference res) { URL url; Palina p = new Palina(); JSONArray json; int howManyRoutes, howManyPassaggi, i, j, pos; // il misto inglese-italiano è un po' ridicolo ma tanto vale... JSONObject thisroute; JSONArray passaggi; try { url = new URL("http://www.gtt.to.it/cms/index.php?option=com_gtt&task=palina.getTransiti&palina=" + routeID + "&realtime=true"); } catch (MalformedURLException e) { - res.set(resultCodes.PARSER_ERROR.ordinal()); + res.set(result.PARSER_ERROR); return p; } - try { - urlConnection = (HttpURLConnection) url.openConnection(); - } catch(IOException e) { - res.set(resultCodes.SERVER_OFFLINE.ordinal()); // TODO: can we assume this is CLIENT_OFFLINE? - return p; - } - - try { - InputStream in = urlConnection.getInputStream(); - json = new JSONArray(streamToString(in)); - } catch (Exception e) { - res.set(resultCodes.PARSER_ERROR.ordinal()); + json = queryURL(url, res); + if(json == null) { return p; - } finally { - urlConnection.disconnect(); } howManyRoutes = json.length(); if(howManyRoutes == 0) { - res.set(resultCodes.EMPTY_RESULT_SET.ordinal()); + res.set(result.EMPTY_RESULT_SET); return p; } try { - // returns [{"PassaggiRT":[],"Passaggi":[]}] for non existing stops - json.getJSONObject(0).getString("Linea"); // if we can get this, then there's something useful in the array. - } catch(JSONException e) { - res.set(resultCodes.EMPTY_RESULT_SET.ordinal()); // could also use NOT_FOUND - return p; - } - - try { // TODO: split this to reuse other part in ReadArrivalTimesRoute for(i = 0; i < howManyRoutes; i++) { thisroute = json.getJSONObject(i); pos = p.addRoute(thisroute.getString("Linea"), thisroute.getString("Direzione")); passaggi = thisroute.getJSONArray("PassaggiRT"); howManyPassaggi = passaggi.length(); for(j = 0; j < howManyPassaggi; i++) { p.addPassaggio(passaggi.getString(j).concat("*"), pos); } passaggi = thisroute.getJSONArray("Passaggi"); // now the non-real-time ones howManyPassaggi = passaggi.length(); for(j = 0; j < howManyPassaggi; i++) { p.addPassaggio(passaggi.getString(j), pos); } } } catch (JSONException e) { - res.set(resultCodes.PARSER_ERROR.ordinal()); + res.set(result.PARSER_ERROR); return p; } - res.set(resultCodes.OK.ordinal()); + res.set(result.OK); return p; } + @Nullable + private JSONArray queryURL(URL url, AtomicReference res) { + HttpURLConnection urlConnection; + JSONArray json; + + try { + urlConnection = (HttpURLConnection) url.openConnection(); + } catch(IOException e) { + res.set(result.SERVER_OFFLINE); // TODO: can we assume this is CLIENT_OFFLINE? + return null; + } + + try { + InputStream in = urlConnection.getInputStream(); + json = new JSONArray(streamToString(in)); + } catch (Exception e) { + res.set(result.PARSER_ERROR); + return null; + } finally { + urlConnection.disconnect(); + } + + try { + // returns [{"PassaggiRT":[],"Passaggi":[]}] for non existing stops! + json.getJSONObject(0).getString("Linea"); // if we can get this, then there's something useful in the array. + } catch(JSONException e) { + res.set(result.EMPTY_RESULT_SET); // could also use NOT_FOUND + return null; + } + + return json; + } + // https://stackoverflow.com/a/14585883 private static String streamToString(InputStream is) { Scanner s = new Scanner(is); // TODO: test this return s.hasNext() ? s.next() : ""; } } diff --git a/src/it/reyboz/bustorino/middleware/AsyncArrivalsFetcherAll.java b/src/it/reyboz/bustorino/middleware/AsyncArrivalsFetcherAll.java index 9a76bf6..e4eb778 100644 --- a/src/it/reyboz/bustorino/middleware/AsyncArrivalsFetcherAll.java +++ b/src/it/reyboz/bustorino/middleware/AsyncArrivalsFetcherAll.java @@ -1,42 +1,43 @@ /* BusTO (backend components) Copyright (C) 2016 Ludovico Pavesi 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 . */ package it.reyboz.bustorino.middleware; import it.reyboz.bustorino.backend.ArrivalsFetcher; +import it.reyboz.bustorino.backend.Fetcher; import it.reyboz.bustorino.backend.Palina; import android.os.AsyncTask; import android.support.annotation.NonNull; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; public class AsyncArrivalsFetcherAll extends AsyncTask { ArrivalsFetcher af; - AtomicInteger result; + AtomicReference result; String stopID; public AsyncArrivalsFetcherAll(@NonNull ArrivalsFetcher af, @NonNull String stopID) { this.af = af; this.stopID = stopID; } @Override protected Palina doInBackground(Void... useless) { - return af.ReadArrivalTimesAll(this.stopID, this.result); + return af.ReadArrivalTimesAll(this.stopID, this.result); // TODO: reuse the same Palina object and update it } }