From 365a1757ab21a0795f3d558b7c9c4432abcab927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20FERREIRA=20DE=20SOUSA?= Date: Sun, 14 Jun 2020 17:57:38 +0200 Subject: [PATCH] Add support for electric bikes Currently on CityBikes API, electric bikes seem to be supported on 4 networks only, as evidenced in these files : - pybikes/bicing.py - pybikes/smovengo.py - pybikes/keolis.py - pybikes/smartbike.py It's not clear if it's standard but every one of them use the same architecture : the 'extra' JSON item of a station contains the two following sub-items : - 'has_ebikes' : boolean - 'ebikes' : integer Since on some networks the 'has_bikes' value is computed from the 'ebikes' value being 0 or not 0, only this very data is relevant. This commit add the 'ebikes' data as Station class's attributes as well as stations table's columns (upgrade database to version 2). It's copied from remote object if it exists, otherwise it is null. --- .../openbikesharing/app/db/DatabaseHelper.java | 9 +++++++-- .../openbikesharing/app/db/StationsDataSource.java | 13 +++++++++---- .../openbikesharing/app/models/Station.java | 10 ++++++++++ .../app/parsers/BikeNetworkParser.java | 5 +++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/DatabaseHelper.java b/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/DatabaseHelper.java index e97ee79..977a323 100644 --- a/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/DatabaseHelper.java +++ b/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/DatabaseHelper.java @@ -25,7 +25,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { private static DatabaseHelper instance; private static final String DB_NAME = "openbikesharing.sqlite"; - private static final int DB_VERSION = 1; + private static final int DB_VERSION = 2; public static final String STATIONS_TABLE_NAME = "stations"; public static final String STATIONS_COLUMN_ID = "id"; @@ -39,6 +39,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { public static final String STATIONS_COLUMN_BANKING = "banking"; public static final String STATIONS_COLUMN_BONUS = "bonus"; public static final String STATIONS_COLUMN_STATUS = "status"; + public static final String STATIONS_COLUMN_EBIKES = "ebikes"; public static final String FAV_STATIONS_TABLE_NAME = "fav_stations"; public static final String FAV_STATIONS_COLUMN_ID = "id"; @@ -61,7 +62,8 @@ public void onCreate(SQLiteDatabase db) { + "(id TEXT PRIMARY KEY, name TEXT NOT NULL, last_update TEXT NOT NULL, " + "latitude NUMERIC NOT NULL, longitude NUMERIC NOT NULL, " + "free_bikes INTEGER NOT NULL, empty_slots INTEGER NOT NULL, " - + "address TEXT, banking INTEGER, bonus INTEGER, status TEXT)" + + "address TEXT, banking INTEGER, bonus INTEGER, status TEXT, " + + "ebikes INTEGER) " ); db.execSQL("CREATE TABLE " + FAV_STATIONS_TABLE_NAME + "(id TEXT PRIMARY KEY)" @@ -71,6 +73,9 @@ public void onCreate(SQLiteDatabase db) { @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + if (oldVersion < 2) { + db.execSQL("ALTER TABLE " + STATIONS_TABLE_NAME + " ADD COLUMN ebikes INTEGER;"); + } } } diff --git a/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/StationsDataSource.java b/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/StationsDataSource.java index db1f84d..7a3dc85 100644 --- a/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/StationsDataSource.java +++ b/app/src/main/java/be/brunoparmentier/openbikesharing/app/db/StationsDataSource.java @@ -57,7 +57,9 @@ public void storeStations(ArrayList stations) { values.put(DatabaseHelper.STATIONS_COLUMN_BONUS, station.isBonus() ? 1 : 0); if (station.getStatus() != null) values.put(DatabaseHelper.STATIONS_COLUMN_STATUS, station.getStatus().name()); - + if (station.getEBikes() != null) { + values.put(DatabaseHelper.STATIONS_COLUMN_EBIKES, String.valueOf(station.getEBikes())); + } db.insert(DatabaseHelper.STATIONS_TABLE_NAME, null, values); } db.setTransactionSuccessful(); @@ -75,7 +77,7 @@ public ArrayList getStations() { SQLiteDatabase db = dbHelper.getReadableDatabase(); ArrayList stations = new ArrayList<>(); Cursor cursor = db.rawQuery("SELECT id as _id, name, last_update, latitude, longitude, " - + "free_bikes, empty_slots, address, banking, bonus, status " + + "free_bikes, empty_slots, address, banking, bonus, status, ebikes " + "FROM " + DatabaseHelper.STATIONS_TABLE_NAME, null); try { @@ -97,7 +99,7 @@ public Station getStation(String id) { SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("SELECT id as _id, name, last_update, latitude, longitude, " - + "free_bikes, empty_slots, address, banking, bonus, status " + + "free_bikes, empty_slots, address, banking, bonus, status, ebikes " + "FROM " + DatabaseHelper.STATIONS_TABLE_NAME + " " + "WHERE id = ?", new String[] { id }); try { @@ -127,7 +129,7 @@ public ArrayList getFavoriteStations() { SQLiteDatabase db = dbHelper.getReadableDatabase(); ArrayList favStations = new ArrayList<>(); Cursor cursor = db.rawQuery("SELECT sta.id as _id, name, last_update, latitude, longitude, " - + "free_bikes, empty_slots, address, banking, bonus, status " + + "free_bikes, empty_slots, address, banking, bonus, status, ebikes " + "FROM " + DatabaseHelper.FAV_STATIONS_TABLE_NAME + " sta " + "INNER JOIN " + DatabaseHelper.STATIONS_TABLE_NAME + " fav " + "ON sta.id = fav.id", null); @@ -181,6 +183,9 @@ private Station toStation(Cursor cursor) { if (!cursor.isNull(10)) { station.setStatus(StationStatus.valueOf(cursor.getString(10))); // status } + if (!cursor.isNull(11)) { + station.setEBikes(cursor.getInt(11)); // ebikes + } return station; } diff --git a/app/src/main/java/be/brunoparmentier/openbikesharing/app/models/Station.java b/app/src/main/java/be/brunoparmentier/openbikesharing/app/models/Station.java index 0fbf0cf..e64c638 100644 --- a/app/src/main/java/be/brunoparmentier/openbikesharing/app/models/Station.java +++ b/app/src/main/java/be/brunoparmentier/openbikesharing/app/models/Station.java @@ -35,6 +35,7 @@ public class Station implements Serializable, Comparable { private Boolean banking; private Boolean bonus; private StationStatus status; + private Integer eBikes; public Station(String id, String name, String lastUpdate, double latitude, double longitude, int freeBikes, int emptySlots) { this.id = id; @@ -49,6 +50,7 @@ public Station(String id, String name, String lastUpdate, double latitude, doubl this.banking = null; this.bonus = null; this.status = null; + this.eBikes = null; } public String getId() { @@ -95,6 +97,10 @@ public Boolean isBonus() { return bonus; } + public Integer getEBikes() { + return eBikes; + } + public StationStatus getStatus() { return status; } @@ -111,6 +117,10 @@ public void setBonus(boolean bonus) { this.bonus = bonus; } + public void setEBikes(int eBikes) { + this.eBikes = eBikes; + } + @Override public int compareTo(Station another) { return name.compareToIgnoreCase(another.getName()) > 0 ? 1 : diff --git a/app/src/main/java/be/brunoparmentier/openbikesharing/app/parsers/BikeNetworkParser.java b/app/src/main/java/be/brunoparmentier/openbikesharing/app/parsers/BikeNetworkParser.java index f4d1bb3..3c2b89d 100644 --- a/app/src/main/java/be/brunoparmentier/openbikesharing/app/parsers/BikeNetworkParser.java +++ b/app/src/main/java/be/brunoparmentier/openbikesharing/app/parsers/BikeNetworkParser.java @@ -145,6 +145,11 @@ public BikeNetworkParser(String toParse, boolean stripIdFromStationName) throws station.setStatus(StationStatus.OPEN); } } + + /* electric bikes */ + if (rawExtra.has("ebikes")) { + station.setEBikes(rawExtra.getInt("ebikes")); + } } stations.add(station); }