Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
Add support for electric bikes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
francoisfds committed Jun 14, 2020
1 parent 7a999f4 commit 4f1e5e7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand All @@ -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)"
Expand All @@ -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;");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public void storeStations(ArrayList<Station> 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();
Expand All @@ -75,7 +77,7 @@ public ArrayList<Station> getStations() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
ArrayList<Station> 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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -127,7 +129,7 @@ public ArrayList<Station> getFavoriteStations() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
ArrayList<Station> 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);
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class Station implements Serializable, Comparable<Station> {
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;
Expand All @@ -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() {
Expand Down Expand Up @@ -95,6 +97,10 @@ public Boolean isBonus() {
return bonus;
}

public Integer getEBikes() {
return eBikes;
}

public StationStatus getStatus() {
return status;
}
Expand All @@ -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 :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 4f1e5e7

Please sign in to comment.