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+ }
0 commit comments