Skip to content

Commit bb3e22e

Browse files
authored
Merge pull request #311 from HSLdevcom/DT-3063
Dt 3063
2 parents 37f79f2 + 144a40c commit bb3e22e

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed

src/main/java/org/opentripplanner/updater/bike_rental/BikeRentalUpdater.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ protected void configurePolling (Graph graph, JsonNode config) throws Exception
9999
source = new BicimadBikeRentalDataSource();
100100
} else if (sourceType.equals("samocat")) {
101101
source = new SamocatScooterRentalDataSource(networkName);
102+
} else if (sourceType.equals("sharingos")) {
103+
source = new SharingOSBikeRentalDataSource(networkName);
102104
}
103105
}
104106

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.opentripplanner.updater.bike_rental;
2+
3+
import java.util.HashSet;
4+
5+
import org.opentripplanner.routing.bike_rental.BikeRentalStation;
6+
import org.opentripplanner.util.NonLocalizedString;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
import com.fasterxml.jackson.databind.JsonNode;
11+
12+
/**
13+
* Implementation of a BikeRentalDataSource for the SharingOS API used in Vantaa.
14+
* @see BikeRentalDataSource
15+
*/
16+
public class SharingOSBikeRentalDataSource extends GenericJsonBikeRentalDataSource {
17+
18+
private static final Logger log = LoggerFactory.getLogger(SharingOSBikeRentalDataSource.class);
19+
20+
private String networkName;
21+
22+
public SharingOSBikeRentalDataSource(String networkName) {
23+
super("data");
24+
this.networkName = defaultIfEmpty(networkName, "sharingos");
25+
}
26+
27+
private String defaultIfEmpty(String value, String defaultValue) {
28+
if (value == null || value.isEmpty())
29+
return defaultValue;
30+
31+
return value;
32+
}
33+
34+
public BikeRentalStation makeStation(JsonNode node) {
35+
BikeRentalStation station = new BikeRentalStation();
36+
station.id = node.path("id").asText();
37+
station.name = new NonLocalizedString(node.path("name").asText());
38+
station.state = node.path("style").asText();
39+
station.networks = new HashSet<String>();
40+
station.networks.add(this.networkName);
41+
try {
42+
station.y = node.path("latitude").asDouble();
43+
station.x = node.path("longitude").asDouble();
44+
if (node.path("is_enable").asInt() == 0) {
45+
station.state = "Station on";
46+
station.spacesAvailable = node.path("available_capacity").asInt();
47+
station.bikesAvailable = node.path("total_capacity").asInt() - station.spacesAvailable;
48+
} else if (node.path("is_enable").asInt() == 1) {
49+
station.state = "Station off";
50+
station.spacesAvailable = 0;
51+
station.bikesAvailable = 0;
52+
} else {
53+
station.state = "Station closed";
54+
station.spacesAvailable = 0;
55+
station.bikesAvailable = 0;
56+
}
57+
return station;
58+
} catch (NumberFormatException e) {
59+
// E.g. coordinates is empty
60+
log.info("Error parsing bike rental station " + station.id, e);
61+
return null;
62+
}
63+
}
64+
}

src/main/resources/logback.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<!-- for now disable realtime logging, leave only statistics -->
5656
<logger name="org.opentripplanner.updater.bike_rental.SmooveBikeRentalDataSource" level="warn" />
5757
<logger name="org.opentripplanner.updater.bike_rental.SamocatScooterRentalDataSource" level="warn" />
58+
<logger name="org.opentripplanner.updater.bike_rental.SharingOSBikeRentalDataSource" level="warn" />
5859
<logger name="org.opentripplanner.updater.bike_rental.BikeRentalUpdater" level="warn" />
5960
<logger name="org.opentripplanner.updater.stoptime.TimetableSnapshotSource" level="error" />
6061
<logger name="org.opentripplanner.updater.stoptime.TimetableSnapshotSource$GtfsRealtimeStatistics" level="info" />

