Skip to content

Commit 02f8b3b

Browse files
Merge pull request #11 from SimpliRoute/FEATURE-SUP875-Implement_logisticsos_provider
Feature sup875 implement logisticsos provider
2 parents c20548e + 0721213 commit 02f8b3b

File tree

3 files changed

+95
-70
lines changed

3 files changed

+95
-70
lines changed

src/main/java/com/osrm/client/DistanceService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
import java.util.List;
44

55
public interface DistanceService {
6-
DistanceMatrix buildDistanceMatrix(List<GeoLocation> coordinates, double speedRate, String country, String token, String profile);
6+
DistanceMatrix buildDistanceMatrix(List<GeoLocation> coordinates, double speedRate, String country, String token, String profile, String startTime);
77
}

src/main/java/com/osrm/client/Main.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,36 @@
55
public class Main {
66
public static void main(String[] args) throws EmptyUrlException {
77

8-
OSRMClient client = new OSRMClient("http://localhost:8080");
9-
List <GeoLocation> locations = new ArrayList<>();
8+
try {
9+
OSRMClient client = new OSRMClient("http://localhost:8008");
10+
List<GeoLocation> locations = new ArrayList<>();
1011

11-
GeoLocation geo1 = new GeoLocation(-33.416943, -70.60952);
12-
GeoLocation geo2 = new GeoLocation(-33.416943, -70.60952);
13-
GeoLocation geo3 = new GeoLocation(-33.4445755, -70.6404943);
14-
GeoLocation geo4 = new GeoLocation(-33.4457167, -70.61926449999999);
12+
GeoLocation geo1 = new GeoLocation(-33.416943, -70.60952);
13+
GeoLocation geo2 = new GeoLocation(-33.416943, -70.60952);
14+
GeoLocation geo3 = new GeoLocation(-33.4445755, -70.6404943);
15+
GeoLocation geo4 = new GeoLocation(-33.4457167, -70.61926449999999);
1516

16-
locations.add(geo1);
17-
locations.add(geo2);
18-
locations.add(geo3);
19-
locations.add(geo4);
17+
locations.add(geo1);
18+
locations.add(geo2);
19+
locations.add(geo3);
20+
locations.add(geo4);
2021

21-
// low fmv
22-
double speedRate = 2;
23-
String country = "CL";
22+
// low fmv
23+
double speedRate = 2;
24+
String country = "CL";
2425

25-
// token
26-
String token = "None";
26+
// token
27+
String token = "None";
2728

28-
String profile = "car";
29+
String profile = "car";
2930

30-
OSRMDistanceResponse response = client.getDistanceMatrix(locations, speedRate, country, token, profile);
31+
OSRMDistanceResponse response = client.getDistanceMatrix(locations, speedRate, country, token, profile,
32+
null);
3133

32-
System.out.println(response);
34+
System.out.println(response);
35+
}catch (Exception e){
36+
e.printStackTrace();
37+
System.out.println("End with errors.");
38+
}
3339
}
3440
}
Lines changed: 70 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,98 @@
11
package com.osrm.client;
22

3-
import okhttp3.OkHttpClient;
4-
import okhttp3.MediaType;
5-
import okhttp3.RequestBody;
6-
import okhttp3.Request;
7-
import okhttp3.Response;
8-
import okhttp3.OkHttpClient.Builder;
9-
10-
import java.io.IOException;
113
import java.util.ArrayList;
124
import java.util.List;
135
import java.util.concurrent.TimeUnit;
6+
import okhttp3.MediaType;
7+
import okhttp3.OkHttpClient;
8+
import okhttp3.OkHttpClient.Builder;
9+
import okhttp3.Request;
10+
import okhttp3.RequestBody;
11+
import okhttp3.Response;
1412

1513

