Skip to content

Commit 044bfb3

Browse files
author
Jarkko Laitinen
committed
Merge branch 'development'
2 parents 4237618 + aa6b529 commit 044bfb3

42 files changed

Lines changed: 3575 additions & 1569 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm
2+
3+
*.iml
4+
5+
## Directory-based project format:
6+
.idea/
7+
# if you remove the above rule, at least ignore the following:
8+
9+
# User-specific stuff:
10+
# .idea/workspace.xml
11+
# .idea/tasks.xml
12+
# .idea/dictionaries
13+
14+
# Sensitive or high-churn files:
15+
# .idea/dataSources.ids
16+
# .idea/dataSources.xml
17+
# .idea/sqlDataSources.xml
18+
# .idea/dynamic.xml
19+
# .idea/uiDesigner.xml
20+
21+
# Gradle:
22+
# .idea/gradle.xml
23+
# .idea/libraries
24+
25+
# Mongo Explorer plugin:
26+
# .idea/mongoSettings.xml
27+
28+
## File-based project format:
29+
*.ipr
30+
*.iws
31+
32+
## Plugin-specific files:
33+
34+
# IntelliJ
35+
out/
36+
37+
# mpeltonen/sbt-idea plugin
38+
.idea_modules/
39+
40+
# JIRA plugin
41+
atlassian-ide-plugin.xml
42+
43+
# Crashlytics plugin (for Android Studio and IntelliJ)
44+
com_crashlytics_export_strings.xml
45+
crashlytics.properties
46+
crashlytics-build.properties
47+
*.class

UsingCoSkyAPIv2.java