src/test/java/org/opentripplanner/updater/bike_rental/TestBikeRentalStationSource.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,52 @@ public void testSamocat() {
124124
BikeRentalStation testitieWithCustomNetwork = rentalStationsWithCustomNetwork.get(0);
125125
assertEquals("[vuosaari]", testitieWithCustomNetwork.networks.toString());
126126
}
127+
128+
public void testSharingos() {
129+
SharingOSBikeRentalDataSource source = new SharingOSBikeRentalDataSource(null);
130+
source.setUrl("file:src/test/resources/bike/sharingos.json");
131+
assertTrue(source.update());
132+
List<BikeRentalStation> rentalStations = source.getStations();
133+
134+
assertEquals(3, rentalStations.size());
135+
for (BikeRentalStation rentalStation : rentalStations) {
136+
System.out.println(rentalStation);
137+
}
138+
139+
BikeRentalStation tempClosedStation = rentalStations.get(0);
140+
assertEquals("Temporarily closed test station", tempClosedStation.name.toString());
141+
assertEquals("1001", tempClosedStation.id);
142+
assertEquals(25.0515797, tempClosedStation.x);
143+
assertEquals(60.2930266, tempClosedStation.y);
144+
assertEquals(0, tempClosedStation.spacesAvailable);
145+
assertEquals(0, tempClosedStation.bikesAvailable);
146+
assertEquals("Station off", tempClosedStation.state);
147+
assertEquals("[sharingos]", tempClosedStation.networks.toString());
148+
149+
BikeRentalStation permClosedStation = rentalStations.get(1);
150+
assertEquals("Closed test station", permClosedStation.name.toString());
151+
assertEquals("1002", permClosedStation.id);
152+
assertEquals(0, permClosedStation.spacesAvailable);
153+
assertEquals(0, permClosedStation.bikesAvailable);
154+
assertEquals(25.0364107, permClosedStation.x);
155+
assertEquals(60.2934127, permClosedStation.y);
156+
assertEquals("Station closed", permClosedStation.state);
157+
158+
BikeRentalStation openTestStation = rentalStations.get(2);
159+
assertEquals("Open test station", openTestStation.name.toString());
160+
assertEquals("1003", openTestStation.id);
161+
assertEquals(25.0424261, openTestStation.x);
162+
assertEquals(60.2932159, openTestStation.y);
163+
assertEquals(2, openTestStation.spacesAvailable);
164+
assertEquals(3, openTestStation.bikesAvailable);
165+
assertEquals("Station on", openTestStation.state);
166+
167+
// Test giving network name to data source
168+
SharingOSBikeRentalDataSource sourceWithCustomNetwork = new SharingOSBikeRentalDataSource("vantaa");
169+
sourceWithCustomNetwork.setUrl("file:src/test/resources/bike/sharingos.json");
170+
assertTrue(sourceWithCustomNetwork.update());
171+
List<BikeRentalStation> rentalStationsWithCustomNetwork = sourceWithCustomNetwork.getStations();
172+
BikeRentalStation tempClosedStationWithCustomNetwork = rentalStationsWithCustomNetwork.get(0);
173+
assertEquals("[vantaa]", tempClosedStationWithCustomNetwork.networks.toString());
174+
}
127175
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"message": "",
3+
"errcode": 0,
4+
"ticks": 0,
5+
"data": [
6+
{
7+
"id": 1001,
8+
"name": "Temporarily closed test station",
9+
"latitude": 60.2930266,
10+
"longitude": 25.0515797,
11+
"total_capacity": 0,
12+
"available_capacity": 0,
13+
"discount": 0,
14+
"is_enable": 1,
15+
"update_time": "2019-05-22T11:15:02+08:00"
16+
},
17+
{
18+
"id": 1002,
19+
"name": "Closed test station",
20+
"latitude": 60.2934127,
21+
"longitude": 25.0364107,
22+
"total_capacity": 0,
23+
"available_capacity": 0,
24+
"discount": 0,
25+
"is_enable": 2,
26+
"update_time": "2019-05-22T11:15:02+08:00"
27+
},
28+
{
29+
"id": 1003,
30+
"name": "Open test station",
31+
"latitude": 60.2932159,
32+
"longitude": 25.0424261,
33+
"total_capacity": 5,
34+
"available_capacity": 2,
35+
"discount": 0,
36+
"is_enable": 0,
37+
"update_time": "2019-05-22T11:15:02+08:00"
38+
}
39+
]
40+
}

0 commit comments

Comments
 (0)