1614
public class OSRMClient {
17-
private final String uri;
15+
private final String uri;
1816

19-
public OSRMClient(String uri) throws EmptyUrlException {
20-
if (uri != null || !uri.isEmpty()) {
21-
this.uri = uri;
22-
}
23-
else {
24-
throw new EmptyUrlException("OSRMClient Constructor requires a OSRM http url");
25-
}
17+
public OSRMClient(String uri) throws EmptyUrlException {
18+
if (uri != null || !uri.isEmpty()) {
19+
this.uri = uri;
20+
} else {
21+
throw new EmptyUrlException("OSRMClient Constructor requires a OSRM http url");
2622
}
23+
}
2724

2825

29-
public OSRMDistanceResponse getDistanceMatrix(List<GeoLocation> locations, double speedRate, String country, String token, String profile) throws OptimizationDistanceMatrixException {
30-
OSRMDistanceResponse osrmDistanceResponse;
26+
public OSRMDistanceResponse getDistanceMatrix(List<GeoLocation> locations, double speedRate, String country,
27+
String token, String profile,
28+
String startTime) throws OptimizationDistanceMatrixException {
29+
Builder requestBuilder = new Builder();
3130

32-
Builder requestBuilder = new Builder();
31+
requestBuilder.readTimeout(900000, TimeUnit.MILLISECONDS);
32+
requestBuilder.writeTimeout(900000, TimeUnit.MILLISECONDS);
3333

34-
requestBuilder.readTimeout(900000, TimeUnit.MILLISECONDS);
35-
requestBuilder.writeTimeout(900000, TimeUnit.MILLISECONDS);
34+
OkHttpClient client = requestBuilder.build();
3635

37-
OkHttpClient client = requestBuilder.build();
36+
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
3837

39-
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
38+
List<String> locationsCollection = new ArrayList<>();
4039

41-
List<String> locationsCollection = new ArrayList<>();
4240

41+
for (GeoLocation geoloc : locations) {
42+
locationsCollection.add(geoloc.getLatLongString());
43+
}
4344

44-
for (GeoLocation geoloc: locations) {
45-
locationsCollection.add(geoloc.getLatLongString());
46-
}
47-
48-
String paramsString = String.join("&loc=", locationsCollection);
45+
String paramsString = String.join("&loc=", locationsCollection);
4946

50-
paramsString += "&speedRate=" + speedRate;
51-
paramsString += "&country=" + country;
47+
paramsString += "&speedRate=" + speedRate;
48+
paramsString += "&country=" + country;
5249

53-
RequestBody body = RequestBody.create(mediaType, "loc=" + paramsString);
50+
if (startTime != null && !startTime.isEmpty()) {
51+
paramsString += "&start_time=" + startTime;
52+
}
5453

55-
Request request = new Request.Builder()
56-
.url(this.uri + "/table/" + profile)
57-
.post(body)
58-
.addHeader("Content-Type", "application/x-www-form-urlencoded")
59-
.addHeader("Authorization", token)
60-
.build();
54+
RequestBody body = RequestBody.create(mediaType, "loc=" + paramsString);
55+
56+
Request request = new Request.Builder()
57+
.url(this.uri + "/table/" + profile)
58+
.post(body)
59+
.addHeader("Content-Type", "application/x-www-form-urlencoded")
60+
.addHeader("Authorization", token)
61+
.build();
62+
63+
Response response;
64+
try {
65+
response = client.newCall(request).execute();
66+
if (response.isSuccessful()) {
67+
return OSRMDistanceResponse.fromJSON(response.body().string());
68+
}
69+
} catch (Exception e) {
70+
System.out.print(e.getMessage());
71+
throw new OptimizationDistanceMatrixException("Error while connecting to OSRM Server");
72+
}
6173

62-
try {
63-
Response response = client.newCall(request).execute();
74+
UnsuccessfulResponse unsuccessfulResponse = this.getUnsuccessfulResponse(response);
75+
if (unsuccessfulResponse != null && unsuccessfulResponse.getMessage() != null) {
76+
throw new DistanceMatrixResponseException("OSRM Error: " + unsuccessfulResponse.getMessage());
77+
}
6478

65-
if (!response.isSuccessful()) {
66-
UnsuccessfulResponse unsuccessfulResponse = UnsuccessfulResponse.fromJSON(response.body().string());
67-
throw new DistanceMatrixResponseException("OSRM Error: " + unsuccessfulResponse.getMessage());
68-
}
79+
throw new DistanceMatrixResponseException("OSRM Error: " + response);
80+
}
6981

70-
osrmDistanceResponse = OSRMDistanceResponse.fromJSON(response.body().string());
71-
}
72-
catch(Exception e) {
73-
System.out.print(e.getMessage());
74-
throw new OptimizationDistanceMatrixException("Error while connecting to OSRM Server");
82+
private UnsuccessfulResponse getUnsuccessfulResponse(Response response){
83+
try {
84+
if (response.body() != null) {
85+
String bodyResponse = response.body().string();
86+
UnsuccessfulResponse unsuccessfulResponse = UnsuccessfulResponse.fromJSON(bodyResponse);
87+
if(unsuccessfulResponse.getMessage() == null){
88+
return new UnsuccessfulResponse(bodyResponse,String.valueOf(response.code()),"");
7589
}
90+
}
91+
} catch (Exception e) {
92+
System.out.print("Error getUnsuccessfulResponse.fromJSON: " + e.getMessage());
93+
}
94+
return null;
95+
}
96+
7697

77-
return osrmDistanceResponse;
78-
}
7998
}

0 commit comments

Comments
 (0)