Lines changed: 187 additions & 187 deletions
Large diffs are not rendered by default.
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
package fi.cosky.examples;
2+
3+
import java.awt.Desktop;
4+
import java.io.Console;
5+
import java.io.IOException;
6+
import java.net.URI;
7+
import java.util.ArrayList;
8+
import java.util.Calendar;
9+
import java.util.Date;
10+
import java.util.List;
11+
12+
import fi.cosky.sdk.*;
13+
import fi.cosky.sdk.CoordinateData.CoordinateSystem;
14+
15+
public class ConsoleAppLoginExample {
16+
private static boolean loggedIn = false;
17+
private static Console console;
18+
private static AppService app;
19+
private static API api;
20+
private static String username;
21+
private static String appServiceUrl = "https://test-appservice.nfleet.fi";
22+
private static String apiUrl = "https://test-api.nfleet.fi";
23+
private static String appUrl = "https://test-app.nfleet.fi";
24+
private static String clientKey = ""; //TODO: use your credentials here
25+
private static String clientSecret = "";
26+
27+
public static void main(String[] args) throws Exception {
28+
console = System.console();
29+
30+
api = new API(apiUrl);
31+
boolean success = api.authenticate(clientKey, clientSecret);
32+
33+
if (!success){
34+
console.printf("Failed to login to API");
35+
return;
36+
}
37+
38+
app = new AppService(appServiceUrl, appUrl, clientKey, clientSecret);
39+
LoginResult userResult = LoginResult.Failure;
40+
41+
if ( ( loggedIn || ( !loggedIn && ( userResult = Login() ) == LoginResult.Success ) ) && ( app.HasValidToken() || app.Login() ) )
42+
{
43+
String url = ExportToAPI( "example" );
44+
if ( url != null ){
45+
String appUrl = app.MakeAppUrl( url );
46+
if (appUrl != null) {
47+
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
48+
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
49+
URI uri = new URI(appUrl);
50+
desktop.browse(uri);
51+
} else {
52+
console.printf("Could not open browser");
53+
}
54+
}
55+
}
56+
57+
}
58+
else if ( userResult != LoginResult.Canceled )
59+
{
60+
console.printf( "Could not send data to NFleet. Username or password does not match." );
61+
}
62+
63+
}
64+
65+
66+
private static LoginResult Login() {
67+
console.printf("Please enter your username: ");
68+
username = console.readLine();
69+
70+
if (username.length() < 1) return LoginResult.Canceled;
71+
72+
console.printf("Please enter your password: ");
73+
String password = new String(console.readPassword());
74+
try {
75+
boolean success = app.Login(username, password);
76+
return success ? LoginResult.Success : LoginResult.Failure;
77+
78+
} catch (IOException e) {
79+
console.printf(e.toString() + "\n");
80+
return LoginResult.Failure;
81+
}
82+
}
83+
84+
private static String ExportToAPI(String caseName) throws Exception {
85+
86+
int id = 4; //TODO: get id from database based on username
87+
88+
ApiData data = api.navigate(ApiData.class, api.getRoot());
89+
UserDataSet users = api.navigate(UserDataSet.class, data.getLink("list-users"));
90+
91+
UserData user = null;
92+
93+
for (UserData u : users.getItems()) {
94+
if (u.getId() == id) {
95+
user = u;
96+
break;
97+
}
98+
}
99+
100+
if (user == null) {
101+
console.printf("Trouble with user" );
102+
return null;
103+
}
104+
105+
user = api.navigate(UserData.class, user.getLink("self"));
106+
107+
RoutingProblemData problem = new RoutingProblemData(caseName);
108+
ResponseData created = api.navigate(ResponseData.class, user.getLink("create-problem"), problem);
109+
problem = api.navigate(RoutingProblemData.class, created.getLocation());
110+
111+
ArrayList<CapacityData> capacities = new ArrayList<CapacityData>();
112+
capacities.add(new CapacityData("Weight", 100000));
113+
ArrayList<TimeWindowData> timeWindows = new ArrayList<TimeWindowData>();
114+
115+
Calendar calendar = Calendar.getInstance();
116+
117+
calendar.set(Calendar.HOUR_OF_DAY, 7);
118+
Date startD = calendar.getTime();
119+
120+
calendar.set(Calendar.HOUR_OF_DAY, 20);
121+
Date endD = calendar.getTime();
122+
123+
TimeWindowData twd = new TimeWindowData(startD, endD);
124+
125+
timeWindows.add(twd);
126+
127+
CoordinateData coordinates = new CoordinateData();
128+
129+
coordinates.setLatitude(62.247906);
130+
coordinates.setLongitude(25.867395);
131+
coordinates.setSystem(CoordinateSystem.WGS84);
132+
133+
LocationData locationData = new LocationData();
134+
locationData.setCoordinatesData(coordinates);
135+
VehicleUpdateRequest vehicleRequest = new VehicleUpdateRequest("Vehicle", capacities, locationData, locationData);
136+
vehicleRequest.setVehicleSpeedProfile( SpeedProfile.Max80Kmh.toString() );
137+
vehicleRequest.setVehicleSpeedFactor(0.7);
138+
vehicleRequest.setTimeWindows(timeWindows);
139+
vehicleRequest.setRelocationType("None");
140+
CoordinateData de = new CoordinateData();
141+
142+
de.setLatitude(62.347906);
143+
de.setLongitude(25.267395);
144+
de.setSystem(CoordinateSystem.WGS84);
145+
146+
LocationData des = new LocationData();
147+
des.setCoordinatesData(de);
148+
CapacityData capacity = new CapacityData("Weight", 20);
149+
List<CapacityData> taskcapacities = new ArrayList<CapacityData>();
150+
taskcapacities.add(capacity);
151+
TaskEventUpdateRequest pickup = new TaskEventUpdateRequest(Type.Pickup, locationData, taskcapacities);
152+
TaskEventUpdateRequest delivery = new TaskEventUpdateRequest(Type.Delivery, des, taskcapacities);
153+
154+
pickup.setTimeWindows(timeWindows);
155+
delivery.setTimeWindows(timeWindows);
156+
List<TaskEventUpdateRequest> both = new ArrayList<TaskEventUpdateRequest>();
157+
both.add(pickup);
158+
both.add(delivery);
159+
TaskUpdateRequest task = new TaskUpdateRequest(both);
160+
task.setName("testTask");
161+
task.setRelocationType("None");
162+
task.setActivityState("Active");
163+
164+
ArrayList<TaskUpdateRequest> tasks = new ArrayList<TaskUpdateRequest>();
165+
tasks.add(task);
166+
TaskSetImportRequest taskImport = new TaskSetImportRequest();
167+
taskImport.setItems(tasks);
168+
169+
ArrayList<VehicleUpdateRequest> vehicles = new ArrayList<VehicleUpdateRequest>();
170+
vehicles.add(vehicleRequest);
171+
VehicleSetImportRequest vehicleImport = new VehicleSetImportRequest();
172+
vehicleImport.setItems(vehicles);
173+
174+
175+
ImportRequest importRequest = new ImportRequest();
176+
importRequest.setTasks(taskImport);
177+
importRequest.setVehicles(vehicleImport);
178+
179+
ResponseData response = api.navigate(ResponseData.class, problem.getLink("import-data"), importRequest);
180+
181+
ImportData result = api.navigate(ImportData.class, response.getLocation());
182+
183+
if (result.getErrorCount() > 0 ) {
184+
console.printf("Trouble with import" );
185+
return null;
186+
}
187+
188+
response = api.navigate(ResponseData.class, result.getLink("apply-import"));
189+
190+
problem = api.navigate(RoutingProblemData.class, problem.getLink("self"));
191+
192+
while (problem.getDataState().equals("Pending")) {
193+
System.out.println("Waiting for initialization");
194+
Thread.sleep(1000);
195+
problem = api.navigate(RoutingProblemData.class, problem.getLink("self"));
196+
}
197+
198+
return problem.getLink("self").getUri();
199+
}
200+
201+
}
202+
203+
204+
enum LoginResult {
205+
Success,
206+
Failure,
207+
Canceled
208+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package fi.cosky.examples;
2+
3+
import java.io.Console;
4+
5+
import fi.cosky.sdk.API;
6+
import fi.cosky.sdk.ApiData;
7+
import fi.cosky.sdk.AppService;
8+
import fi.cosky.sdk.AppUserData;
9+
import fi.cosky.sdk.AppUserDataSet;
10+
import fi.cosky.sdk.AppUserUpdateRequest;
11+
import fi.cosky.sdk.ResponseData;
12+
import fi.cosky.sdk.UserData;
13+
14+
public class ConsoleAppUserCreationExample {
15+
private static Console console;
16+
private static API api;
17+
private static AppService app;
18+
private static String appServiceUrl = "https://test-appservice.nfleet.fi";
19+
private static String apiUrl = "https://test-api.nfleet.fi";
20+
private static String appUrl = "https://test-app.nfleet.fi";
21+
private static String clientKey = ""; //TODO: use your API credentials here
22+
private static String clientSecret = "";
23+
24+
25+
public static void main(String[] args) {
26+
try {
27+
console = System.console();
28+
29+
api = new API(apiUrl);
30+
boolean success = api.authenticate(clientKey, clientSecret);
31+
32+
if (!success){
33+
console.printf("Failed to login to API \n");
34+
return;
35+
} else
36+
console.printf("Login success to API \n");
37+
38+
39+
ApiData apiData = api.navigate(ApiData.class, api.getRoot());
40+
UserData user = new UserData();
41+
42+
ResponseData createdUser = api.navigate(ResponseData.class, apiData.getLink("create-user"), user);
43+
user = api.navigate(UserData.class, createdUser.getLocation());
44+
45+
console.printf("Created user to API with id %d \n", user.getId()); //TODO: Store id and username to database
46+
47+
app = new AppService(appServiceUrl, appUrl, clientKey, clientSecret);
48+
49+
AppUserDataSet users = app.Root;
50+
AppUserUpdateRequest req = new AppUserUpdateRequest();
51+
req.setEmail("somsadfe@thing.ccom"); //TODO: replace these with proper values
52+
req.setPassword("password"); //Note that App does not allow the same username or email on multiple users.
53+
req.setUsername("user2");
54+
req.setId(user.getId()); // this should be the same id as the one that has been created
55+
56+
AppUserData appuser = null;
57+
58+
ResponseData response = app.navigate(ResponseData.class, users.getLink("create-user"), req);
59+
appuser = app.navigate(AppUserData.class, response.getLocation());
60+
console.printf("Created user to app with id %d \n", appuser.getId() );
61+
62+
63+
64+
} catch (Exception e) {
65+
console.printf(e.toString());
